@@ -5,6 +5,17 @@ import { BadRequestException } from '@nestjs/common';
55import { Types } from 'mongoose' ;
66
77export class IdentitiesDoublonService extends AbstractIdentitiesService {
8+ private removeInjectedValues ( baseValues : string [ ] = [ ] , injectedValues : string [ ] = [ ] ) : string [ ] {
9+ const nextValues = [ ...baseValues ]
10+ injectedValues . forEach ( ( injectedValue ) => {
11+ const index = nextValues . findIndex ( ( value ) => value === injectedValue )
12+ if ( index !== - 1 ) {
13+ nextValues . splice ( index , 1 )
14+ }
15+ } )
16+ return nextValues
17+ }
18+
819 public async ignoreFusionForIdentities ( ids : Types . ObjectId [ ] ) {
920 if ( ids . length !== 2 ) {
1021 throw new BadRequestException ( 'Deux IDs doivent être fournis pour ignorer la fusion.' ) ;
@@ -289,4 +300,71 @@ export class IdentitiesDoublonService extends AbstractIdentitiesService {
289300 await super . update ( identity1 . _id , identity1 ) ;
290301 return identity1 . _id ;
291302 }
303+
304+ public async cancelFusion ( id1 , id2 ) {
305+ let identity1 : Identities = null
306+ let identity2 : Identities & any = null
307+ try {
308+ identity1 = await this . findById < Identities > ( id1 )
309+ } catch ( error ) {
310+ throw new BadRequestException ( 'Id1 not found' )
311+ }
312+ try {
313+ identity2 = await this . findById < Identities > ( id2 )
314+ } catch ( error ) {
315+ throw new BadRequestException ( 'Id2 not found' )
316+ }
317+
318+ if ( ! identity1 . srcFusionId || identity1 . srcFusionId . toString ( ) !== identity2 . _id . toString ( ) ) {
319+ throw new BadRequestException ( 'Id1 ne contient pas de fusion avec Id2' )
320+ }
321+ if ( ! identity2 . destFusionId || identity2 . destFusionId . toString ( ) !== identity1 . _id . toString ( ) ) {
322+ throw new BadRequestException ( 'Id2 ne contient pas de destination de fusion vers Id1' )
323+ }
324+
325+ identity1 . inetOrgPerson . employeeNumber = this . removeInjectedValues (
326+ identity1 . inetOrgPerson . employeeNumber ,
327+ identity2 . inetOrgPerson . employeeNumber . map ( ( value ) => value . replace ( / ^ F / , '' ) ) ,
328+ )
329+ identity1 . inetOrgPerson . departmentNumber = this . removeInjectedValues (
330+ identity1 . inetOrgPerson . departmentNumber ,
331+ identity2 . inetOrgPerson . departmentNumber ,
332+ )
333+
334+ if (
335+ identity1 . additionalFields . objectClasses . includes ( 'supannPerson' ) &&
336+ identity2 . additionalFields . objectClasses . includes ( 'supannPerson' )
337+ ) {
338+ const identity1SupannPerson = identity1 . additionalFields . attributes . supannPerson as any
339+ const identity2SupannPerson = identity2 . additionalFields . attributes . supannPerson as any
340+
341+ if ( identity1SupannPerson ?. supannTypeEntiteAffectation && identity2SupannPerson ?. supannTypeEntiteAffectation ) {
342+ identity1SupannPerson . supannTypeEntiteAffectation = this . removeInjectedValues (
343+ identity1SupannPerson . supannTypeEntiteAffectation ,
344+ identity2SupannPerson . supannTypeEntiteAffectation ,
345+ )
346+ }
347+
348+ if ( identity1SupannPerson ?. supannRefId && identity2SupannPerson ?. supannRefId ) {
349+ identity1SupannPerson . supannRefId = this . removeInjectedValues (
350+ identity1SupannPerson . supannRefId ,
351+ identity2SupannPerson . supannRefId ,
352+ )
353+ }
354+ }
355+
356+ identity1 . srcFusionId = null
357+ identity1 . primaryEmployeeNumber = null
358+ identity1 . state = IdentityState . TO_VALIDATE
359+
360+ identity2 . destFusionId = null
361+ identity2 . state = IdentityState . TO_VALIDATE
362+ if ( identity2 . inetOrgPerson . employeeNumber ?. [ 0 ] ?. startsWith ( 'F' ) ) {
363+ identity2 . inetOrgPerson . employeeNumber [ 0 ] = identity2 . inetOrgPerson . employeeNumber [ 0 ] . substring ( 1 )
364+ }
365+
366+ await super . update ( identity2 . _id , identity2 )
367+ await super . update ( identity1 . _id , identity1 )
368+ return identity1 . _id
369+ }
292370}
0 commit comments