Podpora notifikací, zatím jen gotify

This commit is contained in:
2023-06-10 18:31:54 +02:00
parent aa873e9c65
commit ae5d53bcf3
6 changed files with 105 additions and 10 deletions

69
server/src/notifikace.ts Normal file
View File

@@ -0,0 +1,69 @@
/** Notifikace pro gotify*/
import axios, {AxiosError, AxiosResponse} from 'axios';
import {NotififaceInput, NotifikaceData, UdalostEnum} from "./types";
const url = 'your_URL_here'; // Place your URL here
export const gotifyCall = async (data: NotififaceInput): Promise<AxiosResponse> => {
const options = {
headers: {"Content-Type": "application/json"},
data: {
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",
};
return response;
} catch (error: unknown) {
if (axios.isAxiosError(error)) {
const axiosError = error as AxiosError;
if (axiosError.response) {
axiosError.response.data = {
success: false,
message: "fail",
};
return axiosError.response;
}
}
// Handle unknown error without a response
return {
status: 500,
statusText: 'Unknown error',
headers: {}, // empty headers
data: {success: false, message: "fail"},
config: {}, // empty config
};
}
};
/** 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) => {
const notifications = [];
if (gotify) {
notifications.push(gotifyCall(input));
}
/* Zatím není
if (teams) {
notifications.push(teamsCall(input));
}*/
// Add more notifications as necessary
try {
const results = await Promise.all(notifications);
return results;
} catch (error) {
console.error("Error in callNotifikace: ", error);
// Handle the error as needed
}
};