沙世明
2022-09-13 bfca7a84bec815da594f1d12558535ed06d2490b
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { LightningElement, api, track, wire } from "lwc";
import getApprovalHistory from "@salesforce/apex/ApprovalHistoryController.getApprovalHistory";
import submitForApprovalApex from "@salesforce/apex/ApprovalHistoryController.submitForApproval";
import reassignStep from "@salesforce/apex/ApprovalHistoryController.reassignStep";
import processStep from "@salesforce/apex/ApprovalHistoryController.processStep";
import searchUsers from "@salesforce/apex/ApprovalHistoryController.searchUsers";
import {
  verifyIfNextApproverWasNeeded,
  hideModal,
  showModal,
  getCommentPropertyFromModal,
  showGetNextApproverModal,
  validateUserLookup,
  setSelectedUser,
  clearModalState,
  displayToast,
  extractErrorMessage,
  modalStates,
  displayToastErrorQuery
} from "./approvalHistoryUtil.js";
import { ShowToastEvent } from "lightning/platformShowToastEvent";
import { refreshApex } from "@salesforce/apex";
 
const ERROR = "error";
const SUCCESS = "success";
 
const columns = [
  {
    label: "Step Name",
    fieldName: "stepUrl",
    type: "url",
    typeAttributes: {
      label: {
        fieldName: "stepName"
      }
    }
  },
  {
    label: "Date",
    fieldName: "createdDate",
    type: "date",
    typeAttributes: {
      month: "numeric",
      day: "numeric",
      year: "numeric",
      hour: "numeric",
      minute: "numeric"
    }
  },
  { label: "Status", fieldName: "stepStatus" },
  {
    label: "Assigned To",
    fieldName: "assignedToUrl",
    type: "url",
    typeAttributes: {
      label: {
        fieldName: "assignedTo"
      }
    }
  }
];
export default class ApprovalHistory extends LightningElement {
  @api recordId;
  @api showComments; //Determines if the component will show the comments column
  @api allowSubmitForApproval; //determines if the component will allow Submit for Approval functionality
  @track approvalHistory; //approval history to display on page
  wiredApprovalHistory; //property used to refreshApex
 
  //modal properties
  currentModalState; //decides which type of modal to display depending on the action happening(approve, reject, recall, etc)
  modalComment; //temporarily stores the value of the comment input field in the modal
  selectedUser; //temporarily stores the selected user from the lookup component in the modal
  //end modal properties
 
  //lookup properties
  lookupErrors = []; //errors related to the lookup component
  isMultiEntry = false;
  initialSelection = [];
  //end lookup properties
 
  get columns() {
    let tempColumns = columns;
    if(this.showComments){
      tempColumns.push({
        label: "Comments", fieldName: "comments" 
      });
    } 
 
    return tempColumns;
  }
 
  //url that will display all of the approval process history
  get viewAllUrl() {
    return "/lightning/r/" + this.recordId + "/related/ProcessSteps/view";
  }
 
 
 
// test    a2K1m00000056bKEAQ  a0y10000005jnql
   connectedCallback(){
      getApprovalHistory({recordId:'a0y10000005jnql'}).then((value)=>{
        console.log(value)
        var a = value;
      
        this.wiredApprovalHistory = value;
        this.approvalHistory = value;
        
        debugger;
      })
  }
 
  //decides if it will show the menu that may two buttons, the recall and the reassign 
  get showButtonMenu() {
    return (
      this.approvalHistory.isCurrentUserApprover ||
      this.approvalHistory.showRecall
    );
  }
 
  //decides if it will show the submit for approval button
  get showSubmitForApprovalButton() {
    return this.approvalHistory &&
      this.approvalHistory.showSubmitForApproval &&
      this.allowSubmitForApproval
      ? true
      : false;
  }
 
  //will return a different modal title depending on the state 
  get modalTitle() {
    if (this.currentModalState && modalStates[this.currentModalState])
      return modalStates[this.currentModalState].title;
    return "";
  }
  // will return a different label for the submit button, depending on the current state.(Approve, Reject, Submit, etc)
  get modalsubmitLabel() {
    if (this.currentModalState && modalStates[this.currentModalState])
      return modalStates[this.currentModalState].submitLabel;
    return "";
  } 
 
  // decides if the comment input field will show in the modal, based on the current state
  get showCommentModal() {
    return (
      this.currentModalState === modalStates.SUBMIT_APPROVAL.state ||
      this.currentModalState === modalStates.APPROVE.state ||
      this.currentModalState === modalStates.REJECT.state ||
      this.currentModalState === modalStates.RECALL.state
    );
  }
 
  // decides if the lookup component will show in the modal, based on the current state
  get showLookupModal() {
    return (
      this.currentModalState === modalStates.GET_NEXT_APPROVER_SUBMIT.state ||
      this.currentModalState === modalStates.REASSIGN.state ||
      this.currentModalState === modalStates.GET_NEXT_APPROVER_APPROVE.state
    );
  }
 
  // decides the label for the lookup component, based on the current state
  get lookupLabel() {
    return modalStates[this.currentModalState].lookupLabel;
  }
 
  get showDataTable() {
    return this.approvalHistory && this.approvalHistory.approvalSteps.length > 0
      ? true
      : false;
  }
 
  @wire(getApprovalHistory, { recordId: "$recordId" })
  debugger
  wiredGetApprovalHist(value) {
    this.wiredApprovalHistory = value;
    if (value.data) {
      this.approvalHistory = value.data;
    } else if (value.error) {
      displayToastErrorQuery(this, ShowToastEvent);
    }
  }
 
