uberf-7090: request enhancements (#5695)

Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
Alexey Zinoviev 2024-05-29 19:33:32 +04:00 committed by GitHub
parent 8d7406cbbb
commit c20c7cacd6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 14 deletions

View File

@ -16,7 +16,7 @@
import activity from '@hcengineering/activity'
import type { PersonAccount } from '@hcengineering/contact'
import contact from '@hcengineering/contact'
import { type Domain, type Ref, type Tx } from '@hcengineering/core'
import { type Timestamp, type Domain, type Ref, type Tx } from '@hcengineering/core'
import {
ArrOf,
type Builder,
@ -59,6 +59,8 @@ export class TRequest extends TAttachedDoc implements Request {
@ReadOnly()
approved!: Ref<PersonAccount>[]
approvedDates?: Timestamp[]
requiredApprovesCount!: number
@Prop(TypeString(), request.string.Status)
@ -66,6 +68,7 @@ export class TRequest extends TAttachedDoc implements Request {
status!: RequestStatus
tx!: Tx
rejectedTx?: Tx
@Prop(TypeRef(contact.class.PersonAccount), request.string.Rejected)
@ReadOnly()

View File

@ -14,7 +14,7 @@
//
import { PersonAccount } from '@hcengineering/contact'
import type { AttachedDoc, Class, Doc, Mixin, Ref, Tx } from '@hcengineering/core'
import type { AttachedDoc, Class, Doc, Mixin, Ref, Timestamp, Tx } from '@hcengineering/core'
import type { Asset, IntlString, Plugin } from '@hcengineering/platform'
import { plugin } from '@hcengineering/platform'
import { AnyComponent } from '@hcengineering/ui'
@ -26,10 +26,12 @@ import { ChatMessage } from '@hcengineering/chunter'
export interface Request extends AttachedDoc {
requested: Ref<PersonAccount>[]
approved: Ref<PersonAccount>[]
approvedDates?: Timestamp[]
requiredApprovesCount: number
rejected?: Ref<PersonAccount>
status: RequestStatus
tx: Tx
rejectedTx?: Tx
comments?: number
}

View File

@ -49,24 +49,52 @@ export async function OnRequest (tx: Tx, control: TriggerControl): Promise<Tx[]>
async function OnRequestUpdate (tx: TxCollectionCUD<Doc, Request>, control: TriggerControl): Promise<Tx[]> {
const ctx = tx.tx as TxUpdateDoc<Request>
if (ctx.operations.$push?.approved === undefined) return []
const request = (await control.findAll(ctx.objectClass, { _id: ctx.objectId }))[0]
if (request.approved.length === request.requiredApprovesCount) {
const collectionTx = control.txFactory.createTxUpdateDoc(ctx.objectClass, ctx.objectSpace, ctx.objectId, {
status: RequestStatus.Completed
})
collectionTx.space = core.space.Tx
const resTx = control.txFactory.createTxCollectionCUD(
const applyTxes: Tx[] = []
if (ctx.operations.$push?.approved !== undefined) {
const request = (await control.findAll(ctx.objectClass, { _id: ctx.objectId }))[0]
if (request.approved.length === request.requiredApprovesCount) {
const collectionTx = control.txFactory.createTxUpdateDoc(ctx.objectClass, ctx.objectSpace, ctx.objectId, {
status: RequestStatus.Completed
})
collectionTx.space = core.space.Tx
const resTx = control.txFactory.createTxCollectionCUD(
tx.objectClass,
tx.objectId,
tx.objectSpace,
'requests',
collectionTx
)
resTx.space = core.space.Tx
applyTxes.push(resTx)
applyTxes.push(request.tx)
}
const approvedDateTx = control.txFactory.createTxCollectionCUD(
tx.objectClass,
tx.objectId,
tx.objectSpace,
'requests',
collectionTx
control.txFactory.createTxUpdateDoc(ctx.objectClass, ctx.objectSpace, ctx.objectId, {
$push: { approvedDates: Date.now() }
})
)
resTx.space = core.space.Tx
await control.apply([resTx, request.tx], true)
applyTxes.push(approvedDateTx)
}
if (ctx.operations.status === RequestStatus.Rejected) {
const request = (await control.findAll(ctx.objectClass, { _id: ctx.objectId }))[0]
if (request.rejectedTx != null) {
applyTxes.push(request.rejectedTx)
}
}
if (applyTxes.length > 0) {
await control.apply(applyTxes, true)
}
return []
}