Skip to content

Commit e4d8cd7

Browse files
authored
fix(Single Statement Deletions): Prevent Mongo timeouts caused by store recounts. (#1506 - [LL-293](https://learningpool.atlassian.net/browse/LL-293))
#1506 https://learningpool.atlassian.net/browse/LL-293
1 parent e14c5c3 commit e4d8cd7

4 files changed

Lines changed: 24 additions & 4 deletions

File tree

api/src/routes/HttpRoutes.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ import personaIdentifierRESTHandler from 'api/routes/personas/personaIdentifierR
6464
import UserOrganisationsRouter from 'api/routes/userOrganisations/router';
6565
import UserOrganisationSettingsRouter from 'api/routes/userOrganisationSettings/router';
6666
import BatchDelete from 'lib/models/batchDelete';
67-
import getOrgFromAuthInfo from 'lib/services/auth/authInfoSelectors/getOrgFromAuthInfo';
68-
import { updateStatementCountsInOrg } from 'lib/services/lrs';
67+
import getLrsFromAuthInfo from 'lib/services/auth/authInfoSelectors/getLrsFromAuthInfo';
68+
import { decrementStatementCount } from 'lib/services/lrs';
6969
import * as routes from 'lib/constants/routes';
7070

7171
const router = new express.Router();
@@ -361,8 +361,9 @@ restify.serve(router, Statement, {
361361
postDelete: (req, _, next) => {
362362
// Update LRS.statementCount
363363
const authInfo = getAuthFromRequest(req);
364-
const organisationId = getOrgFromAuthInfo(authInfo);
365-
updateStatementCountsInOrg(organisationId)
364+
const lrsId = getLrsFromAuthInfo(authInfo);
365+
366+
decrementStatementCount(lrsId)
366367
.then(() => next())
367368
.catch(err => next(err));
368369
},

lib/models/lrs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ schema.statics.updateStatementCount = async (lrs) => {
5858
});
5959
};
6060

61+
schema.statics.decrementStatementCount = async (lrs) => {
62+
getConnection()
63+
.model('Lrs')
64+
.update({ _id: lrs._id }, { $inc: { statementCount: -1 } })
65+
.exec();
66+
};
67+
6168
export default getConnection().model('Lrs', schema, 'lrs');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import get from 'lodash/get';
2+
3+
export default authInfo =>
4+
get(authInfo, ['client', 'lrs_id'], {});
5+

lib/services/lrs.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ export const updateStatementCountsInOrg = async (organisationId) => {
66
const lrsList = await Lrs.find({ organisation: organisationId });
77
await Promise.map(lrsList, Lrs.updateStatementCount);
88
};
9+
10+
export const decrementStatementCount = async (lrsId) => {
11+
const lrs = await Lrs.findOne({ _id: lrsId });
12+
if (lrs) {
13+
await Lrs.decrementStatementCount(lrs);
14+
}
15+
};

0 commit comments

Comments
 (0)