Skip to content

Commit

Permalink
- Add new scope for kbMatchedStatements to not include kbMatches for …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
bnguyen-bcgsc and Nithriel committed Oct 29, 2024
1 parent 1d90f89 commit e29b8fb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/models/reports/kbMatchedStatements.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ module.exports = (sequelize, Sq) => {
},
],
},
kbMatchesInclude: {
attributes: {exclude: ['id', 'reportId', 'deletedAt', 'updatedBy']},
},
},
hooks: {
...DEFAULT_REPORT_OPTIONS.hooks,
Expand Down
40 changes: 33 additions & 7 deletions app/routes/report/kbMatch/kbMatchedStatements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Check failure on line 47 in app/routes/report/kbMatch/kbMatchedStatements.js

View workflow job for this annotation

GitHub Actions / eslint

Missing semicolon
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}

Check failure on line 55 in app/routes/report/kbMatch/kbMatchedStatements.js

View workflow job for this annotation

GitHub Actions / eslint

Missing trailing comma
});

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,
},
},
});
};

Check failure on line 72 in app/routes/report/kbMatch/kbMatchedStatements.js

View workflow job for this annotation

GitHub Actions / eslint

Unnecessary semicolon
await db.models.kbMatchJoin.destroy({
where: {
id: {
[Op.in]: bindingsIds,
},
},
})

Check failure on line 79 in app/routes/report/kbMatch/kbMatchedStatements.js

View workflow job for this annotation

GitHub Actions / eslint

Missing semicolon

return res.status(HTTP_STATUS.NO_CONTENT).send();
} catch (error) {
logger.error(`Unable to remove kb matched statement ${error}`);
Expand Down
2 changes: 1 addition & 1 deletion app/routes/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion test/routes/report/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e29b8fb

Please sign in to comment.