Add Car Owner Attribute
MODIFY
src/server/rest/service/CarService.ts
src/server/rest/service/CarService.ts
@@ -182,7 +182,7 @@ export default class CarService {
}
public static async handleCarCreate(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
- let newCar: Car;
+ let newCar: Car, isNewCar = false;
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.CAR,
Action.CREATE, Entity.CAR, MODULE_NAME, 'handleCarCreate');
@@ -255,6 +255,7 @@ export default class CarService {
createdOn: new Date()
} as Car;
newCar.id = await CarStorage.saveCar(req.user.tenantID, newCar);
+ isNewCar = true;
}
Logging.logSecurityInfo({
tenantID: req.user.tenantID,
@@ -275,7 +276,8 @@ export default class CarService {
userID: req.user.id,
default: filteredRequest.isDefault,
createdBy: { id: req.user.id },
- createdOn: new Date()
+ createdOn: new Date(),
+ owner: isNewCar
} as UserCar;
newUserCar.id = await CarStorage.saveUserCar(req.user.tenantID, newUserCar);
}

MODIFY
src/server/rest/service/security/CarSecurity.ts
src/server/rest/service/security/CarSecurity.ts
@@ -247,7 +247,8 @@ export default class CarSecurity {
user: userCar.user,
carID: userCar.carID,
userID: userCar.userID,
- default: userCar.default
+ default: userCar.default,
+ owner: userCar.owner
};
return filteredUserCar;
}
@@ -286,6 +287,7 @@ export default class CarSecurity {
usersCar.push({
userID: userCar.user.id,
default: userCar.default,
+ owner: userCar.owner
} as UserCar);
}
}

MODIFY
src/storage/mongodb/CarStorage.ts
src/storage/mongodb/CarStorage.ts
@@ -490,7 +490,8 @@ export default class CarStorage {
userID: Utils.convertToObjectID(userCarToSave.userID),
carID: Utils.convertToObjectID(userCarToSave.carID),
default: userCarToSave.default,
- active: true
+ active: true,
+ owner: (userCarToSave.owner === true ? true : false)
};
// Add Last Changed/Created props
DatabaseUtils.addLastChangedCreatedProps(userCarMDB, userCarToSave);
@@ -571,6 +572,10 @@ export default class CarStorage {
if (userCar.default) {
await CarStorage.clearDefaultUserCar(tenantID, userCar.userID);
}
+ if (userCar.owner !== userCarDB.owner) {
+ await CarStorage.clearUserCarOwner(tenantID, carID);
+ }
+ userCarDB.owner = userCar.owner;
userCarDB.active = true;
userCarDB.default = userCar.default;
await CarStorage.saveUserCar(tenantID, userCarDB);
@@ -580,6 +585,9 @@ export default class CarStorage {
if (userCar.default) {
await CarStorage.clearDefaultUserCar(tenantID, userCar.userID);
}
+ if (userCar.owner) {
+ await CarStorage.clearUserCarOwner(tenantID, carID);
+ }
userCar.carID = carID;
userCar.lastChangedOn = new Date();
userCar.active = true;
@@ -760,6 +768,21 @@ export default class CarStorage {
Logging.traceEnd(MODULE_NAME, 'clearDefaultCar', uniqueTimerID, { userID });
}
+ public static async clearUserCarOwner(tenantID: string, carID: string): Promise<void> {
+ const uniqueTimerID = Logging.traceStart(MODULE_NAME, 'clearDefaultCar');
+ await Utils.checkTenant(tenantID);
+
+ await global.database.getCollection<any>(tenantID, 'userscars').updateMany(
+ {
+ carID: Utils.convertToObjectID(carID),
+ owner: true
+ },
+ {
+ $set: { owner: false }
+ });
+ Logging.traceEnd(MODULE_NAME, 'clearUserCarOwner', uniqueTimerID, { carID });
+ }
+
public static async getUserCarByCarUser(tenantID: string,
carID: string = Constants.UNKNOWN_STRING_ID, userID: string = Constants.UNKNOWN_STRING_ID,
projectFields?: string[]): Promise<UserCar> {

MODIFY
src/types/Car.ts
src/types/Car.ts
@@ -147,6 +147,7 @@ export interface UserCar extends CreatedUpdatedProps {
car?: Car;
default?: boolean;
active?: boolean;
+ owner?: boolean;
}
export interface CarMaker {

Add Car Owner Attribute