Skip to content

Commit 6ba95bb

Browse files
committed
feat(structures): adding migration to set default value to structure sms schedule
1 parent a7c3bb1 commit 6ba95bb

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { MigrationInterface } from "typeorm";
2+
import { domifaConfig } from "../config";
3+
import { structureRepository } from "../database";
4+
import { StructureSmsParams } from "@domifa/common";
5+
import { appLogger } from "../util";
6+
7+
export class ManualMigration1755033531923 implements MigrationInterface {
8+
name: string = "ManualMigration1755033531923";
9+
public async up(): Promise<void> {
10+
const emailList = [];
11+
if (
12+
domifaConfig().envId === "prod" ||
13+
domifaConfig().envId === "preprod" ||
14+
domifaConfig().envId === "local"
15+
) {
16+
appLogger.info("Starting migration: Updating structures SMS schedules");
17+
18+
const structures = await structureRepository.find({
19+
select: {
20+
id: true,
21+
email: true,
22+
sms: true,
23+
},
24+
});
25+
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+
}
52+
appLogger.info("Migration end");
53+
appLogger.info("Liste des emails des structures à contacter:", emailList);
54+
}
55+
}
56+
57+
public async down(): Promise<void> {
58+
if (
59+
domifaConfig().envId === "prod" ||
60+
domifaConfig().envId === "preprod" ||
61+
domifaConfig().envId === "local"
62+
) {
63+
appLogger.info("No down migration can be applied");
64+
}
65+
}
66+
}

packages/backend/src/database/entities/structure/StructureTable.typeorm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class StructureTable
131131
@Column({
132132
type: "jsonb",
133133
default: () =>
134-
`'{"senderName": null, "senderDetails": null, "enabledByDomifa": true, "enabledByStructure": false, "schedule" :{ "monday": false "tuesday": false "wednesday": false "thursday": false "friday": false } }'`,
134+
`'{"senderName": null, "senderDetails": null, "enabledByDomifa": true, "enabledByStructure": false, "schedule" :{ "monday": false "tuesday": true "wednesday": false "thursday": true "friday": false } }'`,
135135
})
136136
public sms: StructureSmsParams;
137137

packages/backend/src/modules/structures/controllers/structures.controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ export class StructuresController {
9898

9999
const values = Object.values(structureSmsDto.schedule);
100100
const checkedDaysCount = values.filter((value) => value === true).length;
101+
102+
if (checkedDaysCount === 0) {
103+
return res
104+
.status(HttpStatus.BAD_REQUEST)
105+
.json({ message: "MUST_SET_AT_LEAST_1_DAY" });
106+
}
107+
101108
if (checkedDaysCount > 2) {
102109
return res
103110
.status(HttpStatus.BAD_REQUEST)

packages/backend/src/modules/structures/dto/schedule.dto.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { IsBoolean } from "class-validator";
2-
32
export class ScheduleDto {
43
@IsBoolean()
54
monday: boolean;

packages/frontend/src/app/modules/structures/components/structures-sms-form/structures-sms-form.component.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ export class StructuresSmsFormComponent implements OnInit, OnDestroy {
181181
if (count === 0) {
182182
return { atLeastOneDay: true };
183183
}
184-
185184
if (count > 2) {
186185
return { moreThanTwoDays: true };
187186
}

0 commit comments

Comments
 (0)