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

TOLK-2142 : Som tolk ønsker jeg å kunne laste opp fil til tolkebruker #2031

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"**/.sfdx": true,
"**/.vscode": true,
"**/.idea": true
}
},
"salesforce.einsteinForDevelopers.enableAutocompletions": false
}
55 changes: 55 additions & 0 deletions force-app/main/default/classes/HOT_HotFilesController.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
public with sharing class HOT_HotFilesController {
@AuraEnabled
public static Id getHotFileRecordId(Id serviceAppointmentId) {
List<HOT_File__c> hotFilesList = [
SELECT Id
FROM HOT_File__c
WHERE Service_Appointment__c = :serviceAppointmentId
LIMIT 1
];
System.debug('hotFilesList >>' + hotFilesList);
if (!hotFilesList.isEmpty()) {
return hotFilesList[0].Id;
}
return null;
}

@AuraEnabled
public static Id createHotFileRecord(Id serviceAppointmentId) {
Id existingId = getHotFileRecordId(serviceAppointmentId);
if (existingId != null) {
// Record already exists; return existing Id
return existingId;
}
ServiceAppointment serviceAppointment = [
SELECT Id, HOT_Account__c
FROM ServiceAppointment
WHERE Id = :serviceAppointmentId
LIMIT 1
];
// Create a new HOT_File__c record
HOT_File__c hotFilesRecord = new HOT_File__c(
Service_Appointment__c = serviceAppointmentId,
Account__c = serviceAppointment.HOT_Account__c
);
HOT_DatabaseOperations.insertRecords(hotFilesRecord);
return hotFilesRecord.Id;
}

@AuraEnabled
public static void shareWithUser(Id hotFileId, List<Id> contentDocumentIds) {
HOT_File__c hotFile = [SELECT Id, Account__c FROM HOT_File__c WHERE Id = :hotFileId LIMIT 1];

if (hotFile != null && hotFile.Account__c != null) {
List<ContentDocumentLink> cdls = new List<ContentDocumentLink>();
for (Id contentDocumentId : contentDocumentIds) {
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = contentDocumentId;
cdl.LinkedEntityId = hotFile.Account__c;
cdl.ShareType = 'V';
cdls.add(cdl);
}
HOT_DatabaseOperations.insertRecords(cdls);
}
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
178 changes: 178 additions & 0 deletions force-app/main/default/classes/HOT_HotFilesControllerTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
@isTest
public class HOT_HotFilesControllerTest {
@testSetup
static void setupTestData() {
// Create a Person__c record
Person__c person = HOT_TestDataFactory.createPerson();
person.Name = '12015678999';
insert person;

// Create an Account linked to the Person__c
Account personAccount = HOT_TestDataFactory.createAccount(true);
personAccount.INT_PersonIdent__c = '12015678999';
personAccount.CRM_Person__c = person.Id;
insert personAccount;

// Create a WorkType
WorkType workType = HOT_TestDataFactory.createWorkType();
insert workType;

// Create a HOT_Request__c
HOT_Request__c request = HOT_TestDataFactory.createRequest('TEST', workType);
insert request;

// Create a WorkOrder
WorkOrder wo = HOT_TestDataFactory.createWorkOrder(request, workType);
insert wo;

// Create a WorkOrderLineItem
WorkOrderLineItem woli = HOT_TestDataFactory.createWorkOrderLineItem(wo, workType);
insert woli;

// Create a ServiceAppointment linked to the WorkOrderLineItem and Account
ServiceAppointment sa = HOT_TestDataFactory.createServiceAppointment(woli);
sa.HOT_Account__c = personAccount.Id;
insert sa;

// Create a HOT_File__c linked to the ServiceAppointment and Account
HOT_File__c hotFile = new HOT_File__c(Service_Appointment__c = sa.Id, Account__c = personAccount.Id);
insert hotFile;

// Create a ContentVersion (which in turn creates a ContentDocument)
ContentVersion contentVersion = new ContentVersion(
Title = 'Test File',
PathOnClient = 'TestFile.txt',
VersionData = Blob.valueOf('Test content')
);
insert contentVersion;
}

@isTest
static void testGetHotFileRecordId() {
Person__c person = HOT_TestDataFactory.createPerson();
person.Name = '12015678999';
insert person;

// Create an Account linked to the Person__c
Account personAccount = HOT_TestDataFactory.createAccount(true);
personAccount.INT_PersonIdent__c = '12015678999';
personAccount.CRM_Person__c = person.Id;
insert personAccount;

// Create a WorkType
WorkType workType = HOT_TestDataFactory.createWorkType();
insert workType;

// Create a HOT_Request__c
HOT_Request__c request = HOT_TestDataFactory.createRequest('TEST', workType);
insert request;

// Create a WorkOrder
WorkOrder wo = HOT_TestDataFactory.createWorkOrder(request, workType);
insert wo;

// Create a WorkOrderLineItem
WorkOrderLineItem woli = HOT_TestDataFactory.createWorkOrderLineItem(wo, workType);
insert woli;

// Create a ServiceAppointment linked to the WorkOrderLineItem and Account
ServiceAppointment sa = HOT_TestDataFactory.createServiceAppointment(woli);
sa.HOT_Account__c = personAccount.Id;
insert sa;

// Create a HOT_File__c linked to the ServiceAppointment and Account
HOT_File__c hotFile = new HOT_File__c(Service_Appointment__c = sa.Id, Account__c = personAccount.Id);
insert hotFile;

Test.startTest();
Id hotFileId = HOT_HotFilesController.getHotFileRecordId(sa.Id);
Test.stopTest();

System.assertNotEquals(null, hotFileId, 'HotFileId should not be null');
}

@isTest
static void testCreateHotFileRecord_New() {
Person__c newPerson = HOT_TestDataFactory.createPerson();
newPerson.Name = '13015678999';
insert newPerson;

Account newPersonAccount = HOT_TestDataFactory.createAccount(true);
newPersonAccount.INT_PersonIdent__c = '13015678999';
newPersonAccount.CRM_Person__c = newPerson.Id;
insert newPersonAccount;

WorkType workType = HOT_TestDataFactory.createWorkType();
insert workType;

HOT_Request__c request = HOT_TestDataFactory.createRequest('TEST_NEW', workType);
insert request;

WorkOrder wo = HOT_TestDataFactory.createWorkOrder(request, workType);
insert wo;

WorkOrderLineItem woli = HOT_TestDataFactory.createWorkOrderLineItem(wo, workType);
insert woli;

ServiceAppointment sa = HOT_TestDataFactory.createServiceAppointment(woli);
sa.HOT_Account__c = newPersonAccount.Id;
insert sa;

Test.startTest();
Id hotFileId = HOT_HotFilesController.createHotFileRecord(sa.Id);
Test.stopTest();

HOT_File__c hotFile = [SELECT Id, Service_Appointment__c, Account__c FROM HOT_File__c WHERE Id = :hotFileId];
System.assertNotEquals(null, hotFile, 'HotFile record should not be null');
System.assertEquals(sa.Id, hotFile.Service_Appointment__c, 'Service Appointment IDs should match');
System.assertEquals(newPersonAccount.Id, hotFile.Account__c, 'Account IDs should match');
}

@isTest
static void testCreateHotFileRecord_Existing() {
ServiceAppointment sa = [SELECT Id FROM ServiceAppointment LIMIT 1];

Test.startTest();
Id hotFileId1 = HOT_HotFilesController.createHotFileRecord(sa.Id);
Id hotFileId2 = HOT_HotFilesController.createHotFileRecord(sa.Id);
Test.stopTest();

System.assertEquals(hotFileId1, hotFileId2, 'HotFile IDs should be the same for existing records');
}

@isTest
static void testShareWithUser() {
HOT_File__c hotFile = [SELECT Id, Account__c FROM HOT_File__c LIMIT 1];
ContentVersion contentVersion = [SELECT Id, ContentDocumentId FROM ContentVersion LIMIT 1];

List<Id> contentDocumentIds = new List<Id>{ contentVersion.ContentDocumentId };

Test.startTest();
HOT_HotFilesController.shareWithUser(hotFile.Id, contentDocumentIds);
Test.stopTest();

// Verify that a ContentDocumentLink has been created
List<ContentDocumentLink> cdls = [
SELECT Id, ContentDocumentId, LinkedEntityId, ShareType
FROM ContentDocumentLink
WHERE LinkedEntityId = :hotFile.Account__c AND ContentDocumentId = :contentVersion.ContentDocumentId
];

System.assertNotEquals(0, cdls.size(), 'ContentDocumentLink should be created');
System.assertEquals('V', cdls[0].ShareType, 'ShareType should be View (V)');
}

@isTest
static void testShareWithUser_NullAccount() {
HOT_File__c hotFile = new HOT_File__c();
insert hotFile;

List<Id> contentDocumentIds = new List<Id>();

Test.startTest();
HOT_HotFilesController.shareWithUser(hotFile.Id, contentDocumentIds);
Test.stopTest();

// No exception should be thrown, and no action taken
}
}
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>62.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
public with sharing class HOT_RecordFilesControllerWithSharing {
@AuraEnabled(cacheable=true)
public static List<ContentDocument> getContentDocuments(String recordId, Boolean isGetAll) {
if (recordId == null) {
return new List<ContentDocument>{};
}

Set<Id> requestId = new Set<Id>();
List<WorkOrder> workOrder = [SELECT Id, HOT_Request__c FROM WorkOrder WHERE Id = :recordId];
for (WorkOrder wo : workOrder) {
requestId.add(wo.HOT_Request__c);
}

List<ServiceAppointment> serviceAppointments = [
SELECT Id
FROM ServiceAppointment
WHERE HOT_WorkOrderLineItem__r.WorkOrderId = :recordId
];
Set<Id> saIds = new Set<Id>();
for (ServiceAppointment sa : serviceAppointments) {
saIds.add(sa.Id);
}

List<HOT_File__c> hotFiles = [SELECT Id FROM HOT_File__c WHERE Service_Appointment__c = :saIds];
Set<Id> hotFileIds = new Set<Id>();
for (HOT_File__c hf : hotFiles) {
hotFileIds.add(hf.Id);
}

List<ContentDocumentLink> contentDocumentLinks = [
SELECT ContentDocumentId
FROM ContentDocumentLink
WHERE
LinkedEntityId = :recordId
OR LinkedEntityId IN :saIds
OR LinkedEntityId IN :requestId
OR LinkedEntityId IN :hotFileIds
];

List<Id> contentDocumentIds = new List<Id>();
for (ContentDocumentLink contentDocumentLink : contentDocumentLinks) {
contentDocumentIds.add(contentDocumentLink.ContentDocumentId);
}

if (!isGetAll) {
contentDocumentIds = RecordFilesControllerWithSharing.getOnlyMyContentDocuments(contentDocumentIds);
}

List<ContentDocument> contentDocuments = [
SELECT Id, FileType, CreatedDate, Title, LatestPublishedVersionId
FROM ContentDocument
WHERE Id IN :contentDocumentIds AND ContentAssetId = NULL
];
return contentDocuments;
}
}
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>56.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading
Loading