Skip to content

Commit 885603f

Browse files
committed
validate listed rcs
1 parent e41442a commit 885603f

File tree

2 files changed

+98
-24
lines changed

2 files changed

+98
-24
lines changed

frontend/src/__tests__/utils/form-actions.test.ts

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -736,23 +736,66 @@ describe('form-actions', () => {
736736
updatedAt: '2021-01-01T00:00:00.000Z',
737737
campaignId: 'campaignId',
738738
clientId: 'clientId',
739-
cascade: [],
740-
cascadeGroupOverrides: [],
741-
} satisfies Partial<RoutingConfig>;
739+
cascade: [
740+
{
741+
channel: 'EMAIL',
742+
channelType: 'primary',
743+
defaultTemplateId: 'id',
744+
cascadeGroups: ['standard'],
745+
},
746+
],
747+
cascadeGroupOverrides: [{ name: 'standard' }],
748+
} satisfies Omit<RoutingConfig, 'id' | 'createdAt'>;
742749

743750
const routingConfigs = [
744-
{ ...fields, id: '06', createdAt: '2022-01-01T00:00:00.000Z' },
745-
{ ...fields, id: '08', createdAt: '2020-01-01T00:00:00.000Z' },
746-
{ ...fields, id: '05', createdAt: '2021-01-01T00:00:00.000Z' },
747-
{ ...fields, id: '02', createdAt: '2021-01-01T00:00:00.000Z' },
748-
{ ...fields, id: '01', createdAt: '2021-01-01T00:00:00.000Z' },
749-
{ ...fields, id: '03', createdAt: '2021-01-01T00:00:00.000Z' },
750-
{ ...fields, id: '04', createdAt: '2021-01-01T00:00:00.000Z' },
751+
{
752+
...fields,
753+
id: 'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
754+
createdAt: '2022-01-01T00:00:00.000Z',
755+
},
756+
{
757+
...fields,
758+
id: '8f5157fe-72d7-4a9c-818f-77c128ec8197',
759+
createdAt: '2020-01-01T00:00:00.000Z',
760+
},
761+
{
762+
...fields,
763+
id: '9be9d25f-81d8-422a-a85c-2fa9019cde1e',
764+
createdAt: '2021-01-01T00:00:00.000Z',
765+
},
766+
{
767+
...fields,
768+
id: '1cfdd62d-9eca-4f15-9772-1937d4524c37',
769+
createdAt: '2021-01-01T00:00:00.000Z',
770+
},
771+
{
772+
...fields,
773+
id: '18da6158-07ef-455c-9c31-1a4d78a133cf',
774+
createdAt: '2021-01-01T00:00:00.000Z',
775+
},
776+
{
777+
...fields,
778+
id: '87fb5cbf-708d-49c3-9360-3e37efdc5278',
779+
createdAt: '2021-01-01T00:00:00.000Z',
780+
},
781+
{
782+
...fields,
783+
id: '0d6408fd-57ea-42f2-aae1-ed9614b67068',
784+
createdAt: '2021-01-01T00:00:00.000Z',
785+
},
751786
];
752787

753-
// 06 is the newest, 08 is the oldest.
754-
// 01 - 05 all have the same createdAt.
755-
const expectedOrder = ['06', '01', '02', '03', '04', '05', '08'];
788+
// a487ed49-e2f7-4871-ac8d-0c6c682c71f5 is the newest, 8f5157fe-72d7-4a9c-818f-77c128ec8197 is the oldest.
789+
// the others all have the same createdAt.
790+
const expectedOrder = [
791+
'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
792+
'0d6408fd-57ea-42f2-aae1-ed9614b67068',
793+
'18da6158-07ef-455c-9c31-1a4d78a133cf',
794+
'1cfdd62d-9eca-4f15-9772-1937d4524c37',
795+
'87fb5cbf-708d-49c3-9360-3e37efdc5278',
796+
'9be9d25f-81d8-422a-a85c-2fa9019cde1e',
797+
'8f5157fe-72d7-4a9c-818f-77c128ec8197',
798+
];
756799

