From 9a9e2396ed1bebe2301bbca114c9c0b4e8f5cbfd Mon Sep 17 00:00:00 2001 From: Roble Date: Fri, 2 Aug 2024 10:08:45 +0100 Subject: [PATCH] Deleted agreements shouldn't show in the setup tab (#1220) * Deleted agreements shouldn't show in the setup tab https://eaflood.atlassian.net/browse/WATER-4604 Currently, when an agreement is deleted within the licence set up tab, the agreement still remains. This is because the agreement is being 'soft deleted' rather than being removed from the database completely by adding the current time and date to the 'deleted_at' column in the licence agreements table. The logic for pulling licence agreements is missing instructions to only show licence agreements where the 'deleted_at' is null. This PR is focused on not showing licence agreements in the licence set up tab where the licence has the 'deleted_at' column populated. --- .../licences/fetch-agreements.service.js | 1 + .../licences/fetch-agreements.service.test.js | 72 ++++++++++++------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/app/services/licences/fetch-agreements.service.js b/app/services/licences/fetch-agreements.service.js index 890fdb03bf..298e096f62 100644 --- a/app/services/licences/fetch-agreements.service.js +++ b/app/services/licences/fetch-agreements.service.js @@ -21,6 +21,7 @@ async function go (licenceRef) { async function _fetch (licenceRef) { return LicenceAgreementModel.query() .where('licenceRef', licenceRef) + .whereNull('deletedAt') .select([ 'id', 'startDate', diff --git a/test/services/licences/fetch-agreements.service.test.js b/test/services/licences/fetch-agreements.service.test.js index 75255d48e6..33f88acb80 100644 --- a/test/services/licences/fetch-agreements.service.test.js +++ b/test/services/licences/fetch-agreements.service.test.js @@ -4,50 +4,70 @@ const Lab = require('@hapi/lab') const Code = require('@hapi/code') -const { describe, it, beforeEach } = exports.lab = Lab.script() +const { describe, it, before, beforeEach } = exports.lab = Lab.script() const { expect } = Code // Test helpers -const DatabaseSupport = require('../../support/database.js') const FinancialAgreementHelper = require('../../support/helpers/financial-agreement.helper.js') const LicenceAgreementHelper = require('../../support/helpers/licence-agreement.helper.js') // Thing under test -const FetchAgreementsService = - require('../../../app/services/licences/fetch-agreements.service.js') +const FetchAgreementsService = require('../../../app/services/licences/fetch-agreements.service.js') describe('Fetch Agreements service', () => { - const licenceRef = '01/12/34/1000' + const endDate = new Date('2040-05-01') + const signedOn = new Date('2022-04-01') + const startDate = new Date('2022-04-01') - beforeEach(async () => { - await DatabaseSupport.clean() - }) + let licenceAgreement + let financialAgreement describe('when the licence has agreements data', () => { - beforeEach(async () => { - const financialAgreement = await FinancialAgreementHelper.add({ - id: '970168ce-06c3-4823-b84d-9da30b742bb8', - code: 'S127' + before(async () => { + financialAgreement = await FinancialAgreementHelper.add() + }) + + describe('and the agreement has not been deleted', () => { + beforeEach(async () => { + licenceAgreement = await LicenceAgreementHelper.add({ + endDate, + financialAgreementId: financialAgreement.id, + startDate, + signedOn + }) }) - await LicenceAgreementHelper.add({ - endDate: new Date('2040-05-01'), - financialAgreementId: financialAgreement.id, - licenceRef, - startDate: new Date('2022-04-01'), - signedOn: new Date('2022-04-01') + it('returns the matching agreements data', async () => { + const results = await FetchAgreementsService.go(licenceAgreement.licenceRef) + + expect(results[0]).to.equal({ + endDate, + financialAgreement: { + id: financialAgreement.id, + code: financialAgreement.code + }, + startDate, + signedOn + }, { skip: ['id'] }) }) }) - it('returns the matching agreements data', async () => { - const results = await FetchAgreementsService.go(licenceRef) + describe('and the agreement has been deleted', () => { + beforeEach(async () => { + licenceAgreement = await LicenceAgreementHelper.add({ + endDate, + financialAgreementId: financialAgreement.id, + startDate, + signedOn, + deletedAt: new Date() + }) + }) + + it('does not return the agreements data', async () => { + const results = await FetchAgreementsService.go(licenceAgreement.licenceRef) - expect(results[0]).to.equal({ - startDate: new Date('2022-04-01'), - signedOn: new Date('2022-04-01'), - endDate: new Date('2040-05-01'), - financialAgreement: { id: '970168ce-06c3-4823-b84d-9da30b742bb8', code: 'S127' } - }, { skip: ['id'] }) + expect(results).to.be.empty() + }) }) }) })