Skip to content

Commit 8dc1ccf

Browse files
committed
Refactor validation logic for employeeNumbers in inetOrgPerson.part.ts
1 parent a7e7758 commit 8dc1ccf

File tree

4 files changed

+78
-9
lines changed

4 files changed

+78
-9
lines changed

src/management/identities/_schemas/_parts/inetOrgPerson.part.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ export class inetOrgPerson extends Document {
5050
validate: [
5151
{
5252
validator: (employeeNumbers: string[]) => {
53-
console.log(employeeNumbers);
54-
employeeNumbers.every((employeeNumber) => /[A-Za-z0-9_-]+/.test(employeeNumber));
53+
if (!Array.isArray(employeeNumbers)) return false;
54+
55+
return employeeNumbers.every((employeeNumber) => /[A-Za-z0-9_-]+/.test(employeeNumber));
5556
},
5657
message: 'EmployeeNumber invalide.',
5758
},

src/management/identities/identities.controller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
UploadedFile,
1515
UseInterceptors,
1616
} from '@nestjs/common';
17-
import {ApiOperation, ApiParam, ApiResponse, ApiTags, PartialType} from '@nestjs/swagger';
17+
import { ApiOperation, ApiParam, ApiResponse, ApiTags, PartialType } from '@nestjs/swagger';
1818
import {
1919
FilterOptions,
2020
FilterSchema,
@@ -47,7 +47,7 @@ import { join } from 'node:path';
4747
import { omit } from 'radash';
4848
import { TransformersFilestorageService } from '~/core/filestorage/_services/transformers-filestorage.service';
4949
import { Public } from '~/_common/decorators/public.decorator';
50-
import {FusionDto} from "~/management/identities/_dto/fusion.dto";
50+
import { FusionDto } from "~/management/identities/_dto/fusion.dto";
5151

5252
@ApiTags('management/identities')
5353
@Controller('identities')
@@ -91,10 +91,10 @@ export class IdentitiesController extends AbstractController {
9191
body.inetOrgPerson.employeeType = 'LOCAL';
9292
}
9393
if (!body.inetOrgPerson.cn) {
94-
body.inetOrgPerson.cn = body.inetOrgPerson.sn.toUpperCase() + ' ' + body.inetOrgPerson.givenName;
94+
body.inetOrgPerson.cn = `${(body.inetOrgPerson.sn || '').toUpperCase()} ${body.inetOrgPerson.givenName}`;
9595
}
9696
if (!body.inetOrgPerson.displayName) {
97-
body.inetOrgPerson.displayName = body.inetOrgPerson.givenName + ' ' + body.inetOrgPerson.sn.toUpperCase();
97+
body.inetOrgPerson.displayName = body.inetOrgPerson.givenName + ' ' + (body.inetOrgPerson.sn || '').toUpperCase();
9898
}
9999
const data = await this._service.create<Identities>(body);
100100
// If the state is TO_COMPLETE, the identity is created but additional fields are missing or invalid
@@ -361,7 +361,7 @@ export class IdentitiesController extends AbstractController {
361361
});
362362
await this.transformerService.transform(mime, res, data, stream, parent);
363363
}
364-
364+
365365
@Get('duplicates')
366366
@ApiOperation({ summary: 'Renvoie la liste des doublons supposés' })
367367
public async getDoublons(@Res() res: Response): Promise<Response> {

src/migrations.service.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,55 @@ import { InjectModel } from '@nestjs/mongoose'
33
import { readdir } from 'fs/promises'
44
import { Model } from 'mongoose'
55
import { glob } from 'glob'
6+
import readline from 'readline'
7+
8+
let stdinBuffer = [];
9+
10+
function hijackStdinWrite(loader) {
11+
const originalStdinWrite = process.stdin.write;
12+
13+
process.stdin.write = (chunk, callback): boolean => {
14+
stdinBuffer.push({ chunk, callback });
15+
stopLoader(loader);
16+
replayStdinBuffer();
17+
startLoader(loader);
18+
19+
return true;
20+
};
21+
}
22+
23+
function restoreStdinWrite() {
24+
delete process.stdin.write;
25+
}
26+
27+
// Fonction pour rejouer les écritures dans stdin
28+
function replayStdinBuffer() {
29+
while (stdinBuffer.length > 0) {
30+
const { chunk, encoding, callback } = stdinBuffer.shift();
31+
process.stdout.write(chunk, encoding, callback);
32+
}
33+
}
34+
35+
function startLoader(message) {
36+
const spinnerFrames = ['-', '\\', '|', '/'];
37+
let currentFrame = 0;
38+
39+
const loaderInterval = setInterval(() => {
40+
readline.cursorTo(process.stdout, 0);
41+
process.stdout.write(`${spinnerFrames[currentFrame]} ${message}`);
42+
currentFrame = (currentFrame + 1) % spinnerFrames.length;
43+
}, 100);
44+
45+
hijackStdinWrite(loaderInterval);
46+
47+
return loaderInterval;
48+
}
49+
50+
function stopLoader(loaderInterval) {
51+
clearInterval(loaderInterval);
52+
readline.cursorTo(process.stdout, 0);
53+
process.stdout.clearLine(0);
54+
}
655

756
@Injectable()
857
export class MigrationsService implements OnModuleInit {
@@ -13,9 +62,13 @@ export class MigrationsService implements OnModuleInit {
1362
}
1463

1564
public async runMigrations() {
65+
const loader = startLoader('Chargement en cours...');
66+
1667
try {
1768
await this.loadMigrationsFiles()
1869
} catch { }
70+
71+
stopLoader(loader);
1972
}
2073

2174
private async loadMigrationsFiles() {
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
import { Logger } from "@nestjs/common"
2+
13
export default class EmployeeNumber1616027432511 {
24
// public constructor(@InjectConnection() private connection: Connection) {
35

46
// }
57

68
public async up(): Promise<void> {
7-
console.log('EmployeeNumber1616027432511 up')
9+
Logger.log('EmployeeNumber1616027432511 up')
10+
11+
return new Promise((resolve) => {
12+
const interval = setInterval(() => {
13+
Logger.log('EmployeeNumber1616027432511 up in progress')
14+
}, 1_000)
15+
16+
setTimeout(() => {
17+
clearInterval(interval)
18+
Logger.log('EmployeeNumber1616027432511 up done')
19+
20+
resolve()
21+
}, 5_000)
22+
})
823
}
924

1025
public async down(): Promise<void> {
11-
console.log('EmployeeNumber1616027432511 down')
26+
Logger.log('EmployeeNumber1616027432511 down')
1227
}
1328
}

0 commit comments

Comments
 (0)