refactor import usage. refactor invoices implementation

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-11-03 12:22:02 +03:00
committed by Daniel Hutzel
parent bcfce87276
commit 3cf02cb567
9 changed files with 91 additions and 88 deletions

View File

@@ -1,7 +1,7 @@
namespace sap.capire.media.store; namespace sap.capire.media.store;
aspect Named { aspect Named {
key ID : Integer; key ID : Integer default 1;
name : String(120); name : String(120);
} }
@@ -76,7 +76,6 @@ entity Invoices {
on invoiceItems.invoice = $self; on invoiceItems.invoice = $self;
status : Integer enum { status : Integer enum {
submitted = 1; submitted = 1;
shipped = 2;
canceled = -1; canceled = -1;
} default 1; } default 1;
} }
@@ -96,9 +95,9 @@ entity Tracks {
mediaType : Association to MediaTypes; mediaType : Association to MediaTypes;
genre : Association to Genres; genre : Association to Genres;
composer : String(220); composer : String(220);
milliseconds : Integer; milliseconds : Integer default 230619;
bytes : Integer; bytes : Integer default 3990994;
unitPrice : Decimal(10, 2); unitPrice : Decimal(10, 2) default 0.99;
invoiceItems : Association to many InvoiceItems invoiceItems : Association to many InvoiceItems
on invoiceItems.track = $self; on invoiceItems.track = $self;
virtual alreadyOrdered : Boolean; virtual alreadyOrdered : Boolean;

View File

@@ -6,7 +6,7 @@
"license": "UNLICENSED", "license": "UNLICENSED",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@sap/cds": "^4", "@sap/cds": "^4.2.8",
"express": "^4", "express": "^4",
"moment": "^2.29.1", "moment": "^2.29.1",
"passport": "^0.4.1" "passport": "^0.4.1"
@@ -17,14 +17,14 @@
"scripts": { "scripts": {
"start": "npx cds run", "start": "npx cds run",
"deploy": "cds deploy --to sqlite:mychinook.db", "deploy": "cds deploy --to sqlite:mychinook.db",
"import": "node ./util/importData.js sqlite:chinook.db sqlite:mychinook.db ./db/schema.cds", "rebuild": "npm run deploy && npm run start",
"deploy:import": "npm run deploy && npm run import",
"test": "mocha test/media-service.test.js --verbose --timeout 10000" "test": "mocha test/media-service.test.js --verbose --timeout 10000"
}, },
"cds": { "cds": {
"requires": { "requires": {
"db": { "db": {
"kind": "sqlite", "kind": "sqlite",
"model": "*",
"credentials": { "credentials": {
"database": "mychinook.db" "database": "mychinook.db"
} }

View File

@@ -1,4 +1,5 @@
const cds = require("@sap/cds"); const cds = require("@sap/cds");
const { importData } = require("./util/importData");
// handle bootstrapping events... // handle bootstrapping events...
cds.on("bootstrap", (app) => { cds.on("bootstrap", (app) => {
// dev only // dev only
@@ -24,7 +25,8 @@ cds.on("bootstrap", (app) => {
}); });
// add your own middleware before any by cds are added // add your own middleware before any by cds are added
}); });
cds.on("served", () => { cds.on("served", async ({ db }) => {
await importData(db);
// add more middleware after all CDS servies // add more middleware after all CDS servies
}); });
// delegate to default server.js: // delegate to default server.js:

View File

@@ -1,10 +1,17 @@
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 LEVERAGE_DURATION = 1; // in hours. should be the same in the frontend
const LEVERAGE_DURATION = 1; // in hours
const CANCEL_STATUS = -1; 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 () { 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, InvoiceItems } = db.entities; const { Invoices, InvoiceItems } = db.entities;
@@ -27,7 +34,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(new Date(), DATE_TIME_PATTERN); const invoiceDate = moment().utc().valueOf();
const total = tracks.reduce( const total = tracks.reduce(
(acc, { unitPrice }) => acc + Number(unitPrice), (acc, { unitPrice }) => acc + Number(unitPrice),
0 0
@@ -44,7 +51,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, new Date(invoiceDate)) .values(lastInvoiceId + 1, customerId, total, invoiceDate)
); );
await transaction.run( await transaction.run(
INSERT.into(InvoiceItems) INSERT.into(InvoiceItems)
@@ -77,18 +84,8 @@ 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);
const x = moment().utc().format(DATE_TIME_PATTERN); if (isLeverageTimeExpired(currentInvoice.invoiceDate)) {
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) {
req.reject(400, "Leverage time was expired"); req.reject(400, "Leverage time was expired");
} }

View File

@@ -28,16 +28,16 @@ module.exports = async function () {
}); });
this.on("READ", "MarkedTracks", async (req) => { this.on("READ", "MarkedTracks", async (req) => {
const myTrackIds = ( const myTrackIds = (await db.run(selectTracksByEmail(req.user.id))).map(
await db.run(cds.parse.cql(selectTracksByEmail(req.user.id))) ({ ID }) => ID
).map(({ ID }) => ID); );
const result = [];
const result = await db.run(req.query); await db.foreach(req.query, (track) => {
return result.map((columns) => { result.push({
return { ...track,
...columns, alreadyOrdered: myTrackIds.includes(track.ID),
alreadyOrdered: myTrackIds.includes(columns.ID), });
};
}); });
return result;
}); });
}; };

View 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;
}

View 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
});
};

View File

@@ -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;
}

View File

