Skip to content

Commit 8c2908d

Browse files
committed
CCM-11492 Move util to get template IDs
1 parent 1422790 commit 8c2908d

File tree

4 files changed

+89
-74
lines changed

4 files changed

+89
-74
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { getMessagePlanTemplateIds } from '@utils/get-message-plan-template-ids';
2+
import type { RoutingConfig } from 'nhs-notify-backend-client';
3+
4+
const baseConfig: RoutingConfig = {
5+
id: 'test-id',
6+
name: 'Test message plan',
7+
status: 'DRAFT',
8+
clientId: 'client-1',
9+
campaignId: 'campaign-1',
10+
createdAt: '2025-01-01T00:00:00.000Z',
11+
updatedAt: '2025-01-01T00:00:00.000Z',
12+
cascade: [],
13+
cascadeGroupOverrides: [{ name: 'standard' }],
14+
};
15+
16+
describe('getMessagePlanTemplateIds', () => {
17+
it('should collect unique template IDs from defaults and conditionals', () => {
18+
const plan: RoutingConfig = {
19+
...baseConfig,
20+
cascade: [
21+
{
22+
cascadeGroups: ['standard'],
23+
channel: 'NHSAPP',
24+
channelType: 'primary',
25+
defaultTemplateId: 'template-1',
26+
},
27+
{
28+
cascadeGroups: ['standard'],
29+
channel: 'EMAIL',
30+
channelType: 'primary',
31+
conditionalTemplates: [
32+
{ templateId: 'template-2', language: 'fr' },
33+
{ templateId: 'template-3', language: 'fr' },
34+
],
35+
},
36+
{
37+
cascadeGroups: ['standard'],
38+
channel: 'SMS',
39+
channelType: 'primary',
40+
defaultTemplateId: 'template-1',
41+
conditionalTemplates: [{ templateId: 'template-2', language: 'fr' }],
42+
},
43+
],
44+
};
45+
46+
const ids = [...getMessagePlanTemplateIds(plan)].sort();
47+
expect(ids).toEqual(['template-1', 'template-2', 'template-3']);
48+
});
49+
50+
it('should return empty set when there are no templates', () => {
51+
const plan: RoutingConfig = {
52+
...baseConfig,
53+
cascade: [
54+
{
55+
cascadeGroups: ['standard'],
56+
channel: 'NHSAPP',
57+
channelType: 'primary',
58+
defaultTemplateId: '',
59+
},
60+
],
61+
};
62+
const ids = getMessagePlanTemplateIds(plan);
63+
expect(ids.size).toBe(0);
64+
});
65+
});

frontend/src/__tests__/utils/message-plans.test.ts

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
getMessagePlan,
33
updateMessagePlan,
4-
getMessagePlanTemplateIds,
54
getTemplatesByIds,
65
getMessagePlanTemplates,
76
getRoutingConfigs,
@@ -25,7 +24,6 @@ import {
2524
NHS_APP_TEMPLATE,
2625
SMS_TEMPLATE,
2726
} from '@testhelpers/helpers';
28-
import { error } from 'node:console';
2927
import { randomUUID } from 'node:crypto';
3028

3129
jest.mock('@utils/amplify-utils');
@@ -456,59 +454,6 @@ describe('Message plans actions', () => {
456454
});
457455
});
458456

459-
describe('getMessagePlanTemplateIds', () => {
460-
it('should collect unique template IDs from defaults and conditionals', () => {
461-
const plan: RoutingConfig = {
462-
...baseConfig,
463-
cascade: [
464-
{
465-
cascadeGroups: ['standard'],
466-
channel: 'NHSAPP',
467-
channelType: 'primary',
468-
defaultTemplateId: 'template-1',
469-
},
470-
{
471-
cascadeGroups: ['standard'],
472-
channel: 'EMAIL',
473-
channelType: 'primary',
474-
conditionalTemplates: [
475-
{ templateId: 'template-2', language: 'fr' },
476-
{ templateId: 'template-3', language: 'fr' },
477-
],
478-
},
479-
{
480-
cascadeGroups: ['standard'],
481-
channel: 'SMS',
482-
channelType: 'primary',
483-
defaultTemplateId: 'template-1',
484-
conditionalTemplates: [
485-
{ templateId: 'template-2', language: 'fr' },
486-
],
487-
},
488-
],
489-
};
490-
491-
const ids = [...getMessagePlanTemplateIds(plan)].sort();
492-
expect(ids).toEqual(['template-1', 'template-2', 'template-3']);
493-
});
494-
495-
it('should return empty set when there are no templates', () => {
496-
const plan: RoutingConfig = {
497-
...baseConfig,
498-
cascade: [
499-
{
500-
cascadeGroups: ['standard'],
501-
channel: 'NHSAPP',
502-
channelType: 'primary',
503-
defaultTemplateId: '',
504-
},
505-
],
506-
};
507-
const ids = getMessagePlanTemplateIds(plan);
508-
expect(ids.size).toBe(0);
509-
});
510-
});
511-
512457
describe('getTemplatesById', () => {
513458
it('should return a map of successful template fetches and ignore undefined/rejected', async () => {
514459
getTemplateMock.mockImplementation(async (id: string) => {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { RoutingConfig } from 'nhs-notify-backend-client';
2+
3+
/**
4+
* Extracts all template IDs from a RoutingConfig
5+
*/
6+
export function getMessagePlanTemplateIds(
7+
messagePlan: RoutingConfig
8+
): Set<string> {
9+
const templateIds = new Set<string>();
10+
11+
for (const cascadeItem of messagePlan.cascade) {
12+
if (cascadeItem.defaultTemplateId)
13+
templateIds.add(cascadeItem.defaultTemplateId);
14+
if (cascadeItem.conditionalTemplates) {
15+
for (const conditionalTemplate of cascadeItem.conditionalTemplates) {
16+
if (conditionalTemplate.templateId)
17+
templateIds.add(conditionalTemplate.templateId);
18+
}
19+
}
20+
}
21+
22+
return templateIds;
23+
}

frontend/src/utils/message-plans.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
TemplateDto,
88
RoutingConfigStatusActive,
99
} from 'nhs-notify-backend-client';
10+
import { getMessagePlanTemplateIds } from './get-message-plan-template-ids';
1011
import { getSessionServer } from './amplify-utils';
1112
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1213
import { getTemplate } from './form-actions';
@@ -171,25 +172,6 @@ export async function getMessagePlanTemplates(
171172
return getTemplatesByIds([...templateIds]);
172173
}
173174

174-
export function getMessagePlanTemplateIds(
175-
messagePlan: RoutingConfig
176-
): Set<string> {
177-
const templateIds = new Set<string>();
178-
179-
for (const cascadeItem of messagePlan.cascade) {
180-
if (cascadeItem.defaultTemplateId)
181-
templateIds.add(cascadeItem.defaultTemplateId);
182-
if (cascadeItem.conditionalTemplates) {
183-
for (const conditionalTemplate of cascadeItem.conditionalTemplates) {
184-
if (conditionalTemplate.templateId)
185-
templateIds.add(conditionalTemplate.templateId);
186-
}
187-
}
188-
}
189-
190-
return templateIds;
191-
}
192-
193175
export async function getTemplatesByIds(templateIds: string[]) {
194176
const results = await Promise.allSettled(
195177
templateIds.map(async (templateId) => {

0 commit comments

Comments
 (0)