Skip to content

Commit

Permalink
Merge branch 'develop' into feature/DEVSU-2430-notification-fail-email
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithriel authored Dec 19, 2024
2 parents 6cb0725 + 23a8c24 commit 043a091
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 112 deletions.
1 change: 1 addition & 0 deletions app/api/graphkb.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const graphkbEvidenceLevels = async (graphkbToken) => {
filters: {name: 'ipr'},
},
},
orderBy: 'displayName',
limit: 100,
skip: 0,
target: 'EvidenceLevel',
Expand Down
3 changes: 3 additions & 0 deletions app/models/reports/kbMatches.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module.exports = (sequelize, Sq) => {
return {model: sequelize.models[modelName].scope('public'), as: modelName};
}),
},
minimal: {
attributes: {exclude: ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy']},
},
extended: {
attributes: {exclude: ['id', 'reportId', 'variantId', 'deletedAt', 'updatedBy']},
include: Object.values(KB_PIVOT_MAPPING).map((modelName) => {
Expand Down
20 changes: 0 additions & 20 deletions app/routes/germlineSmallMutation/reports.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const {GERMLINE_EXCLUDE} = require('../../schemas/exclude');

const db = require('../../models');
const logger = require('../../log');
const cache = require('../../cache');

const {generateKey} = require('../../libs/cacheFunctions');

const Variants = require('./util/variants');
const gsmMiddleware = require('../../middleware/germlineSmallMutation/reports');
Expand Down Expand Up @@ -95,19 +92,6 @@ router.route('/')
return group.name.trim().toLowerCase() === 'projects';
});

// Generate cache key
const key = generateKey('/germline', req.query, {inProjectsGroup});

try {
const cacheResults = await cache.get(key);
if (cacheResults) {
res.type('json');
return res.send(cacheResults);
}
} catch (error) {
logger.error(`Error while checking cache for germline reports ${error}`);
}

// Setup query options
const opts = {
where: {
Expand Down Expand Up @@ -160,10 +144,6 @@ router.route('/')
const gsmReports = await db.models.germlineSmallMutation.scope('public').findAndCountAll(opts);
const results = {total: gsmReports.count, reports: gsmReports.rows};

if (key) {
cache.set(key, JSON.stringify(results), 'EX', 14400);
}

return res.json(results);
} catch (error) {
logger.error(`There was an error while finding all germline reports ${error}`);
Expand Down
18 changes: 0 additions & 18 deletions app/routes/germlineSmallMutation/reviews.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const router = express.Router({mergeParams: true});

const logger = require('../../log');
const db = require('../../models');
const cache = require('../../cache');

const schemaGenerator = require('../../schemas/schemaGenerator');
const validateAgainstSchema = require('../../libs/validateAgainstSchema');
Expand Down Expand Up @@ -106,28 +105,11 @@ router.route('/:review')
// Handles requests for all germline reviews for a report
router.route('/')
.get(async (req, res) => {
const key = `/germline/${req.report.ident}/reviews`;

try {
const cacheResults = await cache.get(key);

if (cacheResults) {
res.type('json');
return res.send(cacheResults);
}
} catch (error) {
logger.error(`Error while checking cache for germline reviews ${error}`);
}

try {
const results = await db.models.germlineSmallMutationReview.scope('public').findAll({
where: {germlineReportId: req.report.id},
});

if (key) {
cache.set(key, JSON.stringify(results), 'EX', 14400);
}

return res.json(results);
} catch (error) {
logger.error(`Unable to retrieve germline reviews ${error}`);
Expand Down
18 changes: 0 additions & 18 deletions app/routes/germlineSmallMutation/variants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const router = express.Router({mergeParams: true});

const db = require('../../models');
const logger = require('../../log');
const cache = require('../../cache');

const schemaGenerator = require('../../schemas/schemaGenerator');
const validateAgainstSchema = require('../../libs/validateAgainstSchema');
Expand Down Expand Up @@ -98,28 +97,11 @@ router.route('/:variant')
// Handles requests for all germline variants for a report
router.route('/')
.get(async (req, res) => {
const key = `/germline/${req.report.ident}/variants`;

try {
const cacheResults = await cache.get(key);

if (cacheResults) {
res.type('json');
return res.send(cacheResults);
}
} catch (error) {
logger.error(`Error while checking cache for germline variants ${error}`);
}

try {
const results = await db.models.germlineSmallMutationVariant.scope('public').findAll({
where: {germlineReportId: req.report.id},
});

if (key) {
cache.set(key, JSON.stringify(results), 'EX', 14400);
}

return res.json(results);
} catch (error) {
logger.error(`Unable to retrieve germline variants ${error}`);
Expand Down
29 changes: 28 additions & 1 deletion app/routes/report/gene.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ const router = express.Router({mergeParams: true});

const schemaGenerator = require('../../schemas/schemaGenerator');
const validateAgainstSchema = require('../../libs/validateAgainstSchema');
const {REPORT_UPDATE_BASE_URI} = require('../../constants');
const {REPORT_CREATE_BASE_URI, REPORT_UPDATE_BASE_URI} = require('../../constants');

// Generate schemas
const createSchema = schemaGenerator(db.models.genes, {
baseUri: REPORT_CREATE_BASE_URI,
});
const updateSchema = schemaGenerator(db.models.genes, {
baseUri: REPORT_UPDATE_BASE_URI, nothingRequired: true,
});
Expand Down Expand Up @@ -138,6 +141,30 @@ router.route('/')
error: {message: 'Unable to retrieve genes'},
});
}
})
.post(async (req, res) => {
// Validate request against schema
try {
validateAgainstSchema(createSchema, req.body);
} catch (error) {
const message = `Error while validating gene create request ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}

// Create new entry in db
try {
const result = await db.models.genes.create({
...req.body,
reportId: req.report.id,
});
return res.status(HTTP_STATUS.CREATED).json(result.view('public'));
} catch (error) {
logger.error(`Unable to create gene ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({
error: {message: 'Unable to create gene'},
});
}
});

module.exports = router;
57 changes: 6 additions & 51 deletions app/routes/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ router.route('/')
.get(async (req, res) => {
let {
query: {
paginated, limit, offset, sort, project, states, role, searchText, keyVariant, matchingThreshold, category, approvedTherapy, kbVariant, disease, relevance, context, status, reference, sample, evidenceLevel, matchedCancer, pmidRef, variantType,
paginated, limit, offset, sort, project, states, role, searchText, keyVariant, matchingThreshold, kbVariant,
},
} = req;

Expand All @@ -125,7 +125,7 @@ router.route('/')
try {
// validate request query parameters
validateAgainstSchema(reportGetSchema, {
paginated, limit, offset, sort, project, states, role, searchText, keyVariant, matchingThreshold, category, approvedTherapy, kbVariant, disease, relevance, context, status, reference, sample, evidenceLevel, matchedCancer, pmidRef, variantType,
paginated, limit, offset, sort, project, states, role, searchText, keyVariant, matchingThreshold, kbVariant,
}, false);
} catch (err) {
const message = `Error while validating the query params of the report GET request ${err}`;
Expand Down Expand Up @@ -184,15 +184,12 @@ router.route('/')
...((kbVariant && matchingThreshold) ? {
'$kbMatches.kb_variant$': {
[Op.in]: literal(
`(SELECT "kb_variant"
FROM (SELECT "kb_variant", word_similarity('${kbVariant}', "kb_variant") FROM reports_kb_matches) AS subquery
`(SELECT "kb_variant"
FROM (SELECT "kb_variant", word_similarity('${kbVariant}', "kb_variant") FROM reports_kb_matches) AS subquery
WHERE word_similarity >= ${matchingThreshold})`,
),
},
} : {}),
...((variantType) ? {
'$kbMatches.variant_type$': {[Op.eq]: `${variantType}`},
} : {}),
},
distinct: 'id',
// **searchText with paginated with patientInformation set to required: true
Expand Down Expand Up @@ -262,51 +259,9 @@ router.route('/')
model: db.models.genomicAlterationsIdentified.scope('public'),
as: 'genomicAlterationsIdentified',
}] : []),
...((category || approvedTherapy || kbVariant || disease || relevance || context || status || reference || sample || evidenceLevel || matchedCancer || pmidRef || variantType) ? [{
model: db.models.kbMatches.scope('public'),
...((kbVariant && matchingThreshold) ? [{
model: db.models.kbMatches.scope('minimal'),
as: 'kbMatches',
include: [
{
model: db.models.kbMatchedStatements.scope('kbMatchesInclude'),
as: 'kbMatchedStatements',
through: {attributes: []},
where: {
...((category) ? {
category: {[Op.eq]: `${category}`},
} : {}),
...((approvedTherapy) ? {
approvedTherapy: {[Op.eq]: `${approvedTherapy}`},
} : {}),
...((disease) ? {
disease: {[Op.eq]: `${disease}`},
} : {}),
...((relevance) ? {
relevance: {[Op.eq]: `${relevance}`},
} : {}),
...((context) ? {
context: {[Op.eq]: `${context}`},
} : {}),
...((status) ? {
status: {[Op.eq]: `${status}`},
} : {}),
...((reference) ? {
reference: {[Op.eq]: `${reference}`},
} : {}),
...((sample) ? {
sample: {[Op.eq]: `${sample}`},
} : {}),
...((evidenceLevel) ? {
evidence_level: {[Op.eq]: `${evidenceLevel}`},
} : {}),
...((matchedCancer) ? {
matched_cancer: {[Op.eq]: `${matchedCancer}`},
} : {}),
...((pmidRef) ? {
pmid_ref: {[Op.eq]: `${pmidRef}`},
} : {}),
},
},
],
}] : []),
],
};
Expand Down
2 changes: 1 addition & 1 deletion app/routes/report/tmburMutationBurden.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ router.route('/')
.post(async (req, res) => {
// Validate request against schema
try {
await validateAgainstSchema(createSchema, req.body);
validateAgainstSchema(createSchema, req.body);
} catch (error) {
const message = `Error while validating tmbur mutation burden create request ${error}`;
logger.error(message);
Expand Down
46 changes: 45 additions & 1 deletion test/routes/report/gene.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('/reports/{report}/genes', () => {
afterEach(async () => {
return db.models.genes.destroy({
where: {ident: getGene.ident},
force: true,
// force: true,
});
});

Expand Down Expand Up @@ -162,6 +162,50 @@ describe('/reports/{report}/genes', () => {
});
});

describe('POST', () => {
test('/ - 201 Created', async () => {
const res = await request
.post(`/api/reports/${report.ident}/genes`)
.send(GENE_DATA)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.CREATED);

checkGene(res.body);
expect(res.body).toEqual(expect.objectContaining(GENE_DATA));

// Check that record was created in the db
const result = await db.models.genes.findOne({
where: {ident: res.body.ident},
});
expect(result).not.toBeNull();

// Delete entry
await result.destroy({force: true});
});

test('/ - 400 Bad Request - Additional Property', async () => {
await request
.post(`/api/reports/${report.ident}/genes`)
.send({
...GENE_UPDATE_DATA,
additionalProperty: 'ADDITIONAL_PROPERTY',
})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});

test('/ - 400 Bad Request - Gene name is required', async () => {
await request
.post(`/api/reports/${report.ident}/genes`)
.send({})
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.BAD_REQUEST);
});
});

describe('PUT', () => {
let putGene;

Expand Down
5 changes: 3 additions & 2 deletions test/routes/report/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('/reports/{REPORTID}', () => {
const UNREVIEWED_ACCESS = 'unreviewed Access';

const KEYVARIANT = 'uniqueKeyVariant';
const KBVARIANT = 'uniqueKbVariant';

let project;
let project2;
Expand Down Expand Up @@ -503,7 +504,7 @@ describe('/reports/{REPORTID}', () => {

test('/ - kb match - 200 Success', async () => {
const res = await request
.get('/api/reports?category=unknown&variantType=cnv')
.get(`/api/reports?kbVariant=${KBVARIANT}&&matchingThreshold=1`)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);
Expand All @@ -512,7 +513,7 @@ describe('/reports/{REPORTID}', () => {

for (const resReport of res.body.reports) {
for (const gAI of resReport.kbMatches) {
expect(gAI.variantType).toEqual('cnv');
expect(gAI.kbVariant).toEqual(KBVARIANT);
}
}
}, LONGER_TIMEOUT);
Expand Down

0 comments on commit 043a091

Please sign in to comment.