diff --git a/api/src/routes/HttpRoutes.js b/api/src/routes/HttpRoutes.js index f40f13f517..5d0bafed9c 100644 --- a/api/src/routes/HttpRoutes.js +++ b/api/src/routes/HttpRoutes.js @@ -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'; @@ -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); diff --git a/lib/models/lrs.js b/lib/models/lrs.js index c40880a766..546542938a 100644 --- a/lib/models/lrs.js +++ b/lib/models/lrs.js @@ -9,6 +9,40 @@ import * as scopes from 'lib/constants/scopes'; import addCRUDFunctions from 'lib/models/plugins/addCRUDFunctions'; import auditRemove from 'lib/models/plugins/auditRemove'; +/** + * Plain object structure without mongoose model methods + * + * @typedef {object} Lrs + * @property {string} title + * @property {string} description + * @property {*} owner_id TODO: define type + * @property {*} organisation TODO: define type + * @property {number} statementCount + */ + +/** @typedef {module:mongoose.Model} lrsModel */ + +/** + * @param {lrsModel} lrsModel + * @returns {Promise} + */ +const createDefaultClient = async (lrsModel) => { + if (!lrsModel.title) { + lrsModel.title = 'New xAPI store'; + } + const ClientModel = getConnection().model('Client'); + const client = new ClientModel({ + organisation: lrsModel.organisation, + lrs_id: lrsModel._id, + scopes: [scopes.XAPI_ALL], + title: `${lrsModel.title} client` + }); + await client.save((err) => { + assert.ifError(err); + }); +}; + +/** @class LrsSchema */ const schema = new mongoose.Schema({ title: { type: String }, description: { type: String }, @@ -20,22 +54,9 @@ schema.plugin(filterByOrg); schema.plugin(timestamps); schema.plugin(addCRUDFunctions); -schema.pre('save', function preSave(next) { - // make a default client +schema.pre('save', async function preSave(next) { if (this.isNew) { - if (!this.title) { - this.title = 'New xAPI store'; - } - const Client = getConnection().model('Client'); - const client = new Client({ - organisation: this.organisation, - lrs_id: this._id, - scopes: [scopes.XAPI_ALL], - title: `${this.title} client` - }); - client.save((err) => { - assert.ifError(err); - }); + await createDefaultClient(this); } next(); }); @@ -49,6 +70,10 @@ schema.plugin(auditRemove, { auditName: 'LRSAudit' }); +/** + * @param {lrsModel} lrs + * @returns {Promise} + */ schema.statics.updateStatementCount = async (lrs) => { const Statement = getConnection().model('Statement'); @@ -58,10 +83,14 @@ schema.statics.updateStatementCount = async (lrs) => { }); }; -schema.statics.decrementStatementCount = async (lrs) => { +/** + * @param {string} lrsId + * @returns {Promise} + */ +schema.statics.decrementStatementCount = async (lrsId) => { getConnection() .model('Lrs') - .update({ _id: lrs._id }, { $inc: { statementCount: -1 } }) + .update({ _id: lrsId }, { $inc: { statementCount: -1 } }) .exec(); }; diff --git a/lib/models/statement.js b/lib/models/statement.js index 727f2aeaa4..511c6543d4 100644 --- a/lib/models/statement.js +++ b/lib/models/statement.js @@ -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); @@ -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 diff --git a/lib/services/auth/authInfoSelectors/getLrsFromAuthInfo.js b/lib/services/auth/authInfoSelectors/getLrsFromAuthInfo.js deleted file mode 100644 index 253afca36c..0000000000 --- a/lib/services/auth/authInfoSelectors/getLrsFromAuthInfo.js +++ /dev/null @@ -1,5 +0,0 @@ -import get from 'lodash/get'; - -export default authInfo => - get(authInfo, ['client', 'lrs_id'], {}); - diff --git a/lib/services/lrs.js b/lib/services/lrs.js index 4c553be201..f66fa6c1fc 100644 --- a/lib/services/lrs.js +++ b/lib/services/lrs.js @@ -6,10 +6,3 @@ 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); - } -};