diff --git a/media-store/srv/browse-tracks-service.js b/media-store/srv/browse-tracks-service.js index 985534c3..e5d70755 100644 --- a/media-store/srv/browse-tracks-service.js +++ b/media-store/srv/browse-tracks-service.js @@ -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); diff --git a/media-store/srv/invoices-service.cds b/media-store/srv/invoices-service.cds index ecbfed00..550023e2 100644 --- a/media-store/srv/invoices-service.cds +++ b/media-store/srv/invoices-service.cds @@ -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); }); diff --git a/media-store/srv/invoices-service.js b/media-store/srv/invoices-service.js index 0e491e4a..1e874d90 100644 --- a/media-store/srv/invoices-service.js +++ b/media-store/srv/invoices-service.js @@ -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, ]) ) diff --git a/media-store/srv/media-service.cds b/media-store/srv/media-service.cds deleted file mode 100644 index 7eff28fc..00000000 --- a/media-store/srv/media-service.cds +++ /dev/null @@ -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; -} diff --git a/media-store/srv/user-service.cds b/media-store/srv/user-service.cds index 27515615..878b06c2 100644 --- a/media-store/srv/user-service.cds +++ b/media-store/srv/user-service.cds @@ -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; diff --git a/media-store/srv/user-service.js b/media-store/srv/user-service.js index d9a19084..be44365c 100644 --- a/media-store/srv/user-service.js +++ b/media-store/srv/user-service.js @@ -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],