UNPKG

veend-sdk

Version:

a nodejs package for veend products

106 lines (105 loc) 5.29 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OnlendingService = void 0; const helper_1 = require("../../utils/helper"); const endpoints_1 = require("./endpoints"); class OnlendingService { constructor(apiClient, account, user) { this.apiClient = apiClient; this.account = account; this.user = user; } loanProducts() { return __awaiter(this, void 0, void 0, function* () { try { let _q = this.apiClient.buildQuery({ accountId: this.account }); const res = yield this.apiClient.get("giro", endpoints_1.ONLENDING_ENDPOINTS.loanProducts + _q); if (res && res.data) { let _products = res.data; _products = _products.filter((prd) => prd.isProductActive); return { status: "success", data: _products }; } else { return { status: "error", data: [] }; } } catch (error) { throw new Error(error.message); } }); } createLoan(payload) { return __awaiter(this, void 0, void 0, function* () { var _a; let _q = this.apiClient.buildQuery({ accountId: this.account }); try { const _body = { title: payload.title, webhookUrl: payload.callbackUrl, tenure: payload.tenure, withdrawalFrequency: payload.withdrawalFrequency, }; const res = yield this.apiClient.post("giro", endpoints_1.ONLENDING_ENDPOINTS.createLoan(this.account) + _q, _body); if (res && res.data) { const _data = res.data; yield (0, helper_1.sleep)(2500); const loanDetails = yield this.apiClient.get("giro", endpoints_1.ONLENDING_ENDPOINTS.getLoan(_data.account._id) + _q); const _userAccount = (_a = loanDetails.data.accountNumbers) === null || _a === void 0 ? void 0 : _a.find((acc) => acc.isWemaVas); let _createLoanResponse = { funding: { accountNumber: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.accountNumber) || "", accountName: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.accountName) || "", bankName: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.bankName) || "", }, _id: loanDetails.data._id, }; return { data: _createLoanResponse || null, status: "success" }; } return { data: null, status: "error" }; } catch (error) { throw new Error(error.message); } }); } getLoan(account) { return __awaiter(this, void 0, void 0, function* () { var _a; let _q = this.apiClient.buildQuery({ accountId: this.account }); try { const res = yield this.apiClient.get("giro", endpoints_1.ONLENDING_ENDPOINTS.getLoan(account) + _q); const _userAccount = (_a = res.data.accountNumbers) === null || _a === void 0 ? void 0 : _a.find((acc) => acc.isWemaVas); return Object.assign(Object.assign({}, res.data), { funding: { accountNumber: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.accountNumber) || "", accountName: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.accountName) || "", bankName: (_userAccount === null || _userAccount === void 0 ? void 0 : _userAccount.bankName) || "", } }); } catch (error) { throw new Error(error.message); } }); } getLoans() { return __awaiter(this, void 0, void 0, function* () { try { let _q = this.apiClient.buildQuery({ accountId: this.account }); const res = yield this.apiClient.get("giro", endpoints_1.ONLENDING_ENDPOINTS.loanSubcriptions(this.user) + _q); return res.data; } catch (error) { throw new Error(error.message); } }); } } exports.OnlendingService = OnlendingService;