Skip to content

Commit 68bfb02

Browse files
Merge pull request #2464 from adaptlearning/issue/2439
Add array tags in callback
2 parents 6adc6e8 + 06dbb34 commit 68bfb02

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

plugins/output/adapt/importsource.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,25 +172,6 @@ function ImportSource(req, done) {
172172
var assetJson = assetsJson[assetName];
173173
var tags = [];
174174

175-
if (assetJson) {
176-
assetTitle = assetJson.title;
177-
assetDescription = assetJson.description;
178-
179-
assetJson.tags.forEach(function(tag) {
180-
const tagTitle = tag.title;
181-
const warn = (error) => logger.log('warn', `Failed to create asset tag '${tagTitle}' ${error}`);
182-
183-
if(!tagTitle) return warn(new Error('Tag has no title'));
184-
185-
app.contentmanager.getContentPlugin('tag', function(error, plugin) {
186-
if(error) return warn(error);
187-
plugin.create({ title: tagTitle }, function(error, record) { // @note retrieves if tag already exists
188-
if(error) return warn(error);
189-
tags.push(record._id);
190-
});
191-
});
192-
});
193-
}
194175
var fileMeta = {
195176
oldId: assetId,
196177
title: assetTitle,
@@ -203,15 +184,38 @@ function ImportSource(req, done) {
203184
repository: repository,
204185
createdBy: app.usermanager.getCurrentUser()._id
205186
};
206-
if(!fileMeta) {
207-
return doneAsset(new helpers.ImportError('No metadata found for asset: ' + assetName));
208-
}
209-
helpers.importAsset(fileMeta, metadata, doneAsset);
187+
188+
if (!assetJson) return helpers.importAsset(fileMeta, metadata, doneAsset);
189+
190+
addAssetTags(assetJson, function(error, assetTags) {
191+
const warn = (error) => logger.log('warn', `Failed to create asset tag ${error}`);
192+
if (error) return warn(new Error(error));
193+
fileMeta.title = assetJson.title;
194+
fileMeta.description = assetJson.description;
195+
fileMeta.tags = assetTags;
196+
helpers.importAsset(fileMeta, metadata, doneAsset);
197+
});
210198
}, doneAssetFolder);
211199
});
212200
}, done);
213201
}
214202

203+
function addAssetTags(assetJson, cb) {
204+
var assetTags = [];
205+
assetJson.tags.forEach(function(tag) {
206+
var tagTitle = tag.title;
207+
if(!tagTitle) return cb('Tag has no title');
208+
app.contentmanager.getContentPlugin('tag', function(error, plugin) {
209+
if(error) return cb(tagTitle.concat(' ', error));
210+
plugin.create({ title: tagTitle }, function(error, record) { // @note retrieves if tag already exists
211+
if(error) return cb(tagTitle.concat(' ', error));
212+
assetTags.push(record._id);
213+
});
214+
});
215+
});
216+
cb(null, assetTags);
217+
}
218+
215219
/**
216220
* Stores plugin metadata for use later
217221
*/

0 commit comments

Comments
 (0)