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

Nytt komponent og controller #1992

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public with sharing class AccountBadgesController {
private static final LoggerUtility logger = new LoggerUtility();
@AuraEnabled(cacheable=true)
public static List<Badge> createBadges(Id accountId) {
List<Badge> accountBadges = new List<Badge>();
try {
Account account = [SELECT TAG_Partner_Status__c FROM Account WHERE Id = :accountId];
addParterStatusBadge(accountBadges, account);
} catch (Exception e) {
logger.logMessage(
LoggerUtility.LogLevel.Error,
'',
accountId,
e.getMessage(),
e.getStackTraceString(),
null,
CRM_ApplicationDomain.Domain.POAB
);
logger.publish();
return new List<Badge>();
}
return accountBadges;
}

private static void addParterStatusBadge(List<Badge> accountBadges, Account account) {
if (AccessControlValidator.fieldIsAccessible('Account', 'TAG_Partner_Status__c')) {
if (
account.TAG_Partner_Status__c == 'Strategisk Partner' ||
account.TAG_Partner_Status__c == 'Samarbeidspartner'
) {
accountBadges.add(new Badge(account.TAG_Partner_Status__c, 'Partner status'));
}
}
}

public class Badge {
public Badge(String label, String helpText) {
this.label = label;
this.helpText = helpText;
}
@AuraEnabled
public String label;
@AuraEnabled
public String helpText;
}
}
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,159 @@
@isTest
private class AccountBadgesControllerTest {
@testSetup
private static void setup() {
Profile profile = [
SELECT Id
FROM Profile
WHERE Name = 'System Administrator'
LIMIT 1
];

User thisUser = new User(
LastName = 'user',
Email = 'user@nav.apextest',
Username = 'user@nav.apextest',
Alias = 'usr',
LanguageLocaleKey = 'no',
LocaleSidKey = 'no_NO',
TimeZoneSidKey = 'Europe/Paris',
EmailEncodingKey = 'UTF-8',
ProfileId = profile.Id
);
insert thisUser;

System.runAs(thisUser) {
List<Account> testAccounts = new List<Account>();
testAccounts.add(
new Account(
Name = 'TestAccount0',
INT_OrganizationNumber__c = '9000000001',
TAG_Partner_Status__c = 'Strategisk Partner'
)
);
testAccounts.add(
new Account(
Name = 'TestAccount1',
INT_OrganizationNumber__c = '9000000002',
TAG_Partner_Status__c = null
)
);
insert testAccounts;

TAG_TestDataFactory.createUserWithPermset(
'ARBEIDSGIVER USER',
new List<String>{
'Arbeidsgiver_arenaActivity',
'Arbeidsgiver_base',
'Arbeidsgiver_contract',
'Arbeidsgiver_opportunity',
'Arbeidsgiver_IA'
}
);
User user1 = [SELECT id FROM User WHERE LastName = 'Arbeidsgiver USER' LIMIT 1];
User user2 = TAG_TestDataFactory.createStandardUser('USER WITHOUT PERMISSIONS');
//Create account share records
insert new List<AccountShare>{
(new AccountShare(
AccountId = testAccounts[0].Id,
UserOrGroupId = user1.id,
AccountAccessLevel = 'Read',
OpportunityAccessLevel = 'None',
CaseAccessLevel = 'None',
RowCause = Schema.AccountShare.RowCause.Manual
)),
(new AccountShare(
AccountId = testAccounts[1].Id,
UserOrGroupId = user1.id,
AccountAccessLevel = 'Read',
OpportunityAccessLevel = 'None',
CaseAccessLevel = 'None',
RowCause = Schema.AccountShare.RowCause.Manual
)),
(new AccountShare(
AccountId = testAccounts[0].Id,
UserOrGroupId = user2.id,
AccountAccessLevel = 'Read',
OpportunityAccessLevel = 'None',
CaseAccessLevel = 'None',
RowCause = Schema.AccountShare.RowCause.Manual
)),
(new AccountShare(
AccountId = testAccounts[1].Id,
UserOrGroupId = user2.id,
AccountAccessLevel = 'Read',
OpportunityAccessLevel = 'None',
CaseAccessLevel = 'None',
RowCause = Schema.AccountShare.RowCause.Manual
))
};
}

}

@isTest
static void badges_Should_Be_Created_When_Account_Match_Criteria() {
Account testAccount = [
SELECT Id, TAG_Partner_Status__c
FROM Account
WHERE TAG_Partner_Status__c = 'Strategisk Partner'
LIMIT 1
];
User user = [SELECT id FROM User WHERE LastName = 'Arbeidsgiver USER' LIMIT 1];
List<AccountBadgesController.Badge> accountBadges;
System.runAs(user) {
Test.startTest();
accountBadges = AccountBadgesController.createBadges(testAccount.Id);
Test.stopTest();
}
Assert.isNotNull(accountBadges, 'Badges should be returned');
Assert.isTrue(accountBadges.size() == 1, 'Badges should be returned');
Assert.areEqual(
testAccount.TAG_Partner_Status__c,
accountBadges[0].label,
'Label should be same as account partner status'
);
}

@isTest
static void no_Badges_Should_Be_Created_When_Account_Match_No_Criteria() {
Account testAccount = [
SELECT Id, TAG_Partner_Status__c
FROM Account
WHERE TAG_Partner_Status__c = NULL
LIMIT 1
];
User user = [SELECT id FROM User WHERE LastName = 'Arbeidsgiver USER' LIMIT 1];
List<AccountBadgesController.Badge> accountBadges;
System.runAs(user) {
Test.startTest();
accountBadges = AccountBadgesController.createBadges(testAccount.Id);
Test.stopTest();
}
Assert.isNotNull(accountBadges, 'Badges should be returned');
Assert.isTrue(accountBadges.size() == 0, 'Badges should not have any items');
}

@isTest
static void badge_Should_Not_Be_Created_For_Non_Accessible_Fields() {
Account testAccount = [
SELECT Id, TAG_Partner_Status__c
FROM Account
WHERE TAG_Partner_Status__c = 'Strategisk Partner'
LIMIT 1
];
User user = [SELECT id FROM User WHERE LastName = 'USER WITHOUT PERMISSIONS' LIMIT 1];
List<AccountBadgesController.Badge> accountBadges;
System.runAs(user) {
Assert.isTrue(AccessControlValidator.sObjectIsAccessible('Account'));
Assert.isFalse(AccessControlValidator.fieldIsAccessible('Account', 'TAG_Partner_Status__c'));

Test.startTest();
accountBadges = AccountBadgesController.createBadges(testAccount.Id);
Test.stopTest();
}

Assert.isNotNull(accountBadges, 'Badges should be returned');
Assert.isTrue(accountBadges.size() == 0, 'Badges should not have any items');
}
}
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 @@
/* Set background for the container */
.account_badges_container {
display: inline-flex;
flex-wrap: wrap;
background-color: white;
}

