Skip to content

Commit

Permalink
Merge pull request #174 from SalesforceLabs/feature/blockBugFixes
Browse files Browse the repository at this point in the history
Feature/block bug fixes
  • Loading branch information
GeekStewie authored Aug 29, 2024
2 parents 29a6a76 + 87f31a2 commit 215b84e
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 49 deletions.
36 changes: 36 additions & 0 deletions force-app/main/default/classes/LocationAnimalController.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @File Name : LocationAnimalController.cls
* @Description : Gets Animal records via Movement for current Location. Used by locationAnimals LWC
* @Author : Chris Rolfe
* @Last Modified By :
* @Last Modified On :
* @Modification Log :
* Ver Date Author Modification
* 1.0 22/08/2024 Chris Rolfe Initial Version
**/

public with sharing class LocationAnimalController {

@AuraEnabled(cacheable=true)
public static List<animalshelters__Movement__c> getAnimalsInLocation(Id locationId) {

if(Schema.SObjectType.animalshelters__Locations__c.isAccessible() && Schema.SObjectType.animalshelters__Movement__c.isAccessible()){

// Fetch Location record to check RecordType
animalshelters__Locations__c location = [SELECT Id, RecordType.Name FROM animalshelters__Locations__c WHERE Id = :locationId LIMIT 1];

// Check if the location is of type 'Unit'
if (location.RecordType.Name == 'Unit') {
// Fetch Movement records where Current = true and Location matches
return [SELECT Id, animalshelters__Animal__r.animalshelters__Animal_Name__c
FROM animalshelters__Movement__c
WHERE animalshelters__Location__c = :locationId
AND animalshelters__Current__c = TRUE];
} else {
// Return an empty list if it's not a Unit
return new List<animalshelters__Movement__c>();
}
}
return null;
}
}
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>
85 changes: 85 additions & 0 deletions force-app/main/default/classes/LocationAnimalControllerTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @File Name : LocationAnimalControllerTest.cls
* @Description : Test Class for LocationAnimalController
* @Author : Chris Rolfe
* @Last Modified By :
* @Last Modified On :
* @Modification Log :
* Ver Date Author Modification
* 1.0 22/08/2024 Chris Rolfe Initial Version
**/
@isTest
public class LocationAnimalControllerTest {

@testSetup
static void setupTestData() {
// Get Unit RecordType for Locations
Id locRecordTypeUnit = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Unit').getRecordTypeId();
Id locRecordTypeBlock = Schema.SObjectType.animalshelters__Locations__c.getRecordTypeInfosByName().get('Block').getRecordTypeId();


// Create a Location of type Unit
animalshelters__Locations__c unitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Unit 1', RecordTypeId = locRecordTypeUnit);
insert unitLocation;

// Create another Location of type Block
animalshelters__Locations__c blockLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Block 1', RecordTypeId = locRecordTypeBlock);
insert blockLocation;

// Create an Animal
animalshelters__Animal__c animal1 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Molly', animalshelters__Date_of_Arrival__c = Date.today());
animalshelters__Animal__c animal2 = new animalshelters__Animal__c(animalshelters__Animal_Name__c = 'Furby', animalshelters__Date_of_Arrival__c = Date.today());
insert new List<animalshelters__Animal__c>{animal1, animal2};

// Create Movement records for the Unit Location
animalshelters__Movement__c movement1 = new animalshelters__Movement__c(animalshelters__Animal__c = animal1.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true);
animalshelters__Movement__c movement2 = new animalshelters__Movement__c(animalshelters__Animal__c = animal2.Id, animalshelters__Location__c = unitLocation.Id, animalshelters__Current__c = true);
insert new List<Movement__c>{movement1, movement2};
}

@isTest
static void testGetAnimalsInLocation_UnitWithAnimals() {
// Fetch the Unit Location created in test setup
animalshelters__Locations__c unitLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Unit 1' LIMIT 1];

// Call the method
Test.startTest();
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(unitLocation.Id);
Test.stopTest();

// Verify the result
System.assertEquals(2, movements.size(), 'There should be two movements for Unit 1');
System.assertEquals('Molly', movements[0].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'First animal should be Lion');
System.assertEquals('Furby', movements[1].animalshelters__Animal__r.animalshelters__Animal_Name__c, 'Second animal should be Tiger');
}

@isTest
static void testGetAnimalsInLocation_UnitWithoutAnimals() {
// Create a new Unit Location without any animals
RecordType unitRecordType = [SELECT Id FROM RecordType WHERE DeveloperName = 'Unit' AND SObjectType = 'animalshelters__Locations__c' LIMIT 1];
animalshelters__Locations__c emptyUnitLocation = new animalshelters__Locations__c(animalshelters__Name__c = 'Empty Unit', RecordTypeId = unitRecordType.Id);
insert emptyUnitLocation;

// Call the method
Test.startTest();
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(emptyUnitLocation.Id);
Test.stopTest();

// Verify the result
System.assertEquals(0, movements.size(), 'There should be no movements for the empty unit');
}

@isTest
static void testGetAnimalsInLocation_NonUnitLocation() {
// Fetch the Block Location created in test setup
animalshelters__Locations__c blockLocation = [SELECT Id FROM animalshelters__Locations__c WHERE animalshelters__Name__c = 'Block 1' LIMIT 1];

// Call the method
Test.startTest();
List<animalshelters__Movement__c> movements = LocationAnimalController.getAnimalsInLocation(blockLocation.Id);
Test.stopTest();

// Verify the result
System.assertEquals(0, movements.size(), 'There should be no movements because the location is not a Unit');
}
}
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
Expand Up @@ -601,6 +601,12 @@
</visibilityRule>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentName>locationAnimals</componentName>
<identifier>animalshelters_locationAnimals</identifier>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentInstanceProperties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
</decisions>
<description>Autolaunched flow to update the Location Status based on availability</description>
<environments>Default</environments>
<formulas>
<description>Checks the Capacity of the Location</description>
<name>allocationFormula</name>
<dataType>Number</dataType>
<expression>IF({!$Record.animalshelters__Capacity__c} = 1, 0, {!$Record.animalshelters__Allocation__c})</expression>
<scale>0</scale>
</formulas>
<formulas>
<name>NoMoreCapacity</name>
<dataType>Boolean</dataType>
Expand Down Expand Up @@ -113,7 +120,7 @@
<inputAssignments>
<field>Allocation__c</field>
<value>
<numberValue>0.0</numberValue>
<elementReference>allocationFormula</elementReference>
</value>
</inputAssignments>
<inputAssignments>
Expand Down
Loading

0 comments on commit 215b84e

Please sign in to comment.