Skip to content

Commit ca7d677

Browse files
committed
CCM-11474: review feedback
1 parent ad7201a commit ca7d677

File tree

10 files changed

+146
-127
lines changed

10 files changed

+146
-127
lines changed

frontend/src/__tests__/components/forms/MessagePlan/MessagePlan.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import React from 'react';
21
import { render, screen } from '@testing-library/react';
32
import userEvent from '@testing-library/user-event';
43
import { mock } from 'jest-mock-extended';

frontend/src/__tests__/components/forms/MessagePlan/__snapshots__/MessagePlan.test.tsx.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ exports[`renders error if form is submitted with empty name 1`] = `
199199
</button>
200200
<a
201201
class="nhsuk-u-font-size-19 nhsuk-u-margin-left-3 nhsuk-u-padding-top-3 inline-block"
202-
href="/templates/message-plans/choose-message-order"
202+
href="/message-plans/choose-message-order"
203203
>
204204
Go back
205205
</a>
@@ -347,7 +347,7 @@ exports[`renders error if form is submitted with name too long 1`] = `
347347
</button>
348348
<a
349349
class="nhsuk-u-font-size-19 nhsuk-u-margin-left-3 nhsuk-u-padding-top-3 inline-block"
350-
href="/templates/message-plans/choose-message-order"
350+
href="/message-plans/choose-message-order"
351351
>
352352
Go back
353353
</a>
@@ -510,7 +510,7 @@ exports[`renders error if form is submitted with no campaign id selected 1`] = `
510510
</button>
511511
<a
512512
class="nhsuk-u-font-size-19 nhsuk-u-margin-left-3 nhsuk-u-padding-top-3 inline-block"
513-
href="/templates/message-plans/choose-message-order"
513+
href="/message-plans/choose-message-order"
514514
>
515515
Go back
516516
</a>
@@ -662,7 +662,7 @@ exports[`renders form with select for multiple campaign ids 1`] = `
662662
</button>
663663
<a
664664
class="nhsuk-u-font-size-19 nhsuk-u-margin-left-3 nhsuk-u-padding-top-3 inline-block"
665-
href="/templates/message-plans/choose-message-order"
665+
href="/message-plans/choose-message-order"
666666
>
667667
Go back
668668
</a>
@@ -798,7 +798,7 @@ exports[`renders form with single campaign id displayed 1`] = `
798798
</button>
799799
<a
800800
class="nhsuk-u-font-size-19 nhsuk-u-margin-left-3 nhsuk-u-padding-top-3 inline-block"
801-
href="/templates/message-plans/choose-message-order"
801+
href="/message-plans/choose-message-order"
802802
>
803803
Go back
804804
</a>

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ import {
2020
import { getSessionServer } from '@utils/amplify-utils';
2121
import { TemplateDto } from 'nhs-notify-backend-client';
2222
import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client';
23+
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
2324
import { randomUUID } from 'node:crypto';
2425

2526
const mockedTemplateClient = jest.mocked(templateApiClient);
27+
const mockedRoutingConfigClient = jest.mocked(routingConfigurationApiClient);
2628
const authIdTokenServerMock = jest.mocked(getSessionServer);
2729

2830
jest.mock('@utils/amplify-utils');
2931
jest.mock('nhs-notify-backend-client/src/template-api-client');
32+
jest.mock('nhs-notify-backend-client/src/routing-config-api-client');
3033

3134
describe('form-actions', () => {
3235
beforeEach(() => {
@@ -701,7 +704,7 @@ describe('form-actions', () => {
701704
const now = new Date();
702705
const id = randomUUID();
703706

704-
mockedTemplateClient.createRoutingConfig.mockImplementationOnce(
707+
mockedRoutingConfigClient.create.mockImplementationOnce(
705708
async (input) => ({
706709
data: {
707710
...input,
@@ -728,7 +731,7 @@ describe('form-actions', () => {
728731
cascadeGroupOverrides: [{ name: 'standard' }],
729732
});
730733

731-
expect(mockedTemplateClient.createRoutingConfig).toHaveBeenCalledWith(
734+
expect(mockedRoutingConfigClient.create).toHaveBeenCalledWith(
732735
{
733736
name: 'My Routing Config',
734737
campaignId: 'my-campaign-id',
@@ -788,11 +791,11 @@ describe('form-actions', () => {
788791
})
789792
).rejects.toThrow('Failed to get access token');
790793

791-
expect(mockedTemplateClient.createRoutingConfig).not.toHaveBeenCalled();
794+
expect(mockedRoutingConfigClient.create).not.toHaveBeenCalled();
792795
});
793796

794797
test('errors when request fails', async () => {
795-
mockedTemplateClient.createRoutingConfig.mockResolvedValueOnce({
798+
mockedRoutingConfigClient.create.mockResolvedValueOnce({
796799
error: {
797800
errorMeta: {
798801
code: 400,

frontend/src/components/forms/MessagePlan/MessagePlan.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import { MessageOrder } from 'nhs-notify-web-template-management-utils';
1313
import { NHSNotifyButton } from '@atoms/NHSNotifyButton/NHSNotifyButton';
1414
import { useTextInput } from '@hooks/use-text-input.hook';
1515
import { NHSNotifyFormWrapper } from '@molecules/NHSNotifyFormWrapper/NHSNotifyFormWrapper';
16-
import { getBasePath } from '@utils/get-base-path';
1716
import { messagePlanServerAction } from './server-action';
1817
import content from '@content/content';
18+
import Link from 'next/link';
1919

2020
const formContent = content.components.messagePlanForm;
2121

@@ -109,17 +109,17 @@ export function MessagePlanForm({
109109
<NHSNotifyButton data-testid='submit-button'>
110110
{formContent.submitButton}
111111
</NHSNotifyButton>
112-
<a
113-
href={`${getBasePath()}/message-plans/choose-message-order`}
112+
<Link
113+
href={formContent.backLink.href}
114114
className={classNames(
115115
'nhsuk-u-font-size-19',
116116
'nhsuk-u-margin-left-3',
117117
'nhsuk-u-padding-top-3',
118118
'inline-block'
119119
)}
120120
>
121-
{formContent.backLink}
122-
</a>
121+
{formContent.backLink.text}
122+
</Link>
123123
</div>
124124
</NHSNotifyFormWrapper>
125125
);

frontend/src/content/content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ const messagePlanForm = {
10791079
},
10801080
},
10811081
submitButton: 'Save and continue',
1082-
backLink: 'Go back',
1082+
backLink: { href: '/message-plans/choose-message-order', text: 'Go back' },
10831083
};
10841084

10851085
const content = {

frontend/src/utils/form-actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'nhs-notify-backend-client';
1111
import { logger } from 'nhs-notify-web-template-management-utils/logger';
1212
import { templateApiClient } from 'nhs-notify-backend-client/src/template-api-client';
13+
import { routingConfigurationApiClient } from 'nhs-notify-backend-client/src/routing-config-api-client';
1314

1415
export async function createTemplate(
1516
template: CreateUpdateTemplate
@@ -209,7 +210,7 @@ export async function createRoutingConfig(
209210
throw new Error('Failed to get access token');
210211
}
211212

212-
const { data, error } = await templateApiClient.createRoutingConfig(
213+
const { data, error } = await routingConfigurationApiClient.create(
213214
routingConfig,
214215
accessToken
215216
);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {
2+
routingConfigurationApiClient as client,
3+
httpClient,
4+
} from '../routing-config-api-client';
5+
import MockAdapter from 'axios-mock-adapter';
6+
import { RoutingConfig } from '../types/generated';
7+
8+
describe('RoutingConfigurationApiClient', () => {
9+
const axiosMock = new MockAdapter(httpClient);
10+
11+
beforeEach(() => {
12+
axiosMock.reset();
13+
});
14+
15+
describe('create', () => {
16+
test('should return error', async () => {
17+
axiosMock.onPost('/v1/routing-configuration').reply(400, {
18+
statusCode: 400,
19+
technicalMessage: 'Bad request',
20+
details: {
21+
message: 'Something went wrong',
22+
},
23+
});
24+
25+
const result = await client.create(
26+
{
27+
name: 'test',
28+
campaignId: 'campaign-id',
29+
cascade: [],
30+
cascadeGroupOverrides: [],
31+
},
32+
'test-token'
33+
);
34+
35+
expect(result.error).toEqual({
36+
errorMeta: {
37+
code: 400,
38+
description: 'Bad request',
39+
details: {
40+
message: 'Something went wrong',
41+
},
42+
},
43+
});
44+
45+
expect(result.data).toBeUndefined();
46+
47+
expect(axiosMock.history.post.length).toBe(1);
48+
});
49+
50+
test('should return routing config', async () => {
51+
const data: RoutingConfig = {
52+
campaignId: 'campaign-id',
53+
cascade: [],
54+
cascadeGroupOverrides: [],
55+
clientId: 'client-id',
56+
createdAt: new Date().toISOString(),
57+
id: 'id',
58+
name: 'name',
59+
status: 'DRAFT',
60+
updatedAt: new Date().toISOString(),
61+
};
62+
63+
axiosMock.onPost('/v1/routing-configuration').reply(201, {
64+
statusCode: 201,
65+
data,
66+
});
67+
68+
const body = {
69+
campaignId: data.campaignId,
70+
cascade: data.cascade,
71+
cascadeGroupOverrides: data.cascadeGroupOverrides,
72+
name: data.name,
73+
};
74+
75+
const result = await client.create(body, 'test-token');
76+
77+
expect(axiosMock.history.post.length).toBe(1);
78+
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual(body);
79+
expect(axiosMock.history.post[0].headers?.Authorization).toBe(
80+
'test-token'
81+
);
82+
83+
expect(result.data).toEqual(data);
84+
85+
expect(result.error).toBeUndefined();
86+
});
87+
});
88+
});

lambdas/backend-client/src/__tests__/template-api-client.test.ts

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import axios from 'axios';
22
import MockAdapter from 'axios-mock-adapter';
33
import { TemplateApiClient } from '../template-api-client';
4-
import { RoutingConfig } from '../types/generated';
54

65
const axiosMock = new MockAdapter(axios);
76

@@ -486,80 +485,4 @@ describe('TemplateAPIClient', () => {
486485
expect(result.error).toBeUndefined();
487486
});
488487
});
489-
490-
describe('createRoutingConfig', () => {
491-
test('should return error', async () => {
492-
axiosMock.onPost('/v1/routing-configuration').reply(400, {
493-
statusCode: 400,
494-
technicalMessage: 'Bad request',
495-
details: {
496-
message: 'Something went wrong',
497-
},
498-
});
499-
500-
const client = new TemplateApiClient();
501-
502-
const result = await client.createRoutingConfig(
503-
{
504-
name: 'test',
505-
campaignId: 'campaign-id',
506-
cascade: [],
507-
cascadeGroupOverrides: [],
508-
},
509-
testToken
510-
);
511-
512-
expect(result.error).toEqual({
513-
errorMeta: {
514-
code: 400,
515-
description: 'Bad request',
516-
details: {
517-
message: 'Something went wrong',
518-
},
519-
},
520-
});
521-
522-
expect(result.data).toBeUndefined();
523-
524-
expect(axiosMock.history.post.length).toBe(1);
525-
});
526-
527-
test('should return routing config', async () => {
528-
const data: RoutingConfig = {
529-
campaignId: 'campaign-id',
530-
cascade: [],
531-
cascadeGroupOverrides: [],
532-
clientId: 'client-id',
533-
createdAt: new Date().toISOString(),
534-
id: 'id',
535-
name: 'name',
536-
status: 'DRAFT',
537-
updatedAt: new Date().toISOString(),
538-
};
539-
540-
axiosMock.onPost('/v1/routing-configuration').reply(201, {
541-
statusCode: 201,
542-
data,
543-
});
544-
545-
const client = new TemplateApiClient();
546-
547-
const body = {
548-
campaignId: data.campaignId,
549-
cascade: data.cascade,
550-
cascadeGroupOverrides: data.cascadeGroupOverrides,
551-
name: data.name,
552-
};
553-
554-
const result = await client.createRoutingConfig(body, testToken);
555-
556-
expect(axiosMock.history.post.length).toBe(1);
557-
expect(JSON.parse(axiosMock.history.post[0].data)).toEqual(body);
558-
expect(axiosMock.history.post[0].headers?.Authorization).toBe(testToken);
559-
560-
expect(result.data).toEqual(data);
561-
562-
expect(result.error).toBeUndefined();
563-
});
564-
});
565488
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { RoutingConfig, RoutingConfigSuccess } from './types/generated';
2+
import { catchAxiosError, createAxiosClient } from './axios-client';
3+
import { Result } from './types/result';
4+
5+
export const httpClient = createAxiosClient();
6+
7+
export const routingConfigurationApiClient = {
8+
async create(
9+
routingConfig: Pick<
10+
RoutingConfig,
11+
'name' | 'campaignId' | 'cascade' | 'cascadeGroupOverrides'
12+
>,
13+
token: string
14+
): Promise<Result<RoutingConfig>> {
15+
const response = await catchAxiosError(
16+
httpClient.post<RoutingConfigSuccess>(
17+
'/v1/routing-configuration',
18+
routingConfig,
19+
{
20+
headers: {
21+
'Content-Type': 'application/json',
22+
Authorization: token,
23+
},
24+
}
25+
)
26+
);
27+
28+
if (response.error) {
29+
return {
30+
error: response.error,
31+
};
32+
}
33+
34+
return {
35+
data: response.data.data,
36+
};
37+
},
38+
};

0 commit comments

Comments
 (0)