Skip to content

Commit

Permalink
fix(deleteStatement): Recounts on single statement deletion
Browse files Browse the repository at this point in the history
Transfer the logic of recounts on single statement deletion from
express-restify-mongoose to a post remove hook.

Closes LL-322
  • Loading branch information
Yevgenii Sharpinskii committed Feb 5, 2020
1 parent 2dd3ab6 commit f22daf2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 19 deletions.
12 changes: 0 additions & 12 deletions api/src/routes/HttpRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ import personaRESTHandler from 'api/routes/personas/personaRESTHandler';
import personaIdentifierRESTHandler from 'api/routes/personas/personaIdentifierRESTHandler';
import UserOrganisationsRouter from 'api/routes/userOrganisations/router';
import UserOrganisationSettingsRouter from 'api/routes/userOrganisationSettings/router';
import getLrsFromAuthInfo from 'lib/services/auth/authInfoSelectors/getLrsFromAuthInfo';
import { decrementStatementCount } from 'lib/services/lrs';

// CONSTANTS
import * as routes from 'lib/constants/routes';
Expand Down Expand Up @@ -359,18 +357,8 @@ restify.serve(router, Statement, {
return res.send('No ID sent', 400);
}
next();
return;
},
preUpdate: (req, res) => res.sendStatus(405),
postDelete: (req, _, next) => {
// Update LRS.statementCount
const authInfo = getAuthFromRequest(req);
const lrsId = getLrsFromAuthInfo(authInfo);

decrementStatementCount(lrsId)
.then(() => next())
.catch(err => next(err));
},
});
restify.serve(router, StatementForwarding);
restify.serve(router, QueryBuilderCache);
Expand Down
4 changes: 2 additions & 2 deletions lib/models/lrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ schema.statics.updateStatementCount = async (lrs) => {
});
};

schema.statics.decrementStatementCount = async (lrs) => {
schema.statics.decrementStatementCount = async (lrsId) => {
getConnection()
.model('Lrs')
.update({ _id: lrs._id }, { $inc: { statementCount: -1 } })
.update({ _id: lrsId }, { $inc: { statementCount: -1 } })
.exec();
};

Expand Down
6 changes: 6 additions & 0 deletions lib/models/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import getScopeFilter from 'lib/services/auth/filters/getScopeFilter';
import filterByOrg from 'lib/models/plugins/filterByOrg';
import decodeDot from 'lib/helpers/decodeDot';
import logger from 'lib/logger';
import Lrs from 'lib/models/lrs';

const ALLOW_AGGREGATION_DISK_USE = boolean(defaultTo(process.env.ALLOW_AGGREGATION_DISK_USE, true));
const AGGREGATION_CACHE_SECONDS = defaultTo(Number(process.env.AGGREGATION_CACHE_SECONDS), 300);
Expand Down Expand Up @@ -94,6 +95,11 @@ schema.plugin(scopeChecks);
schema.plugin(filterByOrg);
schema.plugin(addCRUDFunctions);

schema.post('remove', async (statement, next) => {
await Lrs.decrementStatementCount(statement.lrs_id);
next();
});

/**
* @param {*} - {
* pipeline
Expand Down
5 changes: 0 additions & 5 deletions lib/services/auth/authInfoSelectors/getLrsFromAuthInfo.js

This file was deleted.

0 comments on commit f22daf2

Please sign in to comment.