Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Board: Fix card members update #1620

Merged
merged 3 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/presentation/src/components/UsersPopup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/board-resources/src/components/KanbanCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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<Card>
Expand Down Expand Up @@ -61,7 +61,7 @@
}

function updateMembers (e: CustomEvent<Ref<Employee>[]>) {
client.update(object, { members: e.detail })
updateCardMembers(object, client, e.detail)
}

function updateDate (e: CustomEvent<CardDate>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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)
}
}
]
Expand Down
5 changes: 3 additions & 2 deletions plugins/board-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ import {
isArchived,
isUnarchived,
archiveCard,
unarchiveCard
unarchiveCard,
updateCardMembers
} from './utils/CardUtils'
import { getPopupAlignment } from './utils/PopupUtils'

Expand Down Expand Up @@ -84,7 +85,7 @@ async function showEditMembersPopup (object: Card, client: Client, e?: Event): P
getPopupAlignment(e),
undefined,
(result: Array<Ref<Employee>>) => {
void client.update(object, { members: result })
updateCardMembers(object, client, result)
}
)
}
Expand Down
21 changes: 19 additions & 2 deletions plugins/board-resources/src/utils/CardUtils.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -67,3 +67,20 @@ export function archiveCard (card: Card, client: Client): Promise<TxResult> | un
export function unarchiveCard (card: Card, client: Client): Promise<TxResult> | undefined {
return updateCard(client, card, 'isArchived', false)
}

export function updateCardMembers (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use one transaction with both $push: { members: [newMembers] } and $pull: { members: [oldMemebrs]}. Please check if it working, or at least two transactions instead of multiple ones.
Also pleas make function async and return Promise, it will be faster and less transactions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$pull doesn't support array
https://github.com/hcengineering/anticrm/blob/eb9526f0153f10550c1eb537dd8c176d31921cf8/packages/core/src/operator.ts#L42
Also using both operators cause
ERROR: platform:status:UnknownError {"message":"Updating the path 'members' would create a conflict at 'members'"}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose $pull: {values: {$in: array}} should work, at least for mongoDb,
if so we could fix it for our $pull implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

card: Card,
client: Client,
users: Array<Ref<Employee>>
): Array<Promise<TxResult>> | undefined {
if (card?.members == null) return
const { members } = card
return [
...members
.filter((member) => !users.includes(member))
.map((member) => updateCard(client, card, '$pull', { members: member })),
...users
.filter((member) => !members.includes(member))
.map((member) => updateCard(client, card, '$push', { members: member }))
] as Array<Promise<TxResult>>
}