Skip to content

Commit 215b84e

Browse files
authored
Merge pull request #174 from SalesforceLabs/feature/blockBugFixes
Feature/block bug fixes
2 parents 29a6a76 + 87f31a2 commit 215b84e

15 files changed

+350
-49
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @File Name : LocationAnimalController.cls
3+
* @Description : Gets Animal records via Movement for current Location. Used by locationAnimals LWC
4+
* @Author : Chris Rolfe
5+
* @Last Modified By :
6+
* @Last Modified On :
7+
* @Modification Log :
8+
* Ver Date Author Modification
9+
* 1.0 22/08/2024 Chris Rolfe Initial Version
10+
**/
11+
12+
public with sharing class LocationAnimalController {
13+
14+
@AuraEnabled(cacheable=true)
15+
public static List<animalshelters__Movement__c> getAnimalsInLocation(Id locationId) {
16+
17+
if(Schema.SObjectType.animalshelters__Locations__c.isAccessible() && Schema.SObjectType.animalshelters__Movement__c.isAccessible()){
18+
19+
// Fetch Location record to check RecordType
20+
animalshelters__Locations__c location = [SELECT Id, RecordType.Name FROM animalshelters__Locations__c WHERE Id = :locationId LIMIT 1];
21+
22+
// Check if the location is of type 'Unit'
23+
if (location.RecordType.Name == 'Unit') {
24+
// Fetch Movement records where Current = true and Location matches
25+
return [SELECT Id, animalshelters__Animal__r.animalshelters__Animal_Name__c
26+
FROM animalshelters__Movement__c
27+
WHERE animalshelters__Location__c = :locationId
28+
AND animalshelters__Current__c = TRUE];
29+
} else {
30+
// Return an empty list if it's not a Unit
31+
return new List<animalshelters__Movement__c>();
32+
}
33+
}
34+
return null;
35+
}
36+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>61.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @File Name : LocationAnimalControllerTest.cls
3+
* @Description : Test Class for LocationAnimalController
4+
* @Author : Chris Rolfe
5+
* @Last Modified By :
6+
* @Last Modified On :
7+
* @Modification Log :
8+
* Ver Date Author Modification
9+
* 1.0 22/08/2024 Chris Rolfe Initial Version
10+
**/
11+
@isTest
12+
public class LocationAnimalControllerTest {
13+
14+
@testSetup
15+
static void setupTestData() {
16+
// Get Unit RecordType for Locations
17+
Id locRecordTypeUnit = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Unit').getRecordTypeId();
18+
Id locRecordTypeBlock = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Block').getRecordTypeId();
19+
20+
21+
// Create a Location of type Unit
22+
animalshelters__Locations__c unitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Unit 1', RecordTypeId = locRecordTypeUnit);
23+
insert unitLocation;
24+
25+
// Create another Location of type Block
26+
animalshelters__Locations__c blockLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Block 1', RecordTypeId = locRecordTypeBlock);
27+
insert blockLocation;
28+
29+
// Create an Animal
30+
animalshelters__Animal__c animal1 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Molly', animalshelters__Date_of_Arrival__c = Date.today());
31+
animalshelters__Animal__c animal2 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Furby', animalshelters__Date_of_Arrival__c = Date.today());
32+
insert new List<animalshelters__Animal__c>{animal1, animal2};
33+
34+
// Create Movement records for the Unit Location
35+
animalshelters__Movement__c movement1 = new animalshelters__Movement__c(animalshelters__Animal__c = animal1.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true);
36+
animalshelters__Movement__c movement2 = new animalshelters__Movement__c(animalshelters__Animal__c = animal2.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true);
37+
insert new List<Movement__c>{movement1, movement2};
38+
}
39+
40+
@isTest
41+
static void testGetAnimalsInLocation_UnitWithAnimals() {
42+
// Fetch the Unit Location created in test setup
43+
animalshelters__Locations__c unitLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Unit 1' LIMIT 1];
44+
45+
// Call the method
46+
Test.startTest();
47+
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(unitLocation.Id);
48+
Test.stopTest();
49+
50+
// Verify the result
51+
System.assertEquals(2, movements.size(), 'There should be two movements for Unit 1');
52+
System.assertEquals('Molly', movements[0].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'First animal should be Lion');
53+
System.assertEquals('Furby', movements[1].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'Second animal should be Tiger');
54+
}
55+
56+
@isTest
57+
static void testGetAnimalsInLocation_UnitWithoutAnimals() {
58+
// Create a new Unit Location without any animals
59+
RecordType unitRecordType = [SELECT Id FROM RecordType WHERE DeveloperName = 'Unit' AND SObjectType = 'animalshelters__Locations__c' LIMIT 1];
60+
animalshelters__Locations__c emptyUnitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Empty Unit', RecordTypeId = unitRecordType.Id);
61+
insert emptyUnitLocation;
62+
63+
// Call the method
64+
Test.startTest();
65+
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(emptyUnitLocation.Id);
66+
Test.stopTest();
67+
68+
// Verify the result
69+
System.assertEquals(0, movements.size(), 'There should be no movements for the empty unit');
70+
}
71+
72+
@isTest
73+
static void testGetAnimalsInLocation_NonUnitLocation() {
74+
// Fetch the Block Location created in test setup
75+
animalshelters__Locations__c blockLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Block 1' LIMIT 1];
76+
77+
// Call the method
78+
Test.startTest();
79+
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(blockLocation.Id);
80+
Test.stopTest();
81+
82+
// Verify the result
83+
System.assertEquals(0, movements.size(), 'There should be no movements because the location is not a Unit');
84+
}
85+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>61.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

force-app/main/default/flexipages/Location_Record_Page.flexipage-meta.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@
601601
</visibilityRule>
602602
</componentInstance>
603603
</itemInstances>
604+
<itemInstances>
605+
<componentInstance>
606+
<componentName>locationAnimals</componentName>
607+
<identifier>animalshelters_locationAnimals</identifier>
608+
</componentInstance>
609+
</itemInstances>
604610
<itemInstances>
605611
<componentInstance>
606612
<componentInstanceProperties>

force-app/main/default/flows/Animal_Update_Location_Unit_Status.flow-meta.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
</decisions>
6666
<description>Autolaunched flow to update the Location Status based on availability</description>
6767
<environments>Default</environments>
68+
<formulas>
69+
<description>Checks the Capacity of the Location</description>
70+
<name>allocationFormula</name>
71+
<dataType>Number</dataType>
72+
<expression>IF({!$Record.animalshelters__Capacity__c} = 1, 0, {!$Record.animalshelters__Allocation__c})</expression>
73+
<scale>0</scale>
74+
</formulas>
6875
<formulas>
6976
<name>NoMoreCapacity</name>
7077
<dataType>Boolean</dataType>
@@ -113,7 +120,7 @@
113120
<inputAssignments>
114121
<field>Allocation__c</field>
115122
<value>
116-
<numberValue>0.0</numberValue>
123+
<elementReference>allocationFormula</elementReference>
117124
</value>
118125
</inputAssignments>
119126
<inputAssignments>

0 commit comments

Comments
 (0)