diff --git a/server/src/notifikace.ts b/server/src/notifikace.ts index ee07e1d..435f1fe 100644 --- a/server/src/notifikace.ts +++ b/server/src/notifikace.ts @@ -1,58 +1,58 @@ -/** Notifikace pro gotify*/ -import { ClientData, GotifyServer, NotififaceInput, NotifikaceData } from '../../types'; -import axios, { AxiosError } from 'axios'; +/** Notifikace */ +import { ClientData, NotififaceInput, NotifikaceData } from '../../types'; +import axios from 'axios'; import dotenv from 'dotenv'; import path from 'path'; import { getToday } from "./service"; -import { formatDate, getUsersByLocation } from "./utils"; +import { formatDate, getUsersByLocation, getHumanTime } from "./utils"; import getStorage from "./storage"; const storage = getStorage(); const ENVIRONMENT = process.env.NODE_ENV || 'production' 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 => { - if (!Array.isArray(gotifyServers)) { - return [] - } - const urls = gotifyServers.flatMap(gotifyServer => - gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`)); - - const dataPayload = { - title: "Luncher", - message: `${data.udalost} - spustil:${data.user}`, - priority: 7, - }; - - 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(error, "unknown error"); - }) - ); - return promises; -}; +// const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}"; +// const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw); +// 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", +// message: `${data.udalost} - spustil:${data.user}`, +// priority: 7, +// }; +// +// 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(error, "unknown error"); +// }) +// ); +// return promises; +// }; export const ntfyCall = async (data: NotififaceInput) => { const url = process.env.NTFY_HOST @@ -97,27 +97,64 @@ export const ntfyCall = async (data: NotififaceInput) => { return promises; } + +export const teamsCall = async (data: NotififaceInput) => { + const url = process.env.TEAMS_WEBHOOK_URL; + 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 card = { + '@type': 'MessageCard', + '@context': 'http://schema.org/extensions', + 'themeColor': "0072C6", // light blue + summary: 'Summary description', + sections: [ + { + activityTitle: title, + text: message, + }, + ], + }; + + if (!url) { + console.log("TEAMS_WEBHOOK_URL není definován v env") + return + } + + try { + const response = await axios.post(url, card, { + headers: { + 'content-type': 'application/vnd.microsoft.teams.card.o365connector' + }, + }); + return `${response.status} - ${response.statusText}`; + } catch (err) { + return err; + } +} + /** 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) => { const notifications = []; + if (ntfy) { const ntfyPromises = await ntfyCall(input); if (ntfyPromises) { notifications.push(...ntfyPromises); } } - /* Zatím není if (teams) { - notifications.push(teamsCall(input)); - }*/ - - // Add more notifications as necessary - - //gotify bych řekl, že už je deprecated - if (gotify) { - const gotifyPromises = await gotifyCall(input, gotifyData); - notifications.push(...gotifyPromises); + 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); + // } try { const results = await Promise.all(notifications); diff --git a/server/src/routes/foodRoutes.ts b/server/src/routes/foodRoutes.ts index 1ad0d98..571b874 100644 --- a/server/src/routes/foodRoutes.ts +++ b/server/src/routes/foodRoutes.ts @@ -136,7 +136,7 @@ router.post("/changeDepartureTime", async (req: Request<{}, any, ChangeDeparture router.post("/jdemeObed", async (req, res, next) => { const login = getLogin(parseToken(req)); try { - await callNotifikace({ input: { user: login, udalost: UdalostEnum.JDEME_OBED }, gotify: false }) + await callNotifikace({ input: { user: login, udalost: UdalostEnum.JDEME_NA_OBED }, gotify: false }) res.status(200).json({}); } catch (e: any) { next(e) } }); diff --git a/types/Types.ts b/types/Types.ts index 84b91ea..68217f0 100644 --- a/types/Types.ts +++ b/types/Types.ts @@ -131,7 +131,7 @@ export type LocationKey = keyof typeof Locations; export enum UdalostEnum { ZAHAJENA_PIZZA = "Zahájen pizza day", OBJEDNANA_PIZZA = "Objednána pizza", - JDEME_OBED = "Jdeme oběd", + JDEME_NA_OBED = "Jdeme na oběd", } export type NotififaceInput = {