From 582bfdf26eff66134119f5fb226e16a4ff5cc3f2 Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Fri, 4 Jul 2025 19:15:37 +0700 Subject: [PATCH] Qfix: adding/deleting social ids (#9466) Signed-off-by: Alexey Zinoviev --- plugins/contact/src/utils.ts | 57 ++++++++++--------- .../socialIds/AddEmailSocialId.svelte | 37 +++++++++++- .../socialIds/SocialIdPresenter.svelte | 18 ++++-- .../socialIds/SocialIdsEditor.svelte | 2 +- 4 files changed, 79 insertions(+), 35 deletions(-) diff --git a/plugins/contact/src/utils.ts b/plugins/contact/src/utils.ts index 359a72afc2..c6f40cea35 100644 --- a/plugins/contact/src/utils.ts +++ b/plugins/contact/src/utils.ts @@ -33,7 +33,8 @@ import { Ref, SocialId, toIdMap, - TxFactory + TxFactory, + DocumentUpdate } from '@hcengineering/core' import { getMetadata } from '@hcengineering/platform' import { ColorDefinition } from '@hcengineering/ui' @@ -459,7 +460,7 @@ export async function ensureEmployeeForPerson ( for (const socialId of socialIds) { const existing = existingIdentifiers.get(socialId._id as SocialIdentityRef) - if (existing === undefined) { + if (existing == null) { await ctx.with('create-social-identity', {}, async () => { if (personRef === undefined) { // something went wrong @@ -491,41 +492,45 @@ export async function ensureEmployeeForPerson ( await client.tx(createSocialIdTx) }) } else { - // This social identity must be attached to the correct person. If it's not the case, something is wrong. - // personRef must be readonly after creation and must NEVER be changed. - if (existing.attachedTo !== personRef) { - throw new Error('Social identity is attached to the wrong person') + // If not confirmed locally can be attached to a different person (persons merge scenario) + // Confirmed social identity should not be attached to a different person for now + // It will change with accounts merge function + if (existing.verifiedOn != null && existing.attachedTo !== personRef) { + throw new Error('Confirmed social identity is attached to the wrong person') } // Check and update if needed. It can: - // 1. Become verified (changes verifiedOn) - if (existing.verifiedOn == null) { - const updateSocialIdentityTx = txFactory.createTxUpdateDoc( - contact.class.SocialIdentity, - contact.space.Contacts, - existing._id, - { - verifiedOn: socialId.verifiedOn, - value: socialId.value, - key: socialId.key, - isDeleted: socialId.isDeleted - } - ) + // 1. Become verified (maybe with persons merge) (changes verifiedOn, attachedTo) + const sidUpdate: DocumentUpdate = {} + let needUpdate = false - await client.tx(updateSocialIdentityTx) + // become verified + if (existing.verifiedOn == null) { + sidUpdate.verifiedOn = socialId.verifiedOn + needUpdate = true } - // 2. Become deleted (changes value/key/isDeleted) + // merged from another person + if (existing.attachedTo !== personRef) { + sidUpdate.attachedTo = personRef + // Bump collection in Person? + needUpdate = true + } + + // become deleted if (existing.isDeleted !== socialId.isDeleted && socialId.isDeleted === true) { + sidUpdate.value = socialId.value + sidUpdate.key = socialId.key + sidUpdate.isDeleted = socialId.isDeleted + needUpdate = true + } + + if (needUpdate) { const updateSocialIdentityTx = txFactory.createTxUpdateDoc( contact.class.SocialIdentity, contact.space.Contacts, existing._id, - { - value: socialId.value, - key: socialId.key, - isDeleted: socialId.isDeleted - } + sidUpdate ) await client.tx(updateSocialIdentityTx) diff --git a/plugins/setting-resources/src/components/socialIds/AddEmailSocialId.svelte b/plugins/setting-resources/src/components/socialIds/AddEmailSocialId.svelte index 3ec1111802..75cda96661 100644 --- a/plugins/setting-resources/src/components/socialIds/AddEmailSocialId.svelte +++ b/plugins/setting-resources/src/components/socialIds/AddEmailSocialId.svelte @@ -14,11 +14,12 @@ -->