chore: add getFilteredWhatsAppTemplates getter

This commit is contained in:
Muhsin Keloth
2025-07-30 16:48:08 +04:00
parent f49c31f7bc
commit 1e2ce070aa
7 changed files with 331 additions and 57 deletions
@@ -19,7 +19,9 @@ const formState = {
uiFlags: useMapGetter('campaigns/getUIFlags'),
labels: useMapGetter('labels/getLabels'),
inboxes: useMapGetter('inboxes/getWhatsAppInboxes'),
getWhatsAppTemplates: useMapGetter('inboxes/getWhatsAppTemplates'),
getFilteredWhatsAppTemplates: useMapGetter(
'inboxes/getFilteredWhatsAppTemplates'
),
};
const initialState = {
@@ -68,7 +70,7 @@ const inboxOptions = computed(() =>
const templateOptions = computed(() => {
if (!state.inboxId) return [];
const templates = formState.getWhatsAppTemplates.value(state.inboxId);
const templates = formState.getFilteredWhatsAppTemplates.value(state.inboxId);
return templates.map(template => {
// Create a more user-friendly label from template name
const friendlyName = template.name
@@ -25,6 +25,7 @@ const props = defineProps({
hasNoInbox: { type: Boolean, default: false },
isDropdownActive: { type: Boolean, default: false },
messageSignature: { type: String, default: '' },
inboxId: { type: Number, default: null },
});
const emit = defineEmits([
@@ -150,9 +151,10 @@ useKeyboardEvents(keyboardEvents);
<div
class="flex items-center justify-between w-full h-[3.25rem] gap-2 px-4 py-3"
>
<div class="flex items-center gap-2">
<div class="flex gap-2 items-center">
<WhatsAppOptions
v-if="isWhatsappInbox"
:inbox-id="inboxId"
:message-templates="messageTemplates"
@send-message="emit('sendWhatsappMessage', $event)"
/>
@@ -206,7 +208,7 @@ useKeyboardEvents(keyboardEvents);
/>
</div>
<div class="flex items-center gap-2">
<div class="flex gap-2 items-center">
<Button
:label="t('COMPOSE_NEW_CONVERSATION.FORM.ACTION_BUTTONS.DISCARD')"
variant="faded"
@@ -336,6 +336,7 @@ const handleSendWhatsappMessage = async ({ message, templateParams }) => {
:is-loading="isCreating"
:disable-send-button="isCreating"
:has-selected-inbox="!!targetInbox"
:inbox-id="targetInbox?.id"
:has-no-inbox="showNoInboxAlert"
:is-dropdown-active="isAnyDropdownActive"
:message-signature="messageSignature"
@@ -1,23 +1,24 @@
<script setup>
import { computed, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useMapGetter } from 'dashboard/composables/store';
import Button from 'dashboard/components-next/button/Button.vue';
import WhatsappTemplateParser from './WhatsappTemplateParser.vue';
const props = defineProps({
messageTemplates: {
type: Array,
default: () => [],
inboxId: {
type: Number,
required: true,
},
});
const emit = defineEmits(['sendMessage']);
const { t } = useI18n();
// TODO: Remove this when we support all formats
const formatsToRemove = ['DOCUMENT', 'IMAGE', 'VIDEO'];
const getFilteredWhatsAppTemplates = useMapGetter(
'inboxes/getFilteredWhatsAppTemplates'
);
const searchQuery = ref('');
const selectedTemplate = ref(null);
@@ -25,19 +26,7 @@ const selectedTemplate = ref(null);
const showTemplatesMenu = ref(false);
const whatsAppTemplateMessages = computed(() => {
// Add null check and ensure it's an array
const templates = Array.isArray(props.messageTemplates)
? props.messageTemplates
: [];
// TODO: Remove the last filter when we support all formats
return templates
.filter(template => template?.status?.toLowerCase() === 'approved')
.filter(template => {
return template?.components?.every(component => {
return !formatsToRemove.includes(component.format);
});
});
return getFilteredWhatsAppTemplates.value(props.inboxId);
});
const filteredTemplates = computed(() => {
@@ -87,7 +76,7 @@ const handleSendMessage = template => {
class="absolute top-full mt-1.5 max-h-96 overflow-y-auto left-0 flex flex-col gap-2 p-4 items-center w-[21.875rem] h-auto bg-n-solid-2 border border-n-strong shadow-sm rounded-lg"
>
<div class="relative w-full">
<span class="absolute i-lucide-search size-3.5 top-2 left-3" />
<span class="absolute top-2 left-3 i-lucide-search size-3.5" />
<input
v-model="searchQuery"
type="search"
@@ -96,13 +85,13 @@ const handleSendMessage = template => {
'COMPOSE_NEW_CONVERSATION.FORM.WHATSAPP_OPTIONS.SEARCH_PLACEHOLDER'
)
"
class="w-full h-8 py-2 pl-10 pr-2 text-sm reset-base outline-none border-none rounded-lg bg-n-alpha-black2 dark:bg-n-solid-1 text-n-slate-12"
class="py-2 pr-2 pl-10 w-full h-8 text-sm rounded-lg border-none outline-none reset-base bg-n-alpha-black2 dark:bg-n-solid-1 text-n-slate-12"
/>
</div>
<div
v-for="template in filteredTemplates"
:key="template.id"
class="flex flex-col w-full gap-2 p-2 rounded-lg cursor-pointer dark:hover:bg-n-alpha-3 hover:bg-n-alpha-1"
class="flex flex-col gap-2 p-2 w-full rounded-lg cursor-pointer dark:hover:bg-n-alpha-3 hover:bg-n-alpha-1"
@click="handleTemplateClick(template)"
>
<span class="text-sm text-n-slate-12">{{ template.name }}</span>
@@ -111,7 +100,7 @@ const handleSendMessage = template => {
</p>
</div>
<template v-if="filteredTemplates.length === 0">
<p class="w-full pt-2 text-sm text-n-slate-11">
<p class="pt-2 w-full text-sm text-n-slate-11">
{{ t('COMPOSE_NEW_CONVERSATION.FORM.WHATSAPP_OPTIONS.EMPTY_STATE') }}
</p>
</template>
@@ -21,38 +21,9 @@ export default {
},
computed: {
whatsAppTemplateMessages() {
const templates = this.$store.getters['inboxes/getWhatsAppTemplates'](
return this.$store.getters['inboxes/getFilteredWhatsAppTemplates'](
this.inboxId
);
if (!templates || !Array.isArray(templates)) {
return [];
}
return templates.filter(template => {
// Ensure template has required properties
if (!template || !template.status || !template.components) {
return false;
}
// Only show approved templates
if (template.status.toLowerCase() !== 'approved') {
return false;
}
// Filter out interactive templates (LIST, PRODUCT, CATALOG) and location templates
const hasUnsupportedComponents = template.components.some(
component =>
['LIST', 'PRODUCT', 'CATALOG'].includes(component.type) ||
(component.type === 'HEADER' && component.format === 'LOCATION')
);
if (hasUnsupportedComponents) {
return false;
}
return true;
});
},
filteredTemplateMessages() {
return this.whatsAppTemplateMessages.filter(template =>
@@ -46,6 +46,49 @@ export const getters = {
return messagesTemplates;
},
getFilteredWhatsAppTemplates: $state => inboxId => {
const [inbox] = $state.records.filter(
record => record.id === Number(inboxId)
);
const {
message_templates: whatsAppMessageTemplates,
additional_attributes: additionalAttributes,
} = inbox || {};
const { message_templates: apiInboxMessageTemplates } =
additionalAttributes || {};
const templates = whatsAppMessageTemplates || apiInboxMessageTemplates;
if (!templates || !Array.isArray(templates)) {
return [];
}
return templates.filter(template => {
// Ensure template has required properties
if (!template || !template.status || !template.components) {
return false;
}
// Only show approved templates
if (template.status.toLowerCase() !== 'approved') {
return false;
}
// Filter out interactive templates (LIST, PRODUCT, CATALOG) and location templates
const hasUnsupportedComponents = template.components.some(
component =>
['LIST', 'PRODUCT', 'CATALOG'].includes(component.type) ||
(component.type === 'HEADER' && component.format === 'LOCATION')
);
if (hasUnsupportedComponents) {
return false;
}
return true;
});
},
getNewConversationInboxes($state) {
return $state.records.filter(inbox => {
const { channel_type: channelType, phone_number: phoneNumber = '' } =
@@ -1,5 +1,6 @@
import { getters } from '../../inboxes';
import inboxList from './fixtures';
import { templates } from '../../../../../shared/mixins/specs/whatsappTemplates/fixtures';
describe('#getters', () => {
it('getInboxes', () => {
@@ -93,4 +94,269 @@ describe('#getters', () => {
provider: 'default',
});
});
describe('getFilteredWhatsAppTemplates', () => {
it('returns empty array when inbox not found', () => {
const state = { records: [] };
expect(getters.getFilteredWhatsAppTemplates(state)(999)).toEqual([]);
});
it('returns empty array when templates is null or undefined', () => {
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: null,
additional_attributes: { message_templates: undefined },
},
],
};
expect(getters.getFilteredWhatsAppTemplates(state)(1)).toEqual([]);
});
it('returns empty array when templates is not an array', () => {
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: 'invalid',
additional_attributes: {},
},
],
};
expect(getters.getFilteredWhatsAppTemplates(state)(1)).toEqual([]);
});
it('filters out templates without required properties', () => {
const invalidTemplates = [
{ name: 'incomplete_template' }, // missing status and components
{ status: 'approved' }, // missing name and components
{ name: 'another_incomplete', status: 'approved' }, // missing components
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: invalidTemplates,
},
],
};
expect(getters.getFilteredWhatsAppTemplates(state)(1)).toEqual([]);
});
it('filters out non-approved templates', () => {
const mixedStatusTemplates = [
{
name: 'pending_template',
status: 'pending',
components: [{ type: 'BODY', text: 'Test' }],
},
{
name: 'rejected_template',
status: 'rejected',
components: [{ type: 'BODY', text: 'Test' }],
},
{
name: 'approved_template',
status: 'approved',
components: [{ type: 'BODY', text: 'Test' }],
},
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: mixedStatusTemplates,
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('approved_template');
});
it('filters out interactive templates (LIST, PRODUCT, CATALOG)', () => {
const interactiveTemplates = [
{
name: 'list_template',
status: 'approved',
components: [
{ type: 'BODY', text: 'Choose an option' },
{ type: 'LIST', sections: [] },
],
},
{
name: 'product_template',
status: 'approved',
components: [
{ type: 'BODY', text: 'Product info' },
{ type: 'PRODUCT', catalog_id: '123' },
],
},
{
name: 'catalog_template',
status: 'approved',
components: [
{ type: 'BODY', text: 'Catalog' },
{ type: 'CATALOG', thumbnail_product_retailer_id: '123' },
],
},
{
name: 'regular_template',
status: 'approved',
components: [{ type: 'BODY', text: 'Regular message' }],
},
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: interactiveTemplates,
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('regular_template');
});
it('filters out location templates', () => {
const locationTemplates = [
{
name: 'location_template',
status: 'approved',
components: [
{ type: 'HEADER', format: 'LOCATION' },
{ type: 'BODY', text: 'Location message' },
],
},
{
name: 'regular_template',
status: 'approved',
components: [
{ type: 'HEADER', format: 'TEXT', text: 'Header' },
{ type: 'BODY', text: 'Regular message' },
],
},
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: locationTemplates,
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('regular_template');
});
it('returns valid templates from fixture data', () => {
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: templates,
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
// All templates in fixtures should be approved and valid
expect(result.length).toBeGreaterThan(0);
// Verify all returned templates are approved
result.forEach(template => {
expect(template.status).toBe('approved');
expect(template.components).toBeDefined();
expect(Array.isArray(template.components)).toBe(true);
});
// Verify specific templates from fixtures are included
const templateNames = result.map(t => t.name);
expect(templateNames).toContain('sample_flight_confirmation');
expect(templateNames).toContain('sample_issue_resolution');
expect(templateNames).toContain('sample_shipping_confirmation');
expect(templateNames).toContain('no_variable_template');
expect(templateNames).toContain('order_confirmation');
});
it('prioritizes message_templates over additional_attributes.message_templates', () => {
const primaryTemplates = [
{
name: 'primary_template',
status: 'approved',
components: [{ type: 'BODY', text: 'Primary' }],
},
];
const fallbackTemplates = [
{
name: 'fallback_template',
status: 'approved',
components: [{ type: 'BODY', text: 'Fallback' }],
},
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: primaryTemplates,
additional_attributes: {
message_templates: fallbackTemplates,
},
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('primary_template');
});
it('falls back to additional_attributes.message_templates when message_templates is null', () => {
const fallbackTemplates = [
{
name: 'fallback_template',
status: 'approved',
components: [{ type: 'BODY', text: 'Fallback' }],
},
];
const state = {
records: [
{
id: 1,
channel_type: 'Channel::Whatsapp',
message_templates: null,
additional_attributes: {
message_templates: fallbackTemplates,
},
},
],
};
const result = getters.getFilteredWhatsAppTemplates(state)(1);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('fallback_template');
});
});
});