Change car type to enum
MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -264,17 +264,15 @@ export default class CarService {
});
// If Basic user, auto assign the car to him
if (Authorizations.isBasic(req.user)) {
- let newUserCar: UserCar = {
+ const defaultCar = await CarStorage.getDefaultCar(req.user.tenantID, req.user.id);
+ const newUserCar: UserCar = {
carID: newCar.id,
userID: req.user.id,
- type: filteredRequest.isPrivate ? 'private' : 'company',
+ type: filteredRequest.type,
+ default: !defaultCar,
createdBy: { id: req.user.id },
createdOn: new Date()
} as UserCar;
- const defaultCar = await CarStorage.getDefaultCar(req.user.tenantID, req.user.id);
- if (!defaultCar) {
- newUserCar = Object.assign(newUserCar, { default: true });
- }
newUserCar.id = await CarStorage.saveUserCar(req.user.tenantID, newUserCar);
if (defaultCar && filteredRequest.isDefault) {
await CarStorage.updateDefaultCar(req.user.tenantID, newCar.id, req.user.id);

MODIFY
src/server/rest/service/security/CarSecurity.ts
src/server/rest/service/security/CarSecurity.ts
@@ -155,7 +155,7 @@ export default class CarSecurity {
licensePlate: sanitize(request.licensePlate),
carCatalogID: Utils.convertToInt(sanitize(request.carCatalogID)),
forced: UtilsSecurity.filterBoolean(request.forced),
- isPrivate: UtilsSecurity.filterBoolean(request.isPrivate),
+ type: sanitize(request.type),
isDefault: UtilsSecurity.filterBoolean(request.isDefault)
};
}

MODIFY
src/types/Car.ts
src/types/Car.ts
@@ -122,6 +122,12 @@ export interface CarCatalog extends CreatedUpdatedProps {
imagesHash?: string;
}
+export enum CarType {
+ PRIVATE = 'private',
+ COMPANY = 'company',
+ POOL = 'pool',
+}
+
export interface Car extends CreatedUpdatedProps {
id: string;
vin: string;
@@ -139,7 +145,7 @@ export interface UserCar extends CreatedUpdatedProps {
user?: User;
car?: Car;
default?: boolean;
- type?: string;
+ type?: CarType;
}
export interface CarMaker {

MODIFY
src/types/requests/HttpCarRequest.ts
src/types/requests/HttpCarRequest.ts
+import { CarType } from '../Car';
import HttpDatabaseRequest from './HttpDatabaseRequest';
export interface HttpCarCatalogsRequest extends HttpDatabaseRequest {
Search?: string;
@@ -21,7 +22,7 @@ export interface HttpCarCreateRequest {
licensePlate: string;
carCatalogID: number;
forced: boolean;
- isPrivate: boolean;
+ type: CarType;
isDefault: boolean;
}

Change car type to enum