Small code enhancements, remove deleted filter in getImportedUsers storage, renamed collections in lower case to importusers and importtags
MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -502,13 +502,15 @@ export default class TagService {
});
res.writeHead(HTTPError.INVALID_FILE_FORMAT);
res.end();
+ return;
}
}
// Set default value
tag.importedBy = importedBy;
tag.importedOn = importedOn;
// Import
- if (await TagService.importTag(action, req, tag)) {
+ const importSuccess = await TagService.importTag(action, req, tag);
+ if (importSuccess) {
result.inSuccess++;
} else {
result.inError++;
@@ -534,7 +536,8 @@ export default class TagService {
tag.importedBy = importedBy;
tag.importedOn = importedOn;
// Import
- if (await TagService.importTag(action, req, tag)) {
+ const importSuccess = await TagService.importTag(action, req, tag);
+ if (importSuccess) {
result.inSuccess++;
} else {
result.inError++;
@@ -566,7 +569,6 @@ export default class TagService {
}
});
busboy.on('finish', () => {
- // Log
void Logging.logActionsResponse(
req.user.tenantID, action,
MODULE_NAME, 'handleImportTags', result,
@@ -575,13 +577,6 @@ export default class TagService {
'{{inSuccess}} Tag(s) were successfully uploaded and ready for asynchronous import and {{inError}} failed to be uploaded',
'No Tag have been uploaded', req.user
);
- void Logging.logInfo({
- tenantID: req.user.tenantID,
- action: action,
- module: MODULE_NAME, method: 'handleImportTags',
- user: req.user,
- message: 'File has been successfully uploaded in database and ready for asynchronous import',
- });
res.end();
next();
});
@@ -592,6 +587,7 @@ export default class TagService {
inSuccess: 0,
inError: 0
};
+ // Delete Tags
for (const tagID of tagsIDs) {
// Get Tag
const tag = await TagStorage.getTag(loggedUser.tenantID, tagID, { withNbrTransactions: true, withUser: true });
@@ -693,6 +689,7 @@ export default class TagService {
};
// Validate Tag data
TagValidator.getInstance().validateImportedTagCreation(newImportedTag);
+ // Set properties
newImportedTag.importedBy = importedTag.importedBy;
newImportedTag.importedOn = importedTag.importedOn;
newImportedTag.status = ImportStatus.READY;

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -900,13 +900,15 @@ export default class UserService {
});
res.writeHead(HTTPError.INVALID_FILE_FORMAT);
res.end();
+ return;
}
}
// Set default value
user.importedBy = importedBy;
user.importedOn = importedOn;
// Import
- if (await UserService.importUser(action, req, user)) {
+ const importSuccess = await UserService.importUser(action, req, user);
+ if (importSuccess) {
result.inSuccess++;
} else {
result.inError++;
@@ -932,7 +934,8 @@ export default class UserService {
user.importedBy = importedBy;
user.importedOn = importedOn;
// Import
- if (await UserService.importUser(action, req, user)) {
+ const importSuccess = await UserService.importUser(action, req, user);
+ if (importSuccess) {
result.inSuccess++;
} else {
result.inError++;
@@ -964,7 +967,6 @@ export default class UserService {
}
});
busboy.on('finish', function() {
- // Log
void Logging.logActionsResponse(
req.user.tenantID, action,
MODULE_NAME, 'handleImportUsers', result,
@@ -973,13 +975,6 @@ export default class UserService {
'{{inSuccess}} User(s) were successfully uploaded and ready for asynchronous import and {{inError}} failed to be uploaded',
'No User have been uploaded', req.user
);
- void Logging.logInfo({
- tenantID: req.user.tenantID,
- action: action,
- module: MODULE_NAME, method: 'handleImportUsers',
- user: req.user,
- message: 'File has been successfully uploaded in database and ready for asynchronous import',
- });
res.end();
next();
});
@@ -1341,6 +1336,7 @@ export default class UserService {
};
// Validate User data
UserValidator.getInstance().validateImportedUserCreation(newImportedUser);
+ // Set properties
newImportedUser.importedBy = importedUser.importedBy;
newImportedUser.importedOn = importedUser.importedOn;
newImportedUser.status = ImportStatus.READY;

MODIFY
src/storage/mongodb/TagStorage.ts
src/storage/mongodb/TagStorage.ts
@@ -44,12 +44,12 @@ export default class TagStorage {
const tagMDB = {
_id: importedTagToSave.id,
description: importedTagToSave.description,
- importedOn: Utils.convertToDate(importedTagToSave.importedOn),
- importedBy: Utils.convertToObjectID(importedTagToSave.importedBy),
status: importedTagToSave.status,
- errorDescription: importedTagToSave.errorDescription
+ errorDescription: importedTagToSave.errorDescription,
+ importedOn: Utils.convertToDate(importedTagToSave.importedOn),
+ importedBy: Utils.convertToObjectID(importedTagToSave.importedBy)
};
- await global.database.getCollection<any>(tenantID, 'tagsImport').findOneAndUpdate(
+ await global.database.getCollection<any>(tenantID, 'importtags').findOneAndUpdate(
{ _id: tagMDB._id },
{ $set: tagMDB },
{ upsert: true, returnOriginal: false }
@@ -65,7 +65,7 @@ export default class TagStorage {
// Check Tenant
await DatabaseUtils.checkTenant(tenantID);
// Delete
- await global.database.getCollection<any>(tenantID, 'tagsImport').deleteOne(
+ await global.database.getCollection<any>(tenantID, 'importtags').deleteOne(
{
'_id': importedTagID,
});
@@ -110,7 +110,7 @@ export default class TagStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const tagsImportCountMDB = await global.database.getCollection<any>(tenantID, 'tagsImport')
+ const tagsImportCountMDB = await global.database.getCollection<any>(tenantID, 'importtags')
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Check if only the total count is requested
@@ -148,7 +148,7 @@ export default class TagStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const tagsImportMDB = await global.database.getCollection<any>(tenantID, 'tagsImport')
+ const tagsImportMDB = await global.database.getCollection<any>(tenantID, 'importtags')
.aggregate(aggregation, {
allowDiskUse: true
})

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -285,7 +285,7 @@ export default class UserStorage {
importedOn: Utils.convertToDate(importedUserToSave.importedOn),
importedBy: Utils.convertToObjectID(importedUserToSave.importedBy)
};
- await global.database.getCollection<any>(tenantID, 'usersImport').findOneAndUpdate(
+ await global.database.getCollection<any>(tenantID, 'importusers').findOneAndUpdate(
{ _id: userMDB._id },
{ $set: userMDB },
{ upsert: true, returnOriginal: false }
@@ -301,7 +301,7 @@ export default class UserStorage {
// Check Tenant
await DatabaseUtils.checkTenant(tenantID);
// Delete
- await global.database.getCollection<any>(tenantID, 'usersImport').deleteOne(
+ await global.database.getCollection<any>(tenantID, 'importusers').deleteOne(
{
'_id': Utils.convertToObjectID(importedUserID),
});
@@ -710,8 +710,6 @@ export default class UserStorage {
if (params.status) {
filters.status = params.status;
}
- // Remove deleted
- filters.deleted = { '$ne': true };
// Add filters
aggregation.push({
$match: filters
@@ -722,13 +720,13 @@ export default class UserStorage {
aggregation.push({ $limit: Constants.DB_RECORD_COUNT_CEIL });
}
// Count Records
- const usersImportCountMDB = await global.database.getCollection<any>(tenantID, 'usersImport')
+ const usersImportCountMDB = await global.database.getCollection<any>(tenantID, 'importusers')
.aggregate([...aggregation, { $count: 'count' }], { allowDiskUse: true })
.toArray();
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
// Return only the count
- await Logging.traceEnd(tenantID, MODULE_NAME, 'getUsersImport', uniqueTimerID, usersImportCountMDB);
+ await Logging.traceEnd(tenantID, MODULE_NAME, 'getImportedUsers', uniqueTimerID, usersImportCountMDB);
return {
count: (usersImportCountMDB.length > 0 ? usersImportCountMDB[0].count : 0),
result: []
@@ -760,7 +758,7 @@ export default class UserStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const usersImportMDB = await global.database.getCollection<any>(tenantID, 'usersImport')
+ const usersImportMDB = await global.database.getCollection<any>(tenantID, 'importusers')
.aggregate(aggregation, {
allowDiskUse: true
})

MODIFY
src/types/Tag.ts
src/types/Tag.ts
@@ -25,5 +25,7 @@ export interface ImportedTag {
errorDescription?: string;
}
-export const TagRequiredImportProperties = ['id'];
+export const TagRequiredImportProperties = [
+ 'id'
+];

MODIFY
src/types/User.ts
src/types/User.ts
@@ -83,5 +83,9 @@ export enum UserRole {
DEMO = 'D',
}
-export const UserRequiredImportProperties = ['email', 'firstName', 'name'];
+export const UserRequiredImportProperties = [
+ 'email',
+ 'firstName',
+ 'name'
+];

Small code enhancements, remove deleted filter in getImportedUsers storage, renamed collections in lower case to importusers and importtags