change invoiceDate impl

This commit is contained in:
Dzmitry_Tamashevich@epam.com
2020-11-16 16:44:35 +03:00
committed by Daniel Hutzel
parent 7045914e57
commit 185e6b939f
8 changed files with 47 additions and 43 deletions

View File

@@ -1 +0,0 @@
API=http://localhost:4004/

View File

@@ -3,7 +3,8 @@ import axios from "axios";
// in dev mode using provided api
// in prod mode using proxy
const API = process.env.API || "api/";
const API =
process.env.NODE_ENV === "development" ? "http://localhost:4004/" : "api/";
const BROWSE_TRACKS_SERVICE = `${API}browse-tracks`;
const INVOICES_SERVICE = `${API}browse-invoices`;

View File

@@ -5,8 +5,6 @@ import { useHistory } from "react-router-dom";
import { useGlobals } from "../../GlobalContext";
import { useErrors } from "../../useErrors";
const USER_SERVICE = "http://localhost:4004/users";
const layout = {
labelCol: {
span: 8,

View File

@@ -1,6 +1,6 @@
import React, { useEffect } from "react";
import { Form, Input, Select } from "antd";
// import { useSearch } from "@umijs/hooks";
import { useSearch } from "@umijs/hooks";
import { useErrors } from "../../useErrors";
import { fetchArtistsByName } from "../../api-service";
@@ -20,16 +20,16 @@ const getArtists = function (value) {
const AddAlbumForm = () => {
const { handleError } = useErrors();
// const {
// data: artists,
// loading: isArtistsLoading,
// onChange: onChangeArtistInput,
// cancel: onArtistCancel,
// } = useSearch(getArtists.bind({ handleError }));
const {
data: artists,
loading: isArtistsLoading,
onChange: onChangeArtistInput,
cancel: onArtistCancel,
} = useSearch(getArtists.bind({ handleError }));
// useEffect(() => {
// onChangeArtistInput();
// }, []);
useEffect(() => {
onChangeArtistInput();
}, []);
return (
<>
@@ -38,7 +38,7 @@ const AddAlbumForm = () => {
<Input />
</Form.Item>
<Form.Item label="Artist" name="artistID" rules={REQUIRED}>
{/* <Select
<Select
showSearch
placeholder="Select artist"
filterOption={false}
@@ -53,7 +53,7 @@ const AddAlbumForm = () => {
{artist.name}
</Select.Option>
))}
</Select> */}
</Select>
</Form.Item>
</>
);

View File

@@ -24,6 +24,7 @@ const INVOICE_STATUS = {
};
const CANCELLED_STATUS = -1;
const DATE_TIME_FORMAT_PATTERN = "LLLL";
const UTC_DATE_TIME_FORMAT = "YYYY-MM-DDThh:mm:ss";
const INVOICE_ITEMS_COLUMNS = [
{
title: "Track name",
@@ -45,16 +46,16 @@ const INVOICE_ITEMS_COLUMNS = [
const LEVERAGE_DURATION = 1; // in hours
const STATUSES = { submitted: 1, shipped: 2, canceled: -1 };
const isLeverageTimeExpired = (invoiceDate) => {
const isLeverageTimeExpired = (utcNowTimestamp, invoiceDate) => {
const duration = moment.duration(
moment(moment().utc().format()).diff(invoiceDate)
moment(utcNowTimestamp).diff(moment(invoiceDate).valueOf())
);
return duration.asHours() > LEVERAGE_DURATION;
};
const chooseStatus = (invoiceDate, statusFromDb) => {
const chooseStatus = (utcNowTimestamp, invoiceDate, statusFromDb) => {
if (
isLeverageTimeExpired(invoiceDate) &&
isLeverageTimeExpired(utcNowTimestamp, invoiceDate) &&
statusFromDb !== STATUSES.canceled
) {
return INVOICE_STATUS[STATUSES.shipped];
@@ -67,7 +68,13 @@ const ExtraHeader = ({ ID, invoiceDate, status: initialStatus }) => {
const { handleError } = useErrors();
const [loadingHeaderId, setLoadingHeaderId] = useState();
const [status, setStatus] = useState(initialStatus);
const statusConfig = chooseStatus(invoiceDate, status);
const statusConfig = useMemo(() => {
const utcNowTimestamp = moment(
moment().utc().format(UTC_DATE_TIME_FORMAT)
).valueOf();
return chooseStatus(utcNowTimestamp, invoiceDate, status);
}, [status]);
const onCancelInvoice = (event, ID) => {
event.stopPropagation();

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect, useRef } from "react";
import React, { useState, useRef } from "react";
import { Card, Button } from "antd";
import { PlusOutlined, MinusOutlined } from "@ant-design/icons";
import { useGlobals } from "../../GlobalContext";

View File

@@ -3,11 +3,12 @@ const moment = require("moment");
const LEVERAGE_DURATION = 1; // in hours. should be the same in the frontend
const CANCEL_STATUS = -1;
const UTC_DATE_TIME_FORMAT = "YYYY-MM-DDThh:mm:ss";
// the same function there is in the frontend
const isLeverageTimeExpired = (invoiceDate) => {
const isLeverageTimeExpired = (utcNowTimestamp, invoiceDate) => {
const duration = moment.duration(
moment(moment().utc().format()).diff(invoiceDate)
moment(utcNowTimestamp).diff(moment(invoiceDate).valueOf())
);
return duration.asHours() > LEVERAGE_DURATION;
};
@@ -23,33 +24,27 @@ module.exports = async function () {
this.on("invoice", async (req) => {
const { tracks } = req.data;
const customerId = req.user.attr.ID;
const invoiceDate = moment().utc().valueOf();
const total = tracks.reduce(
(acc, { unitPrice }) => acc + Number(unitPrice),
0
);
const { ID: lastInvoiceItemId } = await db.run(
SELECT.one(InvoiceItems).columns("ID").orderBy({ ID: "desc" })
);
const { ID: lastInvoiceId } = await db.run(
SELECT.one(Invoices).columns("ID").orderBy({ ID: "desc" })
);
const utcNowDateTime = moment().utc().format(UTC_DATE_TIME_FORMAT);
const transaction = await db.tx(req);
await transaction.run(
const {
results: [{ lastID: invoiceID }],
} = await transaction.run(
INSERT.into(Invoices)
.columns("ID", "customer_ID", "total", "invoiceDate")
.values(lastInvoiceId + 1, customerId, total, invoiceDate)
.columns("customer_ID", "total", "invoiceDate")
.values(customerId, total, utcNowDateTime)
);
await transaction.run(
INSERT.into(InvoiceItems)
.columns("ID", "invoice_ID", "track_ID", "unitPrice")
.columns("invoice_ID", "track_ID", "unitPrice")
.rows(
tracks.map(({ ID, unitPrice }, index) => [
lastInvoiceItemId + (index + 1),
lastInvoiceId + 1,
ID,
tracks.map(({ ID: trackID, unitPrice }) => [
invoiceID,
trackID,
unitPrice,
])
)
@@ -59,6 +54,7 @@ module.exports = async function () {
this.on("cancelInvoice", async (req) => {
const { ID } = req.data;
const currentInvoice = await db.run(
SELECT.one(Invoices)
.where({
@@ -74,7 +70,10 @@ module.exports = async function () {
);
}
if (isLeverageTimeExpired(currentInvoice.invoiceDate)) {
const utcNowTimestamp = moment(
moment().utc().format(UTC_DATE_TIME_FORMAT)
).valueOf();
if (isLeverageTimeExpired(utcNowTimestamp, currentInvoice.invoiceDate)) {
req.reject(400, "Leverage time was expired");
}

View File

@@ -28,9 +28,9 @@ module.exports = async function () {
}
const userEqualPassword = await new Promise((resolve, reject) =>
bcrypt.compare(password, userFromDb.password, (err, res) => {
if (err) {
if (err || res === false) {
reject(err);
} else if (res) {
} else {
resolve(res);
}
})