From 2f39d04120b94eccb4e4e0a27a1cecdb5dc01091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20H=C3=A1jek?= Date: Sun, 23 Feb 2025 01:11:25 +0100 Subject: [PATCH] =?UTF-8?q?Do=20funkce=20jdemeObed=20dopln=C4=9Bn=20parame?= =?UTF-8?q?tr=20locationKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/App.tsx | 9 ++++--- client/src/api/FoodApi.ts | 14 +++++++--- server/src/notifikace.ts | 46 ++++++++++++++++----------------- server/src/pizza.ts | 4 +-- server/src/routes/foodRoutes.ts | 15 ++++++++--- server/src/utils.ts | 2 +- types/RequestTypes.ts | 2 ++ types/Types.ts | 10 ++----- 8 files changed, 59 insertions(+), 43 deletions(-) diff --git a/client/src/App.tsx b/client/src/App.tsx index 26cb66f..038719f 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -206,9 +206,9 @@ function App() { } } - const doJdemeObed = async () => { + const doJdemeObed = async (locationKey: LocationKey) => { if (auth?.login) { - await jdemeObed(); + await jdemeObed(locationKey); } } @@ -471,6 +471,7 @@ function App() { return; } const locationLoginList = Object.entries(loginObject); + const disabled = false; return ( {locationName} @@ -519,6 +520,9 @@ function App() { + + + ) } )} @@ -542,7 +546,6 @@ function App() { setLoadingPizzaDay(true); await createPizzaDay().then(() => setLoadingPizzaDay(false)); }}>Založit Pizza day - } diff --git a/client/src/api/FoodApi.ts b/client/src/api/FoodApi.ts index ef2f865..3290205 100644 --- a/client/src/api/FoodApi.ts +++ b/client/src/api/FoodApi.ts @@ -1,4 +1,12 @@ -import { AddChoiceRequest, ChangeDepartureTimeRequest, LocationKey, RemoveChoiceRequest, RemoveChoicesRequest, UpdateNoteRequest } from "../../../types"; +import { + AddChoiceRequest, + ChangeDepartureTimeRequest, + JdemeObedRequest, + LocationKey, + RemoveChoiceRequest, + RemoveChoicesRequest, + UpdateNoteRequest +} from "../../../types"; import { api } from "./Api"; const FOOD_API_PREFIX = '/api/food'; @@ -23,6 +31,6 @@ export const changeDepartureTime = async (time: string, dayIndex?: number) => { return await api.post(`${FOOD_API_PREFIX}/changeDepartureTime`, { time, dayIndex }); } -export const jdemeObed = async () => { - return await api.post(`${FOOD_API_PREFIX}/jdemeObed`); +export const jdemeObed = async (locationKey: LocationKey) => { + return await api.post(`${FOOD_API_PREFIX}/jdemeObed`, { locationKey }); } diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index 435f1fe..886501b 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -1,10 +1,10 @@ /** Notifikace */ -import { ClientData, NotififaceInput, NotifikaceData } from '../../types'; +import {ClientData, Locations, NotififaceInput} from '../../types'; import axios from 'axios'; import dotenv from 'dotenv'; import path from 'path'; import { getToday } from "./service"; -import { formatDate, getUsersByLocation, getHumanTime } from "./utils"; +import {formatDate, getUsersByLocation, getHumanTime} from "./utils"; import getStorage from "./storage"; const storage = getStorage(); @@ -72,10 +72,10 @@ export const ntfyCall = async (data: NotififaceInput) => { } const today = formatDate(getToday()); let clientData: ClientData = await storage.getData(today); - const userByCLocation = getUsersByLocation(clientData.choices, data.user) + const usersByLocation = getUsersByLocation(clientData.choices, data.user) const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64'); - const promises = userByCLocation.map(async user => { + const promises = usersByLocation.map(async user => { try { // Odstraníme mezery a diakritiku a převedeme na lowercase const topic = user.normalize('NFD').replace(' ', '').replace(/[\u0300-\u036f]/g, '').toLowerCase(); @@ -100,10 +100,16 @@ export const ntfyCall = async (data: NotififaceInput) => { export const teamsCall = async (data: NotififaceInput) => { const url = process.env.TEAMS_WEBHOOK_URL; + if (!url) { + console.log("TEAMS_WEBHOOK_URL není definován v env") + return + } const title = data.udalost; - let time = new Date(); - time.setTime(time.getTime() + 1000 * 60); - const message = 'Odcházíme v ' + getHumanTime(time) + ', ' + data.user; + // const today = formatDate(getToday()); + // let clientData: ClientData = await storage.getData(today); + // const usersByLocation = getUsersByLocation(clientData.choices, data.user) + let location = data.locationKey ? ` odcházíme do ${Locations[data.locationKey] ?? ''}` : ' jdeme na oběd'; + const message = 'V ' + getHumanTime(getToday()) + location; const card = { '@type': 'MessageCard', '@context': 'http://schema.org/extensions', @@ -135,26 +141,20 @@ export const teamsCall = async (data: NotififaceInput) => { } /** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/ -export const callNotifikace = async ({ input, teams = true, gotify = false, ntfy = true }: NotifikaceData) => { +export const callNotifikace = async (input: NotififaceInput) => { const notifications = []; - if (ntfy) { - const ntfyPromises = await ntfyCall(input); - if (ntfyPromises) { - notifications.push(...ntfyPromises); - } + const ntfyPromises = await ntfyCall(input); + if (ntfyPromises) { + notifications.push(...ntfyPromises); } - if (teams) { - const teamsPromises = await teamsCall(input); - if (teamsPromises) { - notifications.push(teamsPromises); - } + const teamsPromises = await teamsCall(input); + if (teamsPromises) { + notifications.push(teamsPromises); } // gotify bych řekl, že už je deprecated - // if (gotify) { - // const gotifyPromises = await gotifyCall(input, gotifyData); - // notifications.push(...gotifyPromises); - // } + // const gotifyPromises = await gotifyCall(input, gotifyData); + // notifications.push(...gotifyPromises); try { const results = await Promise.all(notifications); @@ -163,4 +163,4 @@ export const callNotifikace = async ({ input, teams = true, gotify = false, ntfy console.error("Error in callNotifikace: ", error); // Handle the error as needed } -}; \ No newline at end of file +}; diff --git a/server/src/pizza.ts b/server/src/pizza.ts index 6dc0129..76fdcd6 100644 --- a/server/src/pizza.ts +++ b/server/src/pizza.ts @@ -52,7 +52,7 @@ export async function createPizzaDay(creator: string): Promise { const pizzaList = await getPizzaList(); const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, pizzaList, ...clientData }; await storage.setData(today, data); - callNotifikace({ input: { udalost: UdalostEnum.ZAHAJENA_PIZZA, user: creator } }) + callNotifikace({ udalost: UdalostEnum.ZAHAJENA_PIZZA, user: creator }) return data; } @@ -207,7 +207,7 @@ export async function finishPizzaOrder(login: string) { } clientData.pizzaDay.state = PizzaDayState.ORDERED; await storage.setData(today, clientData); - callNotifikace({ input: { udalost: UdalostEnum.OBJEDNANA_PIZZA, user: clientData?.pizzaDay?.creator } }) + callNotifikace({ udalost: UdalostEnum.OBJEDNANA_PIZZA, user: clientData?.pizzaDay?.creator }) return clientData; } diff --git a/server/src/routes/foodRoutes.ts b/server/src/routes/foodRoutes.ts index 571b874..662b631 100644 --- a/server/src/routes/foodRoutes.ts +++ b/server/src/routes/foodRoutes.ts @@ -4,7 +4,16 @@ import { addChoice, addVolatileData, getDateForWeekIndex, getToday, removeChoice import { getDayOfWeekIndex, parseToken } from "../utils"; import { getWebsocket } from "../websocket"; import { callNotifikace } from "../notifikace"; -import { AddChoiceRequest, ChangeDepartureTimeRequest, IDayIndex, RemoveChoiceRequest, RemoveChoicesRequest, UdalostEnum, UpdateNoteRequest } from "../../../types"; +import { + AddChoiceRequest, + ChangeDepartureTimeRequest, + IDayIndex, + JdemeObedRequest, + RemoveChoiceRequest, + RemoveChoicesRequest, + UdalostEnum, + UpdateNoteRequest +} from "../../../types"; /** * Ověří a vrátí index dne v týdnu z požadavku, za předpokladu, že byl předán, a je zároveň @@ -133,10 +142,10 @@ router.post("/changeDepartureTime", async (req: Request<{}, any, ChangeDeparture } catch (e: any) { next(e) } }); -router.post("/jdemeObed", async (req, res, next) => { +router.post("/jdemeObed", async (req: Request<{}, any, JdemeObedRequest>, res, next) => { const login = getLogin(parseToken(req)); try { - await callNotifikace({ input: { user: login, udalost: UdalostEnum.JDEME_NA_OBED }, gotify: false }) + await callNotifikace({ user: login, udalost: UdalostEnum.JDEME_OBED, locationKey: req.body.locationKey }); res.status(200).json({}); } catch (e: any) { next(e) } }); diff --git a/server/src/utils.ts b/server/src/utils.ts index e8e8e35..fcedbcf 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -1,4 +1,4 @@ -import { Choices, LocationKey } from "../../types"; +import {Choices, LocationKey } from "../../types"; /** Vrátí datum v ISO formátu. */ export function formatDate(date: Date, format?: string) { diff --git a/types/RequestTypes.ts b/types/RequestTypes.ts index 7e15862..a0fda17 100644 --- a/types/RequestTypes.ts +++ b/types/RequestTypes.ts @@ -18,6 +18,8 @@ export type RemoveChoiceRequest = IDayIndex & ILocationKey & { foodIndex: number, } +export type JdemeObedRequest = ILocationKey; + export type UpdateNoteRequest = IDayIndex & { note?: string, } diff --git a/types/Types.ts b/types/Types.ts index 68217f0..fdadfc7 100644 --- a/types/Types.ts +++ b/types/Types.ts @@ -131,21 +131,15 @@ export type LocationKey = keyof typeof Locations; export enum UdalostEnum { ZAHAJENA_PIZZA = "Zahájen pizza day", OBJEDNANA_PIZZA = "Objednána pizza", - JDEME_NA_OBED = "Jdeme na oběd", + JDEME_OBED = "Jdeme na oběd", } export type NotififaceInput = { + locationKey?: LocationKey, udalost: UdalostEnum, user: string, } -export type NotifikaceData = { - input: NotififaceInput, - gotify?: boolean, - teams?: boolean, - ntfy?: boolean, -} - export type GotifyServer = { server: string; api_keys: string[];