// Copyright © 2025 Hardcore Engineering Inc. // // Licensed under the Eclipse Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. You may // obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // // See the License for the specific language governing permissions and // limitations under the License. import { Card, ExportFunc, MasterTag, Tag } from '@hcengineering/card' import { Association, Class, Client, Doc, DocumentUpdate, ObjQueryType, Rank, Ref, Tx, Type } from '@hcengineering/core' import { Asset, IntlString, Plugin, plugin, Resource } from '@hcengineering/platform' import { ToDo } from '@hcengineering/time' import { AnyComponent } from '@hcengineering/ui' import { AttributeCategory } from '@hcengineering/view' import { SelectedExecutionContext } from './types' /** * @public */ export const processId = 'process' as Plugin // Process model dscription export interface Process extends Doc { masterTag: Ref name: string description: string parallelExecutionForbidden?: boolean autoStart?: boolean automationOnly?: boolean context: Record resultType?: Type } export interface ProcessContext { name: string _class: Ref> action: StepId index: number producer: Ref isResult?: boolean type?: Type value: SelectedExecutionContext } export type ContextId = string & { __contextId: true } export interface Trigger extends Doc { icon: Asset label: IntlString requiredParams: string[] editor?: AnyComponent presenter?: AnyComponent checkFunction?: Resource init: boolean auto?: boolean } export interface Transition extends Doc { process: Ref from: Ref | null to: Ref actions: Step[] trigger: Ref triggerParams: Record rank: Rank } export interface ExecutionLog extends Doc { execution: Ref card: Ref process: Ref transition?: Ref action: ExecutionLogAction } export enum ExecutionLogAction { Started = 'started', Transition = 'transition', Rollback = 'rollback' } export type CheckFunc = ( client: Client, execution: Execution, params: Record, context: Record ) => Promise export enum ExecutionStatus { Active = 'active', Done = 'done', Cancelled = 'cancelled' } export interface Execution extends Doc { process: Ref currentState: Ref card: Ref rollback: Tx[][] // just stack error?: ExecutionError[] | null context: ExecutionContext result?: any parentId?: Ref status: ExecutionStatus } export type ExecutionContext = Record & { __contextId: true } export interface ExecutionError { error: IntlString props: Record intlProps: Record transition: Ref | undefined } export interface ProcessToDo extends ToDo { execution: Ref withRollback: boolean results?: UserResult[] field?: string } export interface ApproveRequest extends ProcessToDo { card: Ref approved?: boolean reason?: string group: string field?: string actionType?: 'approve' | 'review' } export type MethodParams = { [P in keyof T]?: ObjQueryType | string } & DocumentUpdate & Record export interface State extends Doc { process: Ref title: string rank: Rank } export type StepId = string & { __stepId: true } export interface Step { _id: StepId context: StepContext | null methodId: Ref> params: MethodParams results?: UserResult[] } export interface StepContext { _id: ContextId // context id _class?: Ref> // class of the context } export interface UserResult { _id: ContextId // context id name: string key?: string type: Type } export interface ProcessCustomEvent extends Doc { eventType: string execution: Ref card: Ref } export interface EventButton extends Doc { title: string eventType: string execution: Ref card: Ref } export interface Method extends Doc { label: IntlString objectClass: Ref> requiredParams: string[] description?: IntlString editor?: AnyComponent presenter?: AnyComponent createdContext: CreatedContext | null defaultParams?: MethodParams } export interface CreatedContext { _class: Ref> nameField?: string } export interface ProcessFunction extends Doc { type: 'transform' | 'reduce' | 'context' of: Ref> editor?: AnyComponent presenter?: AnyComponent category: AttributeCategory | undefined allowMany?: boolean label: IntlString } export interface UpdateCriteriaComponent extends Doc { category: AttributeCategory editor: AnyComponent of: Ref> props: Record } export * from './dslContext' export * from './errors' export * from './types' export * from './utils' export default plugin(processId, { class: { Process: '' as Ref>, Execution: '' as Ref>, ProcessToDo: '' as Ref>, ApproveRequest: '' as Ref>, Method: '' as Ref>>, State: '' as Ref>, ProcessFunction: '' as Ref>, Transition: '' as Ref>, Trigger: '' as Ref>, ProcessCustomEvent: '' as Ref>, ExecutionLog: '' as Ref>, UpdateCriteriaComponent: '' as Ref>, EventButton: '' as Ref> }, method: { RunSubProcess: '' as Ref>, CancelSubProcess: '' as Ref>, CreateAction: '' as Ref>, CancellAction: '' as Ref>, CreateToDo: '' as Ref>, CloseToDo: '' as Ref>, UpdateCard: '' as Ref>, CreateCard: '' as Ref>, AddRelation: '' as Ref>, AddTag: '' as Ref>, RequestApproval: '' as Ref>, CancelToDo: '' as Ref>, LockCard: '' as Ref>, LockSection: '' as Ref>, UnlockCard: '' as Ref>, UnlockSection: '' as Ref>, LockField: '' as Ref>, UnlockField: '' as Ref> }, trigger: { OnCardUpdate: '' as Ref, // in fact WhenCardMatches, should migrate in future WhenFieldChanges: '' as Ref, OnSubProcessesDone: '' as Ref, OnSubProcessMatch: '' as Ref, OnToDoClose: '' as Ref, OnToDoRemove: '' as Ref, OnExecutionStart: '' as Ref, OnExecutionContinue: '' as Ref, OnTime: '' as Ref, OnEvent: '' as Ref, OnApproveRequestApproved: '' as Ref, OnApproveRequestRejected: '' as Ref }, triggerCheck: { ToDo: '' as Resource, MatchCheck: '' as Resource, FieldChangedCheck: '' as Resource, SubProcessesDoneCheck: '' as Resource, SubProcessMatchCheck: '' as Resource, Time: '' as Resource, OnEventCheck: '' as Resource, ApproveRequestApproved: '' as Resource, ApproveRequestRejected: '' as Resource }, string: { Method: '' as IntlString, Execution: '' as IntlString, Process: '' as IntlString, Step: '' as IntlString, Error: '' as IntlString, From: '' as IntlString, To: '' as IntlString, Trigger: '' as IntlString, Actions: '' as IntlString, ProcessRunned: '' as IntlString, ProcessStateChanged: '' as IntlString, ProcessFinished: '' as IntlString, SyncWithField: '' as IntlString, ActionType: '' as IntlString, ApproveAction: '' as IntlString, ReviewAction: '' as IntlString, Review: '' as IntlString }, error: { MethodNotFound: '' as IntlString, InternalServerError: '' as IntlString, EmptyRelatedObjectValue: '' as IntlString, RelatedObjectNotFound: '' as IntlString, RelationNotExists: '' as IntlString, EmptyAttributeContextValue: '' as IntlString, ObjectNotFound: '' as IntlString, AttributeNotExists: '' as IntlString, UserRequestedValueNotProvided: '' as IntlString, ResultNotProvided: '' as IntlString, EmptyFunctionResult: '' as IntlString, ContextValueNotProvided: '' as IntlString, RequiredParamsNotProvided: '' as IntlString, TooDeepTransitionRecursion: '' as IntlString, ToDoAlreadyCompleted: '' as IntlString }, icon: { Process: '' as Asset, Steps: '' as Asset, States: '' as Asset, ToDo: '' as Asset, WhenCardMatches: '' as Asset, WhenFieldChanges: '' as Asset, WaitSubprocesses: '' as Asset, ToDoRemove: '' as Asset, Start: '' as Asset, Time: '' as Asset, OnEvent: '' as Asset }, function: { FirstMatchValue: '' as Ref, Filter: '' as Ref, FirstValue: '' as Ref, LastValue: '' as Ref, Random: '' as Ref, UpperCase: '' as Ref, LowerCase: '' as Ref, Trim: '' as Ref, Prepend: '' as Ref, Append: '' as Ref, Replace: '' as Ref, ReplaceAll: '' as Ref, Split: '' as Ref, Cut: '' as Ref, Add: '' as Ref, Subtract: '' as Ref, Multiply: '' as Ref, Divide: '' as Ref, Modulo: '' as Ref, Power: '' as Ref, Sqrt: '' as Ref, Round: '' as Ref, Absolute: '' as Ref, Ceil: '' as Ref, Floor: '' as Ref, Offset: '' as Ref, FirstWorkingDayAfter: '' as Ref, RoleContext: '' as Ref, All: '' as Ref, Insert: '' as Ref, Remove: '' as Ref, RemoveFirst: '' as Ref, RemoveLast: '' as Ref, CurrentEmployee: '' as Ref, CurrentUser: '' as Ref, ExecutionStarted: '' as Ref, ExecutionEmployeeInitiator: '' as Ref, ExecutionInitiator: '' as Ref, EmptyArray: '' as Ref, CurrentDate: '' as Ref, ExportProcess: '' as Resource, CheckProcessSectionVisibility: '' as Resource<(doc: Card) => Promise>, CheckRequestsSectionVisibility: '' as Resource<(doc: Card) => Promise> } })