Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ALR-1802 #1017

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 43 additions & 105 deletions src/main/default/classes/ScheduleSendEmail.cls
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,10 @@

public with sharing class ScheduleSendEmail implements Schedulable {

public static Set<Id> accountIds = new Set<Id>();
List<AccountContactRelation> contacts =new List<AccountContactRelation>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

public void execute(SchedulableContext sc) {
try {
Map<Id, Set<Id>> accountIdToContactIdsMap = new Map<Id, Set<Id>>();

List<Account> accounts = [
SELECT Id ,ParentId
FROM Account
WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement')
];

for (Account acc : accounts) {
accountIds.add(acc.Id);
accountIds.add(acc.ParentId);
accountIdToContactIdsMap.put(acc.Id, new Set<Id>());
accountIdToContactIdsMap.put(acc.ParentId, new Set<Id>());
}

Map<Id, Account> accountIdToAccountMap = new Map<Id, Account>([
SELECT Id,parentId,
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email != null)
FROM Account
WHERE Id IN :accountIds
]);

for (Account account : [
SELECT parentId,
(SELECT Id, ContactId, Contact.Name, Contact.Email
FROM AccountContactRelations
WHERE IsActive = true AND Contact.Email!= null)
FROM Account
WHERE Id IN : accountIds
]) {
accountIdToAccountMap.put(account.Id, account);
}

for (Account acc : accountIdToAccountMap.values()) {
for (AccountContactRelation acr : acc.AccountContactRelations) {
accountIdToContactIdsMap.get(acc.Id).add(acr.ContactId);
}
}

List<Id> contactIdsList = new List<Id>();
for (Set<Id> contactIds : accountIdToContactIdsMap.values()) {
contactIdsList.addAll(contactIds);
}

if (!contactIdsList.isEmpty()) {
sendRenewalDueEmail(contactIdsList);
}

} catch (Exception e) {
System.debug('Exception occurred in execute method: ' + e.getMessage());
}

}
public static void sendRenewalDueEmail(List<Id> contactIds) {
List<Id> accListforKeepingIds = new List<Id>();
public void execute(SchedulableContext sc) {
try {
EmailTemplate emailTemplate = getEmailTemplate('Upcoming_Renewal_Email_Template');

Expand All @@ -76,66 +19,61 @@ public with sharing class ScheduleSendEmail implements Schedulable {
FROM OrgWideEmailAddress
WHERE DisplayName = 'ALR Support Email'
];
List<AccountContactRelation> AccountcontactRecList = [SELECT Id, AccountId, Account.Name, ContactId, Contact.Email, Contact.IsEmailBounced FROM AccountContactRelation WHERE ContactId IN :contactIds];

for(AccountContactRelation AccountconList : AccountcontactRecList){
accListforKeepingIds.add(AccountconList.ContactId);
}

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<String, List<String>> processedEmails = new Map<String, List<String>>();
Integer currentyear = Date.today().year();
Integer previousyear = Date.today().year() - 1;

for (AccountContactRelation acrList : AccountcontactRecList) {
if (acrList.contact.Email != null) {
String key = acrList.AccountId + '_' + acrList.contact.Email.toLowerCase();
if (!processedEmails.containsKey(key)) {
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(emailTemplate.Id);
email.setOrgWideEmailAddressId(orgWideAddr.Id);
email.setToAddresses(new String[]{acrList.contact.Email});
email.setTargetObjectId(acrList.contact.Id);
String emailBody = emailTemplate.HtmlValue;
String emailSubject = emailTemplate.Subject;
emailBody = emailBody.Replace('{%%Account.Name%%}', acrList.Account.Name);
emailBody = emailBody.Replace('{%%CurrentYear%%}', String.valueOf(currentyear));
emailBody = emailBody.Replace('{%%PreviousYear%%}', String.valueOf(previousyear));
emailSubject = emailSubject.Replace('{%%CurrentYear%%}', String.valueOf(currentyear));
emailSubject = emailSubject.Replace('{%%PreviousYear%%}', String.valueOf(previousyear));
email.setSubject(emailSubject);
email.setHtmlBody(emailBody);
emails.add(email);
processedEmails.put(key, new List<String>{acrList.contact.Email});
}
}
}



for(Account accounts : [SELECT Id ,Name,ParentId FROM Account
WHERE Status__c IN ('Registered Active', 'Registered Active with Conditions', 'Registered Active Progressive Enforcement')
]){
contacts = getContacts(accounts.Id,accounts.ParentId);
Map<String, List<String>> processedEmails = new Map<String, List<String>>();
Integer currentyear = Date.today().year();
Integer previousyear = Date.today().year() - 1;

for (AccountContactRelation acrList : contacts) {
if (acrList.contact.Email != null) {
String key = acrList.AccountId + '_' + acrList.contact.Email.toLowerCase();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setTemplateId(emailTemplate.Id);
email.setOrgWideEmailAddressId(orgWideAddr.Id);
email.setToAddresses(new String[]{acrList.contact.Email});
email.setTargetObjectId(acrList.contact.Id);
String emailBody = emailTemplate.HtmlValue;
String emailSubject = emailTemplate.Subject;
emailBody = emailBody.Replace('{%%Account.Name%%}', accounts.Name);
emailBody = emailBody.Replace('{%%CurrentYear%%}', String.valueOf(currentyear));
emailBody = emailBody.Replace('{%%PreviousYear%%}', String.valueOf(previousyear));
emailSubject = emailSubject.Replace('{%%CurrentYear%%}', String.valueOf(currentyear));
emailSubject = emailSubject.Replace('{%%PreviousYear%%}', String.valueOf(previousyear));
email.setSubject(emailSubject);
email.setHtmlBody(emailBody);
emails.add(email);
}
}
}
if (!emails.isEmpty()) {
sendEmailBatch(emails);
}
} catch (Exception e) {
System.debug('Exception occurred in sendRenewalDueEmail method: ' + e.getMessage());
System.debug('Exception occurred in execute method: ' + e.getMessage());
}
}

public Static List<AccountContactRelation> getContacts(Id accountId, Id parentId){
return [Select Id,ContactId,AccountId,IsActive,Contact.Email from AccountContactRelation where (AccountId = :accountId OR AccountId =: parentId) AND IsActive =true AND Contact.Email != NULL];
}


public static void sendEmailBatch(List<Messaging.SingleEmailMessage> emailBatch) {
List<Messaging.SingleEmailMessage> singleEmailList = new List<Messaging.SingleEmailMessage>();
try{
for (Messaging.SingleEmailMessage email : emailBatch) {
singleEmailList.add(email);
}
List<Messaging.SendEmailResult> results = Messaging.sendEmail(singleEmailList, false);

}
List<Messaging.SendEmailResult> results = Messaging.sendEmail(singleEmailList, false);
}catch (Exception e) {
System.debug('Exception occurred while sending email to ' + e.getMessage());
System.debug('Exception occurred while sending email to ' + e.getLineNumber());
}
System.debug('Exception occurred while sending email to ' + e.getMessage());
}

}

