refactor import usage. refactor invoices implementation
This commit is contained in:
committed by
Daniel Hutzel
parent
bcfce87276
commit
3cf02cb567
@@ -1,10 +1,17 @@
|
||||
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 LEVERAGE_DURATION = 1; // in hours. should be the same in the frontend
|
||||
const CANCEL_STATUS = -1;
|
||||
|
||||
// the same function there is in the frontend
|
||||
const isLeverageTimeExpired = (invoiceDate) => {
|
||||
const duration = moment.duration(
|
||||
moment(moment().utc().format()).diff(invoiceDate)
|
||||
);
|
||||
return duration.asHours() > LEVERAGE_DURATION;
|
||||
};
|
||||
|
||||
module.exports = async function () {
|
||||
const db = await cds.connect.to("db"); // connect to database service
|
||||
const { Invoices, InvoiceItems } = db.entities;
|
||||
@@ -27,7 +34,7 @@ module.exports = async function () {
|
||||
this.on("invoice", async (req) => {
|
||||
const { tracks } = req.data;
|
||||
const customerId = req.user.attr.ID;
|
||||
const invoiceDate = moment(new Date(), DATE_TIME_PATTERN);
|
||||
const invoiceDate = moment().utc().valueOf();
|
||||
const total = tracks.reduce(
|
||||
(acc, { unitPrice }) => acc + Number(unitPrice),
|
||||
0
|
||||
@@ -44,7 +51,7 @@ module.exports = async function () {
|
||||
await transaction.run(
|
||||
INSERT.into(Invoices)
|
||||
.columns("ID", "customer_ID", "total", "invoiceDate")
|
||||
.values(lastInvoiceId + 1, customerId, total, new Date(invoiceDate))
|
||||
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
|
||||
);
|
||||
await transaction.run(
|
||||
INSERT.into(InvoiceItems)
|
||||
@@ -77,18 +84,8 @@ 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) {
|
||||
if (isLeverageTimeExpired(currentInvoice.invoiceDate)) {
|
||||
req.reject(400, "Leverage time was expired");
|
||||
}
|
||||
|
||||
|
||||
@@ -28,16 +28,16 @@ module.exports = async function () {
|
||||
});
|
||||
|
||||
this.on("READ", "MarkedTracks", async (req) => {
|
||||
const myTrackIds = (
|
||||
await db.run(cds.parse.cql(selectTracksByEmail(req.user.id)))
|
||||
).map(({ ID }) => ID);
|
||||
|
||||
const result = await db.run(req.query);
|
||||
return result.map((columns) => {
|
||||
return {
|
||||
...columns,
|
||||
alreadyOrdered: myTrackIds.includes(columns.ID),
|
||||
};
|
||||
const myTrackIds = (await db.run(selectTracksByEmail(req.user.id))).map(
|
||||
({ ID }) => ID
|
||||
);
|
||||
const result = [];
|
||||
await db.foreach(req.query, (track) => {
|
||||
result.push({
|
||||
...track,
|
||||
alreadyOrdered: myTrackIds.includes(track.ID),
|
||||
});
|
||||
});
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
9
media-store/srv/manage-store-service.cds
Normal file
9
media-store/srv/manage-store-service.cds
Normal file
@@ -0,0 +1,9 @@
|
||||
using {sap.capire.media.store as my} from '../db/schema';
|
||||
|
||||
@(requires : 'employee')
|
||||
service ManageStore {
|
||||
entity Tracks as projection on my.Tracks;
|
||||
action addTrack(name : String(25), albumTitle : String(255), genreName : String(255), composer : String(255));
|
||||
entity Albums as projection on my.Albums;
|
||||
entity Genres as projection on my.Genres;
|
||||
}
|
||||
26
media-store/srv/manage-store-service.js
Normal file
26
media-store/srv/manage-store-service.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const cds = require("@sap/cds");
|
||||
|
||||
module.exports = async function () {
|
||||
const db = await cds.connect.to("db"); // connect to database service
|
||||
|
||||
const { Genres, Albums } = db.entities;
|
||||
|
||||
this.before("*", (req) => {
|
||||
console.log(
|
||||
"[USER]:",
|
||||
req.user.id,
|
||||
" [LEVEL]: ",
|
||||
req.user.attr.level,
|
||||
"[ROLE]",
|
||||
req.user.is("user") ? "user" : "other"
|
||||
);
|
||||
});
|
||||
|
||||
this.on("addTrack", async (req) => {
|
||||
const { albumTitle, genreName, name: trackName, composer } = req.data;
|
||||
|
||||
const genre = await db.run(SELECT.one(Genres).where({ name: genreName }));
|
||||
const album = await db.run(SELECT.one(Albums).where({ title: albumTitle }));
|
||||
// todo impl
|
||||
});
|
||||
};
|
||||
@@ -1,13 +0,0 @@
|
||||
using {sap.capire.media.store as my} from '../db/schema';
|
||||
|
||||
@(requires : 'authenticated-user')
|
||||
service ManageTracks {
|
||||
@(restrict : [{
|
||||
grant : [
|
||||
'READ',
|
||||
'WRITE'
|
||||
],
|
||||
to : 'employee'
|
||||
}, ])
|
||||
entity Genres as projection on my.Genres;
|
||||
}
|
||||
Reference in New Issue
Block a user