Var naming, use of Utils.isEmptyArray(), add comment, code readability
MODIFY
src/migration/tasks/CleanUpCarUsersWithDeletedUsersTask.ts
src/migration/tasks/CleanUpCarUsersWithDeletedUsersTask.ts
@@ -25,28 +25,28 @@ export default class CleanUpCarUsersWithDeletedUsersTask extends MigrationTask {
// Get users cars
const carUsersMDB = await global.database.getCollection<any>(tenant.id, 'carusers').find().toArray();
if (!Utils.isEmptyArray(carUsersMDB)) {
- for (const carUser of carUsersMDB) {
+ for (const carUserMDB of carUsersMDB) {
// Get the User
- const user = await UserStorage.getUser(tenant.id, carUser.userID.toString());
+ const user = await UserStorage.getUser(tenant.id, carUserMDB.userID.toString());
if (!user) {
// Owner?
- if (carUser.owner) {
+ if (carUserMDB.owner) {
// Get the Private Cars of the Deleted User
- const car = await CarStorage.getCar(tenant.id, carUser.carID.toString(), { type: CarType.PRIVATE });
- if (car) {
+ const privateCar = await CarStorage.getCar(tenant.id, carUserMDB.carID.toString(), { type: CarType.PRIVATE });
+ if (privateCar) {
// Delete All Private Cars assignment
- carUsersCounter += await CarStorage.deleteCarUsersByCarID(tenant.id, car.id);
+ carUsersCounter += await CarStorage.deleteCarUsersByCarID(tenant.id, privateCar.id);
// Delete Private Car
- await CarStorage.deleteCar(tenant.id, car.id);
+ await CarStorage.deleteCar(tenant.id, privateCar.id);
carsCounter++;
} else {
// Delete Car assignment
- await CarStorage.deleteCarUser(tenant.id, carUser._id);
+ await CarStorage.deleteCarUser(tenant.id, carUserMDB._id);
carUsersCounter++;
}
} else {
// Delete Car assignment
- await CarStorage.deleteCarUser(tenant.id, carUser._id);
+ await CarStorage.deleteCarUser(tenant.id, carUserMDB._id);
carUsersCounter++;
}
}

MODIFY
src/migration/tasks/CleanUpLogicallyDeletedUsersTask.ts
src/migration/tasks/CleanUpLogicallyDeletedUsersTask.ts
@@ -22,36 +22,35 @@ export default class CleanUpLogicallyDeletedUsersTask extends MigrationTask {
async migrateTenant(tenant: Tenant): Promise<void> {
// Get logically deleted users
- const userCollection = global.database.getCollection<any>(tenant.id, 'users');
- const users = await userCollection.find({
+ const deletedUsersMDB = await global.database.getCollection<any>(tenant.id, 'users').find({
deleted : true
}).toArray();
-
- let counter = 0, userID: string;
- for (const user of users) {
- userID = user._id.toString();
+ let counter = 0;
+ for (const deletedUserMDB of deletedUsersMDB) {
+ const userID = deletedUserMDB._id.toString();
const carUsers = await CarStorage.getCarUsers(tenant.id, { userIDs : [userID] }, Constants.DB_PARAMS_MAX_LIMIT);
- if (carUsers.count > 0) {
+ if (!Utils.isEmptyArray(carUsers.result)) {
for (const carUser of carUsers.result) {
// Owner?
if (carUser.owner) {
- // Private ?
- const car = await CarStorage.getCar(tenant.id, carUser.carID, { type: CarType.PRIVATE });
- if (car) {
- // Delete All Users Car
- await CarStorage.deleteCarUsersByCarID(tenant.id, car.id);
- // Delete Car
- await CarStorage.deleteCar(tenant.id, car.id);
+ // Get the Private Cars of the Deleted User
+ const privateCar = await CarStorage.getCar(tenant.id, carUser.carID, { type: CarType.PRIVATE });
+ if (privateCar) {
+ // Delete All Private Cars assignment
+ await CarStorage.deleteCarUsersByCarID(tenant.id, privateCar.id);
+ // Delete Private Car
+ await CarStorage.deleteCar(tenant.id, privateCar.id);
} else {
- // Delete User Car
+ // Delete Car assignment
await CarStorage.deleteCarUser(tenant.id, carUser.id);
}
} else {
- // Delete User Car
+ // Delete Car assignment
await CarStorage.deleteCarUser(tenant.id, carUser.id);
}
}
}
+ // Delete the User physically
await UserStorage.deleteUser(tenant.id, userID);
counter++;
}

Var naming, use of Utils.isEmptyArray(), add comment, code readability