Get only one transaction record to check if the tag is already used in a transaction at creation time + Add the transaction number check in the Import Tag
MODIFY
src/async-task/tasks/TagsImportAsyncTask.ts
src/async-task/tasks/TagsImportAsyncTask.ts
@@ -44,7 +44,7 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
for (const importedTag of importedTags.result) {
try {
// Existing tags
- const foundTag = await TagStorage.getTag(tenant.id, importedTag.id);
+ const foundTag = await TagStorage.getTag(tenant.id, importedTag.id, { withNbrTransactions: true });
if (foundTag) {
// Check tag is already in use
if (!foundTag.issuer) {
@@ -56,6 +56,9 @@ export default class TagsImportAsyncTask extends AbstractAsyncTask {
if (foundTag.active) {
throw new Error('Tag is already active');
}
+ if (foundTag.transactionsCount > 0) {
+ throw new Error(`Tag is already used in ${foundTag.transactionsCount} transaction(s)`);
+ }
// Update it
await TagStorage.saveTag(tenant.id, { ...foundTag, ...importedTag });
// Remove the imported Tag

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -175,8 +175,8 @@ export default class TagService {
});
}
const transactions = await TransactionStorage.getTransactions(req.user.tenantID,
- { tagIDs:[filteredRequest.id.toUpperCase()] }, Constants.DB_PARAMS_COUNT_ONLY);
- if (transactions.count > 0) {
+ { tagIDs: [filteredRequest.id.toUpperCase()] }, Constants.DB_PARAMS_SINGLE_RECORD);
+ if (!Utils.isEmptyArray(transactions.result)) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.TAG_HAS_TRANSACTIONS,
@@ -634,7 +634,7 @@ export default class TagService {
// Delete Tags
for (const tagID of tagsIDs) {
// Get Tag
- const tag = await TagStorage.getTag(loggedUser.tenantID, tagID, { withNbrTransactions: true, withUser: true });
+ const tag = await TagStorage.getTag(loggedUser.tenantID, tagID, { withUser: true });
// Not Found
if (!tag) {
result.inError++;

Get only one transaction record to check if the tag is already used in a transaction at creation time + Add the transaction number check in the Import Tag