Skip to content

Commit

Permalink
support VRS Job or Contact changing
Browse files Browse the repository at this point in the history
  • Loading branch information
David Habib committed Nov 30, 2016
1 parent 3858789 commit c6d5a39
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/classes/VOL_JRS.cls
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public with sharing class VOL_JRS {
listVRS = [select Id, Name, Contact__c, Schedule_Start_Date_Time__c, Schedule_End_Date__c, Duration__c,
Weekly_Occurrence__c, Days_Of_Week__c, Volunteer_Job__c, Volunteer_Hours_Status__c, Number_of_Volunteers__c, Comments__c
from Volunteer_Recurrence_Schedule__c where Volunteer_Job__c in : setJobId];
VOL_VRS.ProcessListVRS(listVRS, fReviewAllShifts);
VOL_VRS.ProcessListVRS(listVRS, null, fReviewAllShifts);
} else {
ProcessListVRSFuture(setJobId, fReviewAllShifts);
}
Expand All @@ -153,7 +153,7 @@ public with sharing class VOL_JRS {
from Volunteer_Recurrence_Schedule__c where Volunteer_Job__c in : setJobId];

// process the VRS's to create hours as needed.
VOL_VRS.ProcessListVRS(listVRS, fReviewAllShifts);
VOL_VRS.ProcessListVRS(listVRS, null, fReviewAllShifts);
}


