From 89fc27b0875c857501fdd41c0577a59835b44650 Mon Sep 17 00:00:00 2001 From: Martin Berka Date: Fri, 16 Jun 2023 21:12:30 +0200 Subject: [PATCH] =?UTF-8?q?Oprava=20p=C3=A1du=20p=C5=99i=20chyb=C4=9Bj?= =?UTF-8?q?=C3=ADc=C3=AD=20konfiguraci=20Gotify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/notifikace.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index efd984b..9929989 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -1,6 +1,6 @@ /** Notifikace pro gotify*/ -import axios, {AxiosError, AxiosResponse} from 'axios'; -import {GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum} from "./types"; +import axios, { AxiosError, AxiosResponse } from 'axios'; +import { GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum } from "./types"; import dotenv from 'dotenv'; import path from 'path'; @@ -10,9 +10,12 @@ dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) }); 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}`)); +export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifyServer[]): Promise => { + if (!Array.isArray(gotifyServers)) { + return [] + } + const urls = gotifyServers.flatMap(gotifyServer => + gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`)); const dataPayload = { title: "Luncher", @@ -20,7 +23,7 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifySer priority: 7, }; - const headers = {"Content-Type": "application/json"}; + const headers = { "Content-Type": "application/json" }; const promises = urls.map(url => axios.post(url, dataPayload, { headers }).then(response => { @@ -42,19 +45,19 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifySer } } // Handle unknown error without a response - console.log(error,"unknown error"); + 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*/ -export const callNotifikace = async ({input, teams = true, gotify = true}: NotifikaceData) => { +export const callNotifikace = async ({ input, teams = true, gotify = true }: NotifikaceData) => { const notifications = []; if (gotify) { - const gotifyPromises = await gotifyCall(input, gotifyData); - notifications.push(...gotifyPromises); + const gotifyPromises = await gotifyCall(input, gotifyData); + notifications.push(...gotifyPromises); } /* Zatím není