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 dev mode using provided api
// in prod mode using proxy // 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 BROWSE_TRACKS_SERVICE = `${API}browse-tracks`;
const INVOICES_SERVICE = `${API}browse-invoices`; const INVOICES_SERVICE = `${API}browse-invoices`;

View File

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

View File

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

View File

@@ -24,6 +24,7 @@ const INVOICE_STATUS = {
}; };
const CANCELLED_STATUS = -1; const CANCELLED_STATUS = -1;
const DATE_TIME_FORMAT_PATTERN = "LLLL"; const DATE_TIME_FORMAT_PATTERN = "LLLL";
const UTC_DATE_TIME_FORMAT = "YYYY-MM-DDThh:mm:ss";
const INVOICE_ITEMS_COLUMNS = [ const INVOICE_ITEMS_COLUMNS = [
{ {
title: "Track name", title: "Track name",
@@ -45,16 +46,16 @@ const INVOICE_ITEMS_COLUMNS = [
const LEVERAGE_DURATION = 1; // in hours const LEVERAGE_DURATION = 1; // in hours
const STATUSES = { submitted: 1, shipped: 2, canceled: -1 }; const STATUSES = { submitted: 1, shipped: 2, canceled: -1 };
const isLeverageTimeExpired = (invoiceDate) => { const isLeverageTimeExpired = (utcNowTimestamp, invoiceDate) => {
const duration = moment.duration( const duration = moment.duration(
moment(moment().utc().format()).diff(invoiceDate) moment(utcNowTimestamp).diff(moment(invoiceDate).valueOf())
); );
return duration.asHours() > LEVERAGE_DURATION; return duration.asHours() > LEVERAGE_DURATION;
}; };
const chooseStatus = (invoiceDate, statusFromDb) => { const chooseStatus = (utcNowTimestamp, invoiceDate, statusFromDb) => {
if ( if (
isLeverageTimeExpired(invoiceDate) && isLeverageTimeExpired(utcNowTimestamp, invoiceDate) &&
statusFromDb !== STATUSES.canceled statusFromDb !== STATUSES.canceled
) { ) {
return INVOICE_STATUS[STATUSES.shipped]; return INVOICE_STATUS[STATUSES.shipped];
@@ -67,7 +68,13 @@ const ExtraHeader = ({ ID, invoiceDate, status: initialStatus }) => {
const { handleError } = useErrors(); const { handleError } = useErrors();
const [loadingHeaderId, setLoadingHeaderId] = useState(); const [loadingHeaderId, setLoadingHeaderId] = useState();
const [status, setStatus] = useState(initialStatus); 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) => { const onCancelInvoice = (event, ID) => {
event.stopPropagation(); 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 { Card, Button } from "antd";
import { PlusOutlined, MinusOutlined } from "@ant-design/icons"; import { PlusOutlined, MinusOutlined } from "@ant-design/icons";
import { useGlobals } from "../../GlobalContext"; 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 LEVERAGE_DURATION = 1; // in hours. should be the same in the frontend
const CANCEL_STATUS = -1; const CANCEL_STATUS = -1;
const UTC_DATE_TIME_FORMAT = "YYYY-MM-DDThh:mm:ss";
// the same function there is in the frontend // the same function there is in the frontend
const isLeverageTimeExpired = (invoiceDate) => { const isLeverageTimeExpired = (utcNowTimestamp, invoiceDate) => {
const duration = moment.duration( const duration = moment.duration(
moment(moment().utc().format()).diff(invoiceDate) moment(utcNowTimestamp).diff(moment(invoiceDate).valueOf())
); );
return duration.asHours() > LEVERAGE_DURATION; return duration.asHours() > LEVERAGE_DURATION;
}; };
@@ -23,33 +24,27 @@ 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().utc().valueOf();
const total = tracks.reduce( const total = tracks.reduce(
(acc, { unitPrice }) => acc + Number(unitPrice), (acc, { unitPrice }) => acc + Number(unitPrice),
0 0
); );
const utcNowDateTime = moment().utc().format(UTC_DATE_TIME_FORMAT);
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 transaction = await db.tx(req); const transaction = await db.tx(req);
await transaction.run( const {
results: [{ lastID: invoiceID }],
} = await transaction.run(
INSERT.into(Invoices) INSERT.into(Invoices)
.columns("ID", "customer_ID", "total", "invoiceDate") .columns("customer_ID", "total", "invoiceDate")
.values(lastInvoiceId + 1, customerId, total, invoiceDate) .values(customerId, total, utcNowDateTime)
); );
await transaction.run( await transaction.run(
INSERT.into(InvoiceItems) INSERT.into(InvoiceItems)
.columns("ID", "invoice_ID", "track_ID", "unitPrice") .columns("invoice_ID", "track_ID", "unitPrice")
.rows( .rows(
tracks.map(({ ID, unitPrice }, index) => [ tracks.map(({ ID: trackID, unitPrice }) => [
lastInvoiceItemId + (index + 1), invoiceID,
lastInvoiceId + 1, trackID,
ID,
unitPrice, unitPrice,
]) ])
) )
@@ -59,6 +54,7 @@ module.exports = async function () {
this.on("cancelInvoice", async (req) => { this.on("cancelInvoice", async (req) => {
const { ID } = req.data; const { ID } = req.data;
const currentInvoice = await db.run( const currentInvoice = await db.run(
SELECT.one(Invoices) SELECT.one(Invoices)
.where({ .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"); req.reject(400, "Leverage time was expired");
} }

View File

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