Skip to content

Commit c1bb8cf

Browse files
committed
CCM-11492 Update message-plans
1 parent 4447d76 commit c1bb8cf

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
createRoutingConfig,
99
} from '@utils/message-plans';
1010
import { getSessionServer } from '@utils/amplify-utils';
11-
import { routingConfigurationApiClient } from 'nhs-notify-backend-client';
11+
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
1212
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1313
import { getTemplate } from '@utils/form-actions';
1414
import type {
@@ -455,23 +455,21 @@ describe('Message plans actions', () => {
455455
});
456456

457457
describe('getTemplatesById', () => {
458-
it('should return a map of successful template fetches and ignore undefined/rejected', async () => {
458+
it('should return a map of successful template fetches and ignore undefined', async () => {
459459
getTemplateMock.mockImplementation(async (id: string) => {
460460
if (id === 'template-1') return { ...EMAIL_TEMPLATE, id: 'template-1' };
461461
if (id === 'template-3') return { ...SMS_TEMPLATE, id: 'template-3' };
462462
if (id === 'template-2') return undefined;
463-
if (id === 'error-template') throw new Error('error');
464463
return undefined;
465464
});
466465

467466
const result = await getTemplatesByIds([
468467
'template-1',
469468
'template-2',
470469
'template-3',
471-
'error-template',
472470
]);
473471

474-
expect(getTemplateMock).toHaveBeenCalledTimes(4);
472+
expect(getTemplateMock).toHaveBeenCalledTimes(3);
475473
expect(result).toEqual({
476474
'template-1': expect.objectContaining({
477475
id: 'template-1',
@@ -483,6 +481,26 @@ describe('Message plans actions', () => {
483481
}),
484482
});
485483
});
484+
485+
it('should throw an error with the template id when a fetch fails', async () => {
486+
getTemplateMock.mockImplementation(async (id: string) => {
487+
if (id === 'template-1') return { ...EMAIL_TEMPLATE, id: 'template-1' };
488+
if (id === 'template-3') return { ...SMS_TEMPLATE, id: 'template-3' };
489+
if (id === 'template-2') return undefined;
490+
if (id === 'error-template') throw new Error('error');
491+
return undefined;
492+
});
493+
494+
await expect(
495+
getTemplatesByIds([
496+
'template-1',
497+
'template-2',
498+
'template-3',
499+
'error-template',
500+
])
501+
).rejects.toThrow('Failed to get template for id error-template');
502+
expect(getTemplateMock).toHaveBeenCalledTimes(4);
503+
});
486504
});
487505

488506
describe('getMessagePlanTemplates', () => {

frontend/src/utils/message-plans.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import {
44
RoutingConfig,
55
$RoutingConfig,
6-
routingConfigurationApiClient,
76
TemplateDto,
87
RoutingConfigStatusActive,
98
} from 'nhs-notify-backend-client';
@@ -12,6 +11,7 @@ import { getSessionServer } from './amplify-utils';
1211
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1312
import { getTemplate } from './form-actions';
1413
import { sortAscByUpdatedAt } from './sort';
14+
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
1515

1616
export async function getRoutingConfigs(): Promise<RoutingConfig[]> {
1717
const { accessToken } = await getSessionServer();
@@ -55,9 +55,7 @@ export async function countRoutingConfigs(
5555
);
5656

5757
if (error) {
58-
logger.error(`Failed to count routing configuration for ${status}`, {
59-
error,
60-
});
58+
logger.error(`Failed to count routing configuration for ${status}`, error);
6159
return 0;
6260
}
6361

@@ -79,19 +77,15 @@ export async function getRoutingConfig(
7977
);
8078

8179
if (error) {
82-
logger.error('Failed to get routing configuration', {
83-
error: error,
84-
});
80+
logger.error('Failed to get routing configuration', error);
8581
}
8682

8783
if (!data) return undefined;
8884

8985
const result = $RoutingConfig.safeParse(data);
9086

9187
if (!result.success) {
92-
logger.error('Invalid routing configuration object', {
93-
error: result.error,
94-
});
88+
logger.error('Invalid routing configuration object', result.error);
9589
return undefined;
9690
}
9791

@@ -116,7 +110,7 @@ export async function createRoutingConfig(
116110
);
117111

118112
if (error) {
119-
logger.error('Failed to create message plan', { error });
113+
logger.error('Failed to create message plan', error);
120114
throw new Error('Failed to create message plan');
121115
}
122116

@@ -140,9 +134,7 @@ export async function updateRoutingConfig(
140134
);
141135

142136
if (error) {
143-
logger.error('Failed to get routing configuration', {
144-
error: error,
145-
});
137+
logger.error('Failed to get routing configuration', error);
146138
return;
147139
}
148140

@@ -151,9 +143,7 @@ export async function updateRoutingConfig(
151143
const result = $RoutingConfig.safeParse(data);
152144

153145
if (!result.success) {
154-
logger.error('Invalid routing configuration object', {
155-
error: result.error,
156-
});
146+
logger.error('Invalid routing configuration object', result.error);
157147
return undefined;
158148
}
159149

@@ -172,11 +162,17 @@ export async function getMessagePlanTemplates(
172162
return getTemplatesByIds([...templateIds]);
173163
}
174164

175-
export async function getTemplatesByIds(templateIds: string[]) {
165+
export async function getTemplatesByIds(
166+
templateIds: string[]
167+
): Promise<MessagePlanTemplates> {
176168
const results = await Promise.allSettled(
177169
templateIds.map(async (templateId) => {
178-
const template = await getTemplate(templateId);
179-
return { id: templateId, template };
170+
try {
171+
const template = await getTemplate(templateId);
172+
return { id: templateId, template };
173+
} catch (error) {
174+
throw { id: templateId, error };
175+
}
180176
})
181177
);
182178

@@ -187,7 +183,8 @@ export async function getTemplatesByIds(templateIds: string[]) {
187183
const { id, template } = result.value;
188184
if (template) templates[id] = template;
189185
} else {
190-
// TODO: Error handling
186+
const { id, error } = result.reason;
187+
throw new Error(`Failed to get template for id ${id}: ${error}`);
191188
}
192189
}
193190

0 commit comments

Comments
 (0)