public static EmailTemplate getEmailTemplate(String templateDevName) {
EmailTemplate emailTemplateRec = null;
Expand Down
35 changes: 6 additions & 29 deletions src/main/default/classes/ScheduleSendEmailTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ private class ScheduleSendEmailTest {
Account acc = TestDataFactory.createResidence('Residence', 'residence1', regAuth.Id, true);
accList.add(acc);

Account accRec = [SELECT Id ,ParentId FROM Account where Id =: acc.Id];
Account accRec = [SELECT Id , Name , ParentId FROM Account where Id =: acc.Id];

Contact con = new Contact(
FirstName = 'Test',
LastName = 'Contact',
Email = '[email protected]'
Email = '[email protected]',
AccountId=accRec.Id
);
insert con;



AccountContactRelation acr = new AccountContactRelation(
AccountId = acc.Id,
ContactId = con.Id,
Expand All @@ -39,30 +41,5 @@ private class ScheduleSendEmailTest {
List<EmailTemplate> emailTem = [SELECT Id FROM EmailTemplate];
System.assertEquals(True, emailTem.size()>0, 'No Email is sent');
}

@isTest
static void testSendRenewalDueEmail() {


List<Account> accList = new List<Account>();

// Create an instance of DMLOptions
Database.DMLOptions dmlOptions = new Database.DMLOptions();
dmlOptions.DuplicateRuleHeader.allowSave = true;

RegulatoryAuthorizationType regAuth = TestDataFactory.createRegAuth('Mental Health', 'License', 'MH', 1, 2, 3, 4, true);

Account Residence = TestDataFactory.createResidence('Residence', 'hgvfjmhhgsu', regAuth.Id, true);

Contact contactRec = TestDataFactory.createContactRecord(Residence.Id, '[email protected]', 'uysgyuskgfh', 'hsguh', true);

//AccountContactRelation acConRelRec = TestDataFactory.createAccContRelRecord(Residence.Id, contactRec.Id , true, true, true);

List<Id> contactIds = new List<Id>{ contactRec.Id };
Test.startTest();
ScheduleSendEmail.sendRenewalDueEmail(contactIds);
Test.stopTest();
List<EmailTemplate> emailTem = [SELECT Id FROM EmailTemplate];
System.assertEquals(True, emailTem.size()>0, 'No Email is sent');
}

}