From a319199e10161087be68acb5407c6c05216f8003 Mon Sep 17 00:00:00 2001 From: "Dzmitry_Tamashevich@epam.com" Date: Thu, 29 Oct 2020 12:20:49 +0300 Subject: [PATCH] add cancel invoice action --- media-store/db/schema.cds | 2 +- media-store/package.json | 4 ++- media-store/srv/invoices-service.cds | 11 +++++--- media-store/srv/invoices-service.js | 40 +++++++++++++++++++++++++--- media-store/util/importData.js | 8 ------ 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/media-store/db/schema.cds b/media-store/db/schema.cds index 74dba005..74fe4e89 100644 --- a/media-store/db/schema.cds +++ b/media-store/db/schema.cds @@ -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; diff --git a/media-store/package.json b/media-store/package.json index b74dbd29..71e0fea6 100644 --- a/media-store/package.json +++ b/media-store/package.json @@ -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" diff --git a/media-store/srv/invoices-service.cds b/media-store/srv/invoices-service.cds index 32bad092..1dbaaca3 100644 --- a/media-store/srv/invoices-service.cds +++ b/media-store/srv/invoices-service.cds @@ -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); } diff --git a/media-store/srv/invoices-service.js b/media-store/srv/invoices-service.js index 5bd3867f..99daafe1 100644 --- a/media-store/srv/invoices-service.js +++ b/media-store/srv/invoices-service.js @@ -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)); + }); }; diff --git a/media-store/util/importData.js b/media-store/util/importData.js index 43ebb948..5411ed41 100644 --- a/media-store/util/importData.js +++ b/media-store/util/importData.js @@ -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(