Skip to content

Commit

Permalink
Begynnelse på sms notifikasjonsklasse
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreMarthinsen committed Sep 5, 2024
1 parent 16d1a9c commit 2cc88c8
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
101 changes: 101 additions & 0 deletions force-app/main/notification/classes/HOT_IncomingSMSNotification.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
public with sharing class HOT_IncomingSMSNotification {
public static String DISPATCH_INCOMING_NUMBER = '41716090';

public static void notifyDispatchersOnIncomingSMS(List<SMS__c> messages) {

Map<Id, SMS__c> senderAccountIdToSms = new Map<Id, SMS__c>();

for(SMS__c sms : messages) {
if(shouldNotifyDispatchersAboutSMS(sms)) {
senderAccountIdToSms.put(sms.Account__c, sms);
}
}

List<Person__c> senders = [
SELECT Id, CRM_Account__c, INT_RegionNumber__c, INT_MunicipalityNumber__c FROM Person__c
WHERE CRM_Account__c IN :senderAccountIdToSms.keySet()
];

List<Id> queueIds = getGroupIdsFromSenders(senders);
Map<Id, Id> groupByQueue = HOT_NotificationHandler.getGroupIdByQueueId(queueIds);

CustomNotificationType notificationType = [
SELECT Id, DeveloperName
FROM CustomNotificationType
WHERE DeveloperName = 'HOT_NotifyDispatcher'
];

Integer index = 0;

for(Person__c sender : senders) {
SMS__c sms = senderAccountIdToSms.get(sender.CRM_Account__c);
String title = 'Melding fra ' + sender.Name;
String body = sms.Message__c;

Messaging.CustomNotification notification = HOT_NotificationHandler.prepareNotification(
title,
body,
notificationType.Id,
sms.Id
);

HOT_NotificationHandler.sendNotification(
notification,
new Set<String>{(String) groupByQueue.get(queueIds[index])},
(SObject) sms
);
index++;
}
}




@TestVisible
private static List<Id> getGroupIdsFromSenders(List<Person__c> senders) {

List<SobjectWrapper> wrappers = new List<SobjectWrapper>();
Integer senderIndex = 0;

for(Person__c sender : senders) {
SobjectWrapper wrapper = new SobjectWrapper(
senderIndex,
sender.INT_MunicipalityNumber__c,
sender.INT_RegionNumber__c
);
wrapper.confidential = 'Ugradert';
wrappers.add(wrapper);
senderIndex++;
}

Map<Integer, ApexSharingRuleWrapper> indexToSharingRule = RecordOwnerService.getQueuesAndNavUnits(
wrappers,
'Person__c'
);

System.assert(indexToSharingRule.size() != 0, indexToSharingRule.size() + ' records i indexToSharingRuleMap should not be empty');

senderIndex = 0;
List<Id> queueIds = new List<Id>();

for(Person__c sender : senders) {
ApexSharingRuleWrapper rule = indexToSharingRule.get(senderIndex);
//TODO: Tar ikke hensyn til manglende funn i RecordOwnerService
queueIds.add(rule.queueId);
senderIndex++;
}

return queueIds;
}




@TestVisible
private static Boolean shouldNotifyDispatchersAboutSMS(SMS__c sms) {
return sms.Account__c != null
&& sms.Domain__c == 'HOT'
&& sms.Type__c == 'Incoming SMS'
&& sms.Recipient__c == DISPATCH_INCOMING_NUMBER;
}
}
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>61.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
@isTest
private class HOT_IncomingSMSNotificationTest {
@TestSetup
static void makeData(){
Account acc = new Account();
acc.Name = 'Gunnar Gunn Gundersen';
insert acc;
}

@isTest
static void getGroupIdsFromSendersTest() {
List<Account> accounts = TestDataFactory_Community.getPersonAccounts(3);
List<Person__c> persons = new List<Person__c>();

for( Account acc : accounts ) {
Person__c person = [
SELECT
Id,
CRM_Account__c,
INT_MunicipalityNumber__c,
INT_RegionNumber__c
FROM Person__c
WHERE CRM_Account__c = :acc.Id];
persons.add(person);
}


persons[0].INT_MunicipalityNumber__c = String.valueOf(3216); //Vestby
persons[0].INT_RegionNumber__c = '0' + String.valueOf(2); //Akershus

persons[1].INT_MunicipalityNumber__c = String.valueOf(5001); //Trondheim
persons[1].INT_RegionNumber__c = String.valueOf(50); //Trøndelag

persons[2].INT_MunicipalityNumber__c = String.valueOf(3407); //Gjøvik
persons[2].INT_RegionNumber__c = String.valueOf(34); // Innlandet

ApexSharingRule__mdt regionOslo = [
SELECT Id, DeveloperName FROM ApexSharingRule__mdt
WHERE DeveloperName = 'HOT_Person_03_Municipality'
];

ApexSharingRule__mdt regionTrondelag = [
SELECT Id, DeveloperName FROM ApexSharingRule__mdt
WHERE DeveloperName = 'HOT_Person_50'
];

ApexSharingRule__mdt regionInnlandet = [
SELECT Id, DeveloperName FROM ApexSharingRule__mdt
WHERE DeveloperName = 'HOT_Person_34_Region'
];



Test.startTest();
List<Id> groupIds = HOT_IncomingSMSNotification.getGroupIdsFromSenders(persons);
Test.stopTest();

System.assertEquals(regionOslo.Id, groupIds[0], 'Person 0 should belong to Oslo region');
System.assertEquals(regionTrondelag.Id, groupIds[1], 'Person 1 should belong to trondelag region');
System.assertEquals(regionInnlandet.Id, groupIds[2], 'Person 2 should belong to Innlandet region');
}

@isTest
static void shouldNotifyDispatchersAboutSMSTest() {
Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Gunnar Gunn Gundersen'];
SMS__c testSMS = new SMS__c();
testSMS.Domain__c = 'HOT';
testSMS.Type__c = 'Incoming SMS';
testSMS.Recipient__c = HOT_IncomingSMSNotification.DISPATCH_INCOMING_NUMBER;
testSMS.Account__c = acc.Id;

Test.startTest();
Boolean shouldNotify =
HOT_IncomingSMSNotification.shouldNotifyDispatchersAboutSMS(testSMS);
Test.stopTest();

System.assertEquals(true, shouldNotify, 'SMS should trigger notification of dispatchers');
}
}
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>61.0</apiVersion>
<status>Active</status>
</ApexClass>

0 comments on commit 2cc88c8

Please sign in to comment.