diff --git a/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls b/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls new file mode 100644 index 00000000..ff47277e --- /dev/null +++ b/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls @@ -0,0 +1,108 @@ +/* +convert UriEvent to Log__c record with lookup to Person Ident +Make it possible for NAV's "snokelogg" to get log elemnts +*/ +public with sharing class HOT_LosUriEventService { + public static void addPersonRelatedRecordsToLog(List uriEvents) { + List logs = new List(); + + Map uriEventByRecordId = new Map(); + + //record ids for each object we want to log + Set hotClaimRecordIds = new Set(); + Set hotClaimLineItemsRecordIds = new Set(); + Set hotEntitlementRecordIds = new Set(); + + //check if object type should be logged + for (UriEvent ue : uriEvents) { + //only standard users are logged + if (ue.UserType != 'Standard') + continue; + + switch on ue.QueriedEntities { + when 'HOT_Claim__c' { + uriEventByRecordId.put(ue.RecordId, ue); + hotClaimRecordIds.add(ue.RecordId); + } + when 'HOT_ClaimLineItem__c' { + uriEventByRecordId.put(ue.RecordId, ue); + hotClaimLineItemsRecordIds.add(ue.RecordId); + } + when 'HOT_Entitlement__c' { + uriEventByRecordId.put(ue.RecordId, ue); + hotEntitlementRecordIds.add(ue.RecordId); + } + } + } + + //get related person ident for each object + Map personIdentByRecordId = getPersonIdentsForEachObject( + hotClaimRecordIds, + hotClaimLineItemsRecordIds, + hotEntitlementRecordIds + ); + + //add log elemnt with person ident + for (Id recordId : personIdentByRecordId.keyset()) { + logs.add(addLogElement(uriEventByRecordId.get(recordId), personIdentByRecordId.get(recordId))); + } + + if (logs.size() > 0) { + insert logs; + } + } + + private static Map getPersonIdentsForEachObject( + Set hotClaimRecordIds, + Set hotClaimLineItemsRecordIds, + Set hotEntitlementRecordIds + ) { + Map personIdentByRecordId = new Map(); + + //HOT Claim + if (hotClaimRecordIds.size() > 0) { + for (HOT_Claim__c claim : [ + SELECT Id, Claimant__r.CRM_Person__r.Name + FROM HOT_Claim__c + WHERE Id IN :hotClaimRecordIds + ]) { + personIdentByRecordId.put(claim.Id, claim.Claimant__r.CRM_Person__r.Name); + } + } + //HOT ClaimLineItem + if (hotClaimLineItemsRecordIds.size() > 0) { + for (HOT_ClaimLineItem__c claimLineItem : [ + SELECT Id, Claim__r.Claimant__r.CRM_Person__r.Name + FROM HOT_ClaimLineItem__c + WHERE Id IN :hotClaimLineItemsRecordIds + ]) { + personIdentByRecordId.put(claimLineItem.Id, claimLineItem.Claim__r.Claimant__r.CRM_Person__r.Name); + } + } + //HOT Entitlement + if (hotEntitlementRecordIds.size() > 0) { + for (HOT_Entitlement__c entitlement : [ + SELECT Id, Account__r.CRM_Person__r.Name + FROM HOT_Entitlement__c + WHERE Id IN :hotEntitlementRecordIds + ]) { + personIdentByRecordId.put(entitlement.Id, entitlement.Account__r.CRM_Person__r.Name); + } + } + + return personIdentByRecordId; + } + + private static Log__c addLogElement(UriEvent ue, String personIdent) { + return new Log__c( + CRM_RecordId__c = ue.RecordId, + CRM_EventDate__c = ue.EventDate, + CRM_QueriedEntity__c = ue.QueriedEntities, + CRM_Username__c = ue.Username, + CRM_Operation__c = ue.Operation, + CRM_PersonIdent__c = personIdent, + CRM_Type__c = 'LightningUriEvent', + CRM_Status__c = 'Completed' + ); + } +} diff --git a/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls-meta.xml b/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls-meta.xml new file mode 100644 index 00000000..998805a8 --- /dev/null +++ b/force-app/main/eventMonitoring/classes/HOT_LosUriEventService.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active + diff --git a/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls b/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls new file mode 100644 index 00000000..68837415 --- /dev/null +++ b/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls @@ -0,0 +1,114 @@ +@IsTest +private class HOT_LosUriEventServiceTest { + @TestSetup + static void makeData() { + Account acc = new Account(); + acc.Name = 'Ola Lesehjelp'; + insert acc; + Account acc2 = new Account(); + acc2.INT_PersonIdent__c = '01010101010'; + acc2.Name = 'Kari Bruker'; + acc2.INT_KrrMobilePhone__c = '12345678'; + insert acc2; + HOT_Claim__c claim = HOT_LesehjelpTestDataFactory.createClaim(acc2, acc); + claim.UserPersonNumber__c = '01010101010'; + insert claim; + HOT_ClaimLineItem__c cli = HOT_LesehjelpTestDataFactory.createClaimLineItems(claim); + insert cli; + HOT_Entitlement__c entitlement = HOT_LesehjelpTestDataFactory.createEntitlement(acc2); + insert entitlement; + } + + @IsTest + private static void addPersonRelatedRecordsToLog_forHotClaim() { + HOT_Claim__c claim = [ + SELECT Id, Claimant__c, Claimant__r.CRM_Person__r.Name + FROM HOT_Claim__c + LIMIT 1 + ]; + + List uriEvents = new List(); + uriEvents.add(new UriEvent(claim.Id, DateTime.now(), 'HOT_Claim__c', 'Read', 'user@user.nav.no', 'Standard')); + + Test.startTest(); + HOT_LosUriEventService.addPersonRelatedRecordsToLog(uriEvents); + Test.stopTest(); + + List logs = [ + SELECT CRM_PersonIdent__c + FROM Log__c + WHERE CRM_Type__c = 'LightningUriEvent' AND CRM_QueriedEntity__c = 'HOT_Claim__c' + ]; + System.assertEquals(1, logs.size(), 'One log element is created'); + System.assertEquals( + claim.Claimant__r.CRM_Person__r.Name, + logs[0].CRM_PersonIdent__c, + 'Log element should have persons fødselsr / dnr' + ); + } + @IsTest + private static void addPersonRelatedRecordsToLog_forHotClaimLineItem() { + HOT_ClaimLineItem__c claimLineItem = [ + SELECT Id, Claim__r.Claimant__r.CRM_Person__r.Name + FROM HOT_ClaimLineItem__c + LIMIT 1 + ]; + + List uriEvents = new List(); + uriEvents.add( + new UriEvent( + claimLineItem.Id, + DateTime.now(), + 'HOT_ClaimLineItem__c', + 'Read', + 'user@user.nav.no', + 'Standard' + ) + ); + + Test.startTest(); + HOT_LosUriEventService.addPersonRelatedRecordsToLog(uriEvents); + Test.stopTest(); + + List logs = [ + SELECT CRM_PersonIdent__c + FROM Log__c + WHERE CRM_Type__c = 'LightningUriEvent' AND CRM_QueriedEntity__c = 'HOT_ClaimLineItem__c' + ]; + System.assertEquals(1, logs.size(), 'One log element is created'); + System.assertEquals( + claimLineItem.Claim__r.Claimant__r.CRM_Person__r.Name, + logs[0].CRM_PersonIdent__c, + 'Log element should have persons fødselsr / dnr' + ); + } + @IsTest + private static void addPersonRelatedRecordsToLog_forHotEntitlement() { + HOT_Entitlement__c entitlement = [ + SELECT Id, Account__r.CRM_Person__r.Name + FROM HOT_Entitlement__c + LIMIT 1 + ]; + + List uriEvents = new List(); + uriEvents.add( + new UriEvent(entitlement.Id, DateTime.now(), 'HOT_Entitlement__c', 'Read', 'user@user.nav.no', 'Standard') + ); + + Test.startTest(); + HOT_LosUriEventService.addPersonRelatedRecordsToLog(uriEvents); + Test.stopTest(); + + List logs = [ + SELECT CRM_PersonIdent__c + FROM Log__c + WHERE CRM_Type__c = 'LightningUriEvent' AND CRM_QueriedEntity__c = 'HOT_Entitlement__c' + ]; + System.assertEquals(1, logs.size(), 'One log element is created'); + System.assertEquals( + entitlement.Account__r.CRM_Person__r.Name, + logs[0].CRM_PersonIdent__c, + 'Log element should have persons fødselsr / dnr' + ); + } +} diff --git a/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls-meta.xml b/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls-meta.xml new file mode 100644 index 00000000..998805a8 --- /dev/null +++ b/force-app/main/eventMonitoring/classes/HOT_LosUriEventServiceTest.cls-meta.xml @@ -0,0 +1,5 @@ + + + 62.0 + Active +