-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from SalesforceLabs/feature/blockBugFixes
Feature/block bug fixes
- Loading branch information
Showing
15 changed files
with
350 additions
and
49 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
force-app/main/default/classes/LocationAnimalController.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/LocationAnimalController.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
85
force-app/main/default/classes/LocationAnimalControllerTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/LocationAnimalControllerTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.