|
| 1 | +import { Injectable, OnModuleInit, Inject } from "@nestjs/common"; |
| 2 | +import { Collections } from "@src/modules/common/enum/database.collection.enum"; |
| 3 | +import { LimitArea, Plan } from "@src/modules/common/models/plan.model"; |
| 4 | +import { Team } from "@src/modules/common/models/team.model"; |
| 5 | +import { Db } from "mongodb"; |
| 6 | + |
| 7 | +@Injectable() |
| 8 | +export class UpdatePlanTestflowScheduleMigration implements OnModuleInit { |
| 9 | + private hasRun = false; |
| 10 | + constructor(@Inject("DATABASE_CONNECTION") private readonly db: Db) {} |
| 11 | + async onModuleInit(): Promise<void> { |
| 12 | + if (this.hasRun) return; |
| 13 | + try { |
| 14 | + console.log( |
| 15 | + "\x1b[36m[Nest]\x1b[0m \x1b[36mStarting UpdatePlanTestflowScheduleMigration...\x1b[0m", |
| 16 | + ); |
| 17 | + |
| 18 | + const planCollection = this.db.collection<Plan>(Collections.PLAN); |
| 19 | + const teamCollection = this.db.collection<Team>(Collections.TEAM); |
| 20 | + |
| 21 | + const plans = await planCollection.find().toArray(); |
| 22 | + const teams = await teamCollection.find().toArray(); |
| 23 | + |
| 24 | + let updatedPlanCount = 0; |
| 25 | + let updatedTeamCount = 0; |
| 26 | + |
| 27 | + // --- Update PLANS --- |
| 28 | + for (const plan of plans) { |
| 29 | + const limits = plan.limits; |
| 30 | + let desiredValue = 3; |
| 31 | + |
| 32 | + switch (plan.name) { |
| 33 | + case "Standard": |
| 34 | + desiredValue = 10; |
| 35 | + break; |
| 36 | + case "Professional": |
| 37 | + desiredValue = 25; |
| 38 | + break; |
| 39 | + case "Community": |
| 40 | + default: |
| 41 | + desiredValue = 3; |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + const existingValue = limits.testflowScheduleRun?.value; |
| 46 | + const fieldExists = limits.testflowScheduleRun !== undefined; |
| 47 | + |
| 48 | + if (!fieldExists || existingValue !== desiredValue) { |
| 49 | + await planCollection.updateOne( |
| 50 | + { _id: plan._id }, |
| 51 | + { |
| 52 | + $set: { |
| 53 | + "limits.testflowScheduleRun": { |
| 54 | + area: LimitArea.TESTFLOW_SCHEDULE_RUN, |
| 55 | + value: desiredValue, |
| 56 | + }, |
| 57 | + updatedAt: new Date(), |
| 58 | + updatedBy: "system", |
| 59 | + }, |
| 60 | + }, |
| 61 | + ); |
| 62 | + updatedPlanCount++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // --- Update TEAM PLANS --- |
| 67 | + for (const team of teams) { |
| 68 | + const teamPlan = team.plan; |
| 69 | + if (!teamPlan) continue; |
| 70 | + |
| 71 | + let desiredValue = 3; |
| 72 | + switch (teamPlan.name) { |
| 73 | + case "Standard": |
| 74 | + desiredValue = 10; |
| 75 | + break; |
| 76 | + case "Professional": |
| 77 | + desiredValue = 25; |
| 78 | + break; |
| 79 | + case "Community": |
| 80 | + default: |
| 81 | + desiredValue = 3; |
| 82 | + break; |
| 83 | + } |
| 84 | + |
| 85 | + const existingValue = teamPlan.limits?.testflowScheduleRun?.value; |
| 86 | + const fieldExists = teamPlan.limits?.testflowScheduleRun !== undefined; |
| 87 | + |
| 88 | + if (!fieldExists || existingValue !== desiredValue) { |
| 89 | + await teamCollection.updateOne( |
| 90 | + { _id: team._id }, |
| 91 | + { |
| 92 | + $set: { |
| 93 | + "plan.limits.testflowScheduleRun": { |
| 94 | + area: LimitArea.TESTFLOW_SCHEDULE_RUN, |
| 95 | + value: desiredValue, |
| 96 | + }, |
| 97 | + updatedAt: new Date(), |
| 98 | + updatedBy: "system", |
| 99 | + }, |
| 100 | + }, |
| 101 | + ); |
| 102 | + updatedTeamCount++; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + console.log( |
| 107 | + `\x1b[32m[Nest]\x1b[0m \x1b[33m${updatedPlanCount}\x1b[0m plans updated and \x1b[33m${updatedTeamCount}\x1b[0m teams updated with correct 'testflowScheduleRun' values.\x1b[0m`, |
| 108 | + ); |
| 109 | + |
| 110 | + this.hasRun = true; |
| 111 | + } catch (error) { |
| 112 | + console.error( |
| 113 | + "\x1b[31m[Nest] Error during UpdatePlanTestflowScheduleMigration:\x1b[0m", |
| 114 | + error, |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments