Skip to content

Commit 34d4e0c

Browse files
Ajouter des contrôleurs pour la validation des identités et les formulaires JSON des identités
1 parent 4a178ea commit 34d4e0c

File tree

4 files changed

+206
-3
lines changed

4 files changed

+206
-3
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Body, Controller, Get, HttpStatus, Param, Post, Res } from '@nestjs/common';
2+
import { AbstractController } from '~/_common/abstracts/abstract.controller';
3+
import { IdentitiesJsonformsService } from './identities.jsonforms.service';
4+
import { Response } from 'express';
5+
6+
@Controller('management/identities/jsonforms')
7+
export class IdentitiesJsonFormsController extends AbstractController {
8+
constructor(private readonly _service: IdentitiesJsonformsService) {
9+
super();
10+
}
11+
12+
@Post('generateAll')
13+
async generateAll(@Res() res: Response): Promise<any> {
14+
const result = await this._service.generateAll();
15+
return res.status(HttpStatus.OK).json({
16+
statusCode: HttpStatus.OK,
17+
data: result,
18+
});
19+
}
20+
21+
@Post('generate')
22+
async generate(@Res() res: Response, @Body('schema') schema: string | null = null): Promise<any> {
23+
const result = await this._service.generate(schema);
24+
return res.status(HttpStatus.OK).json({
25+
statusCode: HttpStatus.OK,
26+
data: result,
27+
});
28+
}
29+
30+
@Get()
31+
async searchAll(@Res() res: Response): Promise<any> {
32+
const result = await this._service.findAll();
33+
return res.status(HttpStatus.OK).json({
34+
statusCode: HttpStatus.OK,
35+
data: result,
36+
});
37+
}
38+
39+
@Get(':schema')
40+
async search(@Res() res: Response, @Param('schema') schema): Promise<any> {
41+
const result = await this._service.findOne(schema);
42+
return res.status(HttpStatus.OK).json({
43+
statusCode: HttpStatus.OK,
44+
data: result,
45+
});
46+
}
47+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { AbstractService } from '~/_common/abstracts/abstract.service';
3+
import { parse, stringify } from 'yaml';
4+
import { existsSync, readFileSync, writeFileSync, readdirSync } from 'fs';
5+
import Ajv from 'ajv';
6+
import { ValidationConfigException } from '~/_common/errors/ValidationException';
7+
8+
@Injectable()
9+
export class IdentitiesJsonformsService extends AbstractService {
10+
private ajv: Ajv = new Ajv({ allErrors: true });
11+
private validateSchema;
12+
13+
constructor() {
14+
super();
15+
//this.validateSchema = this.ajv.compile(validSchema);
16+
}
17+
18+
async generate(schema: string): Promise<any> {
19+
if (schema) {
20+
console.log(`Generating jsonforms for schema: ${schema}`);
21+
const filePath = `./src/management/identities/validations/_config/${schema}.yml`;
22+
23+
if (!existsSync(filePath)) {
24+
console.log(`File not found: ${filePath}`);
25+
const message = `File not found: ${filePath}`;
26+
throw new ValidationConfigException({ message });
27+
}
28+
29+
const jsonSchema = parse(readFileSync(filePath, 'utf-8'));
30+
31+
// const jsonForm = {
32+
// type: 'VerticalLayout',
33+
// elements: Object.keys(jsonSchema.properties).map((key) => {
34+
// return {
35+
// type: 'Control',
36+
// label: key,
37+
// scope: `#/properties/${key}`,
38+
// options: {
39+
// required: jsonSchema.required && jsonSchema.required.includes(key),
40+
// },
41+
// };
42+
// }),
43+
// };
44+
45+
const jsonForm = {
46+
type: 'Group',
47+
label: schema,
48+
elements: Object.keys(jsonSchema.properties)
49+
.reduce((acc, key, index) => {
50+
const groupIndex = Math.floor(index / 3); // Determine the current group based on the index
51+
if (!acc[groupIndex]) {
52+
acc[groupIndex] = {
53+
// Initialize a new HorizontalLayout group if it doesn't exist
54+
type: 'HorizontalLayout',
55+
elements: [],
56+
};
57+
}
58+
59+
acc[groupIndex].elements.push({
60+
// Add the current element to its corresponding group
61+
type: 'Control',
62+
label: key,
63+
scope: `#/properties/${key}`,
64+
options: {
65+
required: jsonSchema.required && jsonSchema.required.includes(key),
66+
showUnfocusedDescription: false,
67+
},
68+
});
69+
70+
return acc;
71+
}, [])
72+
.map((group) => group), // Flatten the structure
73+
};
74+
75+
writeFileSync(`./src/management/identities/jsonforms/_config/${schema}.ui.yml`, stringify(jsonForm));
76+
return jsonForm;
77+
}
78+
}
79+
80+
async generateAll(): Promise<any> {
81+
const configPath = './src/management/identities/validations/_config';
82+
const files = readdirSync(configPath);
83+
for (const file of files) {
84+
this.generate(file);
85+
}
86+
return files.length;
87+
}
88+
89+
async findAll(): Promise<any> {
90+
this.logger.debug(['findAll', JSON.stringify(Object.values(arguments))].join(' '));
91+
const configPath = './src/management/identities/jsonforms/_config';
92+
const files = readdirSync(configPath);
93+
const result = [];
94+
for (const file of files) {
95+
const filePath = `${configPath}/${file}`;
96+
const data = parse(readFileSync(filePath, 'utf-8'));
97+
const key = file.replace('.ui.yml', '');
98+
result.push({ [key]: data });
99+
}
100+
return [result, files.length];
101+
}
102+
103+
async findOne(schema): Promise<any> {
104+
this.logger.debug(['findOne', JSON.stringify(Object.values(arguments))].join(' '));
105+
const filePath = `./src/management/identities/jsonforms/_config/${schema}.ui.yml`;
106+
if (!existsSync(filePath)) {
107+
const message = `File not found: ${filePath}`;
108+
throw new ValidationConfigException({ message });
109+
}
110+
111+
return parse(readFileSync(filePath, 'utf-8'));
112+
}
113+
}

src/management/identities/validations/identities.validation.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Body, Controller, HttpStatus, Post, Res } from '@nestjs/common';
1+
import { Body, Controller, Get, HttpStatus, Param, Post, Res } from '@nestjs/common';
22
import { AbstractController } from '~/_common/abstracts/abstract.controller';
33
import { ApiTags } from '@nestjs/swagger';
44
import { Response } from 'express';
@@ -7,7 +7,7 @@ import { AdditionalFieldsPart } from '../_schemas/_parts/additionalFields.part.s
77
import { IdentitiesValidationService } from './identities.validation.service';
88

99
@ApiTags('management')
10-
@Controller('identities/validation')
10+
@Controller('management/identities/validation')
1111
export class IdentitiesValidationController extends AbstractController {
1212
constructor(protected readonly _service: IdentitiesValidationService) {
1313
super();
@@ -43,4 +43,22 @@ export class IdentitiesValidationController extends AbstractController {
4343
});
4444
}
4545
}
46+
47+
@Get()
48+
async searchAll(@Res() res: Response): Promise<any> {
49+
const result = await this._service.findAll();
50+
return res.status(HttpStatus.OK).json({
51+
statusCode: HttpStatus.OK,
52+
data: result,
53+
});
54+
}
55+
56+
@Get(':schema')
57+
async search(@Res() res: Response, @Param('schema') schema): Promise<any> {
58+
const result = await this._service.findOne(schema);
59+
return res.status(HttpStatus.OK).json({
60+
statusCode: HttpStatus.OK,
61+
data: result,
62+
});
63+
}
4664
}

src/management/identities/validations/identities.validation.service.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable, Logger } from '@nestjs/common';
22
import { parse } from 'yaml';
3-
import { existsSync, readFileSync } from 'fs';
3+
import { existsSync, readFileSync, readdirSync } from 'fs';
44
import { ConfigObjectSchemaDTO } from './_dto/config.dto';
55
import { diff } from 'radash';
66
import { AdditionalFieldsPart } from '../_schemas/_parts/additionalFields.part.schema';
@@ -113,4 +113,29 @@ export class IdentitiesValidationService {
113113
}, {});
114114
}
115115
}
116+
117+
async findAll(): Promise<any> {
118+
this.logger.debug(['findAll', JSON.stringify(Object.values(arguments))].join(' '));
119+
const configPath = './src/management/identities/validations/_config';
120+
const files = readdirSync(configPath);
121+
const result = [];
122+
for (const file of files) {
123+
const filePath = `${configPath}/${file}`;
124+
const data = parse(readFileSync(filePath, 'utf-8'));
125+
const key = file.replace('.yml', '');
126+
result.push({ [key]: data });
127+
}
128+
return [result, files.length];
129+
}
130+
131+
async findOne(schema): Promise<any> {
132+
this.logger.debug(['findOne', JSON.stringify(Object.values(arguments))].join(' '));
133+
const filePath = `./src/management/identities/validations/_config/${schema}.yml`;
134+
if (!existsSync(filePath)) {
135+
const message = `File not found: ${filePath}`;
136+
throw new ValidationConfigException({ message });
137+
}
138+
139+
return parse(readFileSync(filePath, 'utf-8'));
140+
}
116141
}

0 commit comments

Comments
 (0)