UNPKG

@crediblex.io/fineract-api-client

Version:
84 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FineractWorkingDaysApi = void 0; const errors_1 = require("../types/errors"); /** * API client for Fineract Working Days related operations. * It uses the HttpClient for making requests. */ class FineractWorkingDaysApi { constructor(httpClient) { this.httpClient = httpClient; } /** * Retrieves working days configuration from Fineract and formats it for easy consumption. * Corresponds to the GET /workingdays endpoint. * Returns a formatted object with each day of the week mapped to a boolean indicating if it's a working day. * * @returns A promise that resolves to the formatted working days configuration or rejects with an error. */ async getWorkingDays() { try { const apiPath = "/fineract-provider/api/v1/workingdays"; const response = await this.httpClient.get(apiPath); // Parse the recurrence string to determine working days const workingDays = this.parseRecurrenceString(response.data.recurrence); return workingDays; } catch (rawError) { const errorPayload = rawError; const message = errorPayload?.message || errorPayload?.error || "Fineract API request failed"; throw new errors_1.FineractApiError(message, errorPayload?.statusCode, errorPayload?.details, errorPayload?.error); } } /** * Parses the RRULE recurrence string to determine which days are working days. * Expected format: "FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR," * * @param recurrence The recurrence string from Fineract API * @returns Formatted working days object * @private */ parseRecurrenceString(recurrence) { // Initialize all days as non-working days const workingDays = { Monday: false, Tuesday: false, Wednesday: false, Thursday: false, Friday: false, Saturday: false, Sunday: false, }; // Extract the BYDAY part from the recurrence string const byDayRegex = /BYDAY=([^;]*)/; const byDayMatch = byDayRegex.exec(recurrence); if (!byDayMatch) { return workingDays; } const byDayPart = byDayMatch[1]; // Map the abbreviations to full day names const dayMapping = { MO: "Monday", TU: "Tuesday", WE: "Wednesday", TH: "Thursday", FR: "Friday", SA: "Saturday", SU: "Sunday", }; // Split by comma and process each day abbreviation const workingDayAbbreviations = byDayPart.split(",").filter(Boolean); for (const dayAbbr of workingDayAbbreviations) { const trimmedDay = dayAbbr.trim(); if (dayMapping[trimmedDay]) { workingDays[dayMapping[trimmedDay]] = true; } } return workingDays; } } exports.FineractWorkingDaysApi = FineractWorkingDaysApi; //# sourceMappingURL=fineract-workingdays-api.js.map