Simplified default car handling
MODIFY
src/assets/configs-aws
src/assets/configs-aws
-Subproject commit 5fb081a12dbcb2cc22acd9046a397eb9138898a5
+Subproject commit 87046b40a0dd48b26421bb2728ad70ef4e278e5c

MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -264,19 +264,20 @@ export default class CarService {
});
// If Basic user, auto assign the car to him
if (Authorizations.isBasic(req.user)) {
- const defaultCar = await CarStorage.getDefaultCar(req.user.tenantID, req.user.id);
+ // Clear default car
+ if (filteredRequest.isDefault) {
+ await CarStorage.clearDefaultUserCar(req.user.tenantID, req.user.id);
+ }
+ // Assign to User
const newUserCar: UserCar = {
carID: newCar.id,
userID: req.user.id,
type: filteredRequest.type,
- default: !defaultCar,
+ default: filteredRequest.isDefault,
createdBy: { id: req.user.id },
createdOn: new Date()
} as UserCar;
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/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -515,17 +515,6 @@ 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> {
@@ -659,8 +648,8 @@ export default class CarStorage {
};
}
- public static async updateDefaultCar(tenantID: string, carID: string, userID: string): Promise<void> {
- const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'updateDefaultCar');
+ 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(
@@ -671,14 +660,6 @@ export default class CarStorage {
{
$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 });
+ Logging.traceEnd(MODULE_NAME, 'clearDefaultCar', uniqueTimerID, { userID });
}
}

Simplified default car handling