|
1 |
| -import { MigrationInterface } from "typeorm"; |
| 1 | +import { MigrationInterface, QueryRunner } from "typeorm"; |
2 | 2 | import { domifaConfig } from "../config";
|
3 |
| -import { structureRepository } from "../database"; |
4 |
| -import { StructureSmsParams } from "@domifa/common"; |
5 | 3 | import { appLogger } from "../util";
|
6 | 4 |
|
7 | 5 | export class ManualMigration1755033531923 implements MigrationInterface {
|
8 | 6 | name: string = "ManualMigration1755033531923";
|
9 |
| - public async up(): Promise<void> { |
10 |
| - const emailList = []; |
| 7 | + public async up(queryRunner: QueryRunner): Promise<void> { |
11 | 8 | if (
|
12 | 9 | domifaConfig().envId === "prod" ||
|
13 | 10 | domifaConfig().envId === "preprod" ||
|
14 | 11 | domifaConfig().envId === "local"
|
15 | 12 | ) {
|
16 | 13 | appLogger.info("Starting migration: Updating structures SMS schedules");
|
| 14 | + await queryRunner.query(` |
| 15 | + UPDATE structure |
| 16 | + SET sms = sms || '{"schedule": {"monday": false, "tuesday": true, "wednesday": false, "thursday": true, "friday": false}}'::jsonb |
| 17 | + WHERE |
| 18 | + (sms->>'enabledByStructure')::boolean = TRUE |
| 19 | + AND (sms->'schedule'->>'friday')::boolean = FALSE |
| 20 | + AND (sms->'schedule'->>'monday')::boolean = FALSE |
| 21 | + AND (sms->'schedule'->>'thursday')::boolean = FALSE |
| 22 | + AND (sms->'schedule'->>'tuesday')::boolean = FALSE |
| 23 | + AND (sms->'schedule'->>'wednesday')::boolean = FALSE; |
| 24 | + `); |
17 | 25 |
|
18 |
| - const structures = await structureRepository.find({ |
19 |
| - select: { |
20 |
| - id: true, |
21 |
| - email: true, |
22 |
| - sms: true, |
23 |
| - }, |
24 |
| - }); |
| 26 | + const count = await queryRunner.query(` |
| 27 | + SELECT COUNT(*) FROM structure |
| 28 | + WHERE |
| 29 | + (sms->>'enabledByStructure')::boolean = TRUE |
| 30 | + AND (sms->'schedule'->>'friday')::boolean = FALSE |
| 31 | + AND (sms->'schedule'->>'monday')::boolean = FALSE |
| 32 | + AND (sms->'schedule'->>'thursday')::boolean = FALSE |
| 33 | + AND (sms->'schedule'->>'tuesday')::boolean = FALSE |
| 34 | + AND (sms->'schedule'->>'wednesday')::boolean = FALSE; |
| 35 | + `); |
25 | 36 |
|
26 |
| - const structuresWithoutSmsSchedule = structures.filter( |
27 |
| - (s) => !Object.values(s.sms?.schedule).some((value) => value === true) |
28 |
| - ); |
29 |
| - |
30 |
| - // Process each structure record |
31 |
| - for (const structure of structuresWithoutSmsSchedule) { |
32 |
| - const smsParams: StructureSmsParams = { |
33 |
| - ...structure.sms, |
34 |
| - schedule: { |
35 |
| - ...structure.sms.schedule, |
36 |
| - tuesday: true, |
37 |
| - thursday: true, |
38 |
| - }, |
39 |
| - }; |
40 |
| - |
41 |
| - await structureRepository.update( |
42 |
| - { |
43 |
| - id: structure.id, |
44 |
| - }, |
45 |
| - { |
46 |
| - sms: smsParams, |
47 |
| - } |
48 |
| - ); |
49 |
| - |
50 |
| - emailList.push(structure.email); |
51 |
| - } |
| 37 | + console.log(count); |
52 | 38 | appLogger.info("Migration end");
|
53 |
| - appLogger.info("Liste des emails des structures à contacter:", emailList); |
54 | 39 | }
|
55 | 40 | }
|
56 | 41 |
|
|
0 commit comments