Podpora mock dat pro vývoj o víkendech

This commit is contained in:
2023-06-03 09:24:36 +02:00
parent b438d2112a
commit c814624dbc
10 changed files with 58 additions and 17 deletions

View File

@@ -110,7 +110,7 @@ export const fetchPizzy = async () => {
// TODO tohle sem absolutně nepatří! dát do vlastní servisky!
export const fetchFood = async () => {
try {
const json = await rp(foodUrl);
const json = await rp({ uri: foodUrl, qs: { "mock": process.env.MOCK_DATA } });
return JSON.parse(json);
} catch (error) {
console.error("Chyba při volání Food API", error);

View File

@@ -4,6 +4,11 @@ import bodyParser from "body-parser";
import { fetchFood, fetchPizzy } from "./chefie";
import cors from 'cors';
import { getData, updateChoice } from "./service";
import dotenv from 'dotenv';
import path from 'path';
const ENVIRONMENT = process.env.NODE_ENV || 'production'
dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
const app = express();
const server = require("http").createServer(app);

View File

@@ -1,7 +1,7 @@
import { ClientData, Locations } from "./types";
import { db } from "./database";
import { getHumanDate, getIsWeekend } from "./utils";
import { getDate } from "./utils";
import { formatDate } from "./utils";
// /** Jedna konkrétní pizza */
// interface Pizza {
@@ -31,17 +31,24 @@ import { getDate } from "./utils";
// orders?: Order[], // seznam objednávek, pokud není vyplněno, není založen pizza day
// }
/** Vrátí dnešní datum, případně fiktivní datum pro účely vývoje a testování. */
function getToday(): Date {
if (process.env.MOCK_DATA) {
return new Date('2023-05-31');
}
return new Date();
}
/** Vrátí "prázdná" (implicitní) data, pokud ještě nikdo nehlasoval. */
function getEmptyData(): ClientData {
return { date: getHumanDate(new Date()), isWeekend: getIsWeekend(new Date()), choices: {} };
return { date: getHumanDate(getToday()), isWeekend: getIsWeekend(getToday()), choices: {} };
}
/**
* Vrátí veškerá klientská data pro aktuální den.
*/
export function getData(): ClientData {
const data = db.get(getDate()) || getEmptyData();
console.log("Vracím data pro dnešní den", data); // TODO smazat
const data = db.get(formatDate(getToday())) || getEmptyData();
return data;
}
@@ -70,7 +77,7 @@ export function getData(): ClientData {
// }
export function initIfNeeded() {
const today = getDate();
const today = formatDate(getToday());
if (!db.has(today)) {
db.set(today, getEmptyData());
}
@@ -91,7 +98,7 @@ export function removeChoice(login: string, data: ClientData) {
export function updateChoice(login: string, choice: Locations | null) {
initIfNeeded();
const today = getDate();
const today = formatDate(getToday());
let data: ClientData = db.get(today);
data = removeChoice(login, data);
if (choice !== null) {

View File

@@ -1,5 +1,5 @@
export function getDate() {
const date = new Date();
/** Vrátí datum v ISO formátu. */
export function formatDate(date: Date) {
let currentDay = String(date.getDate()).padStart(2, '0');
let currentMonth = String(date.getMonth() + 1).padStart(2, "0");
let currentYear = date.getFullYear();