@crediblex.io/fineract-api-client
Version:
TypeScript client for Fineract APIs
196 lines • 8.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FineractCreditLinesApi = void 0;
const errors_1 = require("../types/errors");
/**
* API client for Fineract Credit Lines related operations.
* It uses the HttpClient for making requests.
*/
class FineractCreditLinesApi {
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Creates a new credit line for a client in Fineract.
* Corresponds to the POST /clients/{clientId}/creditlines endpoint.
*
* @param clientId The ID of the client to create the credit line for.
* @param creditLineData The data for the new credit line.
* @returns A promise that resolves to the creation response or rejects with an error.
*/
async createCreditLine(clientId, creditLineData) {
try {
const apiPath = `/fineract-provider/api/v1/clients/${clientId}/creditlines`;
const response = await this.httpClient.post(apiPath, creditLineData);
return response.data;
}
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);
}
}
/**
* Retrieves all credit lines for a specific client.
* Corresponds to the GET /clients/{clientId}/creditlines endpoint.
*
* @param clientId The ID of the client to get credit lines for.
* @returns A promise that resolves to the list of credit lines with their associated loans or rejects with an error.
*/
async getCreditLines(clientId) {
try {
const apiPath = `/fineract-provider/api/v1/clients/${clientId}/creditlines`;
const response = await this.httpClient.get(apiPath);
return response.data;
}
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);
}
}
/**
* Retrieves all available charges from Fineract.
* Corresponds to the GET /charges endpoint.
* This is useful for discovering which charges can be applied to credit lines.
*
* @param params Optional parameters for filtering charges.
* @returns A promise that resolves to the list of charges or rejects with an error.
*/
async getCharges(params) {
try {
let apiPath = "/fineract-provider/api/v1/charges";
// Add query parameters if provided
const queryParams = [];
if (params?.chargeAppliesTo) {
queryParams.push(`chargeAppliesTo=${encodeURIComponent(params.chargeAppliesTo)}`);
}
if (params?.active !== undefined) {
queryParams.push(`active=${params.active}`);
}
if (queryParams.length > 0) {
apiPath += `?${queryParams.join("&")}`;
}
const response = await this.httpClient.get(apiPath);
return response.data;
}
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);
}
}
/**
* Retrieves charges that are specifically applicable to Line of Credit.
* This is a convenience method that filters charges for credit line use.
*
* @returns A promise that resolves to the list of credit line applicable charges or rejects with an error.
*/
async getCreditLineCharges() {
try {
const allCharges = await this.getCharges();
// Filter charges that are applicable to Line of Credit
const creditLineCharges = allCharges.filter((charge) => charge.active &&
(charge.chargeAppliesTo.code === "chargeAppliesTo.lineOfCredit" ||
charge.chargeAppliesTo.code === "chargeAppliesTo.loan"));
return creditLineCharges;
}
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);
}
}
/**
* Retrieves the credit line template for a specific client.
* Corresponds to the GET /clients/{clientId}/creditlines/template endpoint.
* This provides all the available options for creating a credit line.
*
* @param clientId The ID of the client to get the template for.
* @returns A promise that resolves to the credit line template or rejects with an error.
*/
async getCreditLineTemplate(clientId) {
try {
const apiPath = `/fineract-provider/api/v1/clients/${clientId}/creditlines/template`;
const response = await this.httpClient.get(apiPath);
return response.data;
}
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);
}
}
/**
* Retrieves status options for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of status options.
*/
async getCreditLineStatusOptions(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.statusOptions;
}
/**
* Retrieves product type options for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of product type options.
*/
async getCreditLineProductTypeOptions(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.productTypeOptions;
}
/**
* Retrieves review period options for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of review period options.
*/
async getCreditLineReviewPeriodOptions(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.reviewPeriodsOptions;
}
/**
* Retrieves cash margin type options for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of cash margin type options.
*/
async getCreditLineCashMarginTypeOptions(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.cashMarginTypeOptions;
}
/**
* Retrieves interest charge time options for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of interest charge time options.
*/
async getCreditLineInterestChargeTimeOptions(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.interestChargeTimeOptions;
}
/**
* Retrieves loan officers available for credit lines.
*
* @param clientId The ID of the client to get options for.
* @returns A promise that resolves to the list of loan officers.
*/
async getCreditLineLoanOfficers(clientId) {
const template = await this.getCreditLineTemplate(clientId);
return template.loanOfficers;
}
}
exports.FineractCreditLinesApi = FineractCreditLinesApi;
//# sourceMappingURL=fineract-creditlines-api.js.map