Skip to content

Commit 8c3ea82

Browse files
committed
Refactor lifecycle migration logic to improve clarity and consistency
1 parent cb02115 commit 8c3ea82

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/migrations/jobs/1752765731-lifecycle.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { Logger } from "@nestjs/common"
22
import { InjectConnection, InjectModel } from "@nestjs/mongoose"
33
import { Connection, Model } from "mongoose"
4+
import { isNumber } from "radash"
45

56
export default class LifeCycle1752765731 {
67
private readonly logger = new Logger(LifeCycle1752765731.name)
78

8-
public constructor(
9-
@InjectConnection() private mongo: Connection,
10-
) {
9+
public constructor(@InjectConnection() private mongo: Connection) {
1110
}
1211

1312
public async up(): Promise<void> {
@@ -20,11 +19,15 @@ export default class LifeCycle1752765731 {
2019
const identities = await this.mongo.collection('identities').find();
2120

2221
for await (const identity of identities) {
23-
const lifecycle = identity.lifecycle;
22+
let lifecycle = identity.lifecycle;
2423

25-
if (typeof lifecycle === 'number') {
24+
if (typeof lifecycle === 'number' || isNumber(lifecycle)) {
2625
this.logger.log(`Migrating lifecycle for identity ${identity._id}`);
2726

27+
if (typeof lifecycle === 'string') {
28+
lifecycle = parseInt(lifecycle, 10);
29+
}
30+
2831
switch (lifecycle) {
2932
case 0:
3033
identity.lifecycle = 'P'; // PROVISIONAL
@@ -54,7 +57,7 @@ export default class LifeCycle1752765731 {
5457
{ _id: identity._id },
5558
{
5659
$set: {
57-
'inetOrgPerson.lifecycle': identity.lifecycle,
60+
lifecycle: identity.lifecycle,
5861
ignoreLifecycle: false,
5962
}
6063
},

0 commit comments

Comments
 (0)