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

UBERF-7090: Request enhancements from QMS #5695

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion models/request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -59,13 +59,16 @@ export class TRequest extends TAttachedDoc implements Request {
@ReadOnly()
approved!: Ref<PersonAccount>[]

approvedDates?: Timestamp[]

requiredApprovesCount!: number

@Prop(TypeString(), request.string.Status)
// @Index(IndexKind.Indexed)
status!: RequestStatus

tx!: Tx
rejectedTx?: Tx

@Prop(TypeRef(contact.class.PersonAccount), request.string.Rejected)
@ReadOnly()
Expand Down
4 changes: 3 additions & 1 deletion plugins/request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
}

Expand Down
50 changes: 39 additions & 11 deletions server-plugins/request-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
}
}

await control.apply([resTx, request.tx], true)
if (applyTxes.length > 0) {
await control.apply(applyTxes, true)
}

return []
}

Expand Down