Skip to content

Commit

Permalink
EMP-434 tidy up code
Browse files Browse the repository at this point in the history
  • Loading branch information
muyinatech committed Aug 14, 2024
1 parent 0e26562 commit a7df9dc
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 59 deletions.
22 changes: 11 additions & 11 deletions server/controllers/generateReportController.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import { createMock, DeepMocked } from '@golevelup/ts-jest'
import type { NextFunction, Request, Response } from 'express'
import { CrmReportResponse } from '@crmReport'
import CrmReportApiService from '../services/crmReportApiService'
import GenerateReportService from '../services/generateReportService'
import GenerateReportController from './generateReportController'

jest.mock('../services/crmReportApiService')
jest.mock('../services/generateReportService')
jest.mock('../utils/userProfileGroups', () => {
return jest.fn().mockReturnValue('1,4,5,6')
})

describe('generateReportController', () => {
let mockCrmReportApiService: jest.Mocked<CrmReportApiService>
let mockGenerateReportService: jest.Mocked<GenerateReportService>
let request: DeepMocked<Request>
let response: DeepMocked<Response>
const next: DeepMocked<NextFunction> = createMock<NextFunction>({})

beforeEach(() => {
request = createMock<Request>({})
response = createMock<Response>({})
mockCrmReportApiService = new CrmReportApiService(null) as jest.Mocked<CrmReportApiService>
mockGenerateReportService = new GenerateReportService(null) as jest.Mocked<GenerateReportService>
})

it('should render generate report page', async () => {
const generateReportController = new GenerateReportController(mockCrmReportApiService)
const generateReportController = new GenerateReportController(mockGenerateReportService)
const requestHandler = generateReportController.show()

await requestHandler(request, response, next)
Expand All @@ -33,9 +33,9 @@ describe('generateReportController', () => {
it('should download the requested CRM report', async () => {
const crmReportResponse = getCrmReportResponse()

mockCrmReportApiService.getCrmReport.mockResolvedValue(crmReportResponse)
mockGenerateReportService.getCrmReport.mockResolvedValue(crmReportResponse)

const generateReportController = new GenerateReportController(mockCrmReportApiService)
const generateReportController = new GenerateReportController(mockGenerateReportService)
const requestHandler = generateReportController.submit()
request.body = {
crmType: 'crm4',
Expand All @@ -48,11 +48,11 @@ describe('generateReportController', () => {
expect(response.setHeader).toHaveBeenCalledWith('Content-Disposition', 'attachment; filename=crm4Report.csv')
expect(response.send).toHaveBeenCalledWith(crmReportResponse.text)

expect(mockCrmReportApiService.getCrmReport).toHaveBeenCalledWith('2023-03-01', '2023-03-30', '1,4,5,6')
expect(mockGenerateReportService.getCrmReport).toHaveBeenCalledWith('2023-03-01', '2023-03-30', '1,4,5,6')
})

it('should render generate report page with field errors', async () => {
const generateReportController = new GenerateReportController(mockCrmReportApiService)
const generateReportController = new GenerateReportController(mockGenerateReportService)
const requestHandler = generateReportController.submit()
request.body = {
crmType: '',
Expand Down Expand Up @@ -100,9 +100,9 @@ describe('generateReportController', () => {
},
}

mockCrmReportApiService.getCrmReport.mockResolvedValue(crmReportResponse)
mockGenerateReportService.getCrmReport.mockResolvedValue(crmReportResponse)

const generateReportController = new GenerateReportController(mockCrmReportApiService)
const generateReportController = new GenerateReportController(mockGenerateReportService)
const requestHandler = generateReportController.submit()
request.body = {
crmType: 'crm4',
Expand Down
6 changes: 3 additions & 3 deletions server/controllers/generateReportController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Request, RequestHandler, Response } from 'express'
import getProfileAcceptedTypes from '../utils/userProfileGroups'
import CrmReportApiService from '../services/crmReportApiService'
import GenerateReportService from '../services/generateReportService'
import validateReportParams from '../utils/generateReportValidation'
import manageBackLink from '../utils/crmBackLink'
import { buildErrors } from '../utils/errorDisplayHelper'
Expand All @@ -9,7 +9,7 @@ const CURRENT_URL = '/generate-report'
const VIEW_PATH = 'pages/generateReport'

export default class GenerateReportController {
constructor(private readonly crmReportApiService: CrmReportApiService) {}
constructor(private readonly generateReportService: GenerateReportService) {}

show(): RequestHandler {
return async (req: Request, res: Response): Promise<void> => {
Expand All @@ -34,7 +34,7 @@ export default class GenerateReportController {
backUrl: manageBackLink(req, CURRENT_URL),
})
} else {
const reportResponse = await this.crmReportApiService.getCrmReport(
const reportResponse = await this.generateReportService.getCrmReport(
req.body.startDate,
req.body.endDate,
getProfileAcceptedTypes(res),
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import GenerateReportController from './generateReportController'
export const controllers = (services: Services) => {
const downloadEvidenceController = new DownloadEvidenceController(services.downloadEvidenceService)
const searchEformController = new SearchEformController(services.searchEformService)
const generateReportController = new GenerateReportController(services.crmReportApiService)
const generateReportController = new GenerateReportController(services.generateReportService)
return { downloadEvidenceController, generateReportController, searchEformController, ...crmControllers(services) }
}

Expand Down
32 changes: 0 additions & 32 deletions server/services/crmReportApiService.test.ts

This file was deleted.

37 changes: 37 additions & 0 deletions server/services/generateReportService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CrmReportResponse } from '@crmReport'
import CrmReportApiClient from '../data/api/crmReportApiClient'
import GenerateReportService from './generateReportService'

jest.mock('../data/api/crmReportApiClient')

describe('Generate Report Service', () => {
let mockCrmReportApiClient: jest.Mocked<CrmReportApiClient>

beforeEach(() => {
mockCrmReportApiClient = new CrmReportApiClient(null, null) as jest.Mocked<CrmReportApiClient>
})

it('should return crm report', async () => {
const expectedResponse = successResponse()

mockCrmReportApiClient.getCrmReport.mockResolvedValue(expectedResponse)

const generateReportService = new GenerateReportService(mockCrmReportApiClient)

const result = await generateReportService.getCrmReport('01/03/2022', '30/03/2024', '1,4,5,6')

expect(result).toEqual(expectedResponse)
expect(mockCrmReportApiClient.getCrmReport).toHaveBeenCalledWith('01/03/2022', '30/03/2024', '1,4,5,6')
})
})

const successResponse = (): CrmReportResponse => {
return {
text:
'Client UFN,Usn,Provider Account,Firm Name,Client Name,Rep Order Number,Maat ID,Prison Law,Date Received,' +
'Decision Date,Decision,Expenditure Type,Expert Name,Quantity,Rate,Unit,Total Cost,Additional Expenditure,' +
'Total Authority,Total Granted,Granting Caseworker\n' +
'031022/777,123456789,1234AB,Some Firm,Some Client,999999999,,No,2023-03-16,2023-03-16,Grant,a Psychiatrist,' +
'tyjtjtjt,4.0,50.0,Hour(s),200.0,0.0,200.0,200.0,Sym-G`',
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CrmReportResponse } from '@crmReport'
import CrmReportApiClient from '../data/api/crmReportApiClient'
import logger from '../../logger'

export default class CrmReportApiService {
export default class GenerateReportService {
constructor(private readonly crmReportApiClient: CrmReportApiClient) {}

async getCrmReport(startDate: string, endDate: string, profileAcceptedTypes: string): Promise<CrmReportResponse> {
Expand Down
6 changes: 3 additions & 3 deletions server/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SearchEformService from './searchEformService'
import CrmApiService from './crmApiService'
import CrmDisplayService from './crmDisplayService'
import DownloadEvidenceService from './downloadEvidenceService'
import CrmReportApiService from './crmReportApiService'
import GenerateReportService from './generateReportService'

export const services = () => {
const {
Expand All @@ -23,7 +23,7 @@ export const services = () => {
const crm14Service = new CrmApiService(crm14ApiClient)
const crmDisplayService = new CrmDisplayService()
const downloadEvidenceService = new DownloadEvidenceService(sdsApiClient)
const crmReportApiService = new CrmReportApiService(crmReportApiClient)
const generateReportService = new GenerateReportService(crmReportApiClient)
const searchEformService = new SearchEformService(searchApiClient)

return {
Expand All @@ -33,7 +33,7 @@ export const services = () => {
crm7Service,
crm14Service,
crmDisplayService,
crmReportApiService,
generateReportService,
downloadEvidenceService,
searchEformService,
}
Expand Down
43 changes: 35 additions & 8 deletions server/utils/searchEformValidation.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import validateSearchParams from './searchEformValidation'
import validateReportParams from './generateReportValidation'

describe('Search Eform Validation', () => {
it('should validate search parameters', () => {
const formData = {
const searchParams: Record<string, string> = {
usn: '123456789',
supplierAccountNumber: '1234AB',
clientName: 'John Doe',
Expand All @@ -11,13 +12,13 @@ describe('Search Eform Validation', () => {
endDate: '2023-11-02',
}

const result = validateSearchParams(formData)
const result = validateSearchParams(searchParams)

expect(result).toBeNull()
})

it('should return error for empty search parameters', () => {
const formData = {
const searchParams: Record<string, string> = {
usn: '',
supplierAccountNumber: '',
clientName: '',
Expand All @@ -26,7 +27,7 @@ describe('Search Eform Validation', () => {
endDate: '',
}

const result = validateSearchParams(formData)
const result = validateSearchParams(searchParams)

expect(result).toEqual({
list: [
Expand Down Expand Up @@ -77,12 +78,12 @@ describe('Search Eform Validation', () => {
['Invalid page specified', 'page', '0'],
['Invalid page specified', 'page', 'w'],
])('should return "%s" error for %s = %s', (errorMessage: string, fieldName: string, fieldValue: string) => {
const formData = {
const searchParams: Record<string, string> = {
[fieldName]: fieldValue,
usn: '123456789',
}

const result = validateSearchParams(formData)
const result = validateSearchParams(searchParams)

expect(result).toEqual({
list: [
Expand All @@ -100,7 +101,7 @@ describe('Search Eform Validation', () => {
})

it('should return multiple errors', () => {
const formData = {
const searchParams: Record<string, string> = {
usn: 'abcd',
supplierAccountNumber: '12A',
clientName: 'J',
Expand All @@ -109,7 +110,7 @@ describe('Search Eform Validation', () => {
endDate: '2000-11-02',
}

const result = validateSearchParams(formData)
const result = validateSearchParams(searchParams)

expect(result).toEqual({
list: [
Expand All @@ -128,4 +129,30 @@ describe('Search Eform Validation', () => {
},
})
})

it('should return errors for missing Start Date', () => {
const searchParams: Record<string, string> = {
usn: '123456789',
supplierAccountNumber: '1234AB',
clientName: 'John Doe',
clientDOB: '1960-02-12',
startDate: '',
endDate: '2023-11-02',
}
const result = validateSearchParams(searchParams)

expect(result).toEqual({
list: [
{
href: '#endDate',
text: 'End date requires a valid Start date',
},
],
messages: {
endDate: {
text: 'End date requires a valid Start date',
},
},
})
})
})
1 change: 1 addition & 0 deletions server/utils/searchEformValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const schema = Joi.object({
endDate: Joi.date().iso().min(Joi.ref('startDate')).optional().allow('').messages({
'date.format': 'End date must be a valid date',
'date.min': 'Your End date cannot be earlier than your Start date',
'any.ref': 'End date requires a valid Start date',
}),
page: Joi.number()
.min(1)
Expand Down

0 comments on commit a7df9dc

Please sign in to comment.