Skip to content

Commit e6592a8

Browse files
committed
Ajouter les modifications du module KeyringsModule
1 parent d8f6609 commit e6592a8

File tree

14 files changed

+331
-14
lines changed

14 files changed

+331
-14
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"schematics:inherit": "nest generate -c @kradihsoy/lt-schematics inherit",
4343
"generate:doc": "npx @compodoc/compodoc -c .compodocrc",
4444
"generate:docServer": "npx @compodoc/compodoc -s -c .compodocrc -d ./documentation",
45+
"console": "node dist/console",
4546
"prepare": "husky"
4647
},
4748
"dependencies": {
@@ -69,6 +70,7 @@
6970
"ioredis": "^5.3.2",
7071
"loglevel": "^1.8.1",
7172
"mongoose": "^8.0.2",
73+
"nest-commander": "^3.12.5",
7274
"nest-winston": "^1.9.4",
7375
"nestjs-request-context": "^3.0.0",
7476
"passport": "^0.6.0",
@@ -96,6 +98,7 @@
9698
"@swc/core": "^1.4.6",
9799
"@types/cookie-parser": "^1.4.6",
98100
"@types/express": "^4.17.17",
101+
"@types/inquirer": "^9.0.7",
99102
"@types/jest": "^29.5.2",
100103
"@types/node": "^18.0.0",
101104
"@types/passport": "^1.0.16",

src/_common/abstracts/abstract.service.schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
219219
{ _id },
220220
{
221221
...update,
222+
$setOnInsert: {
223+
'metadata.createdBy': this.request?.user?.username || 'anonymous',
224+
'metadata.createdAt': new Date(),
225+
},
222226
$set: {
223227
...(update?.$set || {}),
224228
'metadata.lastUpdatedBy': this.request?.user?.username || 'anonymous',
@@ -270,6 +274,10 @@ export abstract class AbstractServiceSchema extends AbstractService implements S
270274
filter,
271275
{
272276
...update,
277+
$setOnInsert: {
278+
'metadata.createdBy': this.request?.user?.username || 'anonymous',
279+
'metadata.createdAt': new Date(),
280+
},
273281
$set: {
274282
...(update?.$set || {}),
275283
'metadata.lastUpdatedBy': this.request?.user?.username || 'anonymous',

src/cli/agents.command.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { ModuleRef } from '@nestjs/core';
2+
import { Command, CommandRunner, InquirerService, Question, QuestionSet, SubCommand } from 'nest-commander';
3+
import { AgentsCreateDto } from '~/core/agents/_dto/agents.dto';
4+
import { AgentsService } from '~/core/agents/agents.service';
5+
6+
@QuestionSet({ name: 'agent-create-questions' })
7+
export class AgentCreateQuestions {
8+
@Question({
9+
message: 'Username ?',
10+
name: 'username',
11+
})
12+
parseUsername(val: string) {
13+
return val;
14+
}
15+
16+
@Question({
17+
message: 'Email ?',
18+
name: 'email',
19+
})
20+
parseEmail(val: string) {
21+
return val;
22+
}
23+
24+
@Question({
25+
message: 'Password ?',
26+
name: 'password',
27+
type: 'password',
28+
})
29+
parsePassword(val: string) {
30+
return val;
31+
}
32+
}
33+
34+
@SubCommand({ name: 'create' })
35+
export class AgentsCreateCommand extends CommandRunner {
36+
public constructor(
37+
protected moduleRef: ModuleRef,
38+
private readonly inquirer: InquirerService,
39+
private readonly agentsService: AgentsService,
40+
) {
41+
super();
42+
}
43+
44+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
45+
async run(inputs: string[], options: any): Promise<void> {
46+
const agent = await this.inquirer.ask<AgentsCreateDto>('agent-create-questions', undefined);
47+
try {
48+
await this.agentsService.create(agent);
49+
console.log('Agent created successfully');
50+
} catch (error) {
51+
console.error('Error creating agent', error);
52+
}
53+
}
54+
}
55+
56+
@Command({ name: 'agents', arguments: '<task>', subCommands: [AgentsCreateCommand] })
57+
export class AgentsCommand extends CommandRunner {
58+
public constructor(protected moduleRef: ModuleRef) {
59+
super();
60+
}
61+
62+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
63+
async run(inputs: string[], options: any): Promise<void> {}
64+
}

src/cli/cli.module.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Module } from '@nestjs/common';
2+
import { AgentCreateQuestions, AgentsCommand } from './agents.command';
3+
import { ConfigModule, ConfigService } from '@nestjs/config';
4+
import { BullModule } from '@nestjs/bullmq';
5+
import { RedisOptions } from 'ioredis';
6+
import { RedisModule } from '@nestjs-modules/ioredis';
7+
import { MongooseModule, MongooseModuleOptions } from '@nestjs/mongoose';
8+
import mongoose from 'mongoose';
9+
import config, { MongoosePlugin } from '~/config';
10+
import { AgentsModule } from '~/core/agents/agents.module';
11+
import { KeyringsCommand, KeyringsCreateQuestions } from './keyrings.command';
12+
import { KeyringsModule } from '~/core/keyrings/keyrings.module';
13+
14+
@Module({
15+
imports: [
16+
ConfigModule.forRoot({
17+
isGlobal: true,
18+
load: [config],
19+
}),
20+
MongooseModule.forRootAsync({
21+
imports: [ConfigModule],
22+
inject: [ConfigService],
23+
useFactory: async (config: ConfigService) => {
24+
for (const plugin of config.get<MongoosePlugin[]>('mongoose.plugins')) {
25+
import(plugin.package).then((plugin) => {
26+
mongoose.plugin(plugin.default ? plugin.default : plugin, plugin.options);
27+
});
28+
}
29+
return {
30+
...config.get<MongooseModuleOptions>('mongoose.options'),
31+
uri: config.get<string>('mongoose.uri'),
32+
};
33+
},
34+
}),
35+
RedisModule.forRootAsync({
36+
imports: [ConfigModule],
37+
inject: [ConfigService],
38+
useFactory: async (config: ConfigService) => ({
39+
config: {
40+
...config.get<RedisOptions>('ioredis.options'),
41+
url: config.get<string>('ioredis.uri'),
42+
},
43+
}),
44+
}),
45+
BullModule.forRootAsync({
46+
imports: [ConfigModule],
47+
inject: [ConfigService],
48+
useFactory: async (configService: ConfigService) => ({
49+
connection: {
50+
host: configService.get('ioredis.host'),
51+
port: configService.get('ioredis.port'),
52+
},
53+
}),
54+
}),
55+
AgentsModule,
56+
KeyringsModule,
57+
],
58+
providers: [
59+
...AgentsCommand.registerWithSubCommands(),
60+
...KeyringsCommand.registerWithSubCommands(),
61+
AgentCreateQuestions,
62+
KeyringsCreateQuestions,
63+
],
64+
})
65+
export class CliModule {}

src/cli/keyrings.command.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { ModuleRef } from '@nestjs/core';
2+
import { Command, CommandRunner, InquirerService, Question, QuestionSet, SubCommand } from 'nest-commander';
3+
import { KeyringsCreateDto } from '~/core/keyrings/_dto/keyrings.dto';
4+
import { KeyringsService } from '~/core/keyrings/keyrings.service';
5+
6+
@QuestionSet({ name: 'keyrings-create-questions' })
7+
export class KeyringsCreateQuestions {
8+
@Question({
9+
message: 'Name ?',
10+
name: 'name',
11+
})
12+
parseName(val: string) {
13+
return val;
14+
}
15+
}
16+
17+
@SubCommand({ name: 'create' })
18+
export class KeyringsCreateCommand extends CommandRunner {
19+
public constructor(
20+
protected moduleRef: ModuleRef,
21+
private readonly inquirer: InquirerService,
22+
private readonly keyringsService: KeyringsService,
23+
) {
24+
super();
25+
}
26+
27+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
28+
async run(inputs: string[], options: any): Promise<void> {
29+
const keyring = await this.inquirer.ask<KeyringsCreateDto>('keyrings-create-questions', undefined);
30+
try {
31+
const key = await this.keyringsService.create(keyring);
32+
console.log('Keyring created successfully', key.toJSON());
33+
} catch (error) {
34+
console.error('Error creating keyring', error);
35+
}
36+
}
37+
}
38+
39+
@Command({ name: 'keyrings', arguments: '<task>', subCommands: [KeyringsCreateCommand] })
40+
export class KeyringsCommand extends CommandRunner {
41+
public constructor(protected moduleRef: ModuleRef) {
42+
super();
43+
}
44+
45+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
46+
async run(inputs: string[], options: any): Promise<void> {}
47+
}

src/console.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { CommandFactory } from 'nest-commander';
2+
import configInstance from '~/config';
3+
import { getLogLevel } from './_common/functions/get-log-level';
4+
import { CliModule } from './cli/cli.module';
5+
import { InternalLogger } from './core/logger/internal.logger';
6+
7+
(async () => {
8+
try {
9+
const cfg = configInstance();
10+
const logger = new InternalLogger({
11+
logLevel: getLogLevel(cfg?.application?.logLevel),
12+
mongoose: cfg?.mongoose,
13+
});
14+
const app = await CommandFactory.runWithoutClosing(CliModule, {
15+
logger,
16+
errorHandler: (err) => {
17+
console.error(err);
18+
process.exit(1);
19+
},
20+
});
21+
await app.close();
22+
} catch (err) {
23+
console.error(err);
24+
process.exit(255);
25+
}
26+
process.exit(0);
27+
})();

src/contextId.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { INestApplicationContext } from '@nestjs/common';
2+
import { ModuleRef } from '@nestjs/core/injector/module-ref';
3+
import { ContextIdFactory } from '@nestjs/core';
4+
5+
// eslint-disable-next-line
6+
export const seedRequestContextId = <T extends { user?: any }>(app: INestApplicationContext | ModuleRef, req?: T) => {
7+
const contextId = ContextIdFactory.create();
8+
app.registerRequestByContextId(
9+
{
10+
...req,
11+
user: {
12+
_id: '000000000000000000000000',
13+
...req?.user,
14+
},
15+
},
16+
contextId,
17+
);
18+
return contextId;
19+
};

src/core/agents/agents.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { MongooseModule } from '@nestjs/mongoose';
33
import { AgentsSchema, Agents } from '~/core/agents/_schemas/agents.schema';
44
import { AgentsService } from './agents.service';
55
import { AgentsController } from './agents.controller';
6+
// import { AgentsCommand } from '~/cli/agents.command';
67

78
@Module({
89
imports: [
@@ -13,7 +14,7 @@ import { AgentsController } from './agents.controller';
1314
},
1415
]),
1516
],
16-
providers: [AgentsService],
17+
providers: [AgentsService /* ...AgentsCommand.registerWithSubCommands() */],
1718
controllers: [AgentsController],
1819
exports: [AgentsService],
1920
})

src/core/keyrings/_dto/keyrings.dto.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { IsDateString, IsIP, IsMongoId, IsNotEmpty, IsString } from 'class-valid
33
import { CustomFieldsDto } from '~/_common/abstracts/dto/custom-fields.dto';
44

55
export class KeyringsCreateDto extends CustomFieldsDto {
6+
@IsString()
7+
@IsNotEmpty()
8+
@ApiProperty()
9+
public name: string;
10+
611
@IsString()
712
@IsNotEmpty()
813
@ApiProperty()

src/core/keyrings/_schemas/keyrings.schema.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export type KeyringsDocument = Keyrings & Document;
66

77
@Schema({ versionKey: false })
88
export class Keyrings extends AbstractSchema {
9+
@Prop({
10+
type: String,
11+
unique: true,
12+
})
13+
public name: string;
14+
915
@Prop({
1016
type: String,
1117
unique: true,
@@ -14,6 +20,7 @@ export class Keyrings extends AbstractSchema {
1420

1521
@Prop({
1622
type: [String],
23+
default: ['0.0.0.0/0'],
1724
})
1825
public allowedNetworks?: string[];
1926

0 commit comments

Comments
 (0)