Skip to content

Commit b57edf0

Browse files
committed
Populate default globals on import
1 parent dcabdc4 commit b57edf0

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

plugins/output/adapt/importsource.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const helpers = require('./outputHelpers');
1111
const logger = require("../../../lib/logger");
1212
const mime = require('mime');
1313
const path = require("path");
14+
const { promisify } = require('util');
1415

1516
function ImportSource(req, done) {
1617
var dbInstance;
@@ -340,6 +341,30 @@ function ImportSource(req, done) {
340341
}, cb2);
341342
}, cb);
342343
},
344+
async function populateGlobals() {
345+
const dbRetrieve = promisify(dbInstance.retrieve.bind(dbInstance));
346+
const dbUpdate = promisify(dbInstance.update.bind(dbInstance));
347+
const courseQuery = await dbRetrieve('course', { _id: courseId });
348+
const courseGlobals = courseQuery[0]._doc._globals || {};
349+
await Promise.all(plugindata.pluginIncludes.map(async ({ type, name }) => {
350+
const pluginQuery = await dbRetrieve(`${type}type`, { name });
351+
const plugin = pluginQuery[0]._doc;
352+
const pluginGlobals = plugin.globals;
353+
if (!pluginGlobals) return;
354+
const typeKey = type === 'component' || type === 'extension' ?
355+
`_${type}s` :
356+
`_${type}`;
357+
const pluginKey = `_${plugin[type]}`;
358+
if (!courseGlobals[typeKey]) {
359+
courseGlobals[typeKey] = {};
360+
}
361+
Object.entries(pluginGlobals).forEach(([key, value]) => {
362+
pluginGlobals[key] = value.default;
363+
});
364+
courseGlobals[typeKey][pluginKey] = _.defaults(courseGlobals[typeKey][pluginKey], pluginGlobals);
365+
}));
366+
await dbUpdate('course', { _id: courseId }, { _globals: courseGlobals });
367+
},
343368
function checkDetachedContent(cb) {
344369
const detachedIds = Object.keys(detachedElementsMap);
345370
if (detachedIds.length === 0) return cb();

0 commit comments

Comments
 (0)