1- import { BadRequestException , Logger , Module } from '@nestjs/common' ;
1+ import { HttpException , Logger , Module } from '@nestjs/common' ;
22import { MongooseModule } from '@nestjs/mongoose' ;
33import { IdentitiesSchema , Identities } from './_schemas/identities.schema' ;
44import { IdentitiesService } from './identities.service' ;
@@ -17,55 +17,54 @@ import { ValidationConfigException, ValidationSchemaException } from '~/_common/
1717 inject : [ IdentitiesValidationService ] ,
1818 useFactory : ( validationService : IdentitiesValidationService ) => {
1919 const schema = IdentitiesSchema ;
20- // Pre validation hook
21- // This hook is used to validate the additionalFields
22- // If the validation fails and the state is TO_CREATE, the state is set to TO_COMPLETE
23- // Else the error is thrown
20+
2421 schema . pre ( 'validate' , async function ( next ) {
25- const commonLogMessage = `additionalFields error for ${ this . inetOrgPerson . cn } ` ;
22+ const logPrefix = `Validation [ ${ this . inetOrgPerson . cn } ]: ` ;
2623 try {
27- Logger . log ( `additionalFields validation start for ${ this . inetOrgPerson . cn } ` ) ;
28-
24+ Logger . log ( `${ logPrefix } Starting additionalFields validation.` ) ;
2925 await validationService . validate ( this . additionalFields ) ;
30-
31- Logger . log ( `additionalFields validation end for ${ this . inetOrgPerson . cn } ` ) ;
26+ Logger . log ( `${ logPrefix } AdditionalFields validation successful.` ) ;
3227 } catch ( error ) {
33- // If the error is a ValidationConfigException, we throw it
34- if ( error instanceof ValidationConfigException ) {
35- Logger . error ( commonLogMessage , error . getResponse ( ) ) ;
36- throw new ValidationConfigException ( error ) ;
37- }
38-
39- // If the error is a ValidationSchemaException and the state is TO_CREATE, we set it to TO_COMPLETE
40- // Else we throw it
41- if ( error instanceof ValidationSchemaException ) {
42- Logger . error ( commonLogMessage , error . getResponse ( ) ) ;
43- if ( this . state === IdentityState . TO_CREATE ) {
44- Logger . warn ( commonLogMessage , 'Setting state to TO_COMPLETE' ) ;
45- this . state = IdentityState . TO_COMPLETE ;
46- } else throw new ValidationSchemaException ( error ) ;
47- } else {
48- Logger . error ( commonLogMessage , error ) ;
49- throw new Error ( error ) ;
50- }
28+ handleValidationError ( error , this , logPrefix ) ;
5129 }
5230 next ( ) ;
5331 } ) ;
5432
55- // Pre save hook
56- // This hook is used to set the state to TO_SYNC if the state is TO_CREATE
5733 schema . pre ( 'save' , async function ( next ) {
58- if ( this . state === IdentityState . TO_CREATE ) this . state = IdentityState . TO_VALIDATE ;
34+ if ( this . state === IdentityState . TO_CREATE ) {
35+ this . state = IdentityState . TO_VALIDATE ;
36+ }
5937 next ( ) ;
6038 } ) ;
6139
6240 return schema ;
6341 } ,
64- //TODO: Si le schema est save, pousser dans la queue de sync
6542 } ,
6643 ] ) ,
6744 ] ,
6845 providers : [ IdentitiesService , IdentitiesValidationService ] ,
6946 controllers : [ IdentitiesController ] ,
7047} )
7148export class IdentitiesModule { }
49+
50+ function handleValidationError ( error : Error | HttpException , identity : Identities , logPrefix : string ) {
51+ if ( error instanceof ValidationConfigException ) {
52+ Logger . error ( `${ logPrefix } Validation config error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
53+ throw new ValidationConfigException ( error . getPayload ( ) ) ;
54+ }
55+
56+ if ( error instanceof ValidationSchemaException ) {
57+ Logger . warn ( `${ logPrefix } Validation schema error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
58+ if ( identity . state === IdentityState . TO_CREATE ) {
59+ Logger . warn ( `${ logPrefix } State set to TO_COMPLETE.` ) ;
60+ identity . state = IdentityState . TO_COMPLETE ;
61+ identity . additionalFields . validations = error . getValidations ( ) ;
62+ } else {
63+ Logger . error ( `${ logPrefix } Validation schema error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
64+ throw new ValidationSchemaException ( error . getPayload ( ) ) ;
65+ }
66+ } else {
67+ Logger . error ( `${ logPrefix } Unhandled error: ${ error . message } ` ) ;
68+ throw error ; // Rethrow the original error if it's not one of the handled types.
69+ }
70+ }
0 commit comments