Skip to content

Commit b052a18

Browse files
Merge branch 'development' into aakash/fix/testflow-executed-time-issue
2 parents b913a7e + 15c441e commit b052a18

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Injectable, Inject, OnModuleInit } from "@nestjs/common";
2+
import { Db, ObjectId } from "mongodb";
3+
import { Collections } from "@src/modules/common/enum/database.collection.enum";
4+
import { LimitArea } from "@src/modules/common/models/plan.model";
5+
import { ConfigService } from "@nestjs/config";
6+
7+
@Injectable()
8+
export class AddCommunityPlanToTeamsMigration implements OnModuleInit {
9+
private hasRun = false;
10+
11+
constructor(
12+
@Inject("DATABASE_CONNECTION") private readonly db: Db,
13+
private readonly configService: ConfigService,
14+
) {}
15+
16+
async onModuleInit(): Promise<void> {
17+
if (this.hasRun) return;
18+
19+
try {
20+
console.log("Running AddCommunityPlanToTeamsMigration...");
21+
22+
const teamCollection = this.db.collection(Collections.TEAM);
23+
const planCollection = this.db.collection(Collections.PLAN);
24+
25+
// Get the default hub plan name from config
26+
const defaultHubPlan =
27+
this.configService.get<string>("app.defaultHubPlan");
28+
// Fetch the plan from plan collection
29+
const planDoc = await planCollection.findOne({ name: defaultHubPlan });
30+
if (!planDoc) {
31+
console.warn(
32+
`Plan '${defaultHubPlan}' not found in plan collection. Skipping team updates.`,
33+
);
34+
return;
35+
}
36+
const { _id, ...planData } = planDoc;
37+
38+
// Update teams that don't have a plan or where plan.id is missing
39+
const query = {
40+
$or: [
41+
{ plan: { $exists: false } },
42+
{ plan: null },
43+
{ "plan.id": { $exists: false } },
44+
],
45+
};
46+
47+
const update = {
48+
$set: {
49+
plan: {
50+
id: _id,
51+
...planData,
52+
},
53+
},
54+
};
55+
56+
const result = await teamCollection.updateMany(query, update);
57+
console.log(
58+
`Updated ${result.modifiedCount} team(s) with the Community plan.`,
59+
);
60+
61+
this.hasRun = true;
62+
} catch (error) {
63+
console.error("Error during AddCommunityPlanToTeamsMigration:", error);
64+
}
65+
}
66+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"start:migration-superadmin": "ts-node -r tsconfig-paths/register src/migration-superadmin.ts",
3737
"start:migration-email": "ts-node -r tsconfig-paths/register src/migration-emailId.ts",
3838
"start:add-test-request": "ts-node -r tsconfig-paths/register src/add-tests-to-request.ts",
39-
"start:add-testflow-schedule": "ts-node -r tsconfig-paths/register src/update-plan-testflow-schedule.ts"
39+
"start:add-testflow-schedule": "ts-node -r tsconfig-paths/register src/update-plan-testflow-schedule.ts",
40+
"start:add-community-plan-to-teams": "ts-node -r tsconfig-paths/register src/add-community-plan-to-teams.ts"
4041
},
4142
"dependencies": {
4243
"@anthropic-ai/sdk": "^0.54.0",

src/add-community-plan-to-teams.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { NestFactory } from "@nestjs/core";
2+
import { Module, Provider } from "@nestjs/common";
3+
import { MongoClient, Db } from "mongodb";
4+
import { ConfigModule, ConfigService } from "@nestjs/config";
5+
import configuration from "./modules/common/config/configuration";
6+
import { AddCommunityPlanToTeamsMigration } from "migrations/add-community-plan-to-teams.migration";
7+
8+
const databaseProvider: Provider = {
9+
provide: "DATABASE_CONNECTION",
10+
useFactory: async (configService: ConfigService): Promise<Db> => {
11+
const dbUrl = configService.get<string>("db.url");
12+
const dbName = configService.get<string>("db.name");
13+
14+
if (!dbUrl) {
15+
throw new Error("Database URL is not defined in the configuration.");
16+
}
17+
18+
const client = new MongoClient(dbUrl);
19+
await client.connect();
20+
return client.db(dbName);
21+
},
22+
inject: [ConfigService],
23+
};
24+
25+
@Module({
26+
imports: [
27+
ConfigModule.forRoot({
28+
isGlobal: true,
29+
load: [configuration],
30+
}),
31+
],
32+
providers: [databaseProvider, AddCommunityPlanToTeamsMigration],
33+
})
34+
class MigrationModule {}
35+
36+
async function run() {
37+
const app = await NestFactory.createApplicationContext(MigrationModule);
38+
const migration = app.get(AddCommunityPlanToTeamsMigration);
39+
await migration.onModuleInit();
40+
await app.close();
41+
}
42+
43+
run();

0 commit comments

Comments
 (0)