reorg code to avoid duplicating assign/save/delete
MODIFY
src/async-task/tasks/TagsImportAsyncTask.ts
src/async-task/tasks/TagsImportAsyncTask.ts
@@ -44,6 +44,7 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
do {
// Get the imported tags
importedTags = await TagStorage.getImportedTags(tenant.id, { status: ImportStatus.READY }, dbParams);
+ let tagToSave: Tag;
for (const importedTag of importedTags.result) {
try {
// Existing tags
@@ -62,19 +63,10 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
if (foundTag.transactionsCount > 0) {
throw new Error(`Tag is already used in ${foundTag.transactionsCount} transaction(s)`);
}
- // Save user if any and get the ID to assign tag
- if (importedTag.email && importedTag.name && importedTag.firstName) {
- await this.assignTag(tenant, importedTag, foundTag);
- }
- // Update it
- await TagStorage.saveTag(tenant.id, { ...foundTag, ...importedTag });
- // Remove the imported Tag
- await TagStorage.deleteImportedTag(tenant.id, importedTag.id);
- result.inSuccess++;
- continue;
- }
+ tagToSave = { ...foundTag, ...importedTag };
+ } else {
// New Tag
- const tag: Tag = {
+ tagToSave = {
id: importedTag.id,
description: importedTag.description,
issuer: true,
@@ -82,12 +74,13 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
createdBy: { id: importedTag.importedBy },
createdOn: importedTag.importedOn,
};
+ }
// Save user if any and get the ID to assign tag
if (importedTag.email && importedTag.name && importedTag.firstName) {
- await this.assignTag(tenant, importedTag, tag);
+ await this.assignTag(tenant, importedTag, tagToSave);
}
// Save the new Tag
- await TagStorage.saveTag(tenant.id, tag);
+ await TagStorage.saveTag(tenant.id, tagToSave);
// Remove the imported Tag
await TagStorage.deleteImportedTag(tenant.id, importedTag.id);
result.inSuccess++;
@@ -106,7 +99,7 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
message: `Error when importing Tag ID '${importedTag.id}': ${error.message}`,
detailedMessages: { tag: importedTag, error: error.message, stack: error.stack }
});
- }
+ } // Do we add a finally that delete tags if the error comes from "foundTag" ? for now the found tags are left as is in the importedTag collection
}
// Log
if (!Utils.isEmptyArray(importedTags.result) && (result.inError + result.inSuccess) > 0) {

reorg code to avoid duplicating assign/save/delete