chore: remove authentication related templates

This commit is contained in:
Muhsin Keloth
2025-07-31 14:15:08 +04:00
parent 35f5371e68
commit 609437ee92
6 changed files with 98 additions and 569 deletions
@@ -9,7 +9,6 @@ import {
buildTemplateParameters,
allKeysRequired,
replaceTemplateVariables,
populateAuthenticationButtonParameters,
} from 'dashboard/helper/templateHelper';
const props = defineProps({
@@ -92,10 +91,7 @@ const sendMessage = () => {
v$.value.$touch();
if (v$.value.$invalid) return;
const finalParams = populateAuthenticationButtonParameters(
props.template,
processedParams.value
);
const finalParams = processedParams.value;
const payload = {
message: renderedTemplate.value,
@@ -3,13 +3,11 @@ import {
buildTemplateParameters,
processVariable,
allKeysRequired,
populateAuthenticationButtonParameters,
} from '../templateHelper';
import { templates } from '../../store/modules/specs/inboxes/templateFixtures';
describe('templateHelper', () => {
const technicianTemplate = templates.find(t => t.name === 'technician_visit');
const otpTemplate = templates.find(t => t.name === 'basic_otp');
describe('processVariable', () => {
it('should remove curly braces from variables', () => {
@@ -126,13 +124,6 @@ describe('templateHelper', () => {
expect(result.body).toBeUndefined();
});
it('should handle AUTHENTICATION category templates with special OTP handling', () => {
const result = buildTemplateParameters(otpTemplate, false);
expect(result.body).toEqual({
otp_code: '',
});
});
it('should handle URL buttons with variables for non-authentication templates', () => {
const templateWithUrlButton = {
category: 'MARKETING',
@@ -164,145 +155,5 @@ describe('templateHelper', () => {
},
]);
});
it('should skip button variables for AUTHENTICATION templates', () => {
const result = buildTemplateParameters(otpTemplate, false);
expect(result.buttons).toBeUndefined();
});
});
describe('populateAuthenticationButtonParameters', () => {
it('should auto-populate URL button parameters with OTP code for authentication templates', () => {
const processedParams = {
body: {
otp_code: '123456',
},
buttons: [
{
type: 'url',
parameter: '',
url: 'https://www.whatsapp.com/otp/code/?code=otp{{1}}',
},
],
};
const result = populateAuthenticationButtonParameters(
otpTemplate,
processedParams
);
expect(result.buttons[0].parameter).toBe('123456');
});
it('should use positional parameter "1" if available for authentication templates', () => {
const processedParams = {
body: {
1: '654321',
otp_code: '123456',
},
buttons: [
{
type: 'url',
parameter: '',
url: 'https://www.whatsapp.com/otp/code/?code=otp{{1}}',
},
],
};
const result = populateAuthenticationButtonParameters(
otpTemplate,
processedParams
);
expect(result.buttons[0].parameter).toBe('654321');
});
it('should not modify non-authentication templates', () => {
const processedParams = {
body: {
name: 'John',
},
buttons: [
{
type: 'url',
parameter: '',
url: 'https://example.com/{{name}}',
},
],
};
const result = populateAuthenticationButtonParameters(
technicianTemplate,
processedParams
);
expect(result.buttons[0].parameter).toBe('');
});
it('should not modify non-URL buttons in authentication templates', () => {
const authTemplateWithQuickReply = {
category: 'AUTHENTICATION',
components: [
{
type: 'BODY',
text: 'Your code is {{1}}',
},
],
};
const processedParams = {
body: {
otp_code: '123456',
},
buttons: [
{
type: 'quick_reply',
parameter: 'original_value',
},
],
};
const result = populateAuthenticationButtonParameters(
authTemplateWithQuickReply,
processedParams
);
expect(result.buttons[0].parameter).toBe('original_value');
});
it('should handle templates without buttons', () => {
const processedParams = {
body: {
otp_code: '123456',
},
};
const result = populateAuthenticationButtonParameters(
otpTemplate,
processedParams
);
expect(result).toEqual(processedParams);
});
it('should not mutate the original processedParams object', () => {
const processedParams = {
body: {
otp_code: '123456',
},
buttons: [
{
type: 'url',
parameter: '',
url: 'https://www.whatsapp.com/otp/code/?code=otp{{1}}',
},
],
};
const originalParams = JSON.parse(JSON.stringify(processedParams));
populateAuthenticationButtonParameters(otpTemplate, processedParams);
expect(processedParams).toEqual(originalParams);
});
});
});
@@ -34,26 +34,7 @@ export const buildTemplateParameters = (template, hasMediaHeaderValue) => {
allVariables.body = {};
matchedVariables.forEach(variable => {
const key = processVariable(variable);
// Special handling for authentication templates
if (template?.category === 'AUTHENTICATION') {
if (
key === '1' ||
key.toLowerCase().includes('otp') ||
key.toLowerCase().includes('code')
) {
allVariables.body.otp_code = '';
} else if (
key === '2' ||
key.toLowerCase().includes('expiry') ||
key.toLowerCase().includes('minute')
) {
allVariables.body.expiry_minutes = '';
} else {
allVariables.body[key] = '';
}
} else {
allVariables.body[key] = '';
}
allVariables.body[key] = '';
});
}
@@ -71,64 +52,31 @@ export const buildTemplateParameters = (template, hasMediaHeaderValue) => {
buttonComponents.forEach(buttonComponent => {
if (buttonComponent.buttons) {
buttonComponent.buttons.forEach((button, index) => {
// Skip button parameter inputs for authentication templates
// as they are auto-populated with OTP codes
if (template?.category !== 'AUTHENTICATION') {
// Handle URL buttons with variables
if (
button.type === 'URL' &&
button.url &&
button.url.includes('{{')
) {
const buttonVars = button.url.match(/{{([^}]+)}}/g) || [];
if (buttonVars.length > 0) {
if (!allVariables.buttons) allVariables.buttons = [];
allVariables.buttons[index] = {
type: 'url',
parameter: '',
url: button.url,
variables: buttonVars.map(v => processVariable(v)),
};
}
}
// Handle copy code buttons
if (button.type === 'COPY_CODE') {
// Handle URL buttons with variables
if (button.type === 'URL' && button.url && button.url.includes('{{')) {
const buttonVars = button.url.match(/{{([^}]+)}}/g) || [];
if (buttonVars.length > 0) {
if (!allVariables.buttons) allVariables.buttons = [];
allVariables.buttons[index] = {
type: 'copy_code',
type: 'url',
parameter: '',
url: button.url,
variables: buttonVars.map(v => processVariable(v)),
};
}
}
// Handle copy code buttons
if (button.type === 'COPY_CODE') {
if (!allVariables.buttons) allVariables.buttons = [];
allVariables.buttons[index] = {
type: 'copy_code',
parameter: '',
};
}
});
}
});
return allVariables;
};
export const populateAuthenticationButtonParameters = (
template,
processedParams
) => {
const finalParams = { ...processedParams };
if (template?.category === 'AUTHENTICATION' && finalParams.buttons) {
// Deep copy the buttons array to avoid mutating the original
finalParams.buttons = finalParams.buttons.map(button => ({ ...button }));
finalParams.buttons.forEach((button, index) => {
if (button.type === 'url') {
// For authentication templates, auto-populate URL button parameter with OTP
if (finalParams.body?.['1']) {
finalParams.buttons[index].parameter = finalParams.body['1'];
} else if (finalParams.body?.otp_code) {
finalParams.buttons[index].parameter = finalParams.body.otp_code;
}
}
});
}
return finalParams;
};
@@ -313,30 +313,6 @@ export const templates = [
],
rejected_reason: 'NONE',
},
{
name: 'basic_otp',
status: 'approved',
category: 'AUTHENTICATION',
language: 'en',
namespace: 'ed41a221_133a_4558_a1d6_192960e3aee9',
components: [
{
text: '*{{1}}* is your verification code. For your security, do not share this code.',
type: 'BODY',
},
{
type: 'BUTTONS',
buttons: [
{
url: 'https://www.whatsapp.com/otp/code/?otp_type=COPY_CODE&code=otp{{1}}',
text: 'Copy code',
type: 'URL',
},
],
},
],
rejected_reason: 'NONE',
},
{
name: 'event_invitation_static',
status: 'approved',
@@ -384,34 +360,6 @@ export const templates = [
],
rejected_reason: 'NONE',
},
{
name: 'secure_login_otp',
status: 'approved',
category: 'AUTHENTICATION',
language: 'en',
namespace: 'ed41a221_133a_4558_a1d6_192960e3aee9',
components: [
{
text: '*{{1}}* is your verification code. For your security, do not share this code.',
type: 'BODY',
},
{
text: 'This code expires in 10 minutes.',
type: 'FOOTER',
},
{
type: 'BUTTONS',
buttons: [
{
url: 'https://www.whatsapp.com/otp/code/?otp_type=ZERO_TAP&cta_display_name=Autofill&package_name=com.chatwoot.app&signature_hash=12121212121&code_expiration_minutes=10&code=otp{{1}}',
text: 'Copy code',
type: 'URL',
},
],
},
],
rejected_reason: 'NONE',
},
{
name: 'discount_coupon',
status: 'approved',
@@ -536,30 +484,6 @@ export const templates = [
],
rejected_reason: 'NONE',
},
{
name: 'otp_verification',
status: 'approved',
category: 'AUTHENTICATION',
language: 'en_US',
namespace: 'ed41a221_133a_4558_a1d6_192960e3aee9',
components: [
{
text: 'Use code *{{1}}* to verify your transaction of {{2}}.',
type: 'BODY',
},
{
type: 'BUTTONS',
buttons: [
{
url: 'https://www.whatsapp.com/otp/code/?otp_type=COPY_CODE&code=otp{{1}}',
text: 'Copy code',
type: 'URL',
},
],
},
],
rejected_reason: 'NONE',
},
{
name: 'feedback_request',
status: 'approved',