User not mandatory in tags
MODIFY
src/assets/server/rest/v1/schemas/tag/tag-create.json
src/assets/server/rest/v1/schemas/tag/tag-create.json
},
"required": [
"id",
- "visualID",
- "userID"
+ "visualID"
]
}

MODIFY
src/assets/server/rest/v1/schemas/tag/tag-update.json
src/assets/server/rest/v1/schemas/tag/tag-update.json
},
"required": [
"id",
- "visualID",
- "userID"
+ "visualID"
]
}

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -5,6 +5,7 @@ import { DataResult, TagDataResult } from '../../../../types/DataResult';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import Tag, { ImportedTag, TagRequiredImportProperties } from '../../../../types/Tag';
+import Tenant, { TenantComponents } from '../../../../types/Tenant';
import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
@@ -28,8 +29,6 @@ import { ServerAction } from '../../../../types/Server';
import { StatusCodes } from 'http-status-codes';
import TagStorage from '../../../../storage/mongodb/TagStorage';
import TagValidator from '../validator/TagValidator';
-import Tenant from '../../../../types/Tenant';
-import { TenantComponents } from '../../../../types/Tenant';
import TransactionStorage from '../../../../storage/mongodb/TransactionStorage';
import UserToken from '../../../../types/UserToken';
import UserValidator from '../validator/UserValidator';
@@ -158,8 +157,9 @@ export default class TagService {
action: action
});
}
+ if (filteredRequest.userID) {
// Get User
- const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID,
+ await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID,
Action.READ, ServerAction.TAG_CREATE);
// Default tag?
if (filteredRequest.default) {
@@ -175,6 +175,7 @@ export default class TagService {
filteredRequest.default = true;
}
}
+ }
// Create
const newTag: Tag = {
id: filteredRequest.id.toUpperCase(),
@@ -194,7 +195,7 @@ export default class TagService {
await Logging.logSecurityInfo({
tenantID: req.user.tenantID,
action: action,
- user: req.user, actionOnUser: user,
+ user: req.user,
module: MODULE_NAME, method: 'handleCreateTag',
message: `Tag with ID '${newTag.id}'has been created successfully`,
detailedMessages: { tag: newTag }
@@ -345,10 +346,10 @@ export default class TagService {
// Check and Get Tag
const tag = await UtilsService.checkAndGetTagAuthorization(req.tenant, req.user, filteredRequest.id, Action.UPDATE, action,
filteredRequest, { withNbrTransactions: true, withUser: true }, true);
- // Get User
- const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID,
+ if (filteredRequest.userID) {
+ await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID,
Action.READ, ServerAction.TAG_UPDATE);
- // Check visualID uniqueness
+ } // Check visualID uniqueness
if (tag.visualID !== filteredRequest.visualID) {
const tagVisualID = await TagStorage.getTagByVisualID(req.tenant, filteredRequest.visualID);
if (tagVisualID) {
@@ -380,17 +381,17 @@ export default class TagService {
formerTagDefault = tag.default;
}
// Clear User's default Tag
- if (filteredRequest.default && !formerTagUserID && (tag.default !== filteredRequest.default)) {
+ if (filteredRequest.default && !formerTagUserID && filteredRequest.userID && (tag.default !== filteredRequest.default)) {
await TagStorage.clearDefaultUserTag(req.tenant, filteredRequest.userID);
}
// Check default Tag existence
- if (!filteredRequest.default) {
+ if (!filteredRequest.default && filteredRequest.userID) {
// Check if another one is the default
const defaultTag = await TagStorage.getDefaultUserTag(req.tenant, filteredRequest.userID, {
issuer: true,
});
// Force default Tag
- if (!defaultTag) {
+ if (!defaultTag && filteredRequest.userID) {
filteredRequest.default = true;
}
}
@@ -415,13 +416,14 @@ export default class TagService {
action: action,
module: MODULE_NAME, method: 'handleUpdateTag',
message: `Tag with ID '${tag.id}' has been updated successfully`,
- user: req.user, actionOnUser: user,
+ user: req.user,
detailedMessages: { tag: tag }
});
res.json(Constants.REST_RESPONSE_SUCCESS);
next();
}
+
// eslint-disable-next-line @typescript-eslint/require-await
public static async handleImportTags(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check auth

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -3,6 +3,7 @@ import { Car, CarCatalog, CarType } from '../../../../types/Car';
import ChargingStation, { ChargePoint, Voltage } from '../../../../types/ChargingStation';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
+import Tenant, { TenantComponents } from '../../../../types/Tenant';
import User, { UserRole, UserStatus } from '../../../../types/User';
import AppAuthError from '../../../../exception/AppAuthError';
@@ -32,8 +33,6 @@ import SiteAreaStorage from '../../../../storage/mongodb/SiteAreaStorage';
import SiteStorage from '../../../../storage/mongodb/SiteStorage';
import Tag from '../../../../types/Tag';
import TagStorage from '../../../../storage/mongodb/TagStorage';
-import Tenant, { TenantComponents } from '../../../../types/Tenant';
-
import { TransactionInErrorType } from '../../../../types/InError';
import UserStorage from '../../../../storage/mongodb/UserStorage';
import UserToken from '../../../../types/UserToken';
@@ -1390,16 +1389,6 @@ export default class UtilsService {
if (!tag.description) {
tag.description = `Tag ID '${tag.id}'`;
}
- // Check user ID
- if (!tag.userID) {
- throw new AppError({
- source: Constants.CENTRAL_SERVER,
- errorCode: HTTPError.GENERAL_ERROR,
- message: 'User ID is mandatory',
- module: MODULE_NAME, method: 'checkIfUserTagIsValid',
- user: req.user.id
- });
- }
// Check user activation
if (!Utils.objectHasProperty(tag, 'active')) {
throw new AppError({

MODIFY
src/storage/mongodb/TagStorage.ts
src/storage/mongodb/TagStorage.ts
@@ -22,7 +22,7 @@ export default class TagStorage {
DatabaseUtils.checkTenantObject(tenant);
const tagMDB = {
_id: tag.id,
- userID: DatabaseUtils.convertToObjectID(tag.userID),
+ userID: tag.userID ? DatabaseUtils.convertToObjectID(tag.userID) : null,
issuer: Utils.convertToBoolean(tag.issuer),
active: Utils.convertToBoolean(tag.active),
default: Utils.convertToBoolean(tag.default),

User not mandatory in tags