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

added amplitude logging to chat view cmp #205

Merged
merged 7 commits into from
Feb 21, 2025
Merged
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
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"printWidth": 120,
"singleQuote": true,
"tabWidth": 4,
"plugins": ["prettier-plugin-apex"],
"overrides": [
{
"files": "**/lwc/**/*.html",
Expand All @@ -15,6 +16,12 @@
"options": {
"parser": "html"
}
},
{
"files": "*.cls",
"options": {
"parser": "apex"
}
}
]
}
11 changes: 8 additions & 3 deletions force-app/main/default/classes/nksChatView.cls
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
public with sharing class nksChatView {
@AuraEnabled(cacheable=true)
public static Id getThreadId(String chatId) {
List<Thread__c> thread = [SELECT Id FROM Thread__c WHERE CRM_Related_Object__c = :chatId LIMIT 1];
public static Thread__c getThread(String chatId) {
List<Thread__c> thread = [
SELECT Id, CRM_Theme_Group_Name__c
FROM Thread__c
WHERE CRM_Related_Object__c = :chatId
LIMIT 1
];
if (thread.size() > 0) {
return thread[0].Id;
return thread[0];
}
return null;
}
Expand Down
14 changes: 7 additions & 7 deletions force-app/main/default/classes/nksChatView_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ public with sharing class nksChatView_Test {
}

@isTest
static void testGetThreadId() {
static void testGetThread() {
LiveChatTranscript transcript = [SELECT Id FROM LiveChatTranscript LIMIT 1];

Test.startTest();
Id threadId = nksChatView.getThreadId(transcript.Id);
Thread__c testThread = nksChatView.getThread(transcript.Id);
Test.stopTest();
System.assert(threadId != null);
Assert.isNotNull(testThread.Id);
}

@isTest
static void testGetThreadIdNoThread() {
LiveChatVisitor visitor = [SELECT Id FROM LiveChatVisitor LIMIT 1];

Test.startTest();
Id threadId = nksChatView.getThreadId(visitor.Id);
Thread__c testThread = nksChatView.getThread(visitor.Id);
Test.stopTest();
System.assert(threadId == null);
Assert.isNull(testThread);
}

@isTest
Expand All @@ -53,7 +53,7 @@ public with sharing class nksChatView_Test {
Test.startTest();
String chatConversation = nksChatView.getChatbotMessage(transcript.Id, user.Id);
Test.stopTest();
System.assert(chatConversation == 'Det oppstod en feil');
Assert.isTrue(chatConversation == 'Det oppstod en feil');
}

@isTest
Expand All @@ -63,6 +63,6 @@ public with sharing class nksChatView_Test {
Test.startTest();
String chatConversation = nksChatView.getChatbotMessage(visitor.Id, user.Id);
Test.stopTest();
System.assert(chatConversation == 'Det oppstod en feil');
Assert.isTrue(chatConversation == 'Det oppstod en feil');
}
}
2 changes: 1 addition & 1 deletion force-app/main/default/lwc/nksChatView/nksChatView.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1 slot="modalHeader" class="slds-var-m-top_x-large typo-sidetittel text-center
</c-crm-messaging-community-thread-viewer> -->
<div class="nav-wrapper">
<div class="header" style="text-align: center">
<h1 class="typo-sidetittel">Chatsamtale</h1>
<h1 class="typo-sidetittel">{pageTitle}</h1>
<div>
<p>
Trykk her for å se
Expand Down
58 changes: 36 additions & 22 deletions force-app/main/default/lwc/nksChatView/nksChatView.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,62 @@
import { LightningElement, api, wire, track } from 'lwc';
import getThreadId from '@salesforce/apex/nksChatView.getThreadId';
import { LightningElement, api, wire } from 'lwc';
import getThread from '@salesforce/apex/nksChatView.getThread';
import markasread from '@salesforce/apex/CRM_MessageHelperExperience.markAsRead';
import getChatbotMessage from '@salesforce/apex/nksChatView.getChatbotMessage';
import { publish, MessageContext } from 'lightning/messageService';
import globalModalOpen from '@salesforce/messageChannel/globalModalOpen__c';
import userId from '@salesforce/user/Id';

///////////// Extra import
import getmessages from '@salesforce/apex/CRM_MessageHelperExperience.getMessagesFromThread';
import getContactId from '@salesforce/apex/CRM_MessageHelperExperience.getUserContactId';
import { logModalEvent, setDecoratorParams, getComponentName } from 'c/inboxAmplitude';

export default class NksChatView extends LightningElement {
@api recordId;

threadId;
errorList = { title: '', errors: [] };
modalOpen = false;
@track chatbotMessage = 'Laster inn samtale';
chatbotMessage = 'Laster inn samtale';
userContactId;
messages;
themeGroup;

@wire(MessageContext)
messageContext;

userContactId;
messages;

connectedCallback() {
getContactId({})
.then((contactId) => {
this.userContactId = contactId;
})
.catch(() => {
//Apex error
.catch((error) => {
console.error('Error retrieving contactId: ', error);
});
}

@wire(getThreadId, { chatId: '$recordId' })
test({ error, data }) {
@wire(getThread, { chatId: '$recordId' })
wiredThread(result) {
const { error, data } = result;
if (error) {
console.log(error);
console.error(error);
}
if (data) {
this.threadId = data;
this.threadId = data.Id;
markasread({ threadId: this.threadId });

this.themeGroup = data.CRM_Theme_Group_Name__c;
if (this.themeGroup) {
setDecoratorParams('Chat', `Chatsamtale - ${this.themeGroup}`, this.themeGroup);
}
}
}

@wire(getmessages, { threadId: '$threadId' }) //Calls apex and extracts messages related to this record
wiremessages(result) {
const { data, error } = result;
if (error) {
console.error(error);
} else if (data) {
this.messages = data;
}
}

Expand All @@ -63,13 +79,17 @@ export default class NksChatView extends LightningElement {
getChatbotMessage({ chatId: this.recordId, userId: userId }).then((res) => {
this.chatbotMessage = res;
});

logModalEvent(true, 'Chatbot samtale', getComponentName(this.template), 'Chatsamtale');
}

closeModal() {
this.modalOpen = false;
publish(this.messageContext, globalModalOpen, { status: 'false' });
const btn = this.template.querySelector('.focusBtn');
btn.focus();

logModalEvent(false, 'Chatbot samtale', getComponentName(this.template), 'Chatsamtale');
}

handleKeyboardEvent(event) {
Expand All @@ -83,14 +103,8 @@ export default class NksChatView extends LightningElement {
get termsModal() {
return this.template.querySelector('c-community-modal');
}
/////////////////////////////////////////////////////////////

@wire(getmessages, { threadId: '$threadId' }) //Calls apex and extracts messages related to this record
wiremessages(result) {
if (result.error) {
this.error = result.error;
} else if (result.data) {
this.messages = result.data;
}
get pageTitle() {
return `Chatsamtale${this.themeGroup ? ' - ' + this.themeGroup : ''}`;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="recordComponent">
<apiVersion>52.0</apiVersion>
<apiVersion>62.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Chat thread viewer</masterLabel>
<targets>
Expand All @@ -10,7 +10,7 @@
<targetConfigs>
<targetConfig targets="lightningCommunity__Default">
<property name="recordId" type="String" label="Record ID"
description="Should be set to {!recordId}"/>
description="Should be set to {!recordId}" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
</LightningComponentBundle>
Loading