@@ -32,7 +32,7 @@ export abstract class MemDb extends TxProcessor implements Storage {
3232 private readonly objectById = new Map < Ref < Doc > , Doc > ( )
3333
3434 private readonly accountByPersonId = new Map < Ref < Doc > , Account [ ] > ( )
35- private readonly accountByEmail = new Map < string , Account > ( )
35+ private readonly accountByEmail = new Map < string , [ string , Account ] [ ] > ( )
3636
3737 constructor ( protected readonly hierarchy : Hierarchy ) {
3838 super ( )
@@ -83,7 +83,14 @@ export abstract class MemDb extends TxProcessor implements Storage {
8383 }
8484
8585 getAccountByEmail ( email : Account [ 'email' ] ) : Account | undefined {
86- return this . accountByEmail . get ( email )
86+ const accounts = this . accountByEmail . get ( email )
87+ if ( accounts === undefined || accounts . length === 0 ) {
88+ return undefined
89+ }
90+
91+ if ( accounts . length > 0 ) {
92+ return accounts [ accounts . length - 1 ] [ 1 ]
93+ }
8794 }
8895
8996 findObject < T extends Doc > ( _id : Ref < T > ) : T | undefined {
@@ -225,21 +232,44 @@ export abstract class MemDb extends TxProcessor implements Storage {
225232 )
226233 }
227234
235+ addAccount ( account : Account ) : void {
236+ if ( ! this . accountByEmail . has ( account . email ) ) {
237+ this . accountByEmail . set ( account . email , [ ] )
238+ }
239+
240+ this . accountByEmail . get ( account . email ) ?. push ( [ account . _id , account ] )
241+ }
242+
228243 addDoc ( doc : Doc ) : void {
229244 this . hierarchy . getAncestors ( doc . _class ) . forEach ( ( _class ) => {
230245 const arr = this . getObjectsByClass ( _class )
231246 arr . set ( doc . _id , doc )
232247 } )
233248 if ( this . hierarchy . isDerived ( doc . _class , core . class . Account ) ) {
234249 const account = doc as Account
235- this . accountByEmail . set ( account . email , account )
250+
251+ this . addAccount ( account )
252+
236253 if ( account . person !== undefined ) {
237254 this . accountByPersonId . set ( account . person , [ ...( this . accountByPersonId . get ( account . person ) ?? [ ] ) , account ] )
238255 }
239256 }
240257 this . objectById . set ( doc . _id , doc )
241258 }
242259
260+ delAccount ( account : Account ) : void {
261+ const accounts = this . accountByEmail . get ( account . email )
262+ if ( accounts !== undefined ) {
263+ const newAccounts = accounts . filter ( ( it ) => it [ 0 ] !== account . _id )
264+
265+ if ( newAccounts . length === 0 ) {
266+ this . accountByEmail . delete ( account . email )
267+ } else {
268+ this . accountByEmail . set ( account . email , newAccounts )
269+ }
270+ }
271+ }
272+
243273 delDoc ( _id : Ref < Doc > ) : void {
244274 const doc = this . objectById . get ( _id )
245275 if ( doc === undefined ) {
@@ -251,7 +281,8 @@ export abstract class MemDb extends TxProcessor implements Storage {
251281 } )
252282 if ( this . hierarchy . isDerived ( doc . _class , core . class . Account ) ) {
253283 const account = doc as Account
254- this . accountByEmail . delete ( account . email )
284+ this . delAccount ( account )
285+
255286 if ( account . person !== undefined ) {
256287 const acc = this . accountByPersonId . get ( account . person ) ?? [ ]
257288 this . accountByPersonId . set (
@@ -280,8 +311,8 @@ export abstract class MemDb extends TxProcessor implements Storage {
280311 }
281312 } else if ( newEmail !== undefined ) {
282313 const account = doc as Account
283- this . accountByEmail . delete ( account . email )
284- this . accountByEmail . set ( newEmail , account )
314+ this . delAccount ( account )
315+ this . addAccount ( { ... account , email : newEmail } )
285316 }
286317 }
287318 }
0 commit comments