757800
routingConfigurationApiClientMock.list.mockResolvedValueOnce({
758801
data: routingConfigs,
@@ -767,6 +810,28 @@ describe('form-actions', () => {
767810

768811
expect(actualOrder).toEqual(expectedOrder);
769812
});
813+
814+
test('invalid routing configs are not listed', async () => {
815+
routingConfigurationApiClientMock.list.mockResolvedValueOnce({
816+
data: [
817+
{
818+
status: 'DRAFT',
819+
name: 'Routing config',
820+
updatedAt: '2021-01-01T00:00:00.000Z',
821+
campaignId: 'campaignId',
822+
clientId: 'clientId',
823+
cascade: [],
824+
cascadeGroupOverrides: [{ name: 'standard' }],
825+
id: 'a487ed49-e2f7-4871-ac8d-0c6c682c71f5',
826+
createdAt: '2022-01-01T00:00:00.000Z',
827+
},
828+
],
829+
});
830+
831+
const response = await getRoutingConfigs();
832+
833+
expect(response).toEqual([]);
834+
});
770835
});
771836

772837
describe('countRoutingConfigs', () => {

frontend/src/utils/form-actions.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { getSessionServer } from '@utils/amplify-utils';
44
import {
5+
$RoutingConfig,
56
CreateUpdateTemplate,
67
isTemplateDtoValid,
78
RoutingConfig,
@@ -29,7 +30,7 @@ export async function createTemplate(
2930
);
3031

3132
if (error) {
32-
logger.error('Failed to create template', { error });
33+
logger.error('Failed to create template', error);
3334
throw new Error('Failed to create new template');
3435
}
3536

@@ -55,7 +56,7 @@ export async function uploadLetterTemplate(
5556
);
5657

5758
if (error) {
58-
logger.error('Failed to create letter template', { error });
59+
logger.error('Failed to create letter template', error);
5960
throw new Error('Failed to create new letter template');
6061
}
6162

@@ -79,7 +80,7 @@ export async function saveTemplate(
7980
);
8081

8182
if (error) {
82-
logger.error('Failed to save template', { error });
83+
logger.error('Failed to save template', error);
8384
throw new Error('Failed to save template data');
8485
}
8586

@@ -101,7 +102,7 @@ export async function setTemplateToSubmitted(
101102
);
102103

103104
if (error) {
104-
logger.error('Failed to save template', { error });
105+
logger.error('Failed to save template', error);
105106
throw new Error('Failed to save template data');
106107
}
107108

@@ -121,7 +122,7 @@ export async function setTemplateToDeleted(templateId: string): Promise<void> {
121122
);
122123

123124
if (error) {
124-
logger.error('Failed to save template', { error });
125+
logger.error('Failed to save template', error);
125126
throw new Error('Failed to save template data');
126127
}
127128
}
@@ -141,7 +142,7 @@ export async function requestTemplateProof(
141142
);
142143

143144
if (error) {
144-
logger.error('Failed to request proof', { error });
145+
logger.error('Failed to request proof', error);
145146
throw new Error('Failed to request proof');
146147
}
147148

@@ -163,7 +164,7 @@ export async function getTemplate(
163164
);
164165

165166
if (error) {
166-
logger.error('Failed to get template', { error });
167+
logger.error('Failed to get template', error);
167168
}
168169

169170
return data;
@@ -202,13 +203,21 @@ export async function getRoutingConfigs(): Promise<RoutingConfig[]> {
202203
const { data, error } = await routingConfigurationApiClient.list(accessToken);
203204

204205
if (error) {
205-
logger.error('Failed to get routing configuration', {
206-
error: error,
207-
});
206+
logger.error('Failed to get routing configuration', error);
208207
return [];
209208
}
210209

211-
return sortAscByCreatedAt(data);
210+
const valid = data.filter((d) => {
211+
const { error: validationError, success } = $RoutingConfig.safeParse(d);
212+
213+
if (!success) {
214+
logger.error('Listed invalid routing configuration', validationError);
215+
}
216+
217+
return success;
218+
});
219+
220+
return sortAscByCreatedAt(valid);
212221
}
213222

214223
export async function countRoutingConfigs(

0 commit comments

Comments
 (0)