Skip to content

Commit

Permalink
Merge pull request #2440 from adaptlearning/issue/2433
Browse files Browse the repository at this point in the history
Return early if target attribute already exists
  • Loading branch information
tomgreenfield authored Oct 18, 2019
2 parents 9c67cf1 + ac2395f commit b5edc5c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 55 deletions.
131 changes: 76 additions & 55 deletions plugins/content/bower/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,93 +803,114 @@ function addPackage (plugin, packageInfo, options, cb) {
return addCb(err);
}

// don't duplicate component.name, component.version
db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) {
const targetAttribute = pkgMeta.targetAttribute;

async.some([ 'componenttype', 'extensiontype', 'menutype', 'themetype' ], (type, asyncCallback) => {
if (!targetAttribute) return asyncCallback();

db.retrieve(type, { targetAttribute: targetAttribute }, (err, results) => {
asyncCallback(err, results && results.length);
});
}, (err, targetAttributeExists) => {
if (err) {
logger.log('error', err);
return addCb(err);
}

if (results && 0 !== results.length) {
// don't add duplicate
if (options.strict) {
return addCb(new PluginPackageError(app.polyglot.t('app.versionexists')));
}
return addCb(null);
if (targetAttributeExists) {
return addCb(new PluginPackageError(app.polyglot.t('app.targetattributeexists', {
targetAttribute
})));
}

db.create(plugin.type, package, function (err, newPlugin) {
// don't duplicate component.name, component.version
db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) {
if (err) {
logger.log('error', err);
return addCb(err);
}

if (results && 0 !== results.length) {
// don't add duplicate
if (options.strict) {
return addCb(err);
return addCb(new PluginPackageError(app.polyglot.t('app.versionexists')));
}

logger.log('error', 'Failed to add package: ' + package.name, err);
return addCb(null);
}

logger.log('info', 'Added package: ' + package.name);

// #509 update content targeted by previous versions of this package
logger.log('info', 'searching old package types ... ');
db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) {

db.create(plugin.type, package, function (err, newPlugin) {
if (err) {
// strictness doesn't matter at this point
logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err);
if (options.strict) {
return addCb(err);
}

logger.log('error', 'Failed to add package: ' + package.name, err);
return addCb(null);
}

if (results && results.length) {
// found previous versions to update
// only update content using the id of the most recent version
var oldPlugin = false;
results.forEach(function (item) {
if (!oldPlugin) {
oldPlugin = item;
} else if (semver.gt(item.version, oldPlugin.version)) {
oldPlugin = item;
}
});
logger.log('info', 'Added package: ' + package.name);

// Persist the _isAvailableInEditor flag.
db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) {
if (err) {
logger.log('error', err);
return addCb(err);
}
// #509 update content targeted by previous versions of this package
logger.log('info', 'searching old package types ... ');
db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) {

if (err) {
// strictness doesn't matter at this point
logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err);
}

if (results && results.length) {
// found previous versions to update
// only update content using the id of the most recent version
var oldPlugin = false;
results.forEach(function (item) {
if (!oldPlugin) {
oldPlugin = item;
} else if (semver.gt(item.version, oldPlugin.version)) {
oldPlugin = item;
}
});

plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) {
// Persist the _isAvailableInEditor flag.
db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) {
if (err) {
logger.log('error', err);
return addCb(err);
}

// Remove older versions of this plugin
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) {
if (err) {
logger.log('error', err);
return addCb(err);
}

logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);
return addCb(null, newPlugin);
// Remove older versions of this plugin
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
if (err) {
logger.log('error', err);
return addCb(err);
}

logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);
return addCb(null, newPlugin);
});
});
});
});
} else {
// nothing to do!
// Remove older versions of this plugin
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
if (err) {
logger.log('error', err);
return addCb(err);
}
} else {
// nothing to do!
// Remove older versions of this plugin
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
if (err) {
logger.log('error', err);
return addCb(err);
}

logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);
logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);

return addCb(null, newPlugin);
});
}
return addCb(null, newPlugin);
});
}
});
});
});
});
Expand Down
1 change: 1 addition & 0 deletions routes/lang/en-application.json
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
"app.unrecognisedplugin": "Unrecognised plugin - a plugin should have a bower.json file.",
"app.unrecognisedpluginforpackage": "Unrecognised plugin type for package %{package}.",
"app.incompatibleframework": "This plugin is incompatible with version %{framework} of the Adapt framework.",
"app.targetattributeexists": "There is a plugin already installed with a target attribute of '%{targetAttribute}'.",
"app.versionexists": "You already have this version of the plugin installed.",
"app.unknownuser": "Unknown User"
}

0 comments on commit b5edc5c

Please sign in to comment.