11import { HttpException , Injectable , Logger } from '@nestjs/common' ;
22import { InjectModel } from '@nestjs/mongoose' ;
33import { Identities } from './_schemas/identities.schema' ;
4- import { Document , Model , ModifyResult , Query , QueryOptions , SaveOptions , Types , UpdateQuery } from 'mongoose' ;
4+ import {
5+ Document ,
6+ FilterQuery ,
7+ Model ,
8+ ModifyResult ,
9+ Query ,
10+ QueryOptions ,
11+ SaveOptions ,
12+ Types ,
13+ UpdateQuery ,
14+ } from 'mongoose' ;
515import { AbstractServiceSchema } from '~/_common/abstracts/abstract.service.schema' ;
616import { AbstractSchema } from '~/_common/abstracts/schemas/abstract.schema' ;
717import { IdentitiesValidationService } from './validations/identities.validation.service' ;
818import { ValidationConfigException , ValidationSchemaException } from '~/_common/errors/ValidationException' ;
919import { IdentityState } from './_enums/states.enum' ;
20+ import { construct , crush , omit } from 'radash' ;
21+ import { IdentitiesUpsertDto } from './_dto/identities.dto' ;
1022
1123@Injectable ( )
1224export class IdentitiesService extends AbstractServiceSchema {
@@ -27,56 +39,41 @@ export class IdentitiesService extends AbstractServiceSchema {
2739 }
2840
2941 public async upsert < T extends AbstractSchema | Document > (
30- data ?: any ,
42+ filters : FilterQuery < T > ,
43+ data ?: IdentitiesUpsertDto ,
3144 options ?: QueryOptions < T > ,
3245 ) : Promise < ModifyResult < Query < T , T , any , T > > > {
33- Logger . log ( `Upserting identity: ${ JSON . stringify ( data ) } ` ) ;
34- const logPrefix = `Validation [${ data . inetOrgPerson . cn } ]:` ;
35- // console.log(options);
36- const identity = await this . _model . findOne ( {
37- 'inetOrgPerson.employeeNumber' : data . inetOrgPerson . employeeNumber ,
38- 'inetOrgPerson.employeeType' : data . inetOrgPerson . employeeType ,
46+ this . logger . log ( `Upserting identity with filters ${ JSON . stringify ( filters ) } ` ) ;
47+ const crushedUpdate = crush ( JSON . parse ( JSON . stringify ( omit ( data || { } , [ '$setOnInsert' ] ) ) ) ) ;
48+ const crushedSetOnInsert = crush ( JSON . parse ( JSON . stringify ( data . $setOnInsert || { } ) ) ) ;
49+
50+ data = construct ( {
51+ ...crushedUpdate ,
52+ ...crushedSetOnInsert ,
3953 } ) ;
40- // console.log(identity);
41- if ( ! identity && options . errorOnNotFound ) {
42- this . logger . error ( `${ logPrefix } Identity not found.` ) ;
43- throw new HttpException ( 'Identity not found.' , 404 ) ;
44- }
4554 data . additionalFields . validations = { } ;
55+ const logPrefix = `Validation [${ data . inetOrgPerson . cn } ]:` ;
56+
4657 try {
4758 this . logger . log ( `${ logPrefix } Starting additionalFields validation.` ) ;
4859 const validations = await this . _validation . validate ( data . additionalFields ) ;
4960 this . logger . log ( `${ logPrefix } AdditionalFields validation successful.` ) ;
50- this . logger . log ( `Validations : ${ validations } ` ) ;
51- data . state = IdentityState . TO_VALIDATE ;
61+ this . logger . log ( `Validations : ${ JSON . stringify ( validations ) } ` ) ;
62+ crushedUpdate [ ' state' ] = IdentityState . TO_VALIDATE ;
5263 } catch ( error ) {
5364 data = this . handleValidationError ( error , data , logPrefix ) ;
65+ crushedUpdate [ 'state' ] = data . state ;
66+ crushedUpdate [ 'additionalFields.validations' ] = data . additionalFields . validations ;
5467 }
5568
56- //TODO: ameliorer la logique d'upsert
57- if ( identity ) {
58- this . logger . log ( `${ logPrefix } Identity already exists. Updating.` ) ;
59- data . inetOrgPerson = {
60- ...identity . inetOrgPerson ,
61- ...data . inetOrgPerson ,
62- } ;
63- data . additionalFields . objectClasses = [
64- ...new Set ( [ ...identity . additionalFields . objectClasses , ...data . additionalFields . objectClasses ] ) ,
65- ] ;
66- data . additionalFields . attributes = {
67- ...identity . additionalFields . attributes ,
68- ...data . additionalFields . attributes ,
69- } ;
70- data . additionalFields . validations = {
71- ...identity . additionalFields . validations ,
72- ...data . additionalFields . validations ,
73- } ;
74- }
75-
76- //TODO: rechercher par uid ou employeeNumber + employeeType ?
77- const upsert = await super . upsert ( { 'inetOrgPerson.uid' : data . inetOrgPerson . uid } , data , options ) ;
78- return upsert ;
79- //TODO: add backends service logic here
69+ return await super . upsert (
70+ filters ,
71+ {
72+ $set : crushedUpdate ,
73+ $setOnInsert : crushedSetOnInsert ,
74+ } ,
75+ options ,
76+ ) ;
8077 }
8178
8279 // public async upsert<T extends AbstractSchema | Document>(
@@ -191,15 +188,19 @@ export class IdentitiesService extends AbstractServiceSchema {
191188 return deleted ;
192189 }
193190
194- private handleValidationError ( error : Error | HttpException , identity : Identities , logPrefix : string ) {
191+ private handleValidationError (
192+ error : Error | HttpException ,
193+ identity : Identities | IdentitiesUpsertDto ,
194+ logPrefix : string ,
195+ ) : any {
195196 if ( error instanceof ValidationConfigException ) {
196197 this . logger . error ( `${ logPrefix } Validation config error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
197198 throw new ValidationConfigException ( error . getPayload ( ) ) ;
198199 }
199200
200201 if ( error instanceof ValidationSchemaException ) {
201202 this . logger . warn ( `${ logPrefix } Validation schema error. ${ JSON . stringify ( error . getValidations ( ) ) } ` ) ;
202- identity . additionalFields . validations = error . getValidations ( ) ;
203+ identity . additionalFields . validations = error . getValidations ( ) as any ;
203204 if ( identity . state === IdentityState . TO_CREATE ) {
204205 this . logger . warn ( `${ logPrefix } State set to TO_COMPLETE.` ) ;
205206 identity . state = IdentityState . TO_COMPLETE ;
0 commit comments