UNPKG

eregistrations-js

Version:

A JavaScript library that simplifies usage of eRegistrations APIs.

195 lines (194 loc) 8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getServiceDeterminants = getServiceDeterminants; exports.createDeterminant = createDeterminant; exports.addDeterminantToComponent = addDeterminantToComponent; /** * Retrieves determinants for a specific service * @param config The configuration object containing API and authentication details including serviceId * @returns A promise resolving to the service determinants data */ async function getServiceDeterminants(config) { if (!config.serviceId) { throw new Error('serviceId is required in config'); } try { console.log('Requesting service determinants for service:', config.serviceId); const response = await fetch(`${config.baseApiUrl}/bparest/bpa/v2016/06/service/${config.serviceId}/determinant`, { headers: { Authorization: `Bearer ${config.token}`, }, }); if (!response.ok) { if (response.status === 401) { throw new Error('Authentication failed: Invalid or expired token'); } else if (response.status === 404) { throw new Error(`Service ${config.serviceId} not found`); } throw new Error(`Failed to fetch service determinants: ${response.statusText}`); } const data = await response.json(); console.log('Received determinants response:', { status: response.status, dataLength: Array.isArray(data) ? data.length : 'not an array' }); return data; } catch (error) { console.error('Error fetching determinants:', error); if (error instanceof Error) { throw error; } throw new Error('Unknown error occurred while fetching service determinants'); } } /** * Creates a new determinant for a specific service * @param config The configuration object containing API, authentication details, and determinant properties: * - serviceId: The ID of the service to create determinant for * - determinantName: The name of the determinant to create * - targetFormFieldKey: The form field key that this determinant targets * - operator: The operator to use for the determinant condition * - value: The value to compare against using the operator * - type: The type of determinant (e.g., "text", "number", "checkbox") * @returns A promise resolving to the created determinant data */ async function createDeterminant(config) { if (!config.serviceId) { throw new Error('serviceId is required in config'); } if (!config.determinantName) { throw new Error('determinantName is required in config'); } if (!config.targetFormFieldKey) { throw new Error('targetFormFieldKey is required in config'); } if (!config.operator) { throw new Error('operator is required in config'); } if (config.value === undefined) { throw new Error('value is required in config'); } if (!config.type) { throw new Error('type is required in config'); } try { console.log('Creating determinant for service:', config.serviceId); const typeUpperCase = config.type.toUpperCase(); const typeLowerCase = config.type.toLowerCase(); // Construct the proper value field name based on type const valueField = `${typeLowerCase}Value`; const requestBody = { determinantType: "FORMFIELD", eventComponent: null, eventName: null, generated: false, usages: [], subject: null, status: true, determinantInsideGrid: false, name: config.determinantName, type: typeUpperCase, operator: config.operator, [valueField]: config.value, targetFormFieldKey: config.targetFormFieldKey, sourceFormFieldKey: null, serviceId: config.serviceId }; const response = await fetch(`${config.baseApiUrl}/bparest/bpa/v2016/06/service/${config.serviceId}/${typeLowerCase}determinant`, { method: 'POST', headers: { 'Authorization': `Bearer ${config.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(requestBody) }); if (!response.ok) { if (response.status === 401) { throw new Error('Authentication failed: Invalid or expired token'); } else if (response.status === 404) { throw new Error(`Service ${config.serviceId} not found`); } const errorText = await response.text(); throw new Error(`Failed to create determinant: ${response.status} ${response.statusText}\n${errorText}`); } const data = await response.json(); console.log('Successfully created determinant:', { id: data.id, name: data.name, type: data.type }); return data; } catch (error) { console.error('Error creating determinant:', error); if (error instanceof Error) { throw error; } throw new Error('Unknown error occurred while creating determinant'); } } /** * Adds a determinant to a form component * @param config The configuration object containing API, authentication details, and component properties: * - serviceId: The ID of the service * - determinantId: The ID of the determinant to add * - formId: The ID of the form * - componentKey: The key of the component to add the determinant to * @returns A promise resolving to the response data */ async function addDeterminantToComponent(config) { if (!config.serviceId) { throw new Error('serviceId is required in config'); } if (!config.determinantId) { throw new Error('determinantId is required in config'); } if (!config.formId) { throw new Error('formId is required in config'); } if (!config.componentKey) { throw new Error('componentKey is required in config'); } try { console.log('Adding determinant to component for service:', config.serviceId); console.log('Determinant ID:', config.determinantId); console.log('Form ID:', config.formId); console.log('Component Key:', config.componentKey); const requestBody = { determinantId: config.determinantId, formId: config.formId, componentKey: config.componentKey }; const response = await fetch(`${config.baseApiUrl}/bparest/bpa/v2016/06/service/${config.serviceId}/add-determinant-to-component`, { method: 'POST', headers: { 'Authorization': `Bearer ${config.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(requestBody) }); if (!response.ok) { if (response.status === 401) { throw new Error('Authentication failed: Invalid or expired token'); } else if (response.status === 404) { throw new Error(`Service ${config.serviceId} or resource not found`); } throw new Error(`Failed to add determinant to component: ${response.statusText}`); } const data = await response.json(); console.log('Successfully added determinant to component:', { status: response.status, message: (data === null || data === void 0 ? void 0 : data.message) || 'No message provided', apiStatus: (data === null || data === void 0 ? void 0 : data.status) || 'Unknown' }); return data; } catch (error) { console.error('Error adding determinant to component:', error); throw error; } }