const express = require("express");
const router = express.Router();
const multer = require("multer");
const authorize = require("../../../../middlewares/authorize-sapps");
const accountEntriesServices = require("./accountEntries.service");

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./public/data/images/");
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname);
  },
});

const upload = multer({ storage: storage });

router.post("/create", authorize(), createAccountEntries);
router.post("/update-debit-balances", authorize(), updateDebitBalances);
router.post("/update-credit-balances", authorize(), updateCreditBalances);
router.post("/revise-debit-balances", authorize(), reviseDebitBalances);
router.post("/revise-credit-balances", authorize(), reviseCreditBalances);
router.post("/saldo-balances", authorize(), saldoBalances);

module.exports = router;

function createAccountEntries(req, res, next) {
  accountEntriesServices
    .createAccountEntries(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
function updateCreditBalances(req, res, next) {
  accountEntriesServices
    .updateCreditBalances(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
function updateDebitBalances(req, res, next) {
  accountEntriesServices
    .updateDebitBalances(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
function reviseDebitBalances(req, res, next) {
  accountEntriesServices
    .reviseDebitBalances(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
function reviseCreditBalances(req, res, next) {
  accountEntriesServices
    .reviseCreditBalances(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
function saldoBalances(req, res, next) {
  accountEntriesServices
    .saldoBalances(req.body)
    .then((result) => res.json(result))
    .catch(next);
}