  refreshApprovalHistory() {
    refreshApex(this.wiredApprovalHistory);
  }
 
  @api
  submitForApproval(){
    this.handleSubmitForApprovalClick();
  }
  //button click handlers
  //the handlers show the modal and change the currentModalState depending on the button clicked
 
  handleSubmitForApprovalClick() {
    showModal(this);
    this.currentModalState = modalStates.SUBMIT_APPROVAL.state;
  }
 
  handleReassignClick() {
    showModal(this);
    this.currentModalState = modalStates.REASSIGN.state;
  }
 
  handleRecallClick() {
    showModal(this);
    this.currentModalState = modalStates.RECALL.state;
  }
 
  handleApproveClick() {
    showModal(this);
    this.currentModalState = modalStates.APPROVE.state;
  }
 
  handleRejectClick() {
    showModal(this);
    this.currentModalState = modalStates.REJECT.state;
  }
  //end button click handlers
 
  //this function submits for approval, if a next approver is needed, 
  //it will show the next approver modal which will trigger the submition again.
  submitForApprovalApexCall() {
    hideModal(this);
    submitForApprovalApex({
      recordId: this.recordId,
      comments: this.modalComment,
      nextApproverId: this.selectedUser
    })
      .then(result => {
        let jsonResult = JSON.parse(result);
        if (jsonResult.success) {
          displayToast(this, ShowToastEvent, SUCCESS);
          this.refreshApprovalHistory();
        } else {
          displayToast(this, ShowToastEvent, ERROR);
        }
        clearModalState(this);
      })
      .catch(error => {
        if (verifyIfNextApproverWasNeeded(error.body.pageErrors)) {
          showGetNextApproverModal(this, modalStates.GET_NEXT_APPROVER_SUBMIT.state);
        } else {
          let errorMessage = extractErrorMessage(error.body.pageErrors);
          displayToast(this, ShowToastEvent, ERROR, errorMessage);
          clearModalState(this);
        }
      });
  }
 
  //function that takes care of reassigning the pending step to the selected user.
  reassignApexCall() {
    hideModal(this);
    reassignStep({ recordId: this.recordId, newActorId: this.selectedUser })
      .then(() => {
        displayToast(this, ShowToastEvent, SUCCESS);
        clearModalState(this);
        this.refreshApprovalHistory();
      })
      .catch(() => {
        displayToast(this, ShowToastEvent, ERROR);
        clearModalState(this);
      });
  }
 
  //function that takes care of approve, reject, and recall. If next approver is needed
  //it will show the next approver modal which will trigger the submition again.
  processStepApexCall(action) {
    hideModal(this);
    processStep({
      recordId: this.recordId,
      comments: this.modalComment,
      nextApproverId: this.selectedUser,
      action: action
    })
      .then(result => {
        let jsonResult = JSON.parse(result);
        if (jsonResult.success) {
          displayToast(this, ShowToastEvent, SUCCESS);
          this.refreshApprovalHistory();
        } else {
          displayToast(this, ShowToastEvent, ERROR);
        }
        clearModalState(this);
      })
      .catch(error => {
        if (verifyIfNextApproverWasNeeded(error.body.pageErrors)) {
          showGetNextApproverModal(this, modalStates.GET_NEXT_APPROVER_APPROVE.state);
        } else {
          let errorMessage = extractErrorMessage(error.body.pageErrors);
          displayToast(this, ShowToastEvent, ERROR, errorMessage);
          clearModalState(this);
        }
      });
  }
 
  handleModalCancel() {
    hideModal(this);
    clearModalState(this);
  }
 
  //function that handles the modal Submit button.
  //depending on the current state, it will call the appropriate imperative method
  handleModalSubmit() {
    switch (this.currentModalState) {
      case modalStates.SUBMIT_APPROVAL.state:
        this.modalComment = getCommentPropertyFromModal(this);
        this.submitForApprovalApexCall();
        break;
      case modalStates.GET_NEXT_APPROVER_SUBMIT.state:
        if (validateUserLookup(this)) {
          setSelectedUser(this);
          this.submitForApprovalApexCall();
        }
        break;
      case modalStates.REASSIGN.state:
        if (validateUserLookup(this)) {
          setSelectedUser(this);
          this.reassignApexCall();
        }
        break;
      case modalStates.APPROVE.state:
        this.modalComment = getCommentPropertyFromModal(this);
        this.processStepApexCall(modalStates.APPROVE.action);
        break;
      case modalStates.GET_NEXT_APPROVER_APPROVE.state:
        if (validateUserLookup(this)) {
          setSelectedUser(this);
          this.processStepApexCall(modalStates.APPROVE.action);
        }
        break;
      case modalStates.RECALL.state:
        this.modalComment = getCommentPropertyFromModal(this);
        this.processStepApexCall(modalStates.RECALL.action);
        break;
      case modalStates.REJECT.state:
        this.modalComment = getCommentPropertyFromModal(this);
        this.processStepApexCall(modalStates.REJECT.action);
        break;
      default:
        break;
    }
  }
 
  //searches the users based on the lookup component search event.
  handleLookupSearch(event) {
    searchUsers(event.detail)
      .then(results => {
        this.template.querySelector("c-lookup").setSearchResults(results);
      })
      .catch(error => {
        this.lookupErrors = [error];
      });
  }
 
  handleSelectionChange() {
    this.lookupErrors = [];
  }
}