From e29b8fb19948996cb1f964ca9c164974b327a79e Mon Sep 17 00:00:00 2001 From: bnguyen-bcgsc Date: Tue, 29 Oct 2024 14:41:05 -0700 Subject: [PATCH] - Add new scope for kbMatchedStatements to not include kbMatches for unit testing - Update kbMatch unit test route for report.test.js - Update delete endpoint of kbMatchedStatements to delete bindings with kbMatches on cascade and kbMatches with no associated statements Co-authored-by: Lara Matiello Pletz --- app/models/reports/kbMatchedStatements.js | 3 ++ .../report/kbMatch/kbMatchedStatements.js | 40 +++++++++++++++---- app/routes/report/report.js | 2 +- test/routes/report/report.test.js | 2 +- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/models/reports/kbMatchedStatements.js b/app/models/reports/kbMatchedStatements.js index 34f7c7069..dc6e9dff9 100644 --- a/app/models/reports/kbMatchedStatements.js +++ b/app/models/reports/kbMatchedStatements.js @@ -127,6 +127,9 @@ module.exports = (sequelize, Sq) => { }, ], }, + kbMatchesInclude: { + attributes: {exclude: ['id', 'reportId', 'deletedAt', 'updatedBy']}, + }, }, hooks: { ...DEFAULT_REPORT_OPTIONS.hooks, diff --git a/app/routes/report/kbMatch/kbMatchedStatements.js b/app/routes/report/kbMatch/kbMatchedStatements.js index bf84bf0c4..b2cdc6cc1 100644 --- a/app/routes/report/kbMatch/kbMatchedStatements.js +++ b/app/routes/report/kbMatch/kbMatchedStatements.js @@ -40,18 +40,44 @@ router.route('/:kbMatchedStatement([A-z0-9-]{36})') .delete(async (req, res) => { // Soft delete the entry try { - const binding = await db.models.kbMatchJoin.findOne({ - where: {kbMatchId: req.kbMatch.id, kbMatchedStatementId: req.kbMatchedStatement.id}, + const bindings = await db.models.kbMatchJoin.findAll({ + where: {kbMatchedStatementId: req.kbMatchedStatement.id}, }); - if (!binding) { - logger.error(`Variant: ${req.kbMatch.ident} is not bound to statement: ${req.kbMatchedStatement.ident}`); - return res.status(HTTP_STATUS.BAD_REQUEST).json({ - error: {message: 'Variant is not bound to Statement'}, + const bindingsIds = [] + for (const bindingEntry of bindings) { + bindingsIds.push(bindingEntry.id); + } + + const kbMatchesIds = []; + for (const kbMatchEntry of bindings) { + const kbMatchesBindings = await db.models.kbMatchJoin.findAll({ + where: {kbMatchId: kbMatchEntry.kbMatchId} }); + + if (kbMatchesBindings.length <= 1) { + kbMatchesIds.push(kbMatchEntry.kbMatchId); + } } - await binding.destroy(); + await req.kbMatchedStatement.destroy(); + if (kbMatchesIds) { + await db.models.kbMatches.destroy({ + where: { + id: { + [Op.in]: kbMatchesIds, + }, + }, + }); + }; + await db.models.kbMatchJoin.destroy({ + where: { + id: { + [Op.in]: bindingsIds, + }, + }, + }) + return res.status(HTTP_STATUS.NO_CONTENT).send(); } catch (error) { logger.error(`Unable to remove kb matched statement ${error}`); diff --git a/app/routes/report/report.js b/app/routes/report/report.js index b81e8bf3b..6c2503f89 100644 --- a/app/routes/report/report.js +++ b/app/routes/report/report.js @@ -267,7 +267,7 @@ router.route('/') as: 'kbMatches', include: [ { - model: db.models.kbMatchedStatements.scope('public'), + model: db.models.kbMatchedStatements.scope('kbMatchesInclude'), as: 'kbMatchedStatements', through: {attributes: []}, where: { diff --git a/test/routes/report/report.test.js b/test/routes/report/report.test.js index 65aa0587c..078bc480d 100644 --- a/test/routes/report/report.test.js +++ b/test/routes/report/report.test.js @@ -502,7 +502,7 @@ describe('/reports/{REPORTID}', () => { test('/ - kb match - 200 Success', async () => { const res = await request - .get('/api/reports?category=unknown&variantType=cnv&matchingThreshold=1') + .get('/api/reports?category=unknown&variantType=cnv') .auth(username, password) .type('json') .expect(HTTP_STATUS.OK);