11import NoModel from 'jscommons/dist/errors/NoModel' ;
2+ import { ObjectID } from 'mongodb' ;
23import * as promiseRetry from 'promise-retry' ;
4+
5+ import { ExpiredLock } from '../errors/ExpiredLock' ;
36import Locked from '../errors/Locked' ;
47import CreateUpdateIdentifierPersonaOptions // tslint:disable-line:import-spacing
58 from '../repoFactory/options/CreateUpdateIdentifierPersonaOptions' ;
@@ -9,11 +12,15 @@ import CreateUpdateIdentifierPersonaResult // tslint:disable-line:import-spacing
912import GetIdentifierResult from '../repoFactory/results/GetIdentifierResult' ;
1013import Lockable from '../repoFactory/utils/Lockable' ;
1114import Config from './Config' ;
12- import createPersona from './createPersona' ;
1315import getTheIdentifier from './getIdentifier' ;
1416import getIdentifierByIfi from './getIdentifierByIfi' ;
15- import setIdentifierPersona from './setIdentifierPersona' ;
1617import createIdentifier from './utils/createIdentifier' ;
18+ import createOrUpdateIdentifier from './utils/createOrUpdateIdentifier' ;
19+ import { createPersonaAndAddToIdentifier } from './utils/createPersonaAndAddToIdentifier' ;
20+
21+ type TheCreateUpdateIdentifierPersonaOptions = CreateUpdateIdentifierPersonaOptions & {
22+ readonly getIdentifier ?: ( opts : GetIdentifierOptions ) => Promise < GetIdentifierResult & Lockable > ;
23+ } ;
1724
1825const create = ( config : Config ) =>
1926 async ( {
@@ -32,23 +39,10 @@ const create = (config: Config) =>
3239 throw new Locked ( identifier ) ;
3340 }
3441
35- const { persona } = await createPersona ( config ) ( {
36- name : personaName ,
37- organisation,
38- } ) ;
39-
40- const { identifier : updatedIdentifier } = await setIdentifierPersona ( config ) ( {
41- id : identifier . id ,
42- organisation,
43- persona : persona . id ,
42+ return await createPersonaAndAddToIdentifier ( config ) ( {
43+ identifier,
44+ personaName,
4445 } ) ;
45-
46- return {
47- identifier : updatedIdentifier ,
48- identifierId : identifier . id ,
49- personaId : persona . id ,
50- wasCreated,
51- } ;
5246 } ;
5347
5448const createUpdateIdentifierPersona = ( config : Config ) =>
@@ -70,7 +64,7 @@ const createUpdateIdentifierPersona = (config: Config) =>
7064 organisation,
7165 } ) ;
7266
73- if ( locked === true ) {
67+ if ( locked === true ) {
7468 // We are locked, wait for unlock
7569 throw new Locked ( foundIdentifier ) ;
7670 }
@@ -85,8 +79,8 @@ const createUpdateIdentifierPersona = (config: Config) =>
8579 // currently it doesn't get updated
8680
8781 return {
88- identifier : foundIdentifier ,
8982 identifierId,
83+ identifier : foundIdentifier ,
9084 personaId : foundIdentifier . persona ,
9185 wasCreated : false ,
9286 } ;
@@ -99,15 +93,38 @@ const createUpdateIdentifierPersona = (config: Config) =>
9993 personaName,
10094 } ) ;
10195 }
96+ if ( err instanceof ExpiredLock ) {
97+ const {
98+ identifier : expiredIdentifier ,
99+ ignorePersonaId,
100+ } = err ;
101+
102+ const { identifier : identifierWithoutPersona } = await createOrUpdateIdentifier ( config ) ( {
103+ filter : {
104+ _id : new ObjectID ( expiredIdentifier . id ) ,
105+ organisation : new ObjectID ( organisation ) ,
106+ } ,
107+ update : {
108+ $set : { lockedAt : new Date ( ) } ,
109+ ...(
110+ ignorePersonaId
111+ ? { $unset : { persona : '' } }
112+ : { }
113+ ) ,
114+ } ,
115+ upsert : false ,
116+ } ) ;
117+
118+ return await createPersonaAndAddToIdentifier ( config ) ( {
119+ identifier : identifierWithoutPersona ,
120+ personaName,
121+ } ) ;
122+ }
123+
102124 throw err ;
103125 }
104-
105126 } ;
106127
107- type TheCreateUpdateIdentifierPersonaOptions = CreateUpdateIdentifierPersonaOptions & {
108- readonly getIdentifier ?: ( opts : GetIdentifierOptions ) => Promise < GetIdentifierResult & Lockable > ;
109- } ;
110-
111128const retryCreateUpdateIdentifierPersona = ( config : Config ) =>
112129 async ( opts : TheCreateUpdateIdentifierPersonaOptions ) :
113130 Promise < CreateUpdateIdentifierPersonaResult > => {
@@ -116,7 +133,8 @@ const retryCreateUpdateIdentifierPersona = (config: Config) =>
116133
117134 return promiseRetry < CreateUpdateIdentifierPersonaResult > ( async ( retry ) => {
118135 try {
119- return await createUpdateIdentifierPersonaFn ( opts ) ;
136+ const res = await createUpdateIdentifierPersonaFn ( opts ) ;
137+ return res ;
120138 } catch ( err ) {
121139 /* istanbul ignore else */
122140 if ( err instanceof Locked ) {
0 commit comments