add customer restriction when browsing invoices
This commit is contained in:
committed by
Daniel Hutzel
parent
a319199e10
commit
bcfce87276
33
media-store/srv/browse-invoices-service.cds
Normal file
33
media-store/srv/browse-invoices-service.cds
Normal file
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
const cds = require("@sap/cds");
|
const cds = require("@sap/cds");
|
||||||
const moment = require("moment");
|
const moment = require("moment");
|
||||||
|
|
||||||
const DATE_TIME_PATTERN = "YYYY-MM-DD HH:MM:SS";
|
const DATE_TIME_PATTERN = "YYYY-MM-DD HH:MM:SS";
|
||||||
const LEVERAGE_DURATION = 1; // in hours
|
const LEVERAGE_DURATION = 1; // in hours
|
||||||
|
const CANCEL_STATUS = -1;
|
||||||
|
|
||||||
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
|
||||||
@@ -25,9 +27,7 @@ module.exports = async function () {
|
|||||||
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);
|
const invoiceDate = moment(new Date(), 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
|
||||||
@@ -44,7 +44,7 @@ module.exports = async function () {
|
|||||||
await transaction.run(
|
await transaction.run(
|
||||||
INSERT.into(Invoices)
|
INSERT.into(Invoices)
|
||||||
.columns("ID", "customer_ID", "total", "invoiceDate")
|
.columns("ID", "customer_ID", "total", "invoiceDate")
|
||||||
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
|
.values(lastInvoiceId + 1, customerId, total, new Date(invoiceDate))
|
||||||
);
|
);
|
||||||
await transaction.run(
|
await transaction.run(
|
||||||
INSERT.into(InvoiceItems)
|
INSERT.into(InvoiceItems)
|
||||||
@@ -64,10 +64,12 @@ module.exports = async function () {
|
|||||||
this.on("cancelInvoice", async (req) => {
|
this.on("cancelInvoice", async (req) => {
|
||||||
const { ID } = req.data;
|
const { ID } = req.data;
|
||||||
const currentInvoice = await db.run(
|
const currentInvoice = await db.run(
|
||||||
SELECT.one(Invoices).where({
|
SELECT.one(Invoices)
|
||||||
ID,
|
.where({
|
||||||
customer_ID: req.user.attr.ID,
|
ID,
|
||||||
})
|
customer_ID: req.user.attr.ID,
|
||||||
|
})
|
||||||
|
.columns("ID", "invoiceDate", "customer_ID")
|
||||||
);
|
);
|
||||||
if (!currentInvoice) {
|
if (!currentInvoice) {
|
||||||
req.reject(
|
req.reject(
|
||||||
@@ -75,17 +77,23 @@ module.exports = async function () {
|
|||||||
"Seems like you are not owning this invoice or it is not exists"
|
"Seems like you are not owning this invoice or it is not exists"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
console.log(currentInvoice);
|
||||||
console.log(currentInvoice.invoiceDate);
|
console.log(currentInvoice.invoiceDate);
|
||||||
|
|
||||||
const x = moment().utc().format(DATE_TIME_PATTERN);
|
const x = moment().utc().format(DATE_TIME_PATTERN);
|
||||||
const y = moment(currentInvoice.invoiceDate).format(DATE_TIME_PATTERN);
|
const y = moment(currentInvoice.invoiceDate).format(DATE_TIME_PATTERN);
|
||||||
const yy = moment(x).diff(y);
|
const yy = moment(x).diff(y);
|
||||||
const durationInHours = moment.duration(yy);
|
const durationInHours = moment.duration(yy);
|
||||||
|
console.log(x);
|
||||||
|
console.log(y);
|
||||||
|
console.log(yy);
|
||||||
console.log(durationInHours.asHours());
|
console.log(durationInHours.asHours());
|
||||||
if (durationInHours.asHours() > LEVERAGE_DURATION) {
|
if (durationInHours.asHours() > LEVERAGE_DURATION) {
|
||||||
req.reject(400, "Leverage time was expired");
|
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 })
|
||||||
|
);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -6,7 +6,8 @@ const selectTracksByEmail = (email) => `
|
|||||||
join sap_capire_media_store_Invoices invoices
|
join sap_capire_media_store_Invoices invoices
|
||||||
on tracks.ID = invoiceItems.track_ID
|
on tracks.ID = invoiceItems.track_ID
|
||||||
join sap_capire_media_store_InvoiceItems invoiceItems
|
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
|
join sap_capire_media_store_Customers customers
|
||||||
on customers.ID = invoices.customer_ID
|
on customers.ID = invoices.customer_ID
|
||||||
where customers.email='${email}'
|
where customers.email='${email}'
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -126,6 +126,13 @@ const logProcessArgs = () => {
|
|||||||
password: "some",
|
password: "some",
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
if (srcEntityName === "Invoices") {
|
||||||
|
columns.push("status");
|
||||||
|
srcResultRows = srcResultRows.map((row) => ({
|
||||||
|
...row,
|
||||||
|
status: 2,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
const transaction = await targetStorage.tx();
|
const transaction = await targetStorage.tx();
|
||||||
await transaction.run(
|
await transaction.run(
|
||||||
|
|||||||
Reference in New Issue
Block a user