UNPKG

synt_backend

Version:

Synt light-weight node backend service

291 lines (270 loc) 8.09 kB
const express = require("express"); const router = express.Router(); var ibanity = require("./../helpers/ibanity"); const userHelper = require("./../helpers/user"); import { getBankTransactionsDB } from "../database/banking"; import db from "../mysql/models"; import { validateVme } from "./../helpers/validations"; // routes router.post("/ibanity/code", postCode); router.get("/ibanity/transactions", getTransactions); router.get("/ibanity/accounts", getAccounts); router.post("/ibanity/payment", initiatePayment); router.post("/ibanity/process/payment", processPayment); module.exports = router; async function postCode(req, res) { const { t } = req; const { code, mobile = false } = req.body; let User = await userHelper.getAuthUser(req); if (User) { // verify vme and get vme let VmeValidation = await validateVme(t, User.VMEId); if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; // ibanity client try { const client = await ibanity.createClient({ VME, User, mobile, }); await client.postCode(code); return res.json({ success: true, message: t("api.banking.messages.bankUpdated", "Bank updated..."), }); } catch (err) { console.log(err); } } } async function getAccounts(req, res) { const { t } = req; let User = await userHelper.getAuthUser(req); if (User) { // verify vme and get vme let VmeValidation = await validateVme(t, User.VMEId); if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; // ibanity client try { const client = await ibanity.createClient({ VME, User }); let response = await client.getAccounts(); let responseFI = await client.getFinancialInstitutions(); if (response.success) { let Accounts = response.data; return res.json({ success: true, message: t("api.banking.messages.bankUpdated", "Bank updated..."), Accounts, Institutions: responseFI.data, }); } else { return res.json(response); } } catch (err) { return res.json({ success: false, error: err }); } } } async function getTransactions(req, res) { const { t } = req; let User = await userHelper.getAuthUser(req); if (User) { // verify vme and get vme let VmeValidation = await validateVme(t, User.VMEId); if (!VmeValidation.success) { return VmeValidation; } const { VME } = VmeValidation; const response = await getBankTransactionsDB({ t, User, VME }); return res.json(response); } } function cutMax(str, length) { if (str.length > length) { return str.slice(0, length - 3) + "..."; } return str; } async function initiatePayment(req, res) { const { t } = req; const { id, type, mobile = false } = req.body; let User = await userHelper.getAuthUser(req); if (User) { // verify vme and get vme let VmeValidation = await validateVme(t, User.VMEId); if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; let bankInfo, model; // get information what needs to be paid if (type === "Purchase") { model = await db.Purchase.findOne({ where: { id }, include: { model: db.Supplier, include: db.Company }, }); bankInfo = { total_amount: model.total_amount, creditor: { name: cutMax(model.Supplier.Company.name, 60), account: model.Supplier.Company.current_account.replace(/ /g, ""), }, statement: model.statement || t("api.banking.payment.statement.viaPayment", { defaultValue: "Payment {{alias}} via synt.be", alias: VME.alias, }), trigger: { type, id, }, }; } else if (type === "Provision") { model = await db.Provision.findOne({ where: { id }, include: { model: db.User, include: db.Company }, }); bankInfo = { total_amount: model.amount, creditor: { name: cutMax(VME.alias, 60), account: VME.Company.current_account, }, statement: model.reference, trigger: { type, id, }, }; } // ibanity client try { const client = await ibanity.createClient({ VME, User, mobile, }); try { const response = await client.createPayment(bankInfo); if (response.success) { model.payment = { id: response.data.id, status: response.data.attributes.status, }; await model.save(); return res.json({ success: true, redirect: response.data.links.redirect, message: t( "api.banking.payment.messages.paymentInitiated", "Payment initiated" ), }); } else { return res.json(response); } } catch (err) { console.log(err); } } catch (err) { return res.json({ success: false, error: err }); } } } async function processPayment(req, res) { const { t } = req; const { id, type, mobile = false } = req.body; let User = await userHelper.getAuthUser(req); if (User) { // verify vme and get vme let VmeValidation = await validateVme(t, User.VMEId); if (!VmeValidation.success) { return res.json(VmeValidation); } const { VME } = VmeValidation; // get information what needs to be paid let payment_id, model; if (type === "Purchase") { model = await db.Purchase.findOne({ where: { id } }); if (!model) { return res.json({ success: false, message: "Cannot find purchase" }); } payment_id = model.payment?.id; } else if (type === "Provision") { model = await db.Provision.findOne({ where: { id } }); if (!model) { return res.json({ success: false, message: "Cannot find provision" }); } payment_id = model.payment?.id; } if (!model) { return res.json({ success: false, message: "Cannot find any model" }); } if (!payment_id) { return res.json({ success: true, status: "open" }); } // ibanity client try { const client = await ibanity.createClient({ VME, User, mobile, }); try { const response = await client.getPayment(payment_id); const status = { unsigned: "pending", "accepted-customer-profile": "paid", "accepted-settlement-completed": "paid", "accepted-settlement-in-process": "initiated", "accepted-technical-validation": "paid", "accepted-with-change": "paid", "accepted-without-posting": "paid", "accepted-funds-checked": "paid", received: "pending", pending: "pending", rejected: "cancelled", "waiting-for-signature": "pending", cancelled: "cancelled", "partial-acceptance": "pending", unknown: "unknown", }[response.data.attributes.status] ?? "open"; if (response.success) { model.payment = { status, id: payment_id, }; if (status === "paid") { model.paid_at = new Date(); } await model.save(); return res.json({ success: true, message: t( "api.banking.payment.messages.paymentStatus", "Payment status" ) + ": " + response.data.attributes.status, status, }); } else { return res.json(response); } } catch (err) { console.log(err); } } catch (err) { return res.json({ success: false, error: err }); } } }