@gsb-core/core
Version:
GSB core services and classes for platform-independent web applications
204 lines • 7.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FunctionService = void 0;
const gsb_entity_service_1 = require("../../services/entity/gsb-entity.service");
const query_params_1 = require("../../types/query-params");
const gsb_config_1 = require("../../config/gsb-config");
const gsb_utils_1 = require("../../utils/gsb-utils");
/**
* Service for managing Serverless Functions in the application
*/
class FunctionService {
constructor() {
this.ENTITY_NAME = 'GsbWfFunction';
this.entityService = gsb_entity_service_1.GsbEntityService.getInstance();
}
/**
* Get function by ID
* @param id The function ID
* @returns The function or null if not found
*/
async getFunctionById(id) {
try {
const func = await this.entityService.getById(this.ENTITY_NAME, id, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return func;
}
catch (error) {
console.error('Error getting function by ID:', error);
return null;
}
}
/**
* Get all functions with pagination
* @param page The page number (starting from 1)
* @param pageSize The number of items per page
* @returns An array of functions and the total count
*/
async getFunctions(page = 1, pageSize = 10) {
try {
const query = new query_params_1.QueryParams(this.ENTITY_NAME);
// Set pagination
query.startIndex = (page - 1) * pageSize;
query.count = pageSize;
query.calcTotalCount = true;
// Sort by last update date
query.sortCols = (0, gsb_utils_1.getGsbDateSortCols)();
const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return {
functions: (response.entities || []),
totalCount: response.totalCount || 0
};
}
catch (error) {
console.error('Error getting functions:', error);
return { functions: [], totalCount: 0 };
}
}
/**
* Search for functions
* @param searchTerm The search term
* @param page The page number (starting from 1)
* @param pageSize The number of items per page
* @returns An array of functions and the total count
*/
async searchFunctions(searchTerm, page = 1, pageSize = 10) {
try {
const query = new query_params_1.QueryParams(this.ENTITY_NAME);
// Set search filter
if (searchTerm) {
query.searchText = searchTerm;
}
// Set pagination
query.startIndex = (page - 1) * pageSize;
query.count = pageSize;
query.calcTotalCount = true;
const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return {
functions: (response.entities || []),
totalCount: response.totalCount || 0
};
}
catch (error) {
console.error('Error searching functions:', error);
return { functions: [], totalCount: 0 };
}
}
/**
* Create a new function
* @param func The function to create
* @returns The created function ID or null if failed
*/
async createFunction(func) {
var _a;
try {
// Set defaults
const newFunc = {
...func,
standalone: (_a = func.standalone) !== null && _a !== void 0 ? _a : true
};
// Set create date fields
const request = {
entDefName: this.ENTITY_NAME,
entity: newFunc,
entityDef: {},
entityId: '',
entDefId: '',
filters: []
};
const response = await this.entityService.save(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
if (!response.id) {
return null;
}
return response.id;
}
catch (error) {
console.error('Error creating function:', error);
return null;
}
}
/**
* Update an existing function
* @param func The function to update
* @returns True if updated successfully, false otherwise
*/
async updateFunction(func) {
try {
if (!func.id) {
throw new Error('Function ID is required for updates');
}
// Get existing function to merge data
const existingFunc = await this.getFunctionById(func.id);
if (!existingFunc) {
throw new Error('Function not found');
}
const updatedFunc = {
...existingFunc,
...func
};
// Set update date fields
const request = {
entDefName: this.ENTITY_NAME,
entity: updatedFunc,
entityDef: {},
entityId: '',
entDefId: '',
filters: []
};
const response = await this.entityService.save(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return !!response.id;
}
catch (error) {
console.error('Error updating function:', error);
return false;
}
}
/**
* Delete a function
* @param id The function ID to delete
* @returns True if deleted successfully, false otherwise
*/
async deleteFunction(id) {
try {
const request = {
entDefName: this.ENTITY_NAME,
entityId: id,
entity: {},
entityDef: {},
entDefId: '',
filters: []
};
const response = await this.entityService.delete(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return response.deleteCount === 1;
}
catch (error) {
console.error('Error deleting function:', error);
return false;
}
}
/**
* Execute a function
* @param id The function ID to execute
* @param params The parameters to pass to the function
* @returns The result of the function execution
*/
async executeFunction(id, params = {}) {
try {
const func = await this.getFunctionById(id);
if (!func) {
throw new Error('Function not found');
}
const request = {
functionId: id,
params: params
};
const response = await this.entityService.runWfFunction(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return response;
}
catch (error) {
console.error('Error executing function:', error);
throw error;
}
}
}
exports.FunctionService = FunctionService;
//# sourceMappingURL=function.service.js.map