Podpora notifikací, zatím jen gotify

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

View File

@ -51,5 +51,7 @@
"last 1 safari version"
]
},
"devDependencies": {}
"devDependencies": {
"prettier": "^2.8.8"
}
}

0
client/src/notifikace.ts Normal file
View File

View File

@ -7569,6 +7569,11 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w==
prettier@^2.8.8:
version "2.8.8"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
pretty-bytes@^5.3.0, pretty-bytes@^5.4.1:
version "5.6.0"
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.6.0.tgz#356256f643804773c82f64723fe78c92c62beaeb"

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
}
};

View File

@ -1,7 +1,7 @@
import { ClientData, Locations, Order, Pizza, PizzaDayState, PizzaOrder, PizzaSize } from "./types";
import { db } from "./database";
import { getHumanDate, getIsWeekend } from "./utils";
import { formatDate } from "./utils";
import {ClientData, Locations, Order, Pizza, PizzaDayState, PizzaOrder, PizzaSize, UdalostEnum} from "./types";
import {db} from "./database";
import {formatDate, getHumanDate, getIsWeekend} from "./utils";
import {callNotifikace} from "./notifikace";
/** Vrátí dnešní datum, případně fiktivní datum pro účely vývoje a testování. */
function getToday(): Date {
@ -36,6 +36,7 @@ export function createPizzaDay(creator: string): ClientData {
}
const data: ClientData = { pizzaDay: { state: PizzaDayState.CREATED, creator, orders: [] }, ...clientData };
db.set(today, data);
callNotifikace({input:{udalost:UdalostEnum.ZAHAJENA_PIZZA,user:creator}})
return data;
}
@ -190,6 +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}})
return clientData;
}

View File

@ -1,3 +1,5 @@
import exp from "constants";
export interface Choices {
[location: string]: string[],
}
@ -66,3 +68,18 @@ export enum Locations {
OBJEDNAVAM = 'Objednávám',
NEOBEDVAM = 'Neobědvám',
}
export enum UdalostEnum{
ZAHAJENA_PIZZA="Zahájen pizza day",
OBJEDNANA_PIZZA="Objednána pizza"
}
export interface NotififaceInput{
udalost:UdalostEnum,
user:string,
}
export interface NotifikaceData{
input:NotififaceInput,
gotify?:boolean,
teams?:boolean,
}