diff --git a/models/request/src/index.ts b/models/request/src/index.ts index da205b531c..81c2e59698 100644 --- a/models/request/src/index.ts +++ b/models/request/src/index.ts @@ -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[] + 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() diff --git a/plugins/request/src/index.ts b/plugins/request/src/index.ts index e012ed3ea9..db79c00d2c 100644 --- a/plugins/request/src/index.ts +++ b/plugins/request/src/index.ts @@ -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[] approved: Ref[] + approvedDates?: Timestamp[] requiredApprovesCount: number rejected?: Ref status: RequestStatus tx: Tx + rejectedTx?: Tx comments?: number } diff --git a/server-plugins/request-resources/src/index.ts b/server-plugins/request-resources/src/index.ts index 04dc71e67b..92a72b3e1d 100644 --- a/server-plugins/request-resources/src/index.ts +++ b/server-plugins/request-resources/src/index.ts @@ -49,24 +49,52 @@ export async function OnRequest (tx: Tx, control: TriggerControl): Promise async function OnRequestUpdate (tx: TxCollectionCUD, control: TriggerControl): Promise { const ctx = tx.tx as TxUpdateDoc - 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 [] }