Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 774be3df6d |
@@ -65,16 +65,6 @@ export function getVapidPublicKey(): string | undefined {
|
|||||||
return process.env.VAPID_PUBLIC_KEY;
|
return process.env.VAPID_PUBLIC_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Najde login uživatele podle push subscription endpointu. */
|
|
||||||
export async function findLoginByEndpoint(endpoint: string): Promise<string | undefined> {
|
|
||||||
const registry = await getRegistry();
|
|
||||||
for (const [login, entry] of Object.entries(registry)) {
|
|
||||||
if (entry.subscription.endpoint === endpoint) {
|
|
||||||
return login;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Zkontroluje a odešle připomínky uživatelům, kteří si nezvolili oběd. */
|
/** Zkontroluje a odešle připomínky uživatelům, kteří si nezvolili oběd. */
|
||||||
async function checkAndSendReminders(): Promise<void> {
|
async function checkAndSendReminders(): Promise<void> {
|
||||||
|
|||||||
@@ -189,7 +189,7 @@ router.post("/testPush", async (req, res, next) => {
|
|||||||
webpush.setVapidDetails(subject, publicKey, privateKey);
|
webpush.setVapidDetails(subject, publicKey, privateKey);
|
||||||
await webpush.sendNotification(
|
await webpush.sendNotification(
|
||||||
entry.subscription,
|
entry.subscription,
|
||||||
JSON.stringify({ title: 'Luncher test', body: 'Push notifikace fungují!' })
|
JSON.stringify({ title: 'Luncher test', body: 'Push notifikace fungují!', login })
|
||||||
);
|
);
|
||||||
res.status(200).json({ ok: true });
|
res.status(200).json({ ok: true });
|
||||||
} catch (e: any) { next(e) }
|
} catch (e: any) { next(e) }
|
||||||
|
|||||||
@@ -82,7 +82,8 @@ const router = express.Router();
|
|||||||
router.post("/addChoice", async (req: Request<{}, any, AddChoiceData["body"]>, res, next) => {
|
router.post("/addChoice", async (req: Request<{}, any, AddChoiceData["body"]>, res, next) => {
|
||||||
const login = getLogin(parseToken(req));
|
const login = getLogin(parseToken(req));
|
||||||
const trusted = getTrusted(parseToken(req));
|
const trusted = getTrusted(parseToken(req));
|
||||||
const slot = parseSlot(req.body);
|
let slot: MealSlot | undefined;
|
||||||
|
try { slot = parseSlot(req.body); } catch (e: any) { return res.status(400).json({ error: e.message }); }
|
||||||
let date = undefined;
|
let date = undefined;
|
||||||
if (req.body.dayIndex != null) {
|
if (req.body.dayIndex != null) {
|
||||||
let dayIndex;
|
let dayIndex;
|
||||||
@@ -103,7 +104,8 @@ router.post("/addChoice", async (req: Request<{}, any, AddChoiceData["body"]>, r
|
|||||||
router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesData["body"]>, res, next) => {
|
router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesData["body"]>, res, next) => {
|
||||||
const login = getLogin(parseToken(req));
|
const login = getLogin(parseToken(req));
|
||||||
const trusted = getTrusted(parseToken(req));
|
const trusted = getTrusted(parseToken(req));
|
||||||
const slot = parseSlot(req.body);
|
let slot: MealSlot | undefined;
|
||||||
|
try { slot = parseSlot(req.body); } catch (e: any) { return res.status(400).json({ error: e.message }); }
|
||||||
let date = undefined;
|
let date = undefined;
|
||||||
if (req.body.dayIndex != null) {
|
if (req.body.dayIndex != null) {
|
||||||
let dayIndex;
|
let dayIndex;
|
||||||
@@ -124,7 +126,8 @@ router.post("/removeChoices", async (req: Request<{}, any, RemoveChoicesData["bo
|
|||||||
router.post("/removeChoice", async (req: Request<{}, any, RemoveChoiceData["body"]>, res, next) => {
|
router.post("/removeChoice", async (req: Request<{}, any, RemoveChoiceData["body"]>, res, next) => {
|
||||||
const login = getLogin(parseToken(req));
|
const login = getLogin(parseToken(req));
|
||||||
const trusted = getTrusted(parseToken(req));
|
const trusted = getTrusted(parseToken(req));
|
||||||
const slot = parseSlot(req.body);
|
let slot: MealSlot | undefined;
|
||||||
|
try { slot = parseSlot(req.body); } catch (e: any) { return res.status(400).json({ error: e.message }); }
|
||||||
let date = undefined;
|
let date = undefined;
|
||||||
if (req.body.dayIndex != null) {
|
if (req.body.dayIndex != null) {
|
||||||
let dayIndex;
|
let dayIndex;
|
||||||
@@ -146,7 +149,8 @@ router.post("/updateNote", async (req: Request<{}, any, UpdateNoteData["body"]>,
|
|||||||
const login = getLogin(parseToken(req));
|
const login = getLogin(parseToken(req));
|
||||||
const trusted = getTrusted(parseToken(req));
|
const trusted = getTrusted(parseToken(req));
|
||||||
const note = req.body.note;
|
const note = req.body.note;
|
||||||
const slot = parseSlot(req.body);
|
let slot: MealSlot | undefined;
|
||||||
|
try { slot = parseSlot(req.body); } catch (e: any) { return res.status(400).json({ error: e.message }); }
|
||||||
try {
|
try {
|
||||||
if (note && note.length > 70) {
|
if (note && note.length > 70) {
|
||||||
throw Error("Poznámka může mít maximálně 70 znaků");
|
throw Error("Poznámka může mít maximálně 70 znaků");
|
||||||
@@ -196,7 +200,8 @@ router.post("/jdemeObed", async (req, res, next) => {
|
|||||||
|
|
||||||
router.post("/updateBuyer", async (req, res, next) => {
|
router.post("/updateBuyer", async (req, res, next) => {
|
||||||
const login = getLogin(parseToken(req));
|
const login = getLogin(parseToken(req));
|
||||||
const slot = parseSlot(req.body ?? {});
|
let slot: MealSlot | undefined;
|
||||||
|
try { slot = parseSlot(req.body ?? {}); } catch (e: any) { return res.status(400).json({ error: e.message }); }
|
||||||
try {
|
try {
|
||||||
const data = await updateBuyer(login, slot);
|
const data = await updateBuyer(login, slot);
|
||||||
getWebsocket().emit("message", data);
|
getWebsocket().emit("message", data);
|
||||||
|
|||||||
@@ -66,13 +66,10 @@ router.post("/push/unsubscribe", async (req, res, next) => {
|
|||||||
} catch (e: any) { next(e) }
|
} catch (e: any) { next(e) }
|
||||||
});
|
});
|
||||||
|
|
||||||
/** Rychlá akce z push notifikace — nastaví volbu bez JWT (identita přes login v payloadu). */
|
/** Rychlá akce z push notifikace — nastaví volbu NEOBEDVAM pro přihlášeného uživatele. */
|
||||||
router.post("/push/quickChoice", async (req, res, next) => {
|
router.post("/push/quickChoice", async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const { login } = req.body;
|
const login = getLogin(parseToken(req));
|
||||||
if (!login) {
|
|
||||||
return res.status(400).json({ error: "Nebyl předán login" });
|
|
||||||
}
|
|
||||||
const data = await addChoice(login, false, 'NEOBEDVAM', undefined, undefined);
|
const data = await addChoice(login, false, 'NEOBEDVAM', undefined, undefined);
|
||||||
getWebsocket().emit("message", data);
|
getWebsocket().emit("message", data);
|
||||||
res.status(200).json({});
|
res.status(200).json({});
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ export async function getData(date?: Date, slot?: MealSlot): Promise<ClientData>
|
|||||||
SENKSERIKOVA: await getRestaurantMenu('SENKSERIKOVA', date),
|
SENKSERIKOVA: await getRestaurantMenu('SENKSERIKOVA', date),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (slot === MealSlot.EXTRA) clientData.slot = MealSlot.EXTRA;
|
|
||||||
return clientData;
|
return clientData;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,9 +530,9 @@ export async function updateNote(login: string, trusted: boolean, note?: string,
|
|||||||
* @param time preferovaný čas odchodu
|
* @param time preferovaný čas odchodu
|
||||||
* @param date datum, ke kterému se čas vztahuje
|
* @param date datum, ke kterému se čas vztahuje
|
||||||
*/
|
*/
|
||||||
export async function updateDepartureTime(login: string, time?: string, date?: Date, slot?: MealSlot) {
|
export async function updateDepartureTime(login: string, time?: string, date?: Date) {
|
||||||
const usedDate = date ?? getToday();
|
const usedDate = date ?? getToday();
|
||||||
let clientData = await getClientData(usedDate, slot);
|
let clientData = await getClientData(usedDate);
|
||||||
const found = Object.values(clientData.choices).find(location => login in location);
|
const found = Object.values(clientData.choices).find(location => login in location);
|
||||||
// TODO validace, že se jedná o restauraci
|
// TODO validace, že se jedná o restauraci
|
||||||
if (found) {
|
if (found) {
|
||||||
@@ -545,7 +544,7 @@ export async function updateDepartureTime(login: string, time?: string, date?: D
|
|||||||
}
|
}
|
||||||
found[login].departureTime = time;
|
found[login].departureTime = time;
|
||||||
}
|
}
|
||||||
await storage.setData(getDataKey(usedDate, slot), clientData);
|
await storage.setData(getDataKey(usedDate), clientData);
|
||||||
}
|
}
|
||||||
return clientData;
|
return clientData;
|
||||||
}
|
}
|
||||||
@@ -581,5 +580,6 @@ export async function getClientData(date?: Date, slot?: MealSlot): Promise<Clien
|
|||||||
return {
|
return {
|
||||||
...clientData,
|
...clientData,
|
||||||
todayDayIndex: getDayOfWeekIndex(getToday()),
|
todayDayIndex: getDayOfWeekIndex(getToday()),
|
||||||
|
...(slot === MealSlot.EXTRA ? { slot: MealSlot.EXTRA } : {}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,8 +27,9 @@ describe('MealSlot storage isolation', () => {
|
|||||||
jest.useRealTimers();
|
jest.useRealTimers();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('addChoice slot=extra writes only to _extra key, not to obed key', async () => {
|
test('addChoice slot=extra writes only to _extra key, not to obed key, and returns slot=EXTRA', async () => {
|
||||||
await addChoice('user1', false, LunchChoice.OBJEDNAVAM, undefined, TODAY, MealSlot.EXTRA);
|
const result = await addChoice('user1', false, LunchChoice.OBJEDNAVAM, undefined, TODAY, MealSlot.EXTRA);
|
||||||
|
expect(result.slot).toBe(MealSlot.EXTRA);
|
||||||
expect(mockStorageData.has(TODAY_EXTRA_STR)).toBe(true);
|
expect(mockStorageData.has(TODAY_EXTRA_STR)).toBe(true);
|
||||||
expect(mockStorageData.has(TODAY_STR)).toBe(false);
|
expect(mockStorageData.has(TODAY_STR)).toBe(false);
|
||||||
const extraData = mockStorageData.get(TODAY_EXTRA_STR);
|
const extraData = mockStorageData.get(TODAY_EXTRA_STR);
|
||||||
|
|||||||
Reference in New Issue
Block a user