@@ -3,20 +3,9 @@ const cds = require("@sap/cds");
const FIRST_INDEX = 0; const FIRST_INDEX = 0;
const ZERO_VALUE = 0; const ZERO_VALUE = 0;
const SECOND_INDEX = 1; const SECOND_INDEX = 1;
const SKIP_CLI_ARGS_NUMBER = 2;
const THIRD_INDEX = 2;
const ID_IF_NOT_FOUND = "ID"; const ID_IF_NOT_FOUND = "ID";
const args = process.argv.slice(SKIP_CLI_ARGS_NUMBER); const SRC_STORAGE_NAME = "sqlite:chinook.db";
const SRC_STORAGE_NAME = args[FIRST_INDEX];
const TARGET_STORAGE_NAME = args[SECOND_INDEX];
const TARGET_SCHEMA_PATH = args[THIRD_INDEX];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const camelCaseToSnake = (str) => const camelCaseToSnake = (str) =>
str.replace( str.replace(
@@ -65,12 +54,6 @@ const constructInsertQuery = (targetEntityName) => {
INSERT.into(targetEntityName).columns(columns).rows(row); INSERT.into(targetEntityName).columns(columns).rows(row);
}; };
const logProcessArgs = () => {
console.log(
`[LOG]: Import data from ${SRC_STORAGE_NAME} to ${TARGET_STORAGE_NAME}, schema path: ${TARGET_SCHEMA_PATH}`
);
};
/** /**
* The MAIN ISSUE such import is that it depends on: * The MAIN ISSUE such import is that it depends on:
* - snake case table names * - snake case table names
@@ -78,38 +61,43 @@ const logProcessArgs = () => {
* of chinook.db * of chinook.db
* There is 'Launch import' task in .vscode folder for debugging. * There is 'Launch import' task in .vscode folder for debugging.
*/ */
(async () => { async function importData(targetDb) {
logProcessArgs();
try { try {
const srcStorage = await cds.connect.to(SRC_STORAGE_NAME); const srcStorage = await cds.connect.to(SRC_STORAGE_NAME);
const targetStorage = await cds.connect.to(TARGET_STORAGE_NAME); const targetCSNEntities = Object.values(targetDb.entities);
const targetCSNModel = await cds.load(TARGET_SCHEMA_PATH); const targetCSNEntitiesNames = Object.keys(targetDb.entities);
const reflectedCSNModel = cds.reflect(targetCSNModel); const someEntry = await targetDb.run(
const targetCSNEntities = Object.values(reflectedCSNModel.entities); SELECT.one(targetCSNEntitiesNames[FIRST_INDEX])
);
if (!!someEntry) {
return;
}
for (index in targetCSNEntities) { for (index in targetCSNEntities) {
const { name: targetEntityName, elements } = targetCSNEntities[index]; const targetEntityName = targetCSNEntitiesNames[index];
console.log(`[LOG]: Processing ${targetEntityName}`); console.log(`[LOG]: Processing ${targetEntityName}`);
const targetColumns = elementsToColumns(elements); // e.g. ['ID', ..., 'total', 'customer_ID']
const insertQuery = constructInsertQuery(targetEntityName, targetColumns);
const srcEntityName = camelCaseToSnake(targetEntityName.split(".").pop()); const { elements } = targetCSNEntities[index];
const targetColumns = elementsToColumns(elements); // e.g. ['ID', ..., 'total', 'customer_ID']
const srcEntityName = camelCaseToSnake(targetEntityName);
const insertQuery = constructInsertQuery(targetEntityName);
let srcResultRows; let srcResultRows;
try { try {
srcResultRows = await srcStorage.read(srcEntityName); // e.g. [ { AlbumId:1, ArtistId:1, Title:'some' }, ... ] srcResultRows = await srcStorage.run(`
SELECT * from ${srcEntityName}
`); // e.g. [ { AlbumId:1, ArtistId:1, Title:'some' }, ... ]
} catch (e) { } catch (e) {
console.log("[ERROR]: while trying to read source table", e.message); console.log("[ERROR]: while trying to read source table", e.message);
continue; continue;
} }
if (!srcResultRows || srcResultRows.length < ZERO_VALUE) { if (!srcResultRows || srcResultRows.length < ZERO_VALUE) {
console.log( console.log(
`[LOG] Skipping ${targetEntityName}. `[LOG] Skipping ${targetEntityName}.
There is no data provided in ${SRC_STORAGE_NAME}, ${srcEntityName}` There is no data provided in ${SRC_STORAGE_NAME}, ${srcEntityName}`
); );
continue; continue;
} }
const srcColumns = Object.keys(srcResultRows[FIRST_INDEX]); const srcColumns = Object.keys(srcResultRows[FIRST_INDEX]);
const columns = reorderTargetColumns(srcColumns, targetColumns); const columns = reorderTargetColumns(srcColumns, targetColumns);
if (new Set(columns).size !== columns.length) { if (new Set(columns).size !== columns.length) {
@@ -117,7 +105,6 @@ const logProcessArgs = () => {
`Some ${targetEntityName} column name is mismatched in ${SRC_STORAGE_NAME} ${srcEntityName}` `Some ${targetEntityName} column name is mismatched in ${SRC_STORAGE_NAME} ${srcEntityName}`
); );
} }
// for mock auth // for mock auth
if (srcEntityName === "Employees" || srcEntityName === "Customers") { if (srcEntityName === "Employees" || srcEntityName === "Customers") {
columns.push("password"); columns.push("password");
@@ -126,21 +113,17 @@ 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 targetDb.tx();
await transaction.run( await transaction.run(
srcResultRows.map((row) => insertQuery(Object.values(row), columns)) srcResultRows.map((row) => insertQuery(Object.values(row), columns))
); );
await transaction.commit(); await transaction.commit();
} }
console.log("[LOG]: ", "Import completed");
} catch (errors) { } catch (errors) {
console.error(errors); console.error(errors);
} }
})(); }
module.exports = { importData };