From 97aa41549b9d03c1f1984707fe16f2f5cc2cb873 Mon Sep 17 00:00:00 2001 From: Reallag Date: Tue, 13 Jun 2023 21:30:00 +0200 Subject: [PATCH] =?UTF-8?q?rozb=C4=9Bh=C3=A1n=C3=AD=20gotify=20notifikac?= =?UTF-8?q?=C3=AD=20podle=20dat=20z=20env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/.env.template | 8 ++++- server/src/notifikace.ts | 72 ++++++++++++++++++++++------------------ server/src/service.ts | 2 +- server/src/types.ts | 4 +++ 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/server/.env.template b/server/.env.template index 0c4ec00..1bceafe 100644 --- a/server/.env.template +++ b/server/.env.template @@ -5,4 +5,10 @@ # Zapne režim mockování jídelních lístků. # Vhodné pro vývoj o víkendech, svátcích a dalších dnech, pro které podniky nenabízejí obědové menu. # V tomto režimu vrací server vždy falešné datum (pracovní den) a Food API pevně nadefinovanou, smyšlenou nabídku jídel. -# MOCK_DATA=true \ No newline at end of file +# MOCK_DATA=true + +# Určuje servery Gotify a příslušné klíče API. +# Formát je pole objektů, kde každý objekt obsahuje adresu serveru a pole klíčů API. +# To je užitečné pro odesílání upozornění na různé servery Gotify s různými klíči API. +# Struktura dat je ve formátu JSON a je uložena jako řetězec. +# GOTIFY_SERVERS_AND_KEYS='[{"server":"https://notification.server.eu", "api_keys":["key1", "key2"]},{"server":"https://notification.server2.eu", "api_keys":["key3", "key4"]}]' diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index fe64f0e..d2546f7 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -1,41 +1,46 @@ /** Notifikace pro gotify*/ import axios, {AxiosError, AxiosResponse} from 'axios'; -import {NotififaceInput, NotifikaceData, UdalostEnum} from "./types"; +import {GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum} from "./types"; +import dotenv from 'dotenv'; +dotenv.config() +const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}"; +const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw); +export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifyServer[]): Promise => { + const urls = gotifyServers.flatMap(gotifyServer => + gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`)); -const url = 'your_URL_here'; // Place your URL here - -export const gotifyCall = async (data: NotififaceInput): Promise => { - - const options = { - headers: {"Content-Type": "application/json"}, - data: { - title: "Luncher", - message: `${data.udalost} - spustil:${data.user}`, - priority: 7, - }, + const dataPayload = { + title: "Luncher", + message: `${data.udalost} - spustil:${data.user}`, + priority: 7, }; - try { - const response = await axios.post(url, options); - response.data = { - success: true, - message: "Notifikace doručena", - }; - console.log(response) - } catch (error: unknown) { - if (axios.isAxiosError(error)) { - const axiosError = error as AxiosError; - if (axiosError.response) { - axiosError.response.data = { - success: false, - message: "fail", - }; - console.log(axiosError.response); + const headers = {"Content-Type": "application/json"}; + + const promises = urls.map(url => + axios.post(url, dataPayload, { headers }).then(response => { + response.data = { + success: true, + message: "Notifikace doručena", + }; + return response; + }).catch(error => { + if (axios.isAxiosError(error)) { + const axiosError = error as AxiosError; + if (axiosError.response) { + axiosError.response.data = { + success: false, + message: "fail", + }; + console.log(error) + return axiosError.response; + } } - } - // Handle unknown error without a response - console.log("unkown error") - } + // Handle unknown error without a response + console.log(error,"unknown error"); + }) + ); + return promises; }; /** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/ @@ -43,7 +48,8 @@ export const callNotifikace = async ({input, teams = true, gotify = true}: Notif const notifications = []; if (gotify) { - notifications.push(gotifyCall(input)); + const gotifyPromises = await gotifyCall(input, gotifyData); + notifications.push(...gotifyPromises); } /* Zatím není diff --git a/server/src/service.ts b/server/src/service.ts index b254654..39f72e0 100644 --- a/server/src/service.ts +++ b/server/src/service.ts @@ -191,7 +191,7 @@ export function finishPizzaOrder(login: string) { } clientData.pizzaDay.state = PizzaDayState.ORDERED; db.set(today, clientData); - callNotifikace({input:{udalost:UdalostEnum.ZAHAJENA_PIZZA,user:clientData?.pizzaDay?.creator}}) + callNotifikace({input:{udalost:UdalostEnum.OBJEDNANA_PIZZA,user:clientData?.pizzaDay?.creator}}) return clientData; } diff --git a/server/src/types.ts b/server/src/types.ts index 6d38c23..5662189 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -82,4 +82,8 @@ export interface NotifikaceData{ input:NotififaceInput, gotify?:boolean, teams?:boolean, +} +export interface GotifyServer { + server: string; + api_keys: string[]; } \ No newline at end of file