binxie
2024-01-20 e0de9222da210f9c8eb1a9f5400f936a14923e11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
const COMMENT_DATA_FIELD = "comment";
const USER_REQUIRED_ERROR_MESSAGE = "You must select a user before submitting.";
 
//constant used to store the toast error information if there is an error
//querying the approval history
const queryError = {
  title: "Error!",
  message:
    "There was an error while fetching the Approval History. Please contact your System Administrator.",
  variant: "error"
};
 
//stores every possible modal state with all properties associated to each state
//this stores the success/error toast message information, the Modal titles, and the submit buttons
export const modalStates = {
  SUBMIT_APPROVAL: {
    state: "SUBMIT_APPROVAL",
    // title: "Submit for Approval",
    title: "提交审批",
    // submitLabel: "Submit",
    submitLabel: "提交",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "Approval Process submitted.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while submitting the Approval Process. Please contact your System Administrator.",
        variant: "error"
      }
    }
  },
  GET_NEXT_APPROVER_SUBMIT: {
    state: "GET_NEXT_APPROVER_SUBMIT",
    // title: "Submit for Approval",
    title: "提交审批",
    // submitLabel: "Submit",
    submitLabel: "提交",
    lookupLabel: "Choose Next Approver",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "Approval Process submitted.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while submitting the Approval Process. Please contact your System Administrator.",
        variant: "error"
      }
    }
  },
  APPROVE: {
    state: "APPROVE",
    title: "批准",
    // submitLabel: "Approve",
    submitLabel: "批准",
    action: "Approve",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "The step was approved successfully.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while approving the step. Please contact your System Administrator.",
        variant: "error"
      }
    }
  }, //want to get sobject type like Approve Account
  GET_NEXT_APPROVER_APPROVE: {
    state: "GET_NEXT_APPROVER_APPROVE",
    title: "批准",
    // submitLabel: "Approve",
    submitLabel: "批准",
    lookupLabel: "Choose Next Approver",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "The step was approved successfully.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while approving the step. Please contact your System Administrator.",
        variant: "error"
      }
    }
  }, //want to get sobject type like Approve Account
  REJECT: {
    state: "REJECT",
    title: "拒绝",
    // submitLabel: "Reject",
    submitLabel: "拒绝",
    action: "Reject",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "The step was rejected successfully.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while rejecting the step. Please contact your System Administrator.",
        variant: "error"
      }
    }
  }, //want to get sobject type like Reject Account
  RECALL: {
    state: "RECALL",
    // title: "Recall Approval Request",
    title: "撤回批准请求",
    // submitLabel: "Recall",
    submitLabel: "撤回",
    action: "Removed",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "The step was recalled successfully.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while recalling the step. Please contact your System Administrator.",
        variant: "error"
      }
    }
  }, //want to get sobject type like Reject Account
  REASSIGN: {
    state: "REASSIGN",
    // title: "Reassign Approval Request",
    title: "重新分配审批请求",
    // submitLabel: "Reassign",
    submitLabel: "重新分配",
    lookupLabel: "Reassign To",
    toastInfo: {
      success: {
        label: "success",
        title: "Success!",
        message: "Approval process step was reassigned.",
        variant: "success"
      },
      error: {
        label: "error",
        title: "Error!",
        message:
          "There was an error while reassigning the approval process step. Please contact your System Administrator.",
        variant: "error"
      }
    }
  } //want to get sobject type like Reject Account
};
 
//error message that indicates the next approver is missing
const MISSING_NEXT_APPROVER_ERROR = "missing required field: [nextApproverIds]";
 
//function called after error message from an imperative method,
//to verify if a next approver is needed
export function verifyIfNextApproverWasNeeded(errors) {
  const missingNextApprover = error =>
    error.message === MISSING_NEXT_APPROVER_ERROR;
  return errors.some(missingNextApprover);
}
 
//modal helper functions
export function hideModal(self) {
  let modalToHide = self.template.querySelector("c-modal");
  modalToHide.hide();
}
 
export function showModal(self) {
  let modalToShow = self.template.querySelector("c-modal");
  modalToShow.show();
}
 
//end modal helper functions
 
export function getCommentPropertyFromModal(self) {
  console.log('getCommentPropertyFromModal');
  return self.template.querySelector(`[data-id="${COMMENT_DATA_FIELD}"]`).value;
}
 
export function showGetNextApproverModal(self, newState) {
  self.currentModalState = newState;
  showModal(self);
}
 
//if a user was not selected in the lookup 
//populate the error message
export function validateUserLookup(self) {
  const selection = self.template.querySelector("c-lookup").getSelection();
  if (selection.length === 0) {
    self.lookupErrors = [{ message: USER_REQUIRED_ERROR_MESSAGE }];
    return false;
  }
  self.lookupErrors = [];
  return true;
  
}
//populates the selected user property based on the selected
//user in the lookup component
export function setSelectedUser(self) {
  self.selectedUser = self.template
    .querySelector("c-lookup")
    .getSelection()[0].id;
}
 
export function clearModalState(self) {
  self.currentModalState = "";
  self.selectedUser = "";
  self.lookupErrors = [];
  self.modalComment = "";
  self.initialSelection = [];
}
 
//displays a toast based on the current modal state and on the status=success/error
export function displayToast(self, ShowToastEvent, status, messageOverride) {
  const event = new ShowToastEvent({
    title: modalStates[self.currentModalState].toastInfo[status].title,
    message: messageOverride
      ? messageOverride
      : modalStates[self.currentModalState].toastInfo[status].message,
    variant: modalStates[self.currentModalState].toastInfo[status].variant
  });
  self.dispatchEvent(event);
}
 
export function displayToastErrorQuery(self, ShowToastEvent) {
  const event = new ShowToastEvent({
    title: queryError.title,
    message: queryError.message,
    variant: queryError.variant
  });
  self.dispatchEvent(event);
}
 
export function extractErrorMessage(errors) {
  let errorMessage = "";
  errors.forEach(element => {
    errorMessage += element.message;
  });
  return errorMessage;
}