From bcfce87276ea2ad83f610327119231fd266a3514 Mon Sep 17 00:00:00 2001 From: "Dzmitry_Tamashevich@epam.com" Date: Fri, 30 Oct 2020 13:26:38 +0300 Subject: [PATCH] add customer restriction when browsing invoices --- media-store/srv/browse-invoices-service.cds | 33 +++++++++++++++++++ ...-service.js => browse-invoices-service.js} | 26 ++++++++++----- media-store/srv/browse-tracks-service.js | 3 +- media-store/srv/invoices-service.cds | 18 ---------- media-store/util/importData.js | 7 ++++ 5 files changed, 59 insertions(+), 28 deletions(-) create mode 100644 media-store/srv/browse-invoices-service.cds rename media-store/srv/{invoices-service.js => browse-invoices-service.js} (81%) delete mode 100644 media-store/srv/invoices-service.cds diff --git a/media-store/srv/browse-invoices-service.cds b/media-store/srv/browse-invoices-service.cds new file mode 100644 index 00000000..c8d1532c --- /dev/null +++ b/media-store/srv/browse-invoices-service.cds @@ -0,0 +1,33 @@ +using {sap.capire.media.store as my} from '../db/schema'; +using {BrowseTracks.Tracks} from './browse-tracks-service'; + +@(requires : 'customer') +service BrowseInvoices { + @readonly + entity Invoices as projection on my.Invoices; + + action invoice(tracks : array of { + ID : Integer; + unitPrice : Decimal(10, 2); + }); + + action cancelInvoice(ID : Integer); + + @readonly + entity Tracks as projection on my.Tracks excluding { + alreadyOrdered + }; + + @readonly + entity Genres as projection on my.Genres { + * , tracks : redirected to Tracks + }; + + @readonly + entity Albums as projection on my.Albums { + * , tracks : redirected to Tracks + }; + + @readonly + entity Artists as projection on my.Artists; +} diff --git a/media-store/srv/invoices-service.js b/media-store/srv/browse-invoices-service.js similarity index 81% rename from media-store/srv/invoices-service.js rename to media-store/srv/browse-invoices-service.js index 99daafe1..a65832de 100644 --- a/media-store/srv/invoices-service.js +++ b/media-store/srv/browse-invoices-service.js @@ -1,7 +1,9 @@ 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 CANCEL_STATUS = -1; module.exports = async function () { const db = await cds.connect.to("db"); // connect to database service @@ -25,9 +27,7 @@ module.exports = async function () { 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 invoiceDate = moment(new Date(), DATE_TIME_PATTERN); const total = tracks.reduce( (acc, { unitPrice }) => acc + Number(unitPrice), 0 @@ -44,7 +44,7 @@ module.exports = async function () { await transaction.run( INSERT.into(Invoices) .columns("ID", "customer_ID", "total", "invoiceDate") - .values(lastInvoiceId + 1, customerId, total, invoiceDate) + .values(lastInvoiceId + 1, customerId, total, new Date(invoiceDate)) ); await transaction.run( INSERT.into(InvoiceItems) @@ -64,10 +64,12 @@ module.exports = async function () { 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, - }) + SELECT.one(Invoices) + .where({ + ID, + customer_ID: req.user.attr.ID, + }) + .columns("ID", "invoiceDate", "customer_ID") ); if (!currentInvoice) { req.reject( @@ -75,17 +77,23 @@ 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) { req.reject(400, "Leverage time was expired"); } - return await db.run(DELETE.from(Invoices, ID)); + return await db.run( + UPDATE(Invoices).set({ status: CANCEL_STATUS }).where({ ID }) + ); }); }; diff --git a/media-store/srv/browse-tracks-service.js b/media-store/srv/browse-tracks-service.js index e5d70755..2339e5a6 100644 --- a/media-store/srv/browse-tracks-service.js +++ b/media-store/srv/browse-tracks-service.js @@ -6,7 +6,8 @@ const selectTracksByEmail = (email) => ` join sap_capire_media_store_Invoices invoices on tracks.ID = invoiceItems.track_ID join sap_capire_media_store_InvoiceItems invoiceItems - on invoices.ID = invoiceItems.invoice_ID + on (invoices.ID = invoiceItems.invoice_ID and invoices.status='2') or + (invoices.ID = invoiceItems.invoice_ID and invoices.status='1') join sap_capire_media_store_Customers customers on customers.ID = invoices.customer_ID where customers.email='${email}' diff --git a/media-store/srv/invoices-service.cds b/media-store/srv/invoices-service.cds deleted file mode 100644 index 1dbaaca3..00000000 --- a/media-store/srv/invoices-service.cds +++ /dev/null @@ -1,18 +0,0 @@ -using {sap.capire.media.store as my} from '../db/schema'; - -@(requires : 'authenticated-user') -service BrowseInvoices { - - @readonly - 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); - }); - - action cancelInvoice(ID : Integer); -} diff --git a/media-store/util/importData.js b/media-store/util/importData.js index 5411ed41..e9a7214a 100644 --- a/media-store/util/importData.js +++ b/media-store/util/importData.js @@ -126,6 +126,13 @@ const logProcessArgs = () => { password: "some", })); } + if (srcEntityName === "Invoices") { + columns.push("status"); + srcResultRows = srcResultRows.map((row) => ({ + ...row, + status: 2, + })); + } const transaction = await targetStorage.tx(); await transaction.run(