Skip to content

Commit f1b017b

Browse files
committed
CCM--11496: move form action to correct file
1 parent 85624c0 commit f1b017b

File tree

6 files changed

+62
-212
lines changed

6 files changed

+62
-212
lines changed

frontend/src/__tests__/app/message-plans/create-message-plan/server-action.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import type {
88
import type { MessageOrder } from 'nhs-notify-web-template-management-utils';
99
import { createMessagePlanServerAction } from '@app/message-plans/create-message-plan/server-action';
1010
import { NextRedirectError } from '@testhelpers/next-redirect';
11-
import { createRoutingConfig } from '@utils/form-actions';
11+
import { createRoutingConfig } from '@utils/message-plans';
1212

1313
jest.mock('next/navigation');
1414
jest.mocked(redirect).mockImplementation((url, type) => {
1515
throw new NextRedirectError(url, type);
1616
});
1717

18-
jest.mock('@utils/form-actions');
18+
jest.mock('@utils/message-plans');
1919
jest.mocked(createRoutingConfig).mockResolvedValue(
2020
mock<RoutingConfig>({
2121
id: 'mock-routing-config-id',

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

Lines changed: 1 addition & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ import {
1515
setTemplateToDeleted,
1616
setTemplateToSubmitted,
1717
requestTemplateProof,
18-
createRoutingConfig,
19-
submitRoutingConfig,
2018
} from '@utils/form-actions';
2119
import { getSessionServer } from '@utils/amplify-utils';
22-
import { RoutingConfig, TemplateDto } from 'nhs-notify-backend-client';
20+
import { TemplateDto } from 'nhs-notify-backend-client';
2321
import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client';
24-
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
25-
import { randomUUID } from 'node:crypto';
26-
import { mock } from 'jest-mock-extended';
2722

2823
const mockedTemplateClient = jest.mocked(templateApiClient);
29-
const mockedRoutingConfigClient = jest.mocked(routingConfigurationApiClient);
3024
const authIdTokenServerMock = jest.mocked(getSessionServer);
3125

3226
jest.mock('@utils/amplify-utils');
@@ -686,163 +680,4 @@ describe('form-actions', () => {
686680
);
687681
});
688682
});
689-
690-
describe('createRoutingConfig', () => {
691-
test('creates a routing config', async () => {
692-
const now = new Date();
693-
const id = randomUUID();
694-
695-
mockedRoutingConfigClient.create.mockImplementationOnce(
696-
async (input) => ({
697-
data: {
698-
...input,
699-
id,
700-
clientId: 'client1',
701-
createdAt: now.toISOString(),
702-
status: 'DRAFT',
703-
updatedAt: now.toISOString(),
704-
},
705-
})
706-
);
707-
708-
const result = await createRoutingConfig({
709-
name: 'My Routing Config',
710-
campaignId: 'my-campaign-id',
711-
cascade: [
712-
{
713-
channelType: 'primary',
714-
channel: 'NHSAPP',
715-
cascadeGroups: ['standard'],
716-
defaultTemplateId: null,
717-
},
718-
],
719-
cascadeGroupOverrides: [{ name: 'standard' }],
720-
});
721-
722-
expect(mockedRoutingConfigClient.create).toHaveBeenCalledWith(
723-
{
724-
name: 'My Routing Config',
725-
campaignId: 'my-campaign-id',
726-
cascade: [
727-
{
728-
channelType: 'primary',
729-
channel: 'NHSAPP',
730-
cascadeGroups: ['standard'],
731-
defaultTemplateId: null,
732-
},
733-
],
734-
cascadeGroupOverrides: [{ name: 'standard' }],
735-
},
736-
'token'
737-
);
738-
739-
expect(result).toEqual({
740-
id,
741-
clientId: 'client1',
742-
createdAt: now.toISOString(),
743-
status: 'DRAFT',
744-
updatedAt: now.toISOString(),
745-
name: 'My Routing Config',
746-
campaignId: 'my-campaign-id',
747-
cascade: [
748-
{
749-
channelType: 'primary',
750-
channel: 'NHSAPP',
751-
cascadeGroups: ['standard'],
752-
defaultTemplateId: null,
753-
},
754-
],
755-
cascadeGroupOverrides: [{ name: 'standard' }],
756-
});
757-
});
758-
759-
test('errors when no token', async () => {
760-
authIdTokenServerMock.mockResolvedValueOnce({});
761-
762-
await expect(
763-
createRoutingConfig({
764-
name: 'My Routing Config',
765-
campaignId: 'my-campaign-id',
766-
cascade: [
767-
{
768-
channelType: 'primary',
769-
channel: 'NHSAPP',
770-
cascadeGroups: ['standard'],
771-
defaultTemplateId: null,
772-
},
773-
],
774-
cascadeGroupOverrides: [{ name: 'standard' }],
775-
})
776-
).rejects.toThrow('Failed to get access token');
777-
778-
expect(mockedRoutingConfigClient.create).not.toHaveBeenCalled();
779-
});
780-
781-
test('errors when request fails', async () => {
782-
mockedRoutingConfigClient.create.mockResolvedValueOnce({
783-
error: {
784-
errorMeta: {
785-
code: 400,
786-
description: 'Bad request',
787-
},
788-
},
789-
});
790-
791-
await expect(
792-
createRoutingConfig({
793-
name: 'My Routing Config',
794-
campaignId: 'my-campaign-id',
795-
cascade: [
796-
{
797-
channelType: 'primary',
798-
channel: 'NHSAPP',
799-
cascadeGroups: ['standard'],
800-
defaultTemplateId: null,
801-
},
802-
],
803-
cascadeGroupOverrides: [{ name: 'standard' }],
804-
})
805-
).rejects.toThrow('Failed to create message plan');
806-
});
807-
});
808-
809-
describe('submitRoutingConfig', () => {
810-
it('submits the routing config', async () => {
811-
mockedRoutingConfigClient.submit.mockResolvedValueOnce({
812-
data: mock<RoutingConfig>(),
813-
});
814-
815-
await submitRoutingConfig('id');
816-
817-
expect(mockedRoutingConfigClient.submit).toHaveBeenCalledWith(
818-
'id',
819-
'token'
820-
);
821-
});
822-
823-
it('throws if there is no token', async () => {
824-
authIdTokenServerMock.mockResolvedValueOnce({});
825-
826-
await expect(() => submitRoutingConfig('id')).rejects.toThrow(
827-
'Failed to get access token'
828-
);
829-
830-
expect(mockedRoutingConfigClient.submit).not.toHaveBeenCalled();
831-
});
832-
833-
it('throws if the api request returns an error', async () => {
834-
mockedRoutingConfigClient.submit.mockResolvedValueOnce({
835-
error: {
836-
errorMeta: {
837-
code: 400,
838-
description: 'Bad request',
839-
},
840-
},
841-
});
842-
843-
await expect(() => submitRoutingConfig('id')).rejects.toThrow(
844-
'Failed to submit message plan'
845-
);
846-
});
847-
});
848683
});

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { randomUUID } from 'node:crypto';
2+
import { mock } from 'jest-mock-extended';
13
import {
24
getRoutingConfig,
35
updateRoutingConfig,
@@ -6,6 +8,7 @@ import {
68
getRoutingConfigs,
79
countRoutingConfigs,
810
createRoutingConfig,
11+
submitRoutingConfig,
912
} from '@utils/message-plans';
1013
import { getSessionServer } from '@utils/amplify-utils';
1114
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
@@ -24,7 +27,6 @@ import {
2427
NHS_APP_TEMPLATE,
2528
SMS_TEMPLATE,
2629
} from '@testhelpers/helpers';
27-
import { randomUUID } from 'node:crypto';
2830

2931
jest.mock('@utils/amplify-utils');
3032
jest.mock('nhs-notify-backend-client/src/routing-config-api-client');
@@ -675,4 +677,44 @@ describe('Message plans actions', () => {
675677
).rejects.toThrow('Failed to create message plan');
676678
});
677679
});
680+
681+
describe('submitRoutingConfig', () => {
682+
it('submits the routing config', async () => {
683+
routingConfigApiMock.submit.mockResolvedValueOnce({
684+
data: mock<RoutingConfig>(),
685+
});
686+
687+
await submitRoutingConfig('id');
688+
689+
expect(routingConfigApiMock.submit).toHaveBeenCalledWith(
690+
'id',
691+
'mock-token'
692+
);
693+
});
694+
695+
it('throws if there is no token', async () => {
696+
getSessionServerMock.mockResolvedValueOnce({});
697+
698+
await expect(() => submitRoutingConfig('id')).rejects.toThrow(
699+
'Failed to get access token'
700+
);
701+
702+
expect(routingConfigApiMock.submit).not.toHaveBeenCalled();
703+
});
704+
705+
it('throws if the api request returns an error', async () => {
706+
routingConfigApiMock.submit.mockResolvedValueOnce({
707+
error: {
708+
errorMeta: {
709+
code: 400,
710+
description: 'Bad request',
711+
},
712+
},
713+
});
714+
715+
await expect(() => submitRoutingConfig('id')).rejects.toThrow(
716+
'Failed to submit message plan'
717+
);
718+
});
719+
});
678720
});

