Skip to content

Commit b5edc5c

Browse files
Merge pull request #2440 from adaptlearning/issue/2433
Return early if target attribute already exists
2 parents 9c67cf1 + ac2395f commit b5edc5c

2 files changed

Lines changed: 77 additions & 55 deletions

File tree

plugins/content/bower/index.js

Lines changed: 76 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -803,93 +803,114 @@ function addPackage (plugin, packageInfo, options, cb) {
803803
return addCb(err);
804804
}
805805

806-
// don't duplicate component.name, component.version
807-
db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) {
806+
const targetAttribute = pkgMeta.targetAttribute;
807+
808+
async.some([ 'componenttype', 'extensiontype', 'menutype', 'themetype' ], (type, asyncCallback) => {
809+
if (!targetAttribute) return asyncCallback();
810+
811+
db.retrieve(type, { targetAttribute: targetAttribute }, (err, results) => {
812+
asyncCallback(err, results && results.length);
813+
});
814+
}, (err, targetAttributeExists) => {
808815
if (err) {
809816
logger.log('error', err);
810817
return addCb(err);
811818
}
812819

813-
if (results && 0 !== results.length) {
814-
// don't add duplicate
815-
if (options.strict) {
816-
return addCb(new PluginPackageError(app.polyglot.t('app.versionexists')));
817-
}
818-
return addCb(null);
820+
if (targetAttributeExists) {
821+
return addCb(new PluginPackageError(app.polyglot.t('app.targetattributeexists', {
822+
targetAttribute
823+
})));
819824
}
820825

821-
db.create(plugin.type, package, function (err, newPlugin) {
826+
// don't duplicate component.name, component.version
827+
db.retrieve(plugin.type, { name: package.name, version: package.version }, function (err, results) {
822828
if (err) {
829+
logger.log('error', err);
830+
return addCb(err);
831+
}
832+
833+
if (results && 0 !== results.length) {
834+
// don't add duplicate
823835
if (options.strict) {
824-
return addCb(err);
836+
return addCb(new PluginPackageError(app.polyglot.t('app.versionexists')));
825837
}
826-
827-
logger.log('error', 'Failed to add package: ' + package.name, err);
828838
return addCb(null);
829839
}
830840

831-
logger.log('info', 'Added package: ' + package.name);
832-
833-
// #509 update content targeted by previous versions of this package
834-
logger.log('info', 'searching old package types ... ');
835-
db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) {
836-
841+
db.create(plugin.type, package, function (err, newPlugin) {
837842
if (err) {
838-
// strictness doesn't matter at this point
839-
logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err);
843+
if (options.strict) {
844+
return addCb(err);
845+
}
846+
847+
logger.log('error', 'Failed to add package: ' + package.name, err);
848+
return addCb(null);
840849
}
841850

842-
if (results && results.length) {
843-
// found previous versions to update
844-
// only update content using the id of the most recent version
845-
var oldPlugin = false;
846-
results.forEach(function (item) {
847-
if (!oldPlugin) {
848-
oldPlugin = item;
849-
} else if (semver.gt(item.version, oldPlugin.version)) {
850-
oldPlugin = item;
851-
}
852-
});
851+
logger.log('info', 'Added package: ' + package.name);
853852

854-
// Persist the _isAvailableInEditor flag.
855-
db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) {
856-
if (err) {
857-
logger.log('error', err);
858-
return addCb(err);
859-
}
853+
// #509 update content targeted by previous versions of this package
854+
logger.log('info', 'searching old package types ... ');
855+
db.retrieve(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err, results) {
856+
857+
if (err) {
858+
// strictness doesn't matter at this point
859+
logger.log('error', 'Failed to retrieve previous packages: ' + err.message, err);
860+
}
861+
862+
if (results && results.length) {
863+
// found previous versions to update
864+
// only update content using the id of the most recent version
865+
var oldPlugin = false;
866+
results.forEach(function (item) {
867+
if (!oldPlugin) {
868+
oldPlugin = item;
869+
} else if (semver.gt(item.version, oldPlugin.version)) {
870+
oldPlugin = item;
871+
}
872+
});
860873

861-
plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) {
874+
// Persist the _isAvailableInEditor flag.
875+
db.update(plugin.type, {_id: newPlugin._id}, {_isAvailableInEditor: oldPlugin._isAvailableInEditor}, function(err, results) {
862876
if (err) {
863877
logger.log('error', err);
864878
return addCb(err);
865879
}
866880

867-
// Remove older versions of this plugin
868-
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
881+
plugin.updateLegacyContent(newPlugin, oldPlugin, function (err) {
869882
if (err) {
870883
logger.log('error', err);
871884
return addCb(err);
872885
}
873886

874-
logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);
875-
return addCb(null, newPlugin);
887+
// Remove older versions of this plugin
888+
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
889+
if (err) {
890+
logger.log('error', err);
891+
return addCb(err);
892+
}
893+
894+
logger.log('info', 'Successfully removed versions of ' + package.name + '(' + plugin.type + ') older than ' + newPlugin.version);
895+
return addCb(null, newPlugin);
896+
});
876897
});
877898
});
878-
});
879-
} else {
880-
// nothing to do!
881-
// Remove older versions of this plugin
882-
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
883-
if (err) {
884-
logger.log('error', err);
885-
return addCb(err);
886-
}
899+
} else {
900+
// nothing to do!
901+
// Remove older versions of this plugin
902+
db.destroy(plugin.type, { name: package.name, version: { $ne: newPlugin.version } }, function (err) {
903+
if (err) {
904+
logger.log('error', err);
905+
return addCb(err);
906+
}
887907

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

890-
return addCb(null, newPlugin);
891-
});
892-
}
910+
return addCb(null, newPlugin);
911+
});
912+
}
913+
});
893914
});
894915
});
895916
});

routes/lang/en-application.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@
415415
"app.unrecognisedplugin": "Unrecognised plugin - a plugin should have a bower.json file.",
416416
"app.unrecognisedpluginforpackage": "Unrecognised plugin type for package %{package}.",
417417
"app.incompatibleframework": "This plugin is incompatible with version %{framework} of the Adapt framework.",
418+
"app.targetattributeexists": "There is a plugin already installed with a target attribute of '%{targetAttribute}'.",
418419
"app.versionexists": "You already have this version of the plugin installed.",
419420
"app.unknownuser": "Unknown User"
420421
}

0 commit comments

Comments
 (0)