Skip to content

Commit

Permalink
fix(Single Statement Deletions): Prevent Mongo timeouts caused by sto…
Browse files Browse the repository at this point in the history
  • Loading branch information
PrinceWaune committed Jan 30, 2020
1 parent e14c5c3 commit e4d8cd7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
9 changes: 5 additions & 4 deletions api/src/routes/HttpRoutes.js
Expand Up @@ -64,8 +64,8 @@ import personaIdentifierRESTHandler from 'api/routes/personas/personaIdentifierR
import UserOrganisationsRouter from 'api/routes/userOrganisations/router';
import UserOrganisationSettingsRouter from 'api/routes/userOrganisationSettings/router';
import BatchDelete from 'lib/models/batchDelete';
import getOrgFromAuthInfo from 'lib/services/auth/authInfoSelectors/getOrgFromAuthInfo';
import { updateStatementCountsInOrg } from 'lib/services/lrs';
import getLrsFromAuthInfo from 'lib/services/auth/authInfoSelectors/getLrsFromAuthInfo';
import { decrementStatementCount } from 'lib/services/lrs';
import * as routes from 'lib/constants/routes';

const router = new express.Router();
Expand Down Expand Up @@ -361,8 +361,9 @@ restify.serve(router, Statement, {
postDelete: (req, _, next) => {
// Update LRS.statementCount
const authInfo = getAuthFromRequest(req);
const organisationId = getOrgFromAuthInfo(authInfo);
updateStatementCountsInOrg(organisationId)
const lrsId = getLrsFromAuthInfo(authInfo);

decrementStatementCount(lrsId)
.then(() => next())
.catch(err => next(err));
},
Expand Down
7 changes: 7 additions & 0 deletions lib/models/lrs.js
Expand Up @@ -58,4 +58,11 @@ schema.statics.updateStatementCount = async (lrs) => {
});
};

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

export default getConnection().model('Lrs', schema, 'lrs');
5 changes: 5 additions & 0 deletions lib/services/auth/authInfoSelectors/getLrsFromAuthInfo.js
@@ -0,0 +1,5 @@
import get from 'lodash/get';

export default authInfo =>
get(authInfo, ['client', 'lrs_id'], {});

7 changes: 7 additions & 0 deletions lib/services/lrs.js
Expand Up @@ -6,3 +6,10 @@ export const updateStatementCountsInOrg = async (organisationId) => {
const lrsList = await Lrs.find({ organisation: organisationId });
await Promise.map(lrsList, Lrs.updateStatementCount);
};

export const decrementStatementCount = async (lrsId) => {
const lrs = await Lrs.findOne({ _id: lrsId });
if (lrs) {
await Lrs.decrementStatementCount(lrs);
}
};

0 comments on commit e4d8cd7

Please sign in to comment.