@crediblex.io/fineract-api-client
Version:
TypeScript client for Fineract APIs
209 lines • 8.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FineractLoansAccountsApi = void 0;
const errors_1 = require("../types/errors");
/**
* API client for Fineract Loan Accounts related operations.
* It uses the HttpClient for making requests.
*/
class FineractLoansAccountsApi {
constructor(httpClient) {
this.httpClient = httpClient;
}
/**
* Retrieves all loan accounts from Fineract.
* Corresponds to the GET /loans endpoint.
*
* @returns A promise that resolves to the list of loan accounts or rejects with an error.
*/
async getAll() {
try {
const apiPath = "/fineract-provider/api/v1/loans";
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 a specific loan account by ID from Fineract.
* Corresponds to the GET /loans/{loanId} endpoint.
*
* @param loanId The ID of the loan account to retrieve.
* @returns A promise that resolves to the loan account details or rejects with an error.
*/
async getById(loanId) {
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}`;
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);
}
}
/**
* Creates a new loan account in Fineract.
* Corresponds to the POST /loans endpoint.
*
* @param loanData The data for the new loan account.
* @returns A promise that resolves to the creation response or rejects with an error.
*/
async create(loanData) {
try {
const apiPath = "/fineract-provider/api/v1/loans";
const response = await this.httpClient.post(apiPath, loanData);
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);
}
}
/**
* Updates a loan account in Fineract.
* Corresponds to the PUT /loans/{loanId} endpoint.
*
* @param loanId The ID of the loan account to update.
* @param loanData The updated data for the loan account.
* @returns A promise that resolves to the update response or rejects with an error.
*/
async update(loanId, loanData) {
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}`;
const response = await this.httpClient.put(apiPath, loanData);
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);
}
}
/**
* Deletes a loan account from Fineract.
* Corresponds to the DELETE /loans/{loanId} endpoint.
*
* @param loanId The ID of the loan account to delete.
* @returns A promise that resolves to the deletion response or rejects with an error.
*/
async delete(loanId) {
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}`;
const response = await this.httpClient.delete(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);
}
}
/**
* Calculates and retrieves the repayment schedule for a loan account.
* Corresponds to the GET /loans?command=calculateLoanSchedule endpoint.
*
* @param loanData The loan data for which to calculate the repayment schedule.
* @returns A promise that resolves to the calculated loan schedule or rejects with an error.
*/
async getRepaymentSchedule(loanData) {
try {
const apiPath = "/fineract-provider/api/v1/loans?command=calculateLoanSchedule";
const response = await this.httpClient.post(apiPath, loanData);
return response.data;
}
catch (rawError) {
const errorPayload = rawError;
const message = errorPayload?.message ||
errorPayload?.error ||
"Fineract API request failed";
console.log("errorPayload.details.errors", errorPayload.details.errors);
throw new errors_1.FineractApiError(message, errorPayload?.statusCode, errorPayload?.details, errorPayload?.error);
}
}
/**
* Retrieves a specific loan account summary by ID from Fineract.
* Corresponds to the GET /loans/{loanId} endpoint.
*
* @param loanId The ID of the loan account to retrieve summary for.
* @returns A promise that resolves to the loan account details or rejects with an error.
*/
async getSummaryById(loanId) {
const params = new URLSearchParams({
staffInSelectedOfficeOnly: "false",
associations: "summary",
exclude: "guarantors, futureSchedule",
});
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}?${params.toString()}`;
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);
}
}
async getRepaymentScheduleById(loanId) {
const params = new URLSearchParams({
staffInSelectedOfficeOnly: "false",
associations: "summary, repaymentSchedule",
exclude: "guarantors, futureSchedule",
});
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}?${params.toString()}`;
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);
}
}
/**
* Approves a loan account in Fineract.
* Corresponds to the POST /loans/{loanId}?command=approve endpoint.
*
* @param loanId The ID of the loan account to approve.
* @param approvalData The data for approving the loan account.
* @returns A promise that resolves to the approval response or rejects with an error.
*/
async approve(loanId, approvalData) {
try {
const apiPath = `/fineract-provider/api/v1/loans/${loanId}?command=approve`;
const response = await this.httpClient.post(apiPath, approvalData);
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);
}
}
}
exports.FineractLoansAccountsApi = FineractLoansAccountsApi;
//# sourceMappingURL=fineract-loansaccounts-api.js.map