add getUser method
This commit is contained in:
committed by
Daniel Hutzel
parent
0690762207
commit
30d5c789bc
@@ -1,17 +1,19 @@
|
|||||||
const cds = require("@sap/cds");
|
const cds = require("@sap/cds");
|
||||||
|
|
||||||
// only for demo cds.run(string, args)
|
const selectTracksByEmail = (email) => `
|
||||||
const SELECT_INVOICES_BY_EMAIL = `
|
select tracks.ID
|
||||||
select invoice.ID
|
from sap_capire_media_store_Tracks tracks
|
||||||
from sap_capire_media_store_Invoices invoice
|
join sap_capire_media_store_Invoices invoices
|
||||||
join sap_capire_media_store_Customers customer
|
on tracks.ID = invoiceItems.track_ID
|
||||||
on customer.ID = invoice.customer_ID
|
join sap_capire_media_store_InvoiceItems invoiceItems
|
||||||
where customer.email=?
|
on invoices.ID = invoiceItems.invoice_ID
|
||||||
|
join sap_capire_media_store_Customers customers
|
||||||
|
on customers.ID = invoices.customer_ID
|
||||||
|
where customers.email='${email}'
|
||||||
`;
|
`;
|
||||||
|
|
||||||
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
|
||||||
const { Invoices } = db.entities;
|
|
||||||
|
|
||||||
this.before("*", (req) => {
|
this.before("*", (req) => {
|
||||||
console.log(
|
console.log(
|
||||||
@@ -26,11 +28,7 @@ module.exports = async function () {
|
|||||||
|
|
||||||
this.on("READ", "MarkedTracks", async (req) => {
|
this.on("READ", "MarkedTracks", async (req) => {
|
||||||
const myTrackIds = (
|
const myTrackIds = (
|
||||||
await db.run(
|
await db.run(cds.parse.cql(selectTracksByEmail(req.user.id)))
|
||||||
SELECT.from(Invoices)
|
|
||||||
.columns("ID")
|
|
||||||
.where({ customer_ID: req.user.attr.ID })
|
|
||||||
)
|
|
||||||
).map(({ ID }) => ID);
|
).map(({ ID }) => ID);
|
||||||
|
|
||||||
const result = await db.run(req.query);
|
const result = await db.run(req.query);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ service InvoicesService {
|
|||||||
entity Invoices as projection on my.Invoices;
|
entity Invoices as projection on my.Invoices;
|
||||||
|
|
||||||
action invoice(tracks : array of {
|
action invoice(tracks : array of {
|
||||||
track_ID : Integer;
|
ID : Integer;
|
||||||
unitPrice : Decimal(10, 2);
|
unitPrice : Decimal(10, 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,17 @@ module.exports = async function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("READ", "Invoices", async (req) => {
|
||||||
|
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 total = tracks.reduce((acc, { unitPrice }) => acc + unitPrice, 0);
|
const total = tracks.reduce(
|
||||||
|
(acc, { unitPrice }) => acc + Number(unitPrice),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
const { ID: lastInvoiceItemId } = await db.run(
|
const { ID: lastInvoiceItemId } = await db.run(
|
||||||
SELECT.one(InvoiceItems).columns("ID").orderBy({ ID: "desc" })
|
SELECT.one(InvoiceItems).columns("ID").orderBy({ ID: "desc" })
|
||||||
@@ -37,10 +44,10 @@ module.exports = async function () {
|
|||||||
INSERT.into(InvoiceItems)
|
INSERT.into(InvoiceItems)
|
||||||
.columns("ID", "invoice_ID", "track_ID", "unitPrice")
|
.columns("ID", "invoice_ID", "track_ID", "unitPrice")
|
||||||
.rows(
|
.rows(
|
||||||
tracks.map(({ track_ID, unitPrice }, index) => [
|
tracks.map(({ ID, unitPrice }, index) => [
|
||||||
lastInvoiceItemId + (index + 1),
|
lastInvoiceItemId + (index + 1),
|
||||||
lastInvoiceId + 1,
|
lastInvoiceId + 1,
|
||||||
track_ID,
|
ID,
|
||||||
unitPrice,
|
unitPrice,
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
using {sap.capire.media.store as my} from '../db/schema';
|
|
||||||
|
|
||||||
service MediaService {
|
|
||||||
entity Employees as projection on my.Employees;
|
|
||||||
entity Customers as projection on my.Customers;
|
|
||||||
entity Albums as projection on my.Albums;
|
|
||||||
entity Artists as projection on my.Artists;
|
|
||||||
entity Genres as projection on my.Genres;
|
|
||||||
entity InvoiceItems as projection on my.InvoiceItems;
|
|
||||||
entity Invoices as projection on my.Invoices;
|
|
||||||
entity MediaTypes as projection on my.MediaTypes;
|
|
||||||
entity PlaylistTrack as projection on my.PlaylistTrack;
|
|
||||||
entity Playlists as projection on my.Playlists;
|
|
||||||
// @(restrict : [{
|
|
||||||
// grant : '*',
|
|
||||||
// where : '$user.level > 1'
|
|
||||||
// }])
|
|
||||||
entity Tracks as projection on my.Tracks;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,27 @@
|
|||||||
using {sap.capire.media.store as my} from '../db/schema';
|
using {sap.capire.media.store as my} from '../db/schema';
|
||||||
|
|
||||||
service UserService {
|
service UserService {
|
||||||
|
|
||||||
|
@restrict : [{
|
||||||
|
grant : '*',
|
||||||
|
to : 'employee'
|
||||||
|
}]
|
||||||
|
entity Customers as projection on my.Customers;
|
||||||
|
|
||||||
|
@(requires : 'authenticated-user')
|
||||||
|
function getUser() returns {
|
||||||
|
lastName : String(20);
|
||||||
|
firstName : String(40);
|
||||||
|
city : String(40);
|
||||||
|
state : String(40);
|
||||||
|
address : String(70);
|
||||||
|
country : String(40);
|
||||||
|
postalCode : String(10);
|
||||||
|
phone : String(24);
|
||||||
|
fax : String(24);
|
||||||
|
email : String(60);
|
||||||
|
};
|
||||||
|
|
||||||
function mockLogin(email : String(111), password : String(200)) returns {
|
function mockLogin(email : String(111), password : String(200)) returns {
|
||||||
roles : array of String(111);
|
roles : array of String(111);
|
||||||
level : Integer;
|
level : Integer;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
const cds = require("@sap/cds");
|
const cds = require("@sap/cds");
|
||||||
|
|
||||||
|
const USER_LEVELS = { customer: 1, employee: 2 };
|
||||||
|
|
||||||
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
|
||||||
const { Employees, Customers } = db.entities;
|
const { Employees, Customers } = db.entities;
|
||||||
@@ -15,6 +17,25 @@ module.exports = async function () {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.on("getUser", async (req) => {
|
||||||
|
return await db.run(
|
||||||
|
SELECT.one(Customers)
|
||||||
|
.columns(
|
||||||
|
"lastName",
|
||||||
|
"firstName",
|
||||||
|
"city",
|
||||||
|
"state",
|
||||||
|
"address",
|
||||||
|
"country",
|
||||||
|
"postalCode",
|
||||||
|
"phone",
|
||||||
|
"fax",
|
||||||
|
"email"
|
||||||
|
)
|
||||||
|
.where({ email: req.user.id })
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
this.on("mockLogin", async (req) => {
|
this.on("mockLogin", async (req) => {
|
||||||
const { email, password } = req.data;
|
const { email, password } = req.data;
|
||||||
|
|
||||||
@@ -30,7 +51,7 @@ module.exports = async function () {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
mockedToken: Buffer.from(`${email}:${password}`).toString("base64"),
|
mockedToken: Buffer.from(`${email}:${password}`).toString("base64"),
|
||||||
level: role === "customer" ? 1 : 2,
|
level: USER_LEVELS[role],
|
||||||
email: userFromDb.email,
|
email: userFromDb.email,
|
||||||
ID: userFromDb.ID,
|
ID: userFromDb.ID,
|
||||||
roles: [role],
|
roles: [role],
|
||||||
|
|||||||
Reference in New Issue
Block a user