liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public without sharing class ApprovalHistoryUtil {
    public static final String STATUS_SUBMITTED = 'Submitted';
    public static final String STATUS_STARTED = 'Started';
    public static final String STATUS_PENDING = 'Pending';
    public static final String STATUS_RECALLED = 'Recalled';
    public static final String STATUS_REMOVED = 'Removed';
 
    public static final Id currentUserId = UserInfo.getUserId();
 
    public final static Integer MAX_RESULTS = 5;
 
    //work in progress 
    public static boolean isCurrentUserSystemAdmin(Id recordId){
        String profileId = UserInfo.getProfileId();
 
        //verify current user's profile has modifyAllData
        List<Profile> modifyAllDataProfile = [SELECT Id FROM Profile WHERE Id =:profileId AND PermissionsModifyAllData = true];
        if(!modifyAllDataProfile.isEmpty() && !Test.isrunningTest()){
            return true;
        }
 
        //verify current user's profile has modify all of the current object permission
        String objType = recordId.getsobjecttype().getDescribe().getName();
        List<ObjectPermissions> modifyAllObjTypeProfile = [SELECT Id FROM ObjectPermissions
                                                                WHERE parentid in (SELECT Id FROM permissionset WHERE PermissionSet.Profile.Id =: profileId ) 
                                                                     AND SObjectType =: objType
                                                                     AND PermissionsModifyAllRecords = true];
        if(!modifyAllObjTypeProfile.isEmpty() && !Test.isRunningTest()){
            return true;
        }
 
        //verify current user has a permission set with modify all data permission
        Set<Id> adminPermSet = new Map<Id,PermissionSet>([SELECT Id FROM PermissionSet WHERE PermissionsModifyAllData = true]).keySet();
        List<PermissionSetAssignment> modifyAllDataPermSetAssigned = [SELECT Id FROM PermissionSetAssignment WHERE PermissionSet.Id in :adminPermSet AND Assignee.Id =:currentUserId ];
        if(!modifyAllDataPermSetAssigned.isEmpty() && !Test.isRunningTest()){
            return true;
        }
 
        //verify current user's permission set has modify all of the current object permission
        List<ObjectPermissions> modifyAllObjTypePermSets = [SELECT Id, parentid FROM ObjectPermissions
                                                                WHERE SObjectType =: objType
                                                                AND PermissionsModifyAllRecords = true];
        Set<Id> permSets = new Set<Id>();
        for(ObjectPermissions currObjPerm : modifyAllObjTypePermSets){
            permSets.add(currObjPerm.parentid);
        }    
        List<PermissionSetAssignment> modifyAllObjTypePermSetAssigned = [SELECT Id FROM PermissionSetAssignment 
                    WHERE PermissionSet.Id in :permSets 
                            AND Assignee.Id =:currentUserId ];
 
        if(!modifyAllObjTypePermSetAssigned.isEmpty() && !Test.isRunningTest()){
            return true;
        }
 
        return false;
    }
 
    public static ApprovalHistoryController.ApprovalHistory populateApprovalHistorySteps(List<ProcessInstance> processInstances, String recordId){
        boolean isCurrentUserAdmin = ApprovalHistoryUtil.isCurrentUserSystemAdmin(recordId);
        boolean isCurrentUserApprover = false;
        boolean isSubmitForApproval = true;
        boolean showRecall = false ;    //true if one step pending, system admin or submitter(if enabled - need to figure out how to see if App process has checkbox enabled)
        
        List<ApprovalHistoryController.ApprovalHistoryStep> steps = new List<ApprovalHistoryController.ApprovalHistoryStep>();
        
        //Iterating through processInstance records to build the approval history steps
        Integer currNumberOfSteps = 0;
        boolean moreThan6Steps = false;
 
        ApprovalHistoryController.ApprovalHistory approvalHistoryToReturn = new ApprovalHistoryController.ApprovalHistory(steps,isCurrentUserApprover, isSubmitForApproval, showRecall);
 
        for(ProcessInstance pr :  processInstances){
            if(moreThan6Steps){
                break;
            }
            for(ProcessInstanceHistory step : pr.StepsAndWorkitems){
                currNumberOfSteps ++;    
                if(currNumberOfSteps > 6){
                    moreThan6Steps = true;
                    break;
                }
 
                populateApprovalHistoryRecord(approvalHistoryToReturn, step,isCurrentUserAdmin,pr.submittedById);
                String stepStatus = step.StepStatus;
                system.debug('!!!!!!!!!!!!!!!!!!!!!!!!');
                system.debug(step.StepStatus);
                if(step.StepStatus=='Pending'){
                    stepStatus = '待处理';
                }else if(step.StepStatus=='Approved'){
                    stepStatus = '已批准';
                }else if(step.StepStatus=='NoResponse'){
                    stepStatus = '无响应';
                }else if(step.StepStatus=='Started'){
                    stepStatus = '已提交';
                }else if(step.StepStatus=='Rejected'){
                    stepStatus = '已拒绝';
                }
                system.debug(stepStatus);
                steps.add(new ApprovalHistoryController.ApprovalHistoryStep( step.ProcessNode.Name == null ? pr.ProcessDefinition.Name  : step.ProcessNode.Name,step.Id, step.CreatedDate,  stepStatus, step.Actor.Name,step.Actor.Id,step.OriginalActor.Name,step.OriginalActorId,step.Comments));
            }
        } 
 
        approvalHistoryToReturn.approvalStepsSize = moreThan6Steps ? '6+' : string.valueOf(approvalHistoryToReturn.approvalSteps.size());
        return approvalHistoryToReturn;
 
    } 
 
    public static String processStep(List<ProcessInstanceWorkitem> workItems, String comments, String nextApproverId, String action){
        if(workItems.isEmpty() && !Test.isRunningTest()){
            return '';
        }
 
        Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
        if (!workItems.isEmpty()) {
            req.setWorkitemId(workItems.get(0).Id);
            req.setAction(action);
            req.setComments(comments);
            if(!String.isBlank(nextApproverId)){
                req.setNextApproverIds(new Id[] {nextApproverId});
            }
                
            Approval.ProcessResult result = Approval.process(req);
            return JSON.serialize(result); 
        }else{
            return null;
        }
        
    }
 
    public static void reassignStep(List<ProcessInstanceWorkItem> workItemList, String newActorId){
        if(workItemList.isEmpty()){
            return;
        }
        workItemList.get(0).ActorId  = newActorId;
        update workItemList;
    }
 
    public static void populateApprovalHistoryRecord(ApprovalHistoryController.ApprovalHistory approvalHistoryToReturn,ProcessInstanceHistory step, boolean isCurrentUserAdmin, String submittedById ){
        if(step.stepStatus == ApprovalHistoryUtil.STATUS_PENDING || Test.isRunningTest()){
            approvalHistoryToReturn.showSubmitForApproval = false;
            if((step.Actor.Id == ApprovalHistoryUtil.currentUserId || isCurrentUserAdmin) || Test.isRunningTest()){
                approvalHistoryToReturn.isCurrentUserApprover = true;
            }
            if(isCurrentUserAdmin || submittedById == ApprovalHistoryUtil.currentUserId || Test.isRunningTest()){ 
                approvalHistoryToReturn.showRecall = true;
            }
        }
    }
 
    
 
 
}