UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

168 lines 6.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GsbWorkflowService = void 0; const gsb_entity_service_1 = require("../../services/entity/gsb-entity.service"); const gsb_config_1 = require("../../config/gsb-config"); const gsb_workflow_model_1 = require("../../models/gsb-workflow.model"); const query_params_1 = require("../../types/query-params"); const requests_1 = require("../../types/requests"); const createEmptyWorkflow = (name) => { const generateId = (prefix = 'node') => { return `${prefix}_${Date.now()}_${Math.floor(Math.random() * 1000)}`; }; const startId = generateId('start'); const endId = generateId('end'); return { id: generateId('wf'), name, title: name, activities: [ { id: startId, name: 'Start', title: 'Start', activityType: gsb_workflow_model_1.ActivityType.Start, position: { x: 250, y: 50 } }, { id: endId, name: 'End', title: 'End', activityType: gsb_workflow_model_1.ActivityType.End, position: { x: 250, y: 250 } } ], transitions: [], enableLog: true }; }; class GsbWorkflowService { constructor() { this.ENTITY_NAME = 'GsbWorkflow'; this.entityService = gsb_entity_service_1.GsbEntityService.getInstance(); } /** * Get a list of all workflows */ async getWorkflows() { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); const queryParams = new query_params_1.QueryParams(this.ENTITY_NAME); queryParams.calcTotalCount = true; queryParams.startIndex = 0; queryParams.count = 1000; // Include activities and transitions queryParams.include('activities', 'transitions'); const response = await this.entityService.query(queryParams, token, tenantCode); return (response.entities || []); } /** * Get a specific workflow by ID */ async getWorkflowById(id) { if (id === 'new') { return createEmptyWorkflow('New Workflow'); } const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { const queryParams = new query_params_1.QueryParams(this.ENTITY_NAME); queryParams.entityId = id; // Include activities and transitions queryParams.include('activities', 'transitions'); const response = await this.entityService.query(queryParams, token, tenantCode); return (response.entity || null); } catch (error) { console.error('Error fetching workflow:', error); return null; } } /** * Save or update a workflow */ async saveWorkflow(workflow) { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { // Prepare the save request const saveRequest = new requests_1.GsbSaveRequest(); saveRequest.entDefId = this.ENTITY_NAME; saveRequest.entDefName = this.ENTITY_NAME; saveRequest.entityDef = {}; saveRequest.entity = workflow; saveRequest.entityId = workflow.id === 'new' ? '' : workflow.id; saveRequest.filters = []; const response = await this.entityService.save(saveRequest, token, tenantCode); // After saving, fetch the updated workflow if (!response.id) { throw new Error('No ID returned from save operation'); } const savedWorkflow = await this.getWorkflowById(response.id); if (!savedWorkflow) { throw new Error('Could not fetch saved workflow'); } return savedWorkflow; } catch (error) { console.error('Error saving workflow:', error); throw error; } } /** * Delete a workflow */ async deleteWorkflow(id) { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { const deleteRequest = new requests_1.GsbSaveRequest(); deleteRequest.entDefName = 'GsbWorkflow'; deleteRequest.entityId = id; await this.entityService.delete(deleteRequest, token, tenantCode); return true; } catch (error) { console.error('Error deleting workflow:', error); return false; } } /** * Start a workflow instance */ async startWorkflow(workflowId, data) { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { const response = await this.entityService.startWorkflow({ workflow_id: workflowId, data }, token, tenantCode); return response; } catch (error) { console.error('Error starting workflow:', error); throw error; } } /** * Run a workflow function */ async runWorkflowFunction(functionId, data) { const token = (0, gsb_config_1.getGsbToken)(); const tenantCode = (0, gsb_config_1.getGsbTenantCode)(); try { const response = await this.entityService.runWfFunction({ function_id: functionId, data }, token, tenantCode); return response; } catch (error) { console.error('Error running workflow function:', error); throw error; } } } exports.GsbWorkflowService = GsbWorkflowService; //# sourceMappingURL=gsb-workflow.service.js.map