.badge-item {
background-color: var(--slds-c-badge-color-background, transparent);
}
.account_badge {
color: var(--slds-c-badge-text-color, #23262a);
border-radius: var(--slds-c-badge-radius-border, 15rem);
background-color: var(--slds-c-badge-color-background, #fff);
border: 1px solid #7f8900;
}

/*
text: Gray 900 #23262A

border: Limegreen 400 #C1CB33
surface: Limegreen 100 #F9FCCC
.style1 {
color: var(--slds-c-badge-text-color, #23262a);
border-radius: var(--slds-c-badge-radius-border, 15rem);
background-color: var(--slds-c-badge-color-background, #f9fccc);
border: 1px solid #c1cb33;
}

border: Deepblue 500 #005B82
surface: Deepblue 100 #CCE2F0
.style2 {
color: var(--slds-c-badge-text-color, #23262a);
border-radius: var(--slds-c-badge-radius-border, 15rem);
background-color: var(--slds-c-badge-color-background, #cce2f0);
border: 1px solid #005b82;
}

border: Deepblue 500 #003453
.style3 {
color: var(--slds-c-badge-text-color, #23262a);
border-radius: var(--slds-c-badge-radius-border, 15rem);
background-color: var(--slds-c-badge-color-background, #fff);
border: 1px solid #003453;
}

border: Limegreen 700 #7F8900
.style4 {
color: var(--slds-c-badge-text-color, #23262a);
border-radius: var(--slds-c-badge-radius-border, 15rem);
background-color: var(--slds-c-badge-color-background, #fff);
border: 1px solid #7f8900;
}


*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<template>
<template if:true={renderBadges}>
<lightning-card>
<div class="account_badges_container slds-card__body slds-card__body_inner">
<template for:each={badges} for:item="badge">
<div key={badge.label} class="slds-var-m-right_x-small slds-var-m-bottom_x-small account_badge">
<lightning-badge label={badge.label} class="badge-item slds-text-title_bold"></lightning-badge>
<lightning-helptext content={badge.helpText}></lightning-helptext>
</div>
</template>
</div>
</lightning-card>
</template>

<template if:false={renderBadges}> </template>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LightningElement, api, wire } from 'lwc';
import createBadges from '@salesforce/apex/AccountBadgesController.createBadges';

export default class AccountBadges extends LightningElement {
@api recordId; // Automatically populated in record context
badges = [];
renderBadges = false; // Should be true if badges are returned, false if not

// Wire service to fetch badges
@wire(createBadges, { accountId: '$recordId' })
wiredBadges({ error, data }) {
if (!this.recordId) {
console.warn('recordId is null or undefined. Skipping Apex call.');
this.renderBadges = false; // No badges to render
return;
}
if (data) {
this.badges = data;
this.renderBadges = this.badges.length > 0; // Check if badges array is empty
console.log('Badges:', JSON.stringify(this.badges));
} else if (error) {
this.badges = [];
this.renderBadges = false; // No badges to render
console.error('Error fetching badges:', error);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Account Badges</masterLabel>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Loading