1- import { Injectable } from '@nestjs/common' ;
1+ import { HttpException , Injectable , Logger } from '@nestjs/common' ;
22import { InjectModel } from '@nestjs/mongoose' ;
33import { Identities } from './_schemas/identities.schema' ;
44import { Document , Model , ModifyResult , Query , QueryOptions , SaveOptions , Types , UpdateQuery } from 'mongoose' ;
55import { AbstractServiceSchema } from '~/_common/abstracts/abstract.service.schema' ;
66import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema' ;
7+ import { IdentitiesValidationService } from './validations/identities.validation.service' ;
8+ import { ValidationConfigException , ValidationSchemaException } from '~/_common/errors/ValidationException' ;
9+ import { IdentityState } from './_enums/states.enum' ;
710
811@Injectable ( )
912export class IdentitiesService extends AbstractServiceSchema {
10- constructor ( @InjectModel ( Identities . name ) protected _model : Model < Identities > ) {
13+ constructor (
14+ @InjectModel ( Identities . name ) protected _model : Model < Identities > ,
15+ protected readonly _validation : IdentitiesValidationService ,
16+ ) {
1117 super ( ) ;
1218 }
1319
1420 public async create < T extends AbstractSchema | Document > (
1521 data ?: any ,
1622 options ?: SaveOptions ,
1723 ) : Promise < Document < T , any , T > > {
18- // noinspection UnnecessaryLocalVariableJS
1924 const created : Document < T , any , T > = await super . create ( data , options ) ;
20- //TODO: add backends service logic here (TO_SYNC)
2125 return created ;
26+ //TODO: add backends service logic here
27+ }
28+
29+ public async upsert < T extends AbstractSchema | Document > (
30+ data ?: any ,
31+ options ?: QueryOptions < T > ,
32+ ) : Promise < ModifyResult < Query < T , T , any , T > > > {
33+ Logger . log ( `Upserting identity: ${ JSON . stringify ( data ) } ` ) ;
34+ const logPrefix = `Validation [${ data . inetOrgPerson . cn } ]:` ;
35+ data . additionalFields . validations = { } ;
36+ try {
37+ Logger . log ( `${ logPrefix } Starting additionalFields validation.` ) ;
38+ const validations = await this . _validation . validate ( data . additionalFields ) ;
39+ Logger . log ( `${ logPrefix } AdditionalFields validation successful.` ) ;
40+ Logger . log ( `Validations : ${ validations } ` ) ;
41+ data . state = IdentityState . TO_VALIDATE ;
42+ } catch ( error ) {
43+ data = this . handleValidationError ( error , data , logPrefix ) ;
44+ }
45+
46+ //TODO: ameliorer la logique d'upsert
47+ const identity = await this . _model . findOne ( { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } ) ;
48+ if ( identity ) {
49+ Logger . log ( `${ logPrefix } Identity already exists. Updating.` ) ;
50+ data . additionalFields . objectClasses = [
51+ ...new Set ( [ ...identity . additionalFields . objectClasses , ...data . additionalFields . objectClasses ] ) ,
52+ ] ;
53+ data . additionalFields . attributes = {
54+ ...identity . additionalFields . attributes ,
55+ ...data . additionalFields . attributes ,
56+ } ;
57+ data . additionalFields . validations = {
58+ ...identity . additionalFields . validations ,
59+ ...data . additionalFields . validations ,
60+ } ;
61+ }
62+
63+ const upsert = await super . upsert ( { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } , data , options ) ;
64+ return upsert ;
65+ //TODO: add backends service logic here
2266 }
2367
2468 public async update < T extends AbstractSchema | Document > (
@@ -42,4 +86,27 @@ export class IdentitiesService extends AbstractServiceSchema {
4286 //TODO: add backends service logic here (TO_SYNC)
4387 return deleted ;
4488 }
89+
90+ private handleValidationError ( error : Error | HttpException , identity : Identities , logPrefix : string ) {
91+ if ( error instanceof ValidationConfigException ) {
92+ Logger . error ( `${ logPrefix } Validation config error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
93+ throw new ValidationConfigException ( error . getPayload ( ) ) ;
94+ }
95+
96+ if ( error instanceof ValidationSchemaException ) {
97+ Logger . warn ( `${ logPrefix } Validation schema error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
98+ identity . additionalFields . validations = error . getValidations ( ) ;
99+ if ( identity . state === IdentityState . TO_CREATE ) {
100+ Logger . warn ( `${ logPrefix } State set to TO_COMPLETE.` ) ;
101+ identity . state = IdentityState . TO_COMPLETE ;
102+ return identity ;
103+ } else {
104+ Logger . error ( `${ logPrefix } Validation schema error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
105+ throw new ValidationSchemaException ( error . getPayload ( ) ) ;
106+ }
107+ } else {
108+ Logger . error ( `${ logPrefix } Unhandled error: ${ error . message } ` ) ;
109+ throw error ; // Rethrow the original error if it's not one of the handled types.
110+ }
111+ }
45112}
0 commit comments