feat(migration): created a script to initialize preferences and modules access
This commit is contained in:
parent
491bd7022c
commit
74e16d7960
1159
package-lock.json
generated
1159
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
|
@ -66,6 +66,18 @@ model Customers {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```graphql
|
||||||
|
### `Structure de prisma/schema.prisma-legaacy`
|
||||||
|
generator client {
|
||||||
|
provider = "prisma-client-js"
|
||||||
|
output = "../node_modules/@prisma/client-legacy"
|
||||||
|
}
|
||||||
|
|
||||||
|
datasource db {
|
||||||
|
provider = "postgresql"
|
||||||
|
url = env("DATABASE_URL_LEGACY")
|
||||||
|
}
|
||||||
|
```
|
||||||
***
|
***
|
||||||
|
|
||||||
### `Conventions`
|
### `Conventions`
|
||||||
|
|
@ -91,6 +103,8 @@ model Customers {
|
||||||
`Générer le client JS`
|
`Générer le client JS`
|
||||||
npx prisma generate
|
npx prisma generate
|
||||||
|
|
||||||
|
npx prisma generate schema=prisma-legacy/schema.prisma
|
||||||
|
|
||||||
`Créer ou appliquer une migration`
|
`Créer ou appliquer une migration`
|
||||||
npx prisma migrate dev --name <titre_de_migration>
|
npx prisma migrate dev --name <titre_de_migration>
|
||||||
|
|
||||||
|
|
|
||||||
63
scripts/init-preferences-access.ts
Normal file
63
scripts/init-preferences-access.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
import { PrismaClient } from "@prisma/client"
|
||||||
|
|
||||||
|
const prisma = new PrismaClient({});
|
||||||
|
|
||||||
|
const admin_list: number[] = [2, 6, 8, 20, 27, 28, 43, 46, 60];
|
||||||
|
|
||||||
|
export const initializePreferences = async () => {
|
||||||
|
console.log('start of preferences and Module Access initialization')
|
||||||
|
for (let id = 1; id <= 61; id++) {
|
||||||
|
const user = await prisma.employees.findUnique({
|
||||||
|
where: { id },
|
||||||
|
select: { user_id: true },
|
||||||
|
});
|
||||||
|
if (!user) {
|
||||||
|
console.log(`user_id for employee ${id} not found`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
console.log(`user_id for employee ${id} found`);
|
||||||
|
|
||||||
|
await prisma.preferences.create({
|
||||||
|
data: {
|
||||||
|
display_language: 'fr-Fr',
|
||||||
|
is_dark_mode: null,
|
||||||
|
is_employee_list_grid: false,
|
||||||
|
is_lefty_mode: false,
|
||||||
|
is_timesheet_approval_grid: false,
|
||||||
|
notifications: true,
|
||||||
|
user_id: user.user_id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(`Preferences for employee ${id} initiated`);
|
||||||
|
|
||||||
|
await prisma.userModuleAccess.create({
|
||||||
|
data: {
|
||||||
|
user_id: user.user_id,
|
||||||
|
dashboard: true,
|
||||||
|
employee_list: true,
|
||||||
|
employee_management: false,
|
||||||
|
personal_profile: true,
|
||||||
|
timesheets: true,
|
||||||
|
timesheets_approval: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(`Module Access for employee ${id} initiated`);
|
||||||
|
|
||||||
|
if (id in admin_list) {
|
||||||
|
console.log(`employee ${id} is and admin`)
|
||||||
|
await prisma.userModuleAccess.update({
|
||||||
|
where: { user_id: user.user_id },
|
||||||
|
data: {
|
||||||
|
dashboard: true,
|
||||||
|
employee_list: true,
|
||||||
|
employee_management: true,
|
||||||
|
personal_profile: true,
|
||||||
|
timesheets: true,
|
||||||
|
timesheets_approval: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
console.log(`Module Access for employee ${id} updated`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
// src/scripts/init-preferences.ts
|
|
||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
|
||||||
|
|
||||||
type UserSummary = {
|
|
||||||
id: string; // UUID
|
|
||||||
email: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
console.log('➡️ Initialisation des préférences utilisateurs…');
|
|
||||||
|
|
||||||
// 1. Récupérer tous les users
|
|
||||||
const users = (await prisma.users.findMany({
|
|
||||||
select: {
|
|
||||||
id: true,
|
|
||||||
email: true,
|
|
||||||
},
|
|
||||||
})) as UserSummary[];
|
|
||||||
|
|
||||||
console.log(`➡️ ${users.length} users trouvés dans la DB`);
|
|
||||||
|
|
||||||
// 2. Récupérer toutes les préférences existantes
|
|
||||||
const existingPrefs = await prisma.preferences.findMany({
|
|
||||||
select: {
|
|
||||||
user_id: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const userIdsWithPrefs = new Set(existingPrefs.map((p) => p.user_id));
|
|
||||||
|
|
||||||
console.log(`➡️ ${existingPrefs.length} users ont déjà des préférences`);
|
|
||||||
|
|
||||||
// 3. Filtrer les users qui n'ont pas encore de preferences
|
|
||||||
const usersWithoutPrefs = users.filter((u) => !userIdsWithPrefs.has(u.id));
|
|
||||||
|
|
||||||
console.log(`➡️ ${usersWithoutPrefs.length} users n'ont pas encore de préférences`);
|
|
||||||
|
|
||||||
if (usersWithoutPrefs.length === 0) {
|
|
||||||
console.log('✅ Rien à faire, toutes les préférences sont déjà créées.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Préparer les entrées pour createMany
|
|
||||||
const prefsToCreate = usersWithoutPrefs.map((u) => ({
|
|
||||||
user_id: u.id,
|
|
||||||
// tous les autres champs prendront leurs valeurs par défaut (0)
|
|
||||||
}));
|
|
||||||
|
|
||||||
// 5. Insertion en batch
|
|
||||||
const result = await prisma.preferences.createMany({
|
|
||||||
data: prefsToCreate,
|
|
||||||
skipDuplicates: true, // sécurité si jamais le script est relancé
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`✅ ${result.count} préférences créées dans la DB`);
|
|
||||||
}
|
|
||||||
|
|
||||||
main()
|
|
||||||
.catch((err) => {
|
|
||||||
console.error('❌ Erreur pendant l’initialisation des préférences', err);
|
|
||||||
process.exit(1);
|
|
||||||
})
|
|
||||||
.finally(async () => {
|
|
||||||
await prisma.$disconnect();
|
|
||||||
});
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
// // import { extractOldTimesheets } from "scripts/migrate-timesheets";
|
|
||||||
// // import { extractOldExpenses } from "scripts/migrate-expenses";
|
|
||||||
// // import { extractOldShifts } from "scripts/migrate-shifts";
|
|
||||||
// import { Injectable } from "@nestjs/common";
|
// import { Injectable } from "@nestjs/common";
|
||||||
|
|
||||||
// @Injectable()
|
// @Injectable()
|
||||||
|
|
@ -8,14 +6,18 @@
|
||||||
// constructor() {}
|
// constructor() {}
|
||||||
|
|
||||||
// async migrateTimesheets() {
|
// async migrateTimesheets() {
|
||||||
// // extractOldTimesheets();
|
// extractOldTimesheets();
|
||||||
// };
|
// };
|
||||||
|
|
||||||
// async migrateShifts() {
|
// async migrateShifts() {
|
||||||
// // extractOldShifts();
|
// extractOldShifts();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// async migrateExpenses() {
|
// async migrateExpenses() {
|
||||||
// // extractOldExpenses();
|
// extractOldExpenses();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// async initializePreferences() {
|
||||||
|
// initializePreferences();
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
Loading…
Reference in New Issue
Block a user