frontend/src/app/message-plans/create-message-plan/server-action.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type FormState,
1313
type MessageOrder,
1414
} from 'nhs-notify-web-template-management-utils';
15-
import { createRoutingConfig } from '@utils/form-actions';
15+
import { createRoutingConfig } from '@utils/message-plans';
1616

1717
const $MessagePlanFormData = z.object({
1818
campaignId: z.string().min(1, { error: 'Select a campaign' }),

frontend/src/utils/form-actions.ts

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import { getSessionServer } from '@utils/amplify-utils';
44
import {
55
CreateUpdateTemplate,
66
isTemplateDtoValid,
7-
RoutingConfig,
87
TemplateDto,
98
ValidatedTemplateDto,
109
} from 'nhs-notify-backend-client';
1110
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1211
import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client';
13-
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
1412
import { sortAscByUpdatedAt } from './sort';
1513

1614
export async function createTemplate(
@@ -190,43 +188,3 @@ export async function getTemplates(): Promise<TemplateDto[]> {
190188

191189
return sortAscByUpdatedAt(sortedData);
192190
}
193-
194-
export async function createRoutingConfig(
195-
routingConfig: Pick<
196-
RoutingConfig,
197-
'name' | 'campaignId' | 'cascade' | 'cascadeGroupOverrides'
198-
>
199-
): Promise<RoutingConfig> {
200-
const { accessToken } = await getSessionServer();
201-
202-
if (!accessToken) {
203-
throw new Error('Failed to get access token');
204-
}
205-
206-
const { data, error } = await routingConfigurationApiClient.create(
207-
routingConfig,
208-
accessToken
209-
);
210-
211-
if (error) {
212-
logger.error('Failed to create message plan', error);
213-
throw new Error('Failed to create message plan');
214-
}
215-
216-
return data;
217-
}
218-
219-
export async function submitRoutingConfig(id: string) {
220-
const { accessToken } = await getSessionServer();
221-
222-
if (!accessToken) {
223-
throw new Error('Failed to get access token');
224-
}
225-
226-
const { error } = await routingConfigurationApiClient.submit(id, accessToken);
227-
228-
if (error) {
229-
logger.error('Failed to submit message plan', error);
230-
throw new Error('Failed to submit message plan');
231-
}
232-
}

frontend/src/utils/message-plans.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,18 @@ export async function getTemplatesByIds(
188188

189189
return templates;
190190
}
191+
192+
export async function submitRoutingConfig(id: string) {
193+
const { accessToken } = await getSessionServer();
194+
195+
if (!accessToken) {
196+
throw new Error('Failed to get access token');
197+
}
198+
199+
const { error } = await routingConfigurationApiClient.submit(id, accessToken);
200+
201+
if (error) {
202+
logger.error('Failed to submit message plan', error);
203+
throw new Error('Failed to submit message plan');
204+
}
205+
}

0 commit comments

Comments
 (0)