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-2826 : DataSyncher LWC #168

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,16 @@
<identifier>forceKnowledge_articleSearchDesktop</identifier>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentInstanceProperties>
<name>relationshipField</name>
<value>Account.CRM_Person__c</value>
</componentInstanceProperties>
<componentName>hot_dataSyncher</componentName>
<identifier>c_hot_dataSyncher</identifier>
</componentInstance>
</itemInstances>
<name>rightsidebar</name>
<type>Region</type>
</flexiPageRegions>
Expand Down Expand Up @@ -744,9 +754,8 @@
</properties>
<properties>
<name>saveOptions</name>
<value>
[{&quot;name&quot;:&quot;UseDefaultAssignmentRule&quot;,&quot;value&quot;:&quot;SHOW_CHECKBOX_WITH_DEFAULT_OFF&quot;},{&quot;name&quot;:&quot;triggerOtherEmail&quot;,&quot;value&quot;:&quot;SHOW_CHECKBOX_WITH_DEFAULT_OFF&quot;}]</value>
<value>[{&quot;name&quot;:&quot;UseDefaultAssignmentRule&quot;,&quot;value&quot;:&quot;SHOW_CHECKBOX_WITH_DEFAULT_OFF&quot;},{&quot;name&quot;:&quot;triggerOtherEmail&quot;,&quot;value&quot;:&quot;SHOW_CHECKBOX_WITH_DEFAULT_OFF&quot;}]</value>
</properties>
</template>
<type>RecordPage</type>
</FlexiPage>
</FlexiPage>
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@
<identifier>forceKnowledge_articleSearchDesktop</identifier>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentInstanceProperties>
<name>relationshipField</name>
<value>CRM_Person__c</value>
</componentInstanceProperties>
<componentName>hot_dataSyncher</componentName>
<identifier>c_hot_dataSyncher</identifier>
</componentInstance>
</itemInstances>
<mode>Replace</mode>
<name>sidebar</name>
<type>Region</type>
Expand Down
42 changes: 42 additions & 0 deletions force-app/main/default/lwc/hot_dataSyncher/hot_dataSyncher.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<template>
<!-- <c-hot_fast-einstein-spinner></c-hot_fast-einstein-spinner> -->
<div class="slds-box slds-theme_shade">
<lightning-layout multiple-rows="true">
<lightning-layout-item class="slds-align_absolute-center" size="8">
<h1 class="slds-text-heading_small">Henter informasjon fra fagsystemer</h1>
</lightning-layout-item>
<lightning-layout-item size="4" class="slds-align_absolute-center">
<c-hot_loading-spinner lwc:if={notSynced}></c-hot_loading-spinner>
</lightning-layout-item>
<lightning-layout-item size="12" style="padding-top: 1vw">
<ul class="slds-list_horizontal slds-has-dividers_left">
<template for:each={syncStatuses} for:item="status">
<li class="slds-item" key={status.name}>
<lightning-icon
if:true={status.isSyncing}
icon-name="utility:sync"
alternative-text="Oppdatering pågår"
size="x-small"
></lightning-icon>
<lightning-icon
if:true={status.isSynced}
icon-name="utility:success"
alternative-text="Oppdatering fullført"
size="x-small"
variant="success"
></lightning-icon>
<lightning-icon
if:true={status.isError}
icon-name="utility:error"
alternative-text="Oppdatering feilet"
variant="error"
size="x-small"
></lightning-icon>
{status.label}
</li>
</template>
</ul>
</lightning-layout-item>
</lightning-layout>
</div>
</template>
159 changes: 159 additions & 0 deletions force-app/main/default/lwc/hot_dataSyncher/hot_dataSyncher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import { LightningElement, api, wire, track } from 'lwc';
import getRelatedRecord from '@salesforce/apex/HOT_RecordInfoController.getRelatedRecord';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import PERSON_IDENT_FIELD from '@salesforce/schema/Person__c.Name';
import PERSON_ACTORID_FIELD from '@salesforce/schema/Person__c.INT_ActorId__c';
import PERSON_ACCOUNT_FIELD from '@salesforce/schema/Person__c.CRM_Account__c';
import { syncActorOppgaver } from 'c/crmOppgaveSyncher';
import { resolve } from 'c/hot_componentsUtils';

const syncStatus = {
SYNCING: 'SYNCING',
SYNCED: 'SYNCED',
ERROR: 'ERROR'
};

