Skip to content

Commit

Permalink
make service as Class and update getServerSideProp also fix util
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lor-cab committed Oct 12, 2023
1 parent 1000965 commit 7495ac8
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { GetServerSidePropsContext } from 'next';
import getConfig from 'next/config';
import {
GrantMandatoryQuestionDto,
getMandatoryQuestionById,
updateMandatoryQuestion,
GrantMandatoryQuestionService,
} from '../../../services/GrantMandatoryQuestionService';
import { Optional } from '../../../testUtils/unitTestHelpers';
import callServiceMethod from '../../../utils/callServiceMethod';
Expand All @@ -22,12 +21,14 @@ export default async function getServerSideProps({
const fromSummary = (query?.fromSummary as string) || null;
const jwt = getJwtFromCookies(req);
let mandatoryQuestion;

const grantMandatoryQuestionService =
GrantMandatoryQuestionService.getInstance();
try {
mandatoryQuestion = await getMandatoryQuestionById(
jwt,
mandatoryQuestionId
);
mandatoryQuestion =
await grantMandatoryQuestionService.getMandatoryQuestionById(
jwt,
mandatoryQuestionId
);
} catch (e) {
const serviceErrorProps = {
errorInformation:
Expand Down Expand Up @@ -61,7 +62,12 @@ export default async function getServerSideProps({
const response = await callServiceMethod(
req,
res,
(body) => updateMandatoryQuestion(jwt, mandatoryQuestionId, body),
(body) =>
grantMandatoryQuestionService.updateMandatoryQuestion(
jwt,
mandatoryQuestionId,
body
),
routes.mandatoryQuestions.addressPage(mandatoryQuestionId),
{
errorInformation:
Expand Down
63 changes: 39 additions & 24 deletions packages/applicant/src/services/GrantMandatoryQuestionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,49 @@ import axios from 'axios';
import getConfig from 'next/config';
import { axiosConfig } from '../utils/jwt';

const { serverRuntimeConfig } = getConfig();
const BACKEND_HOST = serverRuntimeConfig.backendHost;
export class GrantMandatoryQuestionService {
private static instance: GrantMandatoryQuestionService;

export async function getMandatoryQuestionById(
jwt: string,
mandatoryQuestionId: string
): Promise<GrantMandatoryQuestionDto> {
const { data } = await axios.get<GrantMandatoryQuestionDto>(
`${BACKEND_HOST}/grant-mandatory-questions/${mandatoryQuestionId}`,
axiosConfig(jwt)
);
return data;
}
private BACKEND_HOST: string;

export async function updateMandatoryQuestion(
jwt: string,
mandatoryQuestionId: string,
body: GrantMandatoryQuestionDto
): Promise<string> {
const { data } = await axios.patch<string>(
`${BACKEND_HOST}/grant-mandatory-questions/${mandatoryQuestionId}`,
body,
axiosConfig(jwt)
);
private constructor() {
const { serverRuntimeConfig } = getConfig();
this.BACKEND_HOST = serverRuntimeConfig.backendHost;
}

return data;
}
public static getInstance(): GrantMandatoryQuestionService {
if (!GrantMandatoryQuestionService.instance) {
GrantMandatoryQuestionService.instance =
new GrantMandatoryQuestionService();
}
return GrantMandatoryQuestionService.instance;
}

public async getMandatoryQuestionById(
jwt: string,
mandatoryQuestionId: string
): Promise<GrantMandatoryQuestionDto> {
const { data } = await axios.get<GrantMandatoryQuestionDto>(
`${this.BACKEND_HOST}/grant-mandatory-questions/${mandatoryQuestionId}`,
axiosConfig(jwt)
);
return data;
}

public async updateMandatoryQuestion(
jwt: string,
mandatoryQuestionId: string,
body: GrantMandatoryQuestionDto
): Promise<string> {
const { data } = await axios.patch<string>(
`${this.BACKEND_HOST}/grant-mandatory-questions/${mandatoryQuestionId}`,
body,
axiosConfig(jwt)
);

return data;
}
}
export interface GrantMandatoryQuestionDto {
name?: string;
addressLine1?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
it('should generate the correct URL for the address page when name is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
name: 'Example Organization',
addressLine1: null,
city: null,
postcode: null,
orgType: null,
companiesHouseNumber: null,
charityCommissionNumber: null,
fundingAmount: null,
fundingLocation: null,
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-address`
);
});
Expand All @@ -23,93 +32,148 @@ describe('generateRedirectForMandatoryQuestionNextPage', () => {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
orgType: null,
companiesHouseNumber: null,
charityCommissionNumber: null,
fundingAmount: null,
fundingLocation: null,
};

const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-type`
);
});

it('should generate the correct URL for the companiesHouseNumberPage when orgType is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
companiesHouseNumber: null,
charityCommissionNumber: null,
fundingAmount: null,
fundingLocation: null,
orgType: 'Example Type',
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-companies-house-number`
);
});

it('should generate the correct URL for the charityCommissionNumberPage when companiesHouseNumber is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
orgType: 'Example Type',
charityCommissionNumber: null,
fundingAmount: null,
fundingLocation: null,
companiesHouseNumber: 'ABC123',
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/organisation-charity-commission-number`
);
});

it('should generate the correct URL for the fundingAmountPage when charityCommissionNumber is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
orgType: 'Example Type',
fundingAmount: null,
fundingLocation: null,
companiesHouseNumber: 'ABC123',
charityCommissionNumber: 'XYZ456',
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/funding-amount`
);
});

it('should generate the correct URL for the fundingLocationPage when fundingAmount is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
orgType: 'Example Type',
fundingAmount: '1000',
fundingLocation: null,
companiesHouseNumber: 'ABC123',
charityCommissionNumber: 'XYZ456',
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/funding-location`
);
});

it('should generate an empty destination when none of the fields are provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {};
it('should generate the correct URL when everything is provided', () => {
const mandatoryQuestion: GrantMandatoryQuestionDto = {
addressLine1: '123 Main St',
city: 'Example City',
postcode: '12345',
name: 'name',
orgType: 'Example Type',
fundingAmount: '1000',
fundingLocation: 'location',
companiesHouseNumber: 'ABC123',
charityCommissionNumber: 'XYZ456',
};
const mandatoryQuestionId = 'mandatoryQuestionId';

const result = generateRedirectForMandatoryQuestionNextPage(
mandatoryQuestion,
mandatoryQuestionId
mandatoryQuestionId,
false
);

expect(result.destination).toBe(
expect(result.redirect.destination).toBe(
`/mandatory-questions/mandatoryQuestionId/summary`
);
});
Expand Down
Loading

0 comments on commit 7495ac8

Please sign in to comment.