refactor import usage. refactor invoices implementation

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-11-03 12:22:02 +03:00
committed by Daniel Hutzel
parent bcfce87276
commit 3cf02cb567
9 changed files with 91 additions and 88 deletions

View File

@@ -1,10 +1,17 @@
const cds = require("@sap/cds");
const moment = require("moment");
const DATE_TIME_PATTERN = "YYYY-MM-DD HH:MM:SS";
const LEVERAGE_DURATION = 1; // in hours
const LEVERAGE_DURATION = 1; // in hours. should be the same in the frontend
const CANCEL_STATUS = -1;
// the same function there is in the frontend
const isLeverageTimeExpired = (invoiceDate) => {
const duration = moment.duration(
moment(moment().utc().format()).diff(invoiceDate)
);
return duration.asHours() > LEVERAGE_DURATION;
};
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
const { Invoices, InvoiceItems } = db.entities;
@@ -27,7 +34,7 @@ module.exports = async function () {
this.on("invoice", async (req) => {
const { tracks } = req.data;
const customerId = req.user.attr.ID;
const invoiceDate = moment(new Date(), DATE_TIME_PATTERN);
const invoiceDate = moment().utc().valueOf();
const total = tracks.reduce(
(acc, { unitPrice }) => acc + Number(unitPrice),
0
@@ -44,7 +51,7 @@ module.exports = async function () {
await transaction.run(
INSERT.into(Invoices)
.columns("ID", "customer_ID", "total", "invoiceDate")
.values(lastInvoiceId + 1, customerId, total, new Date(invoiceDate))
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
);
await transaction.run(
INSERT.into(InvoiceItems)
@@ -77,18 +84,8 @@ module.exports = async function () {
"Seems like you are not owning this invoice or it is not exists"
);
}
console.log(currentInvoice);
console.log(currentInvoice.invoiceDate);
const x = moment().utc().format(DATE_TIME_PATTERN);
const y = moment(currentInvoice.invoiceDate).format(DATE_TIME_PATTERN);
const yy = moment(x).diff(y);
const durationInHours = moment.duration(yy);
console.log(x);
console.log(y);
console.log(yy);
console.log(durationInHours.asHours());
if (durationInHours.asHours() > LEVERAGE_DURATION) {
if (isLeverageTimeExpired(currentInvoice.invoiceDate)) {
req.reject(400, "Leverage time was expired");
}