export default class NksDataSyncher extends LightningElement {
@api recordId;
@api objectApiName;
@api relationshipField;

@track syncStatuses = [];

wireFields = [this.objectApiName + '.Id'];
personId;
personFields = [PERSON_ACTORID_FIELD, PERSON_IDENT_FIELD, PERSON_ACCOUNT_FIELD];
initialized = false;
synced = false;

connectedCallback() {
this.addSyncStatus('oppgave', 'Oppgave', syncStatus.SYNCING);
this.getRelatedRecordId(this.relationshipField, this.objectApiName);
this.initialized = true;
}

@wire(getRecord, {
recordId: '$recordId',
fields: '$wireFields'
})
wiredRecordInfo({ error, data }) {
if (data) {
if (this.initialized && this.relationshipField && this.objectApiName) {
this.getRelatedRecordId(this.relationshipField, this.objectApiName);
}
} else if (error) {
console.log('Problem getting record: ', JSON.stringify(error, null, 2));
}
}

@wire(getRecord, {
recordId: '$personId',
fields: '$personFields'
})
wiredPersonInfo({ error, data }) {
if (data) {
let personIdent = getFieldValue(data, PERSON_IDENT_FIELD);
let personActorId = getFieldValue(data, PERSON_ACTORID_FIELD);
let personAccountId = getFieldValue(data, PERSON_ACCOUNT_FIELD);

if (personIdent) {
this.doSynch(personIdent, personActorId);
}
}
if (error) {
console.log('Problem getting person information: ', JSON.stringify(error, null, 2));
}
}

async doSynch(personIdent, personActorId, eventName = 'e.force:refreshView') {
try {
this.synced = false;
await this.oppgaveSync(personActorId);
this.synced = true;
const refreshEvent = new CustomEvent(eventName);
this.dispatchEvent(refreshEvent);
} catch (error) {
console.error('Problem syncing oppgave: ', JSON.stringify(error, null, 2));
}
}

async oppgaveSync(personActorId) {
try {
const syncStatusObj = this.getSyncStatus('oppgave');
if (syncStatusObj.status !== syncStatus.SYNCING) {
return;
}

await syncActorOppgaver(personActorId);
this.setSyncStatus('oppgave', syncStatus.SYNCED);
} catch (error) {
this.setSyncStatus('oppgave', syncStatus.ERROR);
console.error('Error in oppgaveSync:', JSON.stringify(error, null, 2));
throw new Error('Error syncing oppgave: ' + error.message);
}
}

getRelatedRecordId(relationshipField, objectApiName) {
getRelatedRecord({
parentId: this.recordId,
relationshipField: relationshipField,
objectApiName: objectApiName
})
.then((record) => {
let resolvedPersonId = resolve(relationshipField, record);
if (this.personId !== resolvedPersonId) {
this.setSyncStatus('oppgave', syncStatus.SYNCING);
this.personId = resolvedPersonId;
}
})
.catch((error) => {
console.log('Problem getting related record: ', JSON.stringify(error, null, 2));
});
}

addSyncStatus(name, label, status) {
let ss = this.getSyncStatus(name);

if (ss) {
ss.label = label;
ss.status = status;
} else {
ss = this.getNewSyncStatus(name, label, status);
this.syncStatuses.push(ss);
}

this.calculateSyncStatus(ss);
}

getNewSyncStatus(name, label, status) {
return {
name: name,
label: label,
status: status,
isSyncing: false,
isSynced: false,
isError: false
};
}

setSyncStatus(name, status) {
let ss = this.getSyncStatus(name);
if (ss) {
ss.status = status;
this.calculateSyncStatus(ss);
}
}

calculateSyncStatus(ss) {
ss.isSyncing = ss.status === syncStatus.SYNCING;
ss.isSynced = ss.status === syncStatus.SYNCED;
ss.isError = ss.status === syncStatus.ERROR;
}

getSyncStatus(name) {
return this.syncStatuses.find((element) => element.name === name);
}
get notSynced() {
return !this.synced;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>External Data Syncher SERVICETJENESTEN</masterLabel>
<description>Data Syncer</description>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<property name="relationshipField" label="Relationship field (API)"
description="Relationship field for identifying the person record" type="String"
required="true" default="CRM_Person__c" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* HTML: <div class="loader"></div> */
.loader {
width: 80px;
aspect-ratio: 1.154;
background: repeating-conic-gradient(from -30deg, #027b7f 0 60deg, #ffa588 0 120deg, #bf1e62 0 180deg);
animation: l13 1.5s infinite alternate;
}
@keyframes l13 {
0%,
10% {
clip-path: polygon(50% 50%, 25% 100%, 25% 100%, 25% 100%, 25% 100%, 75% 100%, 75% 100%, 75% 100%);
}
33%,
43% {
clip-path: polygon(50% 50%, 0 50%, 0 50%, 0 50%, 25% 100%, 75% 100%, 100% 50%, 100% 50%);
}
66%,
76% {
clip-path: polygon(50% 50%, 25% 0, 25% 0, 0 50%, 25% 100%, 75% 100%, 100% 50%, 75% 0);
}
95%,
100% {
clip-path: polygon(50% 50%, 75% 0, 25% 0, 0 50%, 25% 100%, 75% 100%, 100% 50%, 75% 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<div class="loader"></div>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { LightningElement } from 'lwc';

export default class loadingSpinner extends LightningElement {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<isExposed>false</isExposed>
</LightningComponentBundle>
Loading