import { Context, DAL, InternalModuleDeclaration, ModulesSdkTypes, WorkflowsSdkTypes, } from "@medusajs/types" import { InjectSharedContext, MedusaContext, ModulesSdkUtils, } from "@medusajs/utils" import type { ReturnWorkflow, UnwrapWorkflowInputDataType, } from "@medusajs/workflows-sdk" import { WorkflowExecution } from "@models" import { WorkflowOrchestratorService } from "@services" type InjectedDependencies = { baseRepository: DAL.RepositoryService workflowExecutionService: ModulesSdkTypes.IMedusaInternalService workflowOrchestratorService: WorkflowOrchestratorService redisDisconnectHandler: () => Promise } export class WorkflowsModuleService< TWorkflowExecution extends WorkflowExecution = WorkflowExecution > extends ModulesSdkUtils.MedusaService<{ WorkflowExecution: { dto: WorkflowExecution } }>({ WorkflowExecution }) { protected baseRepository_: DAL.RepositoryService protected workflowExecutionService_: ModulesSdkTypes.IMedusaInternalService protected workflowOrchestratorService_: WorkflowOrchestratorService protected redisDisconnectHandler_: () => Promise constructor( { baseRepository, workflowExecutionService, workflowOrchestratorService, redisDisconnectHandler, }: InjectedDependencies, protected readonly moduleDeclaration: InternalModuleDeclaration ) { // @ts-ignore super(...arguments) this.baseRepository_ = baseRepository this.workflowExecutionService_ = workflowExecutionService this.workflowOrchestratorService_ = workflowOrchestratorService this.redisDisconnectHandler_ = redisDisconnectHandler } __hooks = { onApplicationShutdown: async () => { await this.workflowOrchestratorService_.onApplicationShutdown() await this.redisDisconnectHandler_() }, onApplicationPrepareShutdown: async () => { await this.workflowOrchestratorService_.onApplicationPrepareShutdown() }, onApplicationStart: async () => { await this.workflowOrchestratorService_.onApplicationStart() }, } @InjectSharedContext() async run>( workflowIdOrWorkflow: TWorkflow, options: WorkflowsSdkTypes.WorkflowOrchestratorRunDTO< TWorkflow extends ReturnWorkflow ? UnwrapWorkflowInputDataType : unknown > = {}, @MedusaContext() context: Context = {} ) { const ret = await this.workflowOrchestratorService_.run< TWorkflow extends ReturnWorkflow ? UnwrapWorkflowInputDataType : unknown >(workflowIdOrWorkflow, options, context) return ret as any } @InjectSharedContext() async getRunningTransaction( workflowId: string, transactionId: string, @MedusaContext() context: Context = {} ) { return await this.workflowOrchestratorService_.getRunningTransaction( workflowId, transactionId, context ) } @InjectSharedContext() async setStepSuccess( { idempotencyKey, stepResponse, options, }: { idempotencyKey: string | object stepResponse: unknown options?: Record }, @MedusaContext() context: Context = {} ) { return await this.workflowOrchestratorService_.setStepSuccess( { idempotencyKey, stepResponse, options, } as any, context ) } @InjectSharedContext() async setStepFailure( { idempotencyKey, stepResponse, options, }: { idempotencyKey: string | object stepResponse: unknown options?: Record }, @MedusaContext() context: Context = {} ) { return await this.workflowOrchestratorService_.setStepFailure( { idempotencyKey, stepResponse, options, } as any, context ) } @InjectSharedContext() async subscribe( args: { workflowId: string transactionId?: string subscriber: Function subscriberId?: string }, @MedusaContext() context: Context = {} ) { return this.workflowOrchestratorService_.subscribe(args as any, context) } @InjectSharedContext() async unsubscribe( args: { workflowId: string transactionId?: string subscriberOrId: string | Function }, @MedusaContext() context: Context = {} ) { return this.workflowOrchestratorService_.unsubscribe(args as any, context) } }