From 0cec2cb1d1bcf59f993f3197ba8854251c819323 Mon Sep 17 00:00:00 2001 From: Alex <41288429+Dvinyanin@users.noreply.github.com> Date: Wed, 4 May 2022 14:53:55 +0700 Subject: [PATCH] Board: Fix card members update (#1620) Signed-off-by: Dvinyanin Alexandr --- packages/core/src/operator.ts | 9 +++++++-- packages/core/src/tx.ts | 7 +++++++ .../presentation/src/components/UsersPopup.svelte | 3 +-- .../src/components/KanbanCard.svelte | 4 ++-- .../src/components/editor/CardDetails.svelte | 4 ++-- plugins/board-resources/src/index.ts | 5 +++-- plugins/board-resources/src/utils/CardUtils.ts | 13 +++++++++++-- 7 files changed, 33 insertions(+), 12 deletions(-) diff --git a/packages/core/src/operator.ts b/packages/core/src/operator.ts index 7cbac2ae10..74465b099a 100644 --- a/packages/core/src/operator.ts +++ b/packages/core/src/operator.ts @@ -15,7 +15,7 @@ // import type { Doc, PropertyType } from './classes' -import type { Position } from './tx' +import type { Position, PullArray } from './tx' /** * @internal @@ -43,7 +43,12 @@ function $pull (document: Doc, keyval: Record): void { const doc = document as any for (const key in keyval) { const arr = doc[key] as Array - doc[key] = arr.filter((val) => val !== keyval[key]) + if (typeof keyval[key] === 'object') { + const { $in } = keyval[key] as PullArray + doc[key] = arr.filter((val) => !$in.includes(val)) + } else { + doc[key] = arr.filter((val) => val !== keyval[key]) + } } } diff --git a/packages/core/src/tx.ts b/packages/core/src/tx.ts index 33cc46f0a8..3637e75d3c 100644 --- a/packages/core/src/tx.ts +++ b/packages/core/src/tx.ts @@ -108,6 +108,13 @@ export interface Position { $position: number } +/** + * @public + */ +export interface PullArray { + $in: X[] +} + /** * @public */ diff --git a/packages/presentation/src/components/UsersPopup.svelte b/packages/presentation/src/components/UsersPopup.svelte index 941b777d11..7fb1e84f94 100644 --- a/packages/presentation/src/components/UsersPopup.svelte +++ b/packages/presentation/src/components/UsersPopup.svelte @@ -63,8 +63,7 @@ return false } const checkSelected = (person: Person): void => { - if (isSelected(person)) selectedUsers = selectedUsers.filter((p) => p !== person._id) - else selectedUsers.push(person._id) + selectedUsers = isSelected(person) ? selectedUsers.filter((p) => p !== person._id) : [...selectedUsers, person._id] objects = objects dispatch('update', selectedUsers) } diff --git a/plugins/board-resources/src/components/KanbanCard.svelte b/plugins/board-resources/src/components/KanbanCard.svelte index 61867624b5..f53e07e971 100644 --- a/plugins/board-resources/src/components/KanbanCard.svelte +++ b/plugins/board-resources/src/components/KanbanCard.svelte @@ -26,7 +26,7 @@ import CardInlineActions from './editor/CardInlineActions.svelte' import CardLabels from './editor/CardLabels.svelte' import DatePresenter from './presenters/DatePresenter.svelte' - import { hasDate, openCardPanel, updateCard } from '../utils/CardUtils' + import { hasDate, openCardPanel, updateCard, updateCardMembers } from '../utils/CardUtils' import { getElementPopupAlignment } from '../utils/PopupUtils' export let object: WithLookup @@ -61,7 +61,7 @@ } function updateMembers (e: CustomEvent[]>) { - client.update(object, { members: e.detail }) + updateCardMembers(object, client, e.detail) } function updateDate (e: CustomEvent) { diff --git a/plugins/board-resources/src/components/editor/CardDetails.svelte b/plugins/board-resources/src/components/editor/CardDetails.svelte index 2c0f70a41a..930d5f978d 100644 --- a/plugins/board-resources/src/components/editor/CardDetails.svelte +++ b/plugins/board-resources/src/components/editor/CardDetails.svelte @@ -22,7 +22,7 @@ import board from '../../plugin' import { getCardActions } from '../../utils/CardActionUtils' - import { hasDate } from '../../utils/CardUtils' + import { hasDate, updateCardMembers } from '../../utils/CardUtils' import DatePresenter from '../presenters/DatePresenter.svelte' import MemberPresenter from '../presenters/MemberPresenter.svelte' import CardLabels from './CardLabels.svelte' @@ -49,7 +49,7 @@ title: board.string.RemoveFromCard, handler: () => { const newMembers = membersIds.filter((m) => m !== member._id) - client.update(value, { members: newMembers }) + updateCardMembers(value, client, newMembers) } } ] diff --git a/plugins/board-resources/src/index.ts b/plugins/board-resources/src/index.ts index 55437dc343..48987081d6 100644 --- a/plugins/board-resources/src/index.ts +++ b/plugins/board-resources/src/index.ts @@ -47,7 +47,8 @@ import { isArchived, isUnarchived, archiveCard, - unarchiveCard + unarchiveCard, + updateCardMembers } from './utils/CardUtils' import { getPopupAlignment } from './utils/PopupUtils' @@ -84,7 +85,7 @@ async function showEditMembersPopup (object: Card, client: Client, e?: Event): P getPopupAlignment(e), undefined, (result: Array>) => { - void client.update(object, { members: result }) + updateCardMembers(object, client, result) } ) } diff --git a/plugins/board-resources/src/utils/CardUtils.ts b/plugins/board-resources/src/utils/CardUtils.ts index 37627f41c0..d4e1cf1b3f 100644 --- a/plugins/board-resources/src/utils/CardUtils.ts +++ b/plugins/board-resources/src/utils/CardUtils.ts @@ -1,6 +1,6 @@ import { Card } from '@anticrm/board' -import { EmployeeAccount } from '@anticrm/contact' -import { TxOperations as Client, TxResult, getCurrentAccount } from '@anticrm/core' +import { Employee, EmployeeAccount } from '@anticrm/contact' +import { TxOperations as Client, TxResult, getCurrentAccount, Ref } from '@anticrm/core' import { showPanel } from '@anticrm/ui' import board from '../plugin' @@ -67,3 +67,12 @@ export function archiveCard (card: Card, client: Client): Promise | un export function unarchiveCard (card: Card, client: Client): Promise | undefined { return updateCard(client, card, 'isArchived', false) } + +export function updateCardMembers (card: Card, client: Client, users: Array>): void { + if (card?.members == null) return + const { members } = card + const membersToPull = members.filter((member) => !users.includes(member)) + const usersToPush = users.filter((member) => !members.includes(member)) + if (membersToPull.length > 0) void updateCard(client, card, '$pull', { members: { $in: membersToPull } }) + if (usersToPush.length > 0) void updateCard(client, card, '$push', { members: { $each: usersToPush, $position: 0 } }) +}