diff --git a/packages/core/src/memdb.ts b/packages/core/src/memdb.ts index 40c4e6591a..f131d6b0ca 100644 --- a/packages/core/src/memdb.ts +++ b/packages/core/src/memdb.ts @@ -263,22 +263,25 @@ export abstract class MemDb extends TxProcessor implements Storage { } updateDoc (_id: Ref, doc: Doc, update: TxUpdateDoc | TxMixin): void { - if ( - this.hierarchy.isDerived(doc._class, core.class.Account) && - update._class === core.class.TxUpdateDoc && - (update as TxUpdateDoc).operations.person !== undefined - ) { - const account = doc as Account - if (account.person !== undefined) { - const acc = this.accountByPersonId.get(account.person) ?? [] - this.accountByPersonId.set( - account.person, - acc.filter((it) => it._id !== _id) - ) - } - const newPerson = (update as TxUpdateDoc).operations.person - if (newPerson !== undefined) { - this.accountByPersonId.set(newPerson, [...(this.accountByPersonId.get(newPerson) ?? []), account]) + if (this.hierarchy.isDerived(doc._class, core.class.Account) && update._class === core.class.TxUpdateDoc) { + const newEmail = (update as TxUpdateDoc).operations.email + if ((update as TxUpdateDoc).operations.person !== undefined) { + const account = doc as Account + if (account.person !== undefined) { + const acc = this.accountByPersonId.get(account.person) ?? [] + this.accountByPersonId.set( + account.person, + acc.filter((it) => it._id !== _id) + ) + } + const newPerson = (update as TxUpdateDoc).operations.person + if (newPerson !== undefined) { + this.accountByPersonId.set(newPerson, [...(this.accountByPersonId.get(newPerson) ?? []), account]) + } + } else if (newEmail !== undefined) { + const account = doc as Account + this.accountByEmail.delete(account.email) + this.accountByEmail.set(newEmail, account) } } }