Added Attributes : default car and car type
ADD
ev-server
+Subproject commit 0075cb84e616834e3f38d122574555e9e14acfd3

MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -250,6 +250,7 @@ export default class CarService {
carCatalogID: filteredRequest.carCatalogID,
licensePlate: filteredRequest.licensePlate,
vin: filteredRequest.vin,
+ type: filteredRequest.isPrivate ? 'private' : 'company',
createdBy: { id: req.user.id },
createdOn: new Date()
} as Car;
@@ -264,13 +265,20 @@ export default class CarService {
});
// If Basic user, auto assign the car to him
if (Authorizations.isBasic(req.user)) {
- const newUserCar: UserCar = {
+ let newUserCar: UserCar = {
carID: newCar.id,
userID: req.user.id,
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);
+ }
}
// Ok
res.json(Object.assign({ id: newCar.id }, Constants.REST_RESPONSE_SUCCESS));

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

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -9,6 +9,7 @@ import DbParams from '../../types/database/DbParams';
import Logging from '../../utils/Logging';
import { ObjectID } from 'mongodb';
import Utils from '../../utils/Utils';
+import { type } from 'os';
const MODULE_NAME = 'CarStorage';
@@ -462,7 +463,8 @@ export default class CarStorage {
_id: carToSave.id ? Utils.convertToObjectID(carToSave.id) : new ObjectID(),
vin: carToSave.vin,
licensePlate: carToSave.licensePlate,
- carCatalogID: Utils.convertToInt(carToSave.carCatalogID)
+ carCatalogID: Utils.convertToInt(carToSave.carCatalogID),
+ type: carToSave.type
};
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(carMDB, carToSave);
@@ -487,6 +489,7 @@ export default class CarStorage {
_id: userCarToSave.id ? Utils.convertToObjectID(userCarToSave.id) : new ObjectID(),
userID: Utils.convertToObjectID(userCarToSave.userID),
carID: Utils.convertToObjectID(userCarToSave.carID),
+ default: userCarToSave.default
};
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(userCarMDB, userCarToSave);
@@ -504,7 +507,7 @@ export default class CarStorage {
public static async getCar(tenantID: string, carID: string = Constants.UNKNOWN_STRING_ID, projectFields?: string[]): Promise<Car> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getCar');
- // Query single Site
+ // Query single Car
const carsMDB = await CarStorage.getCars(tenantID,
{ carIDs: [carID] },
Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
@@ -512,12 +515,23 @@ export default class CarStorage {
return carsMDB.count > 0 ? carsMDB.result[0] : null;
}
+ public static async getDefaultCar(tenantID: string, userID: string = Constants.UNKNOWN_STRING_ID, projectFields?: string[]): Promise<Car> {
+ // Debug
+ const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getDefaultCar');
+ // Query single Car
+ const carsMDB = await CarStorage.getCars(tenantID,
+ { userIDs: [userID], defaultCar: true },
+ Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
+ Logging.traceEnd(MODULE_NAME, 'getDefaultCar', uniqueTimerID, { userID });
+ return carsMDB.count > 0 ? carsMDB.result[0] : null;
+ }
+
public static async getCarByVinLicensePlate(tenantID: string,
licensePlate: string = Constants.UNKNOWN_STRING_ID, vin: string = Constants.UNKNOWN_STRING_ID,
withUsers?: boolean, projectFields?: string[]): Promise<Car> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getCarByVinLicensePlate');
- // Query single Site
+ // Query single Car
const carsMDB = await CarStorage.getCars(tenantID,
{ licensePlate: licensePlate, vin: vin, withUsers: withUsers },
Constants.DB_PARAMS_SINGLE_RECORD, projectFields);
@@ -526,7 +540,7 @@ export default class CarStorage {
}
public static async getCars(tenantID: string,
- params: { search?: string; userIDs?: string[]; carIDs?: string[]; licensePlate?: string; vin?: string; withUsers?: boolean } = {},
+ params: { search?: string; userIDs?: string[]; carIDs?: string[]; licensePlate?: string; vin?: string; withUsers?: boolean; defaultCar?: boolean } = {},
dbParams?: DbParams, projectFields?: string[]): Promise<DataResult<Car>> {
// Debug
const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'getCars');
@@ -564,6 +578,9 @@ export default class CarStorage {
if (params.userIDs && params.userIDs.length > 0) {
filters['userscars.userID'] = { $in: params.userIDs.map((userID) => Utils.convertToObjectID(userID)) };
}
+ if (params.defaultCar) {
+ filters['userscars.default'] = true;
+ }
}
// Filters
if (filters) {
@@ -641,4 +658,27 @@ export default class CarStorage {
result: cars
};
}
+
+ public static async updateDefaultCar(tenantID: string, carID: string, userID: string): Promise<void> {
+ const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'updateDefaultCar');
+ await Utils.checkTenant(tenantID);
+
+ await global.database.getCollection<any>(tenantID, 'userscars').updateMany(
+ {
+ userID: Utils.convertToObjectID(userID),
+ default: true
+ },
+ {
+ $set: { default: false }
+ });
+ await global.database.getCollection<any>(tenantID, 'userscars').updateOne(
+ {
+ carID: Utils.convertToObjectID(carID),
+ userID: Utils.convertToObjectID(userID)
+ },
+ {
+ $set: { default: true }
+ });
+ Logging.traceEnd(MODULE_NAME, 'updateDefaultCar', uniqueTimerID, { carID, userID });
+ }
}

MODIFY
src/types/Car.ts
src/types/Car.ts
@@ -130,6 +130,7 @@ export interface Car extends CreatedUpdatedProps {
carCatalog?: CarCatalog;
userIDs?: string;
users?: User[];
+ type?: string;
}
export interface UserCar extends CreatedUpdatedProps {
@@ -138,6 +139,7 @@ export interface UserCar extends CreatedUpdatedProps {
carID: string;
user?: User;
car?: Car;
+ default?: boolean;
}
export interface CarMaker {

MODIFY
src/types/requests/HttpCarRequest.ts
src/types/requests/HttpCarRequest.ts
@@ -21,6 +21,8 @@ export interface HttpCarCreateRequest {
licensePlate: string;
carCatalogID: number;
forced: boolean;
+ isPrivate: boolean;
+ isDefault: boolean;
}
export interface HttpCarsRequest extends HttpDatabaseRequest {

Added Attributes : default car and car type