diff --git a/package-lock.json b/package-lock.json index 983e6d5..503c238 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@aws-sdk/client-sqs": "^3.433.0", "@ministryofjustice/frontend": "^1.8.0", + "@ministryofjustice/hmpps-audit-client": "^1.0.1", "agentkeepalive": "^4.5.0", "applicationinsights": "^2.9.0", "body-parser": "^1.20.2", @@ -2089,6 +2090,18 @@ "jquery": "^3.6.0" } }, + "node_modules/@ministryofjustice/hmpps-audit-client": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@ministryofjustice/hmpps-audit-client/-/hmpps-audit-client-1.0.1.tgz", + "integrity": "sha512-LLPDbejKRvPP0kS8nbnNqw7kIvbKf1pAouTbF+NxkV8BNrhsn80sxeUuHmrFyEbMevnPOpCaeqjqfbao/EI5sA==", + "dependencies": { + "@aws-sdk/client-sqs": "^3.410.0", + "@types/bunyan-format": "^0.2.5", + "bunyan": "^1.8.15", + "bunyan-format": "^0.2.1", + "hmpps-audit-client": "^1.0.3" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -2883,7 +2896,6 @@ "version": "0.2.6", "resolved": "https://registry.npmjs.org/@types/bunyan-format/-/bunyan-format-0.2.6.tgz", "integrity": "sha512-p8UiEPFBWOaIgPlRs5czRHkQZhLjyk8IX9Cks1gMi5CoBwJ/QPOlo9zr0gSotEK7YGbU7j/sVwEIScAfODvoQw==", - "dev": true, "dependencies": { "@types/node": "*" } @@ -3058,8 +3070,7 @@ "node_modules/@types/node": { "version": "18.18.5", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.18.5.tgz", - "integrity": "sha512-4slmbtwV59ZxitY4ixUZdy1uRLf9eSIvBWPQxNjhHYWEtn0FryfKpyS2cvADYXTayWdKEIsJengncrVvkI4I6A==", - "dev": true + "integrity": "sha512-4slmbtwV59ZxitY4ixUZdy1uRLf9eSIvBWPQxNjhHYWEtn0FryfKpyS2cvADYXTayWdKEIsJengncrVvkI4I6A==" }, "node_modules/@types/nunjucks": { "version": "3.2.4", @@ -7165,6 +7176,19 @@ "node": ">=8" } }, + "node_modules/hmpps-audit-client": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/hmpps-audit-client/-/hmpps-audit-client-1.0.6.tgz", + "integrity": "sha512-PkDHh4vXtAdG0LnmJOUslFtT4yXoDk1t2tsv7keW7QnJkry152xD7PoSwjddno9KYw+/cB6sH85hRj6A2RMVvQ==", + "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", + "dependencies": { + "@aws-sdk/client-sqs": "^3.410.0", + "@types/bunyan-format": "^0.2.5", + "bunyan": "^1.8.15", + "bunyan-format": "^0.2.1", + "hmpps-audit-client": "^1.0.3" + } + }, "node_modules/html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", diff --git a/package.json b/package.json index 26e1d76..6b97cb6 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,10 @@ "npm": "^9" }, "jest": { + "transformIgnorePatterns": ["/node_modules/(?!@ministryofjustice/hmpps-audit-client)"], "transform": { + "^.+\\.(ts|tsx)?$": "ts-jest", + "^.+\\.(js|jsx)$": "babel-jest", "^.+\\.tsx?$": [ "ts-jest", { @@ -93,6 +96,7 @@ "dependencies": { "@aws-sdk/client-sqs": "^3.433.0", "@ministryofjustice/frontend": "^1.8.0", + "@ministryofjustice/hmpps-audit-client": "^1.0.1", "agentkeepalive": "^4.5.0", "applicationinsights": "^2.9.0", "body-parser": "^1.20.2", diff --git a/server/routes/index.test.ts b/server/routes/index.test.ts index 1910f65..6331475 100644 --- a/server/routes/index.test.ts +++ b/server/routes/index.test.ts @@ -1,21 +1,14 @@ import type { Express } from 'express' import request from 'supertest' +import { auditService } from '@ministryofjustice/hmpps-audit-client' import { appWithAllRoutes } from './testutils/appSetup' -import auditService from '../services/auditService' let app: Express - -const sendAuditMessageMock = jest.fn() - afterEach(() => { jest.resetAllMocks() }) describe('index.test.ts', () => { - jest.mock('../services/auditService', () => ({ - sendAuditMessage: sendAuditMessageMock, - })) - jest.mock('uuid', () => ({ v1: jest.fn(() => 'mocked-uuid'), })) diff --git a/server/routes/index.ts b/server/routes/index.ts index 23f4493..70ccc31 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -1,9 +1,9 @@ import { type RequestHandler, Router } from 'express' import { v1 as uuidv1 } from 'uuid' +import { auditService } from '@ministryofjustice/hmpps-audit-client' import asyncMiddleware from '../middleware/asyncMiddleware' import type { Services } from '../services' -import auditService from '../services/auditService' // eslint-disable-next-line @typescript-eslint/no-unused-vars export default function routes(service: Services): Router { diff --git a/server/services/auditService.test.ts b/server/services/auditService.test.ts deleted file mode 100644 index 7f0bc99..0000000 --- a/server/services/auditService.test.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { SQSClient } from '@aws-sdk/client-sqs' -import auditService from './auditService' - -const adminId = 'some admin' -const userId = 'some user' -const userIdSubjectType = 'USER_ID' - -describe('Audit service', () => { - beforeEach(() => { - jest.resetAllMocks() - }) - - it('sends audit message', async () => { - jest.spyOn(SQSClient.prototype, 'send').mockResolvedValue({} as never) - const expectedWhat = 'DISABLE_USER' - - jest.spyOn(SQSClient.prototype, 'send').mockResolvedValue({} as never) - await auditService.sendAuditMessage({ - action: 'DISABLE_USER', - who: adminId, - subjectId: userId, - subjectType: userIdSubjectType, - details: JSON.stringify({ something: 'some details' }), - }) - const { MessageBody, QueueUrl } = (SQSClient.prototype.send as jest.Mock).mock.calls[0][0].input - const { what, when, who, service, subjectId, subjectType, details } = JSON.parse(MessageBody) - - expect(QueueUrl).toEqual('http://localhost:4566/000000000000/mainQueue') - expect(what).toEqual(expectedWhat) - expect(when).toBeDefined() - expect(who).toEqual(adminId) - expect(subjectId).toEqual(subjectId) - expect(subjectType).toEqual(userIdSubjectType) - expect(service).toEqual('hmpps-audit-poc-ui') - expect(details).toEqual('{"something":"some details"}') - }) -}) diff --git a/server/services/auditService.ts b/server/services/auditService.ts deleted file mode 100644 index 2a5fb33..0000000 --- a/server/services/auditService.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs' -import logger from '../../logger' -import config from '../config' - -class AuditService { - private sqsClient: SQSClient - - constructor(private readonly queueUrl = config.apis.audit.queueUrl) { - this.sqsClient = new SQSClient({ - region: config.apis.audit.region, - }) - } - - async sendAuditMessage({ - action, - who, - subjectId, - subjectType, - correlationId, - details, - }: { - action: string - who: string - subjectId?: string - subjectType?: string - correlationId?: string - details?: string - }) { - try { - const message = JSON.stringify({ - what: action, - when: new Date(), - who, - subjectId, - subjectType, - correlationId, - service: config.apis.audit.serviceName, - details, - }) - - const messageResponse = await this.sqsClient.send( - new SendMessageCommand({ - MessageBody: message, - QueueUrl: this.queueUrl, - }), - ) - - logger.info(`SQS message sent (${messageResponse.MessageId})`) - return message - } catch (error) { - logger.error('Problem sending message to SQS queue', error) - throw error - } - } -} - -export default new AuditService()