// 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, MasterTag, Tag } from '@hcengineering/card' import { Class, Doc, DocumentUpdate, ObjQueryType, 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 { SelectedContext } from './types' /** * @public */ export const processId = 'process' as Plugin // Process model dscription export interface Process extends Doc { masterTag: Ref name: string description: string initState: Ref parallelExecutionForbidden?: boolean autoStart?: boolean context: Record resultType?: Type } export interface ProcessContext { name: string _class: Ref> action: StepId producer: Ref value: SelectedContext } export type ContextId = string & { __contextId: true } export interface Trigger extends Doc { icon: Asset label: IntlString requiredParams: string[] editor?: AnyComponent checkFunction?: Resource } export interface Transition extends Doc { process: Ref from: Ref to: Ref | null actions: Step[] trigger: Ref triggerParams: Record } export type CheckFunc = (params: Record, doc: Doc) => 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 | null } export interface ProcessToDo extends ToDo { execution: Ref // state which created todo state: Ref } export type MethodParams = { [P in keyof T]?: ObjQueryType | string } & DocumentUpdate export interface State extends Doc { process: Ref title: string actions: Step[] } export type StepId = string & { __stepId: true } export interface Step { _id: StepId contextId: ContextId | null methodId: Ref> params: MethodParams result?: StepResult } export interface StepResult { _id: ContextId // context id type: Type } export interface Method extends Doc { label: IntlString objectClass: Ref> requiredParams: string[] description?: IntlString editor?: AnyComponent presenter?: AnyComponent contextClass: Ref> | null } export interface ProcessFunction extends Doc { type: 'transform' | 'reduce' | 'context' of: Ref> editor?: AnyComponent category: AttributeCategory | undefined allowMany?: boolean label: IntlString } export * from './errors' export * from './types' export * from './utils' export default plugin(processId, { class: { Process: '' as Ref>, Execution: '' as Ref>, ProcessToDo: '' as Ref>, Method: '' as Ref>>, State: '' as Ref>, ProcessFunction: '' as Ref>, Transition: '' as Ref>, Trigger: '' as Ref> }, method: { RunSubProcess: '' as Ref>, CreateToDo: '' as Ref>, UpdateCard: '' as Ref> }, trigger: { OnSubProcessesDone: '' as Ref, OnToDoClose: '' as Ref, OnToDoRemove: '' as Ref }, triggerCheck: { ToDo: '' 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 }, 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 }, icon: { Process: '' as Asset, Steps: '' as Asset, States: '' as Asset, ToDo: '' as Asset, WaitSubprocesses: '' as Asset, ToDoRemove: '' as Asset }, function: { FirstValue: '' as Ref, LastValue: '' as Ref, Random: '' as Ref, UpperCase: '' as Ref, LowerCase: '' as Ref, Trim: '' as Ref, Add: '' as Ref, Subtract: '' as Ref, Offset: '' as Ref, FirstWorkingDayAfter: '' as Ref, RoleContext: '' as Ref } })