add cancel invoice action

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-10-29 12:20:49 +03:00
committed by Daniel Hutzel
parent 52f00c62b7
commit a319199e10
5 changed files with 48 additions and 17 deletions

View File

@@ -72,7 +72,7 @@ entity Invoices {
billingCountry : String(40);
billingPostalCode : String(40);
total : Decimal(10, 2);
invoiceItems : Association to many InvoiceItems
invoiceItems : Composition of many InvoiceItems
on invoiceItems.invoice = $self;
status : Integer enum {
submitted = 1;

View File

@@ -7,7 +7,9 @@
"private": true,
"dependencies": {
"@sap/cds": "^4",
"express": "^4"
"express": "^4",
"moment": "^2.29.1",
"passport": "^0.4.1"
},
"devDependencies": {
"sqlite3": "^5"

View File

@@ -1,15 +1,18 @@
using {sap.capire.media.store as my} from '../db/schema';
@(requires : 'authenticated-user')
service Invoices {
service BrowseInvoices {
@readonly
entity MyInvoices as projection on my.Invoices;
entity Invoices as projection on my.Invoices;
@readonly
entity Tracks as projection on my.Tracks;
action invoice(tracks : array of {
ID : Integer;
unitPrice : Decimal(10, 2);
});
@readonly
entity InvoiceItems as projection on my.InvoiceItems;
action cancelInvoice(ID : Integer);
}

View File

@@ -1,4 +1,7 @@
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
module.exports = async function () {
const db = await cds.connect.to("db"); // connect to database service
@@ -15,13 +18,16 @@ module.exports = async function () {
);
});
this.on("READ", "MyInvoices", async (req) => {
this.on("READ", "Invoices", async (req) => {
return await db.run(req.query.where({ customer_ID: req.user.attr.ID }));
});
this.on("invoice", async (req) => {
const { tracks } = req.data;
const customerId = req.user.attr.ID;
const invoiceDate = moment().utc().format(DATE_TIME_PATTERN);
console.log("invoiceDate", invoiceDate);
console.log(invoiceDate);
const total = tracks.reduce(
(acc, { unitPrice }) => acc + Number(unitPrice),
0
@@ -37,8 +43,8 @@ module.exports = async function () {
const transaction = await db.tx(req);
await transaction.run(
INSERT.into(Invoices)
.columns("ID", "customer_ID", "total")
.values(lastInvoiceId + 1, customerId, total)
.columns("ID", "customer_ID", "total", "invoiceDate")
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
);
await transaction.run(
INSERT.into(InvoiceItems)
@@ -54,4 +60,32 @@ module.exports = async function () {
);
await transaction.commit();
});
this.on("cancelInvoice", async (req) => {
const { ID } = req.data;
const currentInvoice = await db.run(
SELECT.one(Invoices).where({
ID,
customer_ID: req.user.attr.ID,
})
);
if (!currentInvoice) {
req.reject(
404,
"Seems like you are not owning this invoice or it is not exists"
);
}
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(durationInHours.asHours());
if (durationInHours.asHours() > LEVERAGE_DURATION) {
req.reject(400, "Leverage time was expired");
}
return await db.run(DELETE.from(Invoices, ID));
});
};

View File

@@ -126,14 +126,6 @@ const logProcessArgs = () => {
password: "some",
}));
}
// for mock invoice data
if (srcEntityName === "Invoices") {
columns.push("status");
srcResultRows = srcResultRows.map((row) => ({
...row,
status: getRandomInt(-1, 2),
}));
}
const transaction = await targetStorage.tx();
await transaction.run(