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");
|
||||
|
||||
// only for demo cds.run(string, args)
|
||||
const SELECT_INVOICES_BY_EMAIL = `
|
||||
select invoice.ID
|
||||
from sap_capire_media_store_Invoices invoice
|
||||
join sap_capire_media_store_Customers customer
|
||||
on customer.ID = invoice.customer_ID
|
||||
where customer.email=?
|
||||
const selectTracksByEmail = (email) => `
|
||||
select tracks.ID
|
||||
from sap_capire_media_store_Tracks tracks
|
||||
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
|
||||
join sap_capire_media_store_Customers customers
|
||||
on customers.ID = invoices.customer_ID
|
||||
where customers.email='${email}'
|
||||
`;
|
||||
|
||||
module.exports = async function () {
|
||||
const db = await cds.connect.to("db"); // connect to database service
|
||||
const { Invoices } = db.entities;
|
||||
|
||||
this.before("*", (req) => {
|
||||
console.log(
|
||||
@@ -26,11 +28,7 @@ module.exports = async function () {
|
||||
|
||||
this.on("READ", "MarkedTracks", async (req) => {
|
||||
const myTrackIds = (
|
||||
await db.run(
|
||||
SELECT.from(Invoices)
|
||||
.columns("ID")
|
||||
.where({ customer_ID: req.user.attr.ID })
|
||||
)
|
||||
await db.run(cds.parse.cql(selectTracksByEmail(req.user.id)))
|
||||
).map(({ ID }) => ID);
|
||||
|
||||
const result = await db.run(req.query);
|
||||
|
||||
@@ -6,7 +6,7 @@ service InvoicesService {
|
||||
entity Invoices as projection on my.Invoices;
|
||||
|
||||
action invoice(tracks : array of {
|
||||
track_ID : Integer;
|
||||
ID : Integer;
|
||||
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) => {
|
||||
const { tracks } = req.data;
|
||||
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(
|
||||
SELECT.one(InvoiceItems).columns("ID").orderBy({ ID: "desc" })
|
||||
@@ -37,10 +44,10 @@ module.exports = async function () {
|
||||
INSERT.into(InvoiceItems)
|
||||
.columns("ID", "invoice_ID", "track_ID", "unitPrice")
|
||||
.rows(
|
||||
tracks.map(({ track_ID, unitPrice }, index) => [
|
||||
tracks.map(({ ID, unitPrice }, index) => [
|
||||
lastInvoiceItemId + (index + 1),
|
||||
lastInvoiceId + 1,
|
||||
track_ID,
|
||||
ID,
|
||||
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';
|
||||
|
||||
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 {
|
||||
roles : array of String(111);
|
||||
level : Integer;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
const cds = require("@sap/cds");
|
||||
|
||||
const USER_LEVELS = { customer: 1, employee: 2 };
|
||||
|
||||
module.exports = async function () {
|
||||
const db = await cds.connect.to("db"); // connect to database service
|
||||
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) => {
|
||||
const { email, password } = req.data;
|
||||
|
||||
@@ -30,7 +51,7 @@ module.exports = async function () {
|
||||
|
||||
return {
|
||||
mockedToken: Buffer.from(`${email}:${password}`).toString("base64"),
|
||||
level: role === "customer" ? 1 : 2,
|
||||
level: USER_LEVELS[role],
|
||||
email: userFromDb.email,
|
||||
ID: userFromDb.ID,
|
||||
roles: [role],
|
||||
|
||||
Reference in New Issue
Block a user