CI / Generate TypeScript types (push) Successful in 13s
CI / Server unit tests (push) Successful in 25s
CI / Build server (push) Successful in 28s
CI / Build client (push) Successful in 41s
CI / Playwright E2E tests (push) Successful in 1m47s
CI / Build and push Docker image (push) Successful in 53s
CI / Notify (push) Successful in 2s
Vedle Bolt Food jde nově sledovat i objednávka z Woltu — stačí u objednané skupiny vložit odkaz z track.wolt.com. Logika sledování je zobecněná do registru rozvozových služeb (trackingProviders.ts): každá služba umí vytáhnout kód ze sdílecího odkazu a dotázat se svého API, scheduler i stepper jsou společné. - pole skupiny bolt* přejmenována na tracking* + nové trackingProvider - endpoint /groups/setBoltTracking → /groups/setTracking - Wolt: čas doručení z delivery_eta v časové zóně objednávky, 404 ukončí sledování, stav kurýra odvozen z is_delivering/is_delivering_other_order - DEV simulace zůstává jen pro Bolt Food
255 lines
8.9 KiB
TypeScript
255 lines
8.9 KiB
TypeScript
import axios from 'axios';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import { getClientData, getToday } from "./service";
|
|
import { getUsersByLocation, getHumanTime } from "./utils";
|
|
import { NotifikaceData, NotifikaceInput, NotificationSettings } from '../../types';
|
|
import getStorage from "./storage";
|
|
import { sendPushToLogins } from "./pushReminder";
|
|
|
|
const ENVIRONMENT = process.env.NODE_ENV ?? 'production';
|
|
dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
|
|
|
|
const storage = getStorage();
|
|
const NOTIFICATION_SETTINGS_PREFIX = 'notif';
|
|
|
|
/** Vrátí klíč pro uložení notifikačních nastavení uživatele. */
|
|
function getNotificationSettingsKey(login: string): string {
|
|
return `${NOTIFICATION_SETTINGS_PREFIX}_${login}`;
|
|
}
|
|
|
|
/** Vrátí nastavení notifikací pro daného uživatele. */
|
|
export async function getNotificationSettings(login: string): Promise<NotificationSettings> {
|
|
return await storage.getData<NotificationSettings>(getNotificationSettingsKey(login)) ?? {};
|
|
}
|
|
|
|
/** Uloží nastavení notifikací pro daného uživatele. */
|
|
export async function saveNotificationSettings(login: string, settings: NotificationSettings): Promise<NotificationSettings> {
|
|
await storage.setData(getNotificationSettingsKey(login), settings);
|
|
return settings;
|
|
}
|
|
|
|
/** Odešle ntfy notifikaci na dané téma. */
|
|
async function ntfyCallToTopic(topic: string, message: string) {
|
|
const url = process.env.NTFY_HOST;
|
|
const username = process.env.NTFY_USERNAME;
|
|
const password = process.env.NTFY_PASSWD;
|
|
if (!url || !username || !password) {
|
|
return;
|
|
}
|
|
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
|
try {
|
|
const response = await axios({
|
|
url: `${url}/${topic}`,
|
|
method: 'POST',
|
|
data: message,
|
|
headers: {
|
|
'Authorization': `Basic ${token}`,
|
|
'Tag': 'meat_on_bone'
|
|
}
|
|
});
|
|
console.log(response.data);
|
|
} catch (error) {
|
|
console.error(`Chyba při odesílání ntfy notifikace na topic ${topic}:`, error);
|
|
}
|
|
}
|
|
|
|
export const ntfyCall = async (data: NotifikaceInput) => {
|
|
const url = process.env.NTFY_HOST
|
|
const username = process.env.NTFY_USERNAME;
|
|
const password = process.env.NTFY_PASSWD;
|
|
if (!url) {
|
|
console.log("NTFY_HOST není definován v env")
|
|
return
|
|
}
|
|
if (!username) {
|
|
console.log("NTFY_USERNAME není definován v env")
|
|
return
|
|
}
|
|
if (!password) {
|
|
console.log("NTFY_PASSWD není definován v env")
|
|
return
|
|
}
|
|
let clientData = await getClientData(getToday());
|
|
const userByCLocation = getUsersByLocation(clientData.choices, data.user)
|
|
|
|
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
|
const promises = userByCLocation.map(async user => {
|
|
try {
|
|
// Odstraníme mezery a diakritiku a převedeme na lowercase
|
|
const topic = user.normalize('NFD').replace(' ', '').replace(/[\u0300-\u036f]/g, '').toLowerCase();
|
|
const response = await axios({
|
|
url: `${url}/${topic}`,
|
|
method: 'POST',
|
|
data: `${data.udalost} - spustil:${data.user}`,
|
|
headers: {
|
|
'Authorization': `Basic ${token}`,
|
|
'Tag': 'meat_on_bone'
|
|
}
|
|
});
|
|
console.log(response.data);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
})
|
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
export const teamsCall = async (data: NotifikaceInput) => {
|
|
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;
|
|
}
|
|
}
|
|
|
|
/** Odešle Teams notifikaci na daný webhook URL. */
|
|
async function teamsCallToUrl(webhookUrl: string, data: NotifikaceInput) {
|
|
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",
|
|
summary: 'Summary description',
|
|
sections: [
|
|
{
|
|
activityTitle: title,
|
|
text: message,
|
|
},
|
|
],
|
|
};
|
|
try {
|
|
await axios.post(webhookUrl, card, {
|
|
headers: {
|
|
'content-type': 'application/vnd.microsoft.teams.card.o365connector'
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error(`Chyba při odesílání Teams notifikace:`, error);
|
|
}
|
|
}
|
|
|
|
/** Odešle Discord notifikaci na daný webhook URL. */
|
|
async function discordCall(webhookUrl: string, data: NotifikaceInput) {
|
|
let time = new Date();
|
|
time.setTime(time.getTime() + 1000 * 60);
|
|
const message = `🍖 **${data.udalost}** — ${data.user} (odchod v ${getHumanTime(time)})`;
|
|
try {
|
|
await axios.post(webhookUrl, {
|
|
content: message,
|
|
}, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error(`Chyba při odesílání Discord notifikace:`, error);
|
|
}
|
|
}
|
|
|
|
/** 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: Promise<any>[] = [];
|
|
|
|
// Globální notifikace (zpětně kompatibilní)
|
|
if (ntfy) {
|
|
const ntfyPromises = await ntfyCall(input);
|
|
if (ntfyPromises) {
|
|
notifications.push(...ntfyPromises);
|
|
}
|
|
}
|
|
if (teams) {
|
|
const teamsPromises = await teamsCall(input);
|
|
if (teamsPromises) {
|
|
notifications.push(Promise.resolve(teamsPromises));
|
|
}
|
|
}
|
|
|
|
// Per-user notifikace: najdeme uživatele se stejnou lokací a odešleme dle jejich nastavení
|
|
const clientData = await getClientData(getToday());
|
|
const usersToNotify = getUsersByLocation(clientData.choices, input.user);
|
|
for (const user of usersToNotify) {
|
|
if (user === input.user) continue; // Neposíláme notifikaci spouštějícímu uživateli
|
|
const userSettings = await getNotificationSettings(user);
|
|
if (!userSettings.enabledEvents?.includes(input.udalost)) continue;
|
|
|
|
if (userSettings.ntfyTopic) {
|
|
notifications.push(ntfyCallToTopic(userSettings.ntfyTopic, `${input.udalost} - spustil: ${input.user}`));
|
|
}
|
|
if (userSettings.discordWebhookUrl) {
|
|
notifications.push(discordCall(userSettings.discordWebhookUrl, input));
|
|
}
|
|
if (userSettings.teamsWebhookUrl) {
|
|
notifications.push(teamsCallToUrl(userSettings.teamsWebhookUrl, input));
|
|
}
|
|
}
|
|
|
|
try {
|
|
const results = await Promise.all(notifications);
|
|
return results;
|
|
} catch (error) {
|
|
console.error("Error in callNotifikace: ", error);
|
|
}
|
|
};
|
|
|
|
/** Cesta na stránku skupinových objednávek (viz OBJEDNANI_URL v klientovi). */
|
|
const OBJEDNANI_PATH = '/objednani';
|
|
|
|
/**
|
|
* Odešle push notifikaci o doručení skupinové objednávky členům skupiny,
|
|
* kteří si to zapnuli v nastavení. Zakladatel skupiny notifikaci nedostává —
|
|
* ten je o doručení informován přímo aplikací rozvozové služby.
|
|
*
|
|
* Klíč nastavení zůstal boltDeliveredPush z doby, kdy sledování uměl jen Bolt Food —
|
|
* přejmenování by uživatelům zahodilo už uložené zapnutí.
|
|
*/
|
|
export async function notifyOrderDelivered(groupName: string, memberLogins: string[], creatorLogin: string): Promise<void> {
|
|
const recipients: string[] = [];
|
|
for (const login of memberLogins) {
|
|
if (login === creatorLogin) continue;
|
|
const settings = await getNotificationSettings(login);
|
|
if (settings.boltDeliveredPush) recipients.push(login);
|
|
}
|
|
if (recipients.length === 0) return;
|
|
|
|
await sendPushToLogins(recipients, {
|
|
title: 'Luncher',
|
|
body: `Objednávka „${groupName}" byla doručena!`,
|
|
tag: `order-delivered-${groupName}`,
|
|
url: OBJEDNANI_PATH,
|
|
});
|
|
}
|