File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
apps/api/src/migrations/jobs Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ import { Logger } from "@nestjs/common"
2+ import { InjectConnection , InjectModel } from "@nestjs/mongoose"
3+ import { Connection , Model } from "mongoose"
4+ import { isNumber } from "radash"
5+
6+ export default class AgentsRoles1772806842 {
7+ private readonly logger = new Logger ( AgentsRoles1772806842 . name )
8+
9+ public constructor ( @InjectConnection ( ) private mongo : Connection ) {
10+ }
11+
12+ public async up ( ) : Promise < void > {
13+ this . logger . log ( 'AgentsRoles1772806842 up started' )
14+
15+ await this . _migrateAgentsRolesToDefaultAdminValue ( )
16+ }
17+
18+ private async _migrateAgentsRolesToDefaultAdminValue ( ) : Promise < void > {
19+ const agents = await this . mongo . collection ( 'agents' ) . find ( ) ;
20+
21+ for await ( const agent of agents ) {
22+ let roles = agent . roles ;
23+
24+ if ( ! roles || roles . length === 0 && agent . metadata . createdAt ?. getTime ( ) < 1772806842 ) {
25+ this . logger . log ( `Migrating role for agent ${ agent . _id } ` ) ;
26+
27+
28+ this . mongo . collection ( 'agents' ) . updateOne (
29+ { _id : agent . _id } ,
30+ {
31+ $set : {
32+ roles : [ 'admin' ] ,
33+ }
34+ } ,
35+ ) ;
36+ }
37+ }
38+
39+ this . logger . log ( 'Migration terminée avec succès' ) ;
40+ }
41+ }
You can’t perform that action at this time.
0 commit comments