add cancel invoice action
This commit is contained in:
committed by
Daniel Hutzel
parent
52f00c62b7
commit
a319199e10
@@ -72,7 +72,7 @@ entity Invoices {
|
|||||||
billingCountry : String(40);
|
billingCountry : String(40);
|
||||||
billingPostalCode : String(40);
|
billingPostalCode : String(40);
|
||||||
total : Decimal(10, 2);
|
total : Decimal(10, 2);
|
||||||
invoiceItems : Association to many InvoiceItems
|
invoiceItems : Composition of many InvoiceItems
|
||||||
on invoiceItems.invoice = $self;
|
on invoiceItems.invoice = $self;
|
||||||
status : Integer enum {
|
status : Integer enum {
|
||||||
submitted = 1;
|
submitted = 1;
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sap/cds": "^4",
|
"@sap/cds": "^4",
|
||||||
"express": "^4"
|
"express": "^4",
|
||||||
|
"moment": "^2.29.1",
|
||||||
|
"passport": "^0.4.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"sqlite3": "^5"
|
"sqlite3": "^5"
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
using {sap.capire.media.store as my} from '../db/schema';
|
using {sap.capire.media.store as my} from '../db/schema';
|
||||||
|
|
||||||
@(requires : 'authenticated-user')
|
@(requires : 'authenticated-user')
|
||||||
service Invoices {
|
service BrowseInvoices {
|
||||||
|
|
||||||
@readonly
|
@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 {
|
action invoice(tracks : array of {
|
||||||
ID : Integer;
|
ID : Integer;
|
||||||
unitPrice : Decimal(10, 2);
|
unitPrice : Decimal(10, 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
@readonly
|
action cancelInvoice(ID : Integer);
|
||||||
entity InvoiceItems as projection on my.InvoiceItems;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
const cds = require("@sap/cds");
|
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 () {
|
module.exports = async function () {
|
||||||
const db = await cds.connect.to("db"); // connect to database service
|
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 }));
|
return await db.run(req.query.where({ customer_ID: req.user.attr.ID }));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.on("invoice", async (req) => {
|
this.on("invoice", async (req) => {
|
||||||
const { tracks } = req.data;
|
const { tracks } = req.data;
|
||||||
const customerId = req.user.attr.ID;
|
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(
|
const total = tracks.reduce(
|
||||||
(acc, { unitPrice }) => acc + Number(unitPrice),
|
(acc, { unitPrice }) => acc + Number(unitPrice),
|
||||||
0
|
0
|
||||||
@@ -37,8 +43,8 @@ module.exports = async function () {
|
|||||||
const transaction = await db.tx(req);
|
const transaction = await db.tx(req);
|
||||||
await transaction.run(
|
await transaction.run(
|
||||||
INSERT.into(Invoices)
|
INSERT.into(Invoices)
|
||||||
.columns("ID", "customer_ID", "total")
|
.columns("ID", "customer_ID", "total", "invoiceDate")
|
||||||
.values(lastInvoiceId + 1, customerId, total)
|
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
|
||||||
);
|
);
|
||||||
await transaction.run(
|
await transaction.run(
|
||||||
INSERT.into(InvoiceItems)
|
INSERT.into(InvoiceItems)
|
||||||
@@ -54,4 +60,32 @@ module.exports = async function () {
|
|||||||
);
|
);
|
||||||
await transaction.commit();
|
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));
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -126,14 +126,6 @@ const logProcessArgs = () => {
|
|||||||
password: "some",
|
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();
|
const transaction = await targetStorage.tx();
|
||||||
await transaction.run(
|
await transaction.run(
|
||||||
|
|||||||
Reference in New Issue
Block a user