Skip to content

Commit

Permalink
TOLK-2253: Frilanstolk skal bare ha tilgang til oppdrag de er tildelt…
Browse files Browse the repository at this point in the history
… (ikke reservert) (#1725)

* Fikset formatering

* Flyttet logikk rundt tilgangsstyring ved sletting av Assigned Resource til riktig klasse

* Endring delingslogikk så tilgang bare blir gitt når status er tildelt (Dispatched)

* Update HOT_ServiceAppointmentsSharingSrvTest.cls

* Update HOT_WageClaimServiceTest.cls

* Flyttet Test.startTest for å fordele limits bedre.

---------

Co-authored-by: Edvard Brekke <[email protected]>
  • Loading branch information
chfosli and EdvardBrekke authored Aug 14, 2023
1 parent c628a1a commit 2f25f27
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ public without sharing class HOT_AssignedResourceAccessHandler extends MyTrigger
Map<Id, Id> apppointmentByAssignment = new Map<Id, Id>();
Map<Id, Id> resourceByAssignment = new Map<Id, Id>();

for (AssignedResource ar : (List<AssignedResource>) records) {
apppointmentByAssignment.put(ar.Id, ar.ServiceAppointmentId);
resourceByAssignment.put(ar.Id, ar.ServiceResourceId);
for (AssignedResource ar : [
SELECT Id, ServiceAppointment.Status, ServiceResourceId
FROM AssignedResource
WHERE Id IN :records
]) {
if (ar.ServiceAppointment.Status == 'Dispatched') {
apppointmentByAssignment.put(ar.Id, ar.ServiceAppointmentId);
resourceByAssignment.put(ar.Id, ar.ServiceResourceId);
}
}

HOT_ServiceAppointmentsSharingService.grantAccess(apppointmentByAssignment, resourceByAssignment);
}

public override void onAfterUpdate(Map<Id, sObject> triggerOldMap) {
Map<Id, Id> apppointmentByAssignment = new Map<Id, Id>();
Map<Id, Id> resourceToRemoveByAssignment = new Map<Id, Id>();
Map<Id, Id> resourceToAddByAssignment = new Map<Id, Id>();
Boolean runSharingService = false;

for (AssignedResource ar : (List<AssignedResource>) records) {
for (AssignedResource ar : [
SELECT Id, ServiceAppointment.Status, ServiceResourceId
FROM AssignedResource
WHERE Id IN :records
]) {
AssignedResource oldRecord = (AssignedResource) triggerOldMap.get(ar.Id);
if (ar.ServiceResourceId != oldRecord.ServiceResourceId) {
if (ar.ServiceResourceId != oldRecord.ServiceResourceId && ar.ServiceAppointment.Status == 'Dispatched') {
apppointmentByAssignment.put(ar.Id, ar.ServiceAppointmentId);
resourceToAddByAssignment.put(ar.Id, ar.ServiceResourceId);
resourceToRemoveByAssignment.put(ar.Id, oldRecord.ServiceResourceId);
runSharingService = true;
}
}
Expand All @@ -34,12 +42,12 @@ public without sharing class HOT_AssignedResourceAccessHandler extends MyTrigger

public override void onBeforeDelete() {
Map<Id, Id> apppointmentByAssignment = new Map<Id, Id>();
Map<Id, Id> resourceByAssignment = new Map<Id, Id>();

for (AssignedResource ar : (List<AssignedResource>) records) {
apppointmentByAssignment.put(ar.Id, ar.ServiceAppointmentId);
resourceByAssignment.put(ar.Id, ar.ServiceResourceId);
}

update setRecalculateManualSharingFlagOnServiceAppointment(apppointmentByAssignment.values());
}

public static List<ServiceAppointment> setRecalculateManualSharingFlagOnServiceAppointment(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@IsTest
private class HOT_AssignedResourceAccessHandlerTest {
@IsTest
private static void testAssignedResourceHasAccessToServiceAppointmentAfterInsert() {
private static void testAssignedResourceHasAccessToServiceAppointmentAfterInsertScheduled() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' OR Name = 'Standardbruker' LIMIT 1];
User user1 = HOT_TestDataFactory.createUser('user1', profile);
insert user1;
Expand Down Expand Up @@ -29,7 +29,7 @@ private class HOT_AssignedResourceAccessHandlerTest {
insert assignedResource;

System.assertEquals(
1,
0,
[
SELECT COUNT()
FROM ServiceAppointmentShare
Expand All @@ -39,7 +39,7 @@ private class HOT_AssignedResourceAccessHandlerTest {
}

@IsTest
private static void testSharingRecalculationFlagWhenAssignedResourceIsDeleted() {
private static void testAssignedResourceHasAccessToServiceAppointmentAfterInsertDispatched() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' OR Name = 'Standardbruker' LIMIT 1];
User user1 = HOT_TestDataFactory.createUser('user1', profile);
insert user1;
Expand All @@ -57,21 +57,59 @@ private class HOT_AssignedResourceAccessHandlerTest {
request.Status__c = 'Godkjent';
update request;

Id serviceAppointmentId = [SELECT Id FROM ServiceAppointment LIMIT 1].Id;
ServiceAppointment sa = [SELECT Id, Status FROM ServiceAppointment LIMIT 1];
Id serviceResourceId = [SELECT Id FROM ServiceResource LIMIT 1].Id;
Id userId = [SELECT RelatedRecordId FROM ServiceResource LIMIT 1].RelatedRecordId;
AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(
serviceAppointmentId,
serviceResourceId
AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(sa.Id, serviceResourceId);
insert assignedResource;

sa.Status = 'Dispatched';
update sa;

System.assertEquals(
1,
[
SELECT COUNT()
FROM ServiceAppointmentShare
WHERE UserOrGroupId = :userId AND ParentId = :sa.Id AND RowCause = 'Manual'
]
);
}

@IsTest
private static void testSharingRecalculationFlagWhenAssignedResourceIsDeleted() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' OR Name = 'Standardbruker' LIMIT 1];
User user1 = HOT_TestDataFactory.createUser('user1', profile);
insert user1;
ServiceResource resource1 = HOT_TestDataFactory.createServiceResource(user1.Id);
insert resource1;
User user2 = HOT_TestDataFactory.createUser('user2', profile);
insert user2;
ServiceResource resource2 = HOT_TestDataFactory.createServiceResource(user2.Id);
insert resource2;

WorkType workType = HOT_TestDataFactory.createWorkType();
insert workType;
HOT_Request__c request = HOT_TestDataFactory.createRequest('TEST', workType);
insert request;
request.Status__c = 'Godkjent';
update request;

ServiceAppointment sa = [SELECT Id, Status FROM ServiceAppointment LIMIT 1];
Id serviceResourceId = [SELECT Id FROM ServiceResource LIMIT 1].Id;
Id userId = [SELECT RelatedRecordId FROM ServiceResource LIMIT 1].RelatedRecordId;
AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(sa.Id, serviceResourceId);
insert assignedResource;

sa.Status = 'Dispatched';
update sa;

System.assertEquals(
1,
[
SELECT COUNT()
FROM ServiceAppointmentShare
WHERE UserOrGroupId = :userId AND ParentId = :serviceAppointmentId AND RowCause = 'Manual'
WHERE UserOrGroupId = :userId AND ParentId = :sa.Id AND RowCause = 'Manual'
]
);

Expand All @@ -82,13 +120,13 @@ private class HOT_AssignedResourceAccessHandlerTest {
ServiceAppointment serviceAppointment = [
SELECT HOT_IsRecalculateManualSharing__c
FROM ServiceAppointment
WHERE Id = :serviceAppointmentId
WHERE Id = :sa.Id
];
System.assertEquals(true, serviceAppointment.HOT_IsRecalculateManualSharing__c);
}

@IsTest
private static void testWhenAssignedResourceIsChanged() {
private static void testWhenAssignedResourceIsChangedBeforeDispatched() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' OR Name = 'Standardbruker' LIMIT 1];
User user1 = HOT_TestDataFactory.createUser('user1', profile);
insert user1;
Expand All @@ -106,19 +144,84 @@ private class HOT_AssignedResourceAccessHandlerTest {
request.Status__c = 'Godkjent';
update request;

Id serviceAppointmentId = [SELECT Id FROM ServiceAppointment LIMIT 1].Id;
ServiceAppointment sa = [SELECT Id, Status FROM ServiceAppointment LIMIT 1];
List<ServiceResource> serviceResources = [SELECT Id, RelatedRecordId FROM ServiceResource];
Id serviceResourceId1 = serviceResources[0].Id;
Id userId1 = serviceResources[0].RelatedRecordId;
AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(
serviceAppointmentId,
serviceResourceId1
AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(sa.Id, serviceResourceId1);
insert assignedResource;

Id serviceResourceId2 = serviceResources[1].Id;
Id userId2 = serviceResources[1].RelatedRecordId;

Test.startTest();
assignedResource.ServiceResourceId = serviceResourceId2;
update assignedResource;
Test.stopTest();

ServiceAppointment serviceAppointment = [
SELECT HOT_IsRecalculateManualSharing__c
FROM ServiceAppointment
WHERE Id = :sa.Id
];
System.assertEquals(false, serviceAppointment.HOT_IsRecalculateManualSharing__c);

System.assertEquals(
0,
[
SELECT COUNT()
FROM ServiceAppointmentShare
WHERE
(UserOrGroupId = :userId1
OR UserOrGroupId = :userId2)
AND ParentId = :sa.Id
AND RowCause = 'Manual'
]
);
}

@IsTest
private static void testWhenAssignedResourceIsChangedAfterDispatched() {
Profile profile = [SELECT Id FROM Profile WHERE Name = 'Standard User' OR Name = 'Standardbruker' LIMIT 1];
User user1 = HOT_TestDataFactory.createUser('user1', profile);
insert user1;
ServiceResource resource1 = HOT_TestDataFactory.createServiceResource(user1.Id);
insert resource1;
User user2 = HOT_TestDataFactory.createUser('user2', profile);
insert user2;
ServiceResource resource2 = HOT_TestDataFactory.createServiceResource(user2.Id);
insert resource2;

WorkType workType = HOT_TestDataFactory.createWorkType();
insert workType;
HOT_Request__c request = HOT_TestDataFactory.createRequest('TEST', workType);
insert request;
request.Status__c = 'Godkjent';
update request;

ServiceAppointment sa = [SELECT Id, Status FROM ServiceAppointment LIMIT 1];
List<ServiceResource> serviceResources = [SELECT Id, RelatedRecordId FROM ServiceResource];
Id serviceResourceId1 = serviceResources[0].Id;
Id userId1 = serviceResources[0].RelatedRecordId;

AssignedResource assignedResource = HOT_TestDataFactory.createAssignedResource(sa.Id, serviceResourceId1);
insert assignedResource;

sa.Status = 'Dispatched';
update sa;

Id serviceResourceId2 = serviceResources[1].Id;
Id userId2 = serviceResources[1].RelatedRecordId;

System.assertEquals(
1,
[
SELECT COUNT()
FROM ServiceAppointmentShare
WHERE UserOrGroupId = :userId1 AND ParentId = :sa.Id AND RowCause = 'Manual'
]
);

Test.startTest();
assignedResource.ServiceResourceId = serviceResourceId2;
update assignedResource;
Expand All @@ -127,8 +230,17 @@ private class HOT_AssignedResourceAccessHandlerTest {
ServiceAppointment serviceAppointment = [
SELECT HOT_IsRecalculateManualSharing__c
FROM ServiceAppointment
WHERE Id = :serviceAppointmentId
WHERE Id = :sa.Id
];
System.assertEquals(true, serviceAppointment.HOT_IsRecalculateManualSharing__c);

System.assertEquals(
1,
[
SELECT COUNT()
FROM ServiceAppointmentShare
WHERE UserOrGroupId = :userId2 AND ParentId = :sa.Id AND RowCause = 'Manual'
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public without sharing class HOT_ServiceAppointmentAccessHandler extends MyTriggers {
public override void onAfterUpdate(Map<Id, sObject> triggerOldMap) {
List<Id> serviceAppointmentsWithUpdatedStatusDispatched = new List<Id>();

for (ServiceAppointment sa : (List<ServiceAppointment>) records) {
ServiceAppointment oldRecord = (ServiceAppointment) triggerOldMap.get(sa.Id);
if (sa.Status != oldRecord.Status && sa.Status == 'Dispatched') {
serviceAppointmentsWithUpdatedStatusDispatched.add(sa.Id);
}
}

Map<Id, Id> apppointmentByAssignment = new Map<Id, Id>();
Map<Id, Id> resourceToAddByAssignment = new Map<Id, Id>();

for (AssignedResource ar : [
SELECT Id, ServiceAppointmentId, ServiceResourceId
FROM AssignedResource
WHERE ServiceAppointmentId IN :serviceAppointmentsWithUpdatedStatusDispatched
]) {
apppointmentByAssignment.put(ar.Id, ar.ServiceAppointmentId);
resourceToAddByAssignment.put(ar.Id, ar.ServiceResourceId);
}

if (serviceAppointmentsWithUpdatedStatusDispatched.size() > 0) {
update HOT_AssignedResourceAccessHandler.setRecalculateManualSharingFlagOnServiceAppointment(
serviceAppointmentsWithUpdatedStatusDispatched
);
HOT_ServiceAppointmentsSharingService.grantAccess(apppointmentByAssignment, resourceToAddByAssignment);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>58.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ private class HOT_ServiceAppointmentsSharingSrvTest {
];
serviceAppointment.HOT_IsRecalculateManualSharing__c = true;
serviceAppointment.HOT_ServiceResource__c = null;
serviceAppointment.Status = 'Dispatched';
update serviceAppointment;

serviceAppointment = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public class HOT_InterpreterOnWorkOrderServiceTest {
resource2.Name = 'resource2';
resource3.Name = 'resource3';
update new List<ServiceResource>{ resource1, resource2, resource3 };

Test.startTest();
// Add assigned resources
AssignedResource assignedResource1 = HOT_TestDataFactory.createAssignedResource(
serviceAppointment1.Id,
Expand All @@ -318,7 +318,6 @@ public class HOT_InterpreterOnWorkOrderServiceTest {
workOrder.Status = 'Dispatched';
update workOrder;

Test.startTest();
delete assignedResource2;
Test.stopTest();

Expand Down Expand Up @@ -453,7 +452,7 @@ public class HOT_InterpreterOnWorkOrderServiceTest {
];
workOrder.Status = 'Dispatched';
update workOrder;

Test.startTest();
// Add assigned resources
AssignedResource assignedResource1 = HOT_TestDataFactory.createAssignedResource(
serviceAppointment1.Id,
Expand All @@ -466,7 +465,7 @@ public class HOT_InterpreterOnWorkOrderServiceTest {
insert new List<AssignedResource>{ assignedResource1, assignedResource2 };

assignedResource2.ServiceResourceId = resource3.Id;
Test.startTest();

update assignedResource2;
Test.stopTest();

Expand Down Expand Up @@ -522,4 +521,4 @@ public class HOT_InterpreterOnWorkOrderServiceTest {
);
System.debug(workOrder.HOT_Interpreters__c);
}
}
}
Loading

0 comments on commit 2f25f27

Please sign in to comment.