Expand Down
18 changes: 16 additions & 2 deletions src/classes/VOL_VRS.cls
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@ public with sharing class VOL_VRS {
// no longer match, and creates new hours into the future.
// called from both the VRS trigger (when the user modifies a specific VRS),
// as well as from the JRS processing, so new shifts get their hours assigned.
// fReviewAllShifts parameter specifies whether called from the trigger on JRS's, in
// @param listVRS the list of VRS's to process
// @param listVRSOld If called from the AfterUpdate trigger, the trigger.old list
// @param fReviewAllShifts Specifies whether called from the trigger on JRS's, in
// which case we should review all shifts under the Jobs, or from the scheduled batch,
// in which case we only need to be looking to add additional shifts in the future.
public static void ProcessListVRS(list<Volunteer_Recurrence_Schedule__c> listVRS, boolean fReviewAllShifts) {
public static void ProcessListVRS(
list<Volunteer_Recurrence_Schedule__c> listVRS,
list<Volunteer_Recurrence_Schedule__c> listVRSOld,
boolean fReviewAllShifts) {

// get a set of the VRS ID's for querying
// also the Job ID's they are associated with
Expand All @@ -96,6 +101,15 @@ public with sharing class VOL_VRS {
setJobId.add(vrs.Volunteer_Job__c);
setContactId.add(vrs.Contact__c);
}

// if the VRS was updated, then deal with potent changes to the VRS's job or contact
if (listVRSOld != null) {
for (Volunteer_Recurrence_Schedule__c vrs : listVRSOld) {
setVRSId.add(vrs.Id);
setJobId.add(vrs.Volunteer_Job__c);
setContactId.add(vrs.Contact__c);
}
}

// get all shifts of the jobs these vrs's are associated with
list<Volunteer_Shift__c> listShift = new list<Volunteer_Shift__c>();
Expand Down
71 changes: 71 additions & 0 deletions src/classes/VOL_VRS_TEST.cls
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,75 @@ public with sharing class VOL_VRS_TEST {
}

}

//******************************************************************************************************
// Test updating existing Volunteer Recurrence Schedule's job, deletes old hours and creates new hours.
public static testmethod void TestVRSHoursChangeJob() {

// create test data
Campaign cmp = new Campaign(recordtypeid=VOL_SharedCode.recordtypeIdVolunteersCampaign,
name='Job Calendar Test Campaign', IsActive=true);
insert cmp;
Volunteer_Job__c job = new Volunteer_Job__c(name='Job1', campaign__c=cmp.Id);
insert job;
Volunteer_Job__c job2 = new Volunteer_Job__c(name='Job2', campaign__c=cmp.Id);
insert job2;
Contact contact = new Contact(firstname='test', lastname='test');
insert contact;

Job_Recurrence_Schedule__c jrs = new Job_Recurrence_Schedule__c(Volunteer_Job__c = job.Id);
jrs.Days_of_Week__c = 'Monday;Wednesday;Friday';
jrs.Duration__c = 1;
jrs.Schedule_Start_Date_Time__c = system.today().toStartOfMonth();
jrs.Schedule_End_Date__c = system.today().addMonths(2).toStartOfMonth().addDays(-1);
jrs.Weekly_Occurrence__c = '2nd';
jrs.Desired_Number_of_Volunteers__c = 5;
insert jrs;

Job_Recurrence_Schedule__c jrs2 = new Job_Recurrence_Schedule__c(Volunteer_Job__c = job2.Id);
jrs2.Days_of_Week__c = 'Monday;Wednesday;Friday';
jrs2.Duration__c = 1;
jrs2.Schedule_Start_Date_Time__c = system.today().toStartOfMonth();
jrs2.Schedule_End_Date__c = system.today().addMonths(2).toStartOfMonth().addDays(-1);
jrs2.Weekly_Occurrence__c = '2nd';
jrs2.Desired_Number_of_Volunteers__c = 5;
insert jrs2;

list<Volunteer_Shift__c> listShift = [select Id, Name, Volunteer_Job__r.Name from Volunteer_Shift__c where Job_Recurrence_Schedule__c = :jrs.Id];
system.assertEquals(6, listShift.size());

Volunteer_Recurrence_Schedule__c vrs = new Volunteer_Recurrence_Schedule__c(
Contact__c = contact.Id,
Volunteer_Job__c = job.Id,
Volunteer_Hours_Status__c = 'My Custom Status',
Days_of_Week__c = 'Monday;Wednesday',
Duration__c = 1.5,
Number_of_Volunteers__c = 2,
Comments__c = 'my comments!',
Schedule_Start_Date_Time__c = system.today().toStartOfMonth(),
Schedule_End_Date__c = system.today().addMonths(2).toStartOfMonth().addDays(-1),
Weekly_Occurrence__c = '2nd');
insert vrs;
Volunteer_Recurrence_Schedule__c vrsOriginal = vrs.clone();

list<Volunteer_Hours__c> listHours = [select Id, Status__c, Number_of_Volunteers__c, Comments__c
from Volunteer_Hours__c where Volunteer_Recurrence_Schedule__c = :vrs.Id order by Planned_Start_Date_Time__c];
system.assertEquals(4, listHours.size());
system.assertEquals(vrs.Number_of_Volunteers__c, listHours[0].Number_of_Volunteers__c);
system.assertEquals(vrs.Comments__c, listHours[0].Comments__c);

// update the Job
vrs.Volunteer_Job__c = job2.Id;
Test.startTest();
update vrs;
Test.stopTest();

listHours = [select Id, Status__c, Planned_Start_Date_Time__c, Hours_Worked__c, Number_Of_Volunteers__c, Comments__c
from Volunteer_Hours__c where Volunteer_Job__c = :job.Id];
system.assertEquals(0, listHours.size());

listHours = [select Id, Status__c, Planned_Start_Date_Time__c, Hours_Worked__c, Number_Of_Volunteers__c, Comments__c
from Volunteer_Hours__c where Volunteer_Job__c = :job2.Id];
system.assertEquals(4, listHours.size());
}
}
2 changes: 1 addition & 1 deletion src/triggers/VOL_VRS_MaintainHours.trigger
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
trigger VOL_VRS_MaintainHours on Volunteer_Recurrence_Schedule__c (after insert, after undelete, after update, before delete) {

if (trigger.isInsert || trigger.isUpdate || trigger.isUnDelete) {
VOL_VRS.ProcessListVRS(trigger.new, true);
VOL_VRS.ProcessListVRS(trigger.new, trigger.old, true);
}

if (trigger.isDelete) {
Expand Down

0 comments on commit c6d5a39

Please sign in to comment.