change readme.md. clean up console.logs. add check for exsisting invoices
This commit is contained in:
committed by
Daniel Hutzel
parent
fe0562f38b
commit
f439119e73
@@ -23,7 +23,7 @@ module.exports = async function () {
|
||||
|
||||
this.on("invoice", async (req) => {
|
||||
const { tracks } = req.data;
|
||||
const trackIds = tracks.map(({ ID }) => ID);
|
||||
const newInvoicedTracks = tracks.map(({ ID }) => ID);
|
||||
const customerId = req.user.attr.ID;
|
||||
const total = tracks.reduce(
|
||||
(acc, { unitPrice }) => acc + Number(unitPrice),
|
||||
@@ -33,6 +33,27 @@ module.exports = async function () {
|
||||
|
||||
const transaction = await db.tx(req);
|
||||
|
||||
// check if already exists
|
||||
const invoicedTracks = await transaction.run(
|
||||
SELECT.from(InvoiceItems)
|
||||
.columns("track_ID")
|
||||
.where(
|
||||
"invoice_ID in",
|
||||
SELECT("ID").from(Invoices).where({
|
||||
customer_ID: req.user.attr.ID,
|
||||
status: 1,
|
||||
})
|
||||
)
|
||||
);
|
||||
const isNewInvoiceHasInvoicedTracks = invoicedTracks.some(
|
||||
({ track_ID: curID }) => newInvoicedTracks.includes(curID)
|
||||
);
|
||||
if (isNewInvoiceHasInvoicedTracks) {
|
||||
await transaction.rollback();
|
||||
req.reject(400, "Invoice contains already owned values");
|
||||
return;
|
||||
}
|
||||
|
||||
// getting last ids for new records
|
||||
let { ID: lastInvoiceId } = await transaction.run(
|
||||
SELECT.one(Invoices).columns("ID").orderBy({ ID: "desc" })
|
||||
|
||||
@@ -3,8 +3,8 @@ const jwt = require("jsonwebtoken");
|
||||
const bcrypt = require("bcryptjs");
|
||||
|
||||
const { ACCESS_TOKEN_SECRET, REFRESH_TOKEN_SECRET } = cds.env;
|
||||
const ACCESS_TOKEN_EXP_IN = "10s";
|
||||
const REFRESH_TOKEN_EXPIRES_IN = "30s";
|
||||
const ACCESS_TOKEN_EXP_IN = "10m";
|
||||
const REFRESH_TOKEN_EXPIRES_IN = "20m";
|
||||
|
||||
const comparePasswords = async (password, hashedPassword) => {
|
||||
return new Promise((resolve, reject) =>
|
||||
|
||||
Reference in New Issue
Block a user