platform/plugins/questions-resources/src/actions/questionMoveUpAction.ts
Andrey Sobolev 4cce003364
UBERF-7959: Fix async issues (#6409)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2024-08-28 11:51:20 +05:00

30 lines
1.0 KiB
TypeScript

//
// Copyright @ 2024 Hardcore Engineering Inc.
//
import { generateId } from '@hcengineering/core'
import { getClient } from '@hcengineering/presentation'
import type { Question } from '@hcengineering/questions'
import { canUpdateQuestion, findPreviousQuestion, updateQuestion } from '../utils'
import { focusActionWithAvailability } from './ActionWithAvailability'
export const questionMoveUpAction = focusActionWithAvailability<Question<unknown>>(
async (object: Question<unknown>) => {
if (!canUpdateQuestion(object)) {
return false
}
const prevQuestion = await findPreviousQuestion(object)
return prevQuestion !== undefined
},
async (object: Question<unknown>) => {
const prevQuestion = await findPreviousQuestion(object)
if (prevQuestion === undefined) {
return
}
const ops = getClient().apply(generateId() + 'up')
await updateQuestion(ops, object, { rank: prevQuestion.rank })
await updateQuestion(ops, prevQuestion, { rank: object.rank })
await ops.commit()
}
)