Skip to content

add put endpt for kb matched statements #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/routes/report/kbMatch/kbMatchedStatements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const router = express.Router({mergeParams: true});
const db = require('../../../models');
const logger = require('../../../log');

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

const updateSchema = schemaGenerator(db.models.kbMatchedStatements, {
baseUri: REPORT_UPDATE_BASE_URI,
nothingRequired: true,
});

// Middleware for kbMatchedStatements
router.param('kbMatchedStatement', async (req, res, next, kbMatchedStatementIdent) => {
let result;
Expand Down Expand Up @@ -43,6 +52,22 @@ router.route('/:kbMatchedStatement([A-z0-9-]{36})')
.get((req, res) => {
return res.json(req.returnStatement);
})
.put(async (req, res) => {
try {
validateAgainstSchema(updateSchema, req.body, false);
} catch (error) {
const message = `Error while validating patient information update request ${error}`;
logger.error(message);
return res.status(HTTP_STATUS.BAD_REQUEST).json({error: {message}});
}
try {
await req.kbMatchedStatement.update(req.body, {userId: req.user.id});
return res.json(req.kbMatchedStatement.view('public'));
} catch (error) {
logger.error(`Unable to update kbMatchedStatement ${error}`);
return res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({error: {message: 'Unable to update kbMatchedStatement'}});
}
})
.delete(async (req, res) => {
// Soft delete the entry
try {
Expand Down
35 changes: 35 additions & 0 deletions test/routes/report/kbMatch/kbMatchedStatements.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ describe('/reports/{REPORTID}/kb-matches/kb-matched-statements', () => {
});
});

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

beforeEach(async () => {
statementUpdate = await db.models.kbMatchedStatements.create(createDataStatement);
await db.models.kbMatchJoin.create({kbMatchId: kbMatch.id, kbMatchedStatementId: statementUpdate.id});
});

afterEach(async () => {
if (statementUpdate) {
await db.models.kbMatchedStatements.destroy({where: {ident: statementUpdate.ident}});
}
});

test('/{kbMatchedStatements} - 200 Successful statement put', async () => {
const UPDATE_DATA = JSON.parse(JSON.stringify(statementUpdate));
UPDATE_DATA.category = 'prognostic';
UPDATE_DATA.kbData = {test: 'test2'};
const popFields = ['createdAt', 'createdBy', 'updatedAt', 'updatedBy', 'deletedAt', 'deletedBy', 'id', 'ident', 'reportId'];
popFields.forEach((item) => {
delete UPDATE_DATA[item];
});

const res = await request
.put(`/api/reports/${report.ident}/kb-matches/kb-matched-statements/${statementUpdate.ident}`)
.send(UPDATE_DATA)
.auth(username, password)
.type('json')
.expect(HTTP_STATUS.OK);

checkStatement(res.body);
expect(res.body).toEqual(expect.objectContaining(UPDATE_DATA));
});
});

describe('DELETE', () => {
let statementDelete;

Expand Down
Loading