Send specific error code when user not the oowner
MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -224,7 +224,7 @@ export default class CarService {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.CAR_ALREADY_EXIST_ERROR,
- message: `The Car with VIN: '${filteredRequest.vin}' and License plate: '${filteredRequest.licensePlate}' already exist for this user`,
+ message: `The Car with VIN '${filteredRequest.vin}' and License Plate '${filteredRequest.licensePlate}' already exist for this user`,
user: req.user,
actionOnUser: foundUser,
module: MODULE_NAME, method: 'handleCarCreate'
@@ -239,7 +239,7 @@ export default class CarService {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.CAR_ALREADY_EXIST_ERROR_DIFFERENT_USER,
- message: `The Car with VIN: '${filteredRequest.vin}' and License plate: '${filteredRequest.licensePlate}' already exist with different user`,
+ message: `The Car with VIN '${filteredRequest.vin}' and License Plate '${filteredRequest.licensePlate}' already exist with different user`,
user: req.user,
module: MODULE_NAME, method: 'handleCarCreate'
});
@@ -317,14 +317,24 @@ export default class CarService {
let userCar;
if (Authorizations.isBasic(req.user)) {
userCar = car.usersCar.find((userCarToFind) => userCarToFind.userID.toString() === req.user.id);
- if (!userCar || !userCar.owner) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.ERROR,
+ // Assigned to this user?
+ if (!userCar) {
+ throw new AppError({
+ source: Constants.CENTRAL_SERVER,
+ errorCode: HTTPError.NO_CAR_FOR_USER,
user: req.user,
- action: Action.UPDATE,
- entity: Entity.CAR,
- module: MODULE_NAME,
- method: 'handleUpdateCar'
+ module: MODULE_NAME, method: 'handleUpdateCar',
+ message: `User is not assigned to the car ID '${car.id}' (${Utils.buildCarName(car.carCatalog)})`,
+ });
+ }
+ // Owner?
+ if (!userCar.owner) {
+ throw new AppError({
+ source: Constants.CENTRAL_SERVER,
+ errorCode: HTTPError.USER_NOT_OWNER_OF_THE_CAR,
+ user: req.user,
+ module: MODULE_NAME, method: 'handleUpdateCar',
+ message: `User is not the owner of the car ID '${car.id}' (${Utils.buildCarName(car.carCatalog)})`,
});
}
}

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -774,7 +774,6 @@ export default class CarStorage {
public static async clearDefaultUserCar(tenantID: string, userID: string): Promise<void> {
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'clearDefaultCar');
await Utils.checkTenant(tenantID);
-
await global.database.getCollection<any>(tenantID, 'userscars').updateMany(
{
userID: Utils.convertToObjectID(userID),

MODIFY
src/types/HTTPError.ts
src/types/HTTPError.ts
@@ -41,7 +41,9 @@ export enum HTTPError {
CAR_ALREADY_EXIST_ERROR = 591,
CAR_ALREADY_EXIST_ERROR_DIFFERENT_USER = 592,
+ USER_NOT_OWNER_OF_THE_CAR = 593,
USER_ALREADY_ASSIGNED_TO_CAR = 594,
+ NO_CAR_FOR_USER = 595,
THREE_PHASE_CHARGER_ON_SINGLE_PHASE_SITE_AREA = 539,
}

MODIFY
src/utils/Utils.ts
src/utils/Utils.ts
import { AnalyticsSettingsType, AssetSettingsType, BillingSettingsType, PricingSettingsType, RefundSettingsType, RoamingSettingsType, SettingDBContent, SmartChargingContentType } from '../types/Setting';
-import { Car, CarType } from '../types/Car';
+import { Car, CarCatalog, CarType } from '../types/Car';
import { ChargePointStatus, OCPPProtocol, OCPPVersion } from '../types/ocpp/OCPPServer';
import ChargingStation, { ChargePoint, Connector, ConnectorCurrentLimitSource, CurrentType, StaticLimitAmps } from '../types/ChargingStation';
import User, { UserRole, UserStatus } from '../types/User';
@@ -855,6 +855,21 @@ export default class Utils {
return fullName;
}
+ public static buildCarName(carCatalog: CarCatalog) {
+ let carName: string;
+ if (!carCatalog) {
+ return '-';
+ }
+ carName = carCatalog.vehicleMake;
+ if (carCatalog.vehicleModel) {
+ carName += ` ${carCatalog.vehicleModel}`;
+ }
+ if (carCatalog.vehicleModelVersion) {
+ carName += ` ${carCatalog.vehicleModelVersion}`;
+ }
+ return carName;
+ }
+
// Save the users in file
public static saveFile(filename: string, content: string): void {
// Save

Send specific error code when user not the oowner