UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

415 lines 14.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GsbEntityService = void 0; const query_params_1 = require("../../types/query-params"); const requests_1 = require("../../types/requests"); const gsb_api_service_1 = require("../../api/gsb-api.service"); const gsb_utils_1 = require("../../utils/gsb-utils"); // Store a singleton instance let serviceInstance = null; class GsbEntityService { constructor(useCache = true) { this.apiService = gsb_api_service_1.GsbApiService.getInstance(); this.useCache = useCache; } // Static method to get the singleton instance static getInstance(useCache = true) { if (!serviceInstance) { serviceInstance = new GsbEntityService(useCache); } return serviceInstance; } reduceEntityDef(request, checkEntityDef = true) { if (checkEntityDef && !gsb_utils_1.GsbUtils.hasEntityDef(request)) { throw new Error('Entity definition is required'); } if (request.entityDef) { request.entityDef = { id: request.entityDef.id, name: request.entityDef.name, }; } } async getToken(request) { try { // Use apiService for consistency const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: request, jsonResponse: true, noAuth: true, useBulk: false }, '/api/auth/getToken'); return response; } catch (error) { console.error('Authentication request failed:', error); throw error; } } async refreshToken(request, token, tenantCode) { try { // Use apiService for consistency const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: request, jsonResponse: true, noAuth: true, useBulk: false }, '/api/auth/refreshToken', token, tenantCode); return response; } catch (error) { console.error('Authentication request failed:', error); throw error; } } async getById(definitionType, id, token, tenantCode) { const req = new query_params_1.QueryParams(definitionType); req.entityId = id; const result = await this.get(req, token, tenantCode); return result.entity; } //todo: use te get copy method of api async getCopy(req, token, tenantCode) { this.reduceEntityDef(req); if (!req.entityId) { throw new Error('Entity id is required'); } req.disableTransaction = true; try { const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/getCopy', token, tenantCode); return response; } catch (error) { console.error(`Get entity failed for ${req.entDefName}:`, error); throw error; } } async get(req, token, tenantCode) { if (!req.entityId) { throw new Error('Entity id is required'); } this.reduceEntityDef(req); req.disableTransaction = true; try { const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/get', token, tenantCode); return response; } catch (error) { console.error(`Get entity failed for ${req.entDefName}:`, error); throw error; } } async query(req, token, tenantCode) { this.reduceEntityDef(req); req.disableTransaction = true; try { const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/query', token, tenantCode); return response; } catch (error) { console.error(`Query failed for ${req.entDefName}:`, error); throw error; } } async queryMapped(req, token, tenantCode) { if (!req.mapColName && !req.propertyName && !req.propName) { throw new Error('Property name is required'); } this.reduceEntityDef(req); req.disableTransaction = true; const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/queryMapped', token, tenantCode); return response; } /** * Save an entity * @param entity The entity to save, should have _entDefName property * @param token Optional auth token * @param tenantCode Optional tenant code * @returns Promise with save response */ async saveEnt(entity, token, tenantCode) { if (!entity) { throw new Error('Entity is required'); } const req = new requests_1.GsbSaveRequest(); if ('_entDefName' in entity) { req.entDefName = entity._entDefName; } req.entity = entity; let response = await this.save(req, token, tenantCode); if (response === null || response === void 0 ? void 0 : response.id) { entity.id = response.id; } return response; } async save(req, token, tenantCode) { if (!req.entity) { throw new Error('Entity is required'); } this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/save', token, tenantCode); if (response === null || response === void 0 ? void 0 : response.id) { req.entity.id = response.id; } return response; } async updateQuery(req, token, tenantCode) { this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/updateQuery', token, tenantCode); return response; } async saveMulti(req, token, tenantCode) { this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/saveMulti', token, tenantCode); return response; } /** * Execute multiple operations in a single bulk request * @param bulkRequest The bulk request containing multiple operations * @param token Optional auth token * @param tenantCode Optional tenant code * @returns Promise with bulk operation results */ async executeBulk(bulkRequest, token, tenantCode) { try { const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: bulkRequest }, '/api/entity/bulk', token, tenantCode); return response; } catch (error) { console.error('Bulk operation failed:', error); throw error; } } async getCode(req, token, tenantCode) { if (!req.codeGeneratorId) { throw new Error('Code generator id is required'); } const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/getCode', token, tenantCode); return response; } async saveMappedItems(req, token, tenantCode) { if (!req.items) { throw new Error('Items are required'); } if (!req.propName && !req.mapColName && !req.propertyName) { throw new Error('Property name is required'); } this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/saveMappedItems', token, tenantCode); return response; } async removeMappedItems(req, token, tenantCode) { if (!req.items) { throw new Error('Items are required'); } if (!req.propName && !req.mapColName && !req.propertyName) { throw new Error('Property name or mapped column name is required'); } this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/removeMappedItems', token, tenantCode); return response; } async getDefinition(req, token, tenantCode) { this.reduceEntityDef(req); try { try { const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entityDef/get', token, tenantCode); return response; } catch (error) { return { entityDef: null, message: error === null || error === void 0 ? void 0 : error.message }; } } catch (error) { throw error; } } async delete(req, token, tenantCode) { if (!req.entityId) { throw new Error('Entity id is required'); } this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/delete', token, tenantCode); return response; } async deleteById(entDefName, id, token, tenantCode) { const req = new query_params_1.QueryParams(entDefName); req.entityId = id; if (!req.entityId) { throw new Error('Entity id is required'); } this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/delete', token, tenantCode); return response; } async deleteEnt(entity, token, tenantCode) { const req = new query_params_1.QueryParams(entity._entDefName); req.entityId = entity.id; if (!req.entityId) { throw new Error('Entity id is required'); } const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/delete', token, tenantCode); return response; } async deleteQuery(req, token, tenantCode) { this.reduceEntityDef(req); const response = await this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req }, '/api/entity/deleteQuery', token, tenantCode); return response; } async runWorkflow(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/workflow/runWorkflow', token, tenantCode); } async startWorkflow(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/workflow/startWorkflow', token, tenantCode); } async runWfFunction(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/workflow/runFunction', token, tenantCode); } async testWfFunction(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/workflow/testFunction', token, tenantCode); } async iterateTask(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/workflow/iterateTask', token, tenantCode); } async changePassword(req, token, tenantCode) { return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, useBulk: false }, '/api/auth/changePassword', token, tenantCode); } async uploadFile(req, token, tenantCode) { // Prepare content for FormData conversion return this.apiService.callApi({ method: 'POST', protocol: 'https', hostName: 'api', content: req, isFormData: true, useBulk: false, }, '/api/file/upload', token, tenantCode); } } exports.GsbEntityService = GsbEntityService; //# sourceMappingURL=gsb-entity.service.js.map