Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 774be3df6d |
@@ -65,16 +65,6 @@ export function getVapidPublicKey(): string | undefined {
|
||||
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. */
|
||||
async function checkAndSendReminders(): Promise<void> {
|
||||
|
||||
@@ -189,7 +189,7 @@ router.post("/testPush", async (req, res, next) => {
|
||||
webpush.setVapidDetails(subject, publicKey, privateKey);
|
||||
await webpush.sendNotification(
|
||||
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 });
|
||||
} catch (e: any) { next(e) }
|
||||
|
||||
@@ -82,7 +82,8 @@ const router = express.Router();
|
||||
router.post("/addChoice", async (req: Request<{}, any, AddChoiceData["body"]>, res, next) => {
|
||||
const login = getLogin(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;
|
||||
if (req.body.dayIndex != null) {
|
||||
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) => {
|
||||
const login = getLogin(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;
|
||||
if (req.body.dayIndex != null) {
|
||||
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) => {
|
||||
const login = getLogin(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;
|
||||
if (req.body.dayIndex != null) {
|
||||
let dayIndex;
|
||||
@@ -146,7 +149,8 @@ router.post("/updateNote", async (req: Request<{}, any, UpdateNoteData["body"]>,
|
||||
const login = getLogin(parseToken(req));
|
||||
const trusted = getTrusted(parseToken(req));
|
||||
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 {
|
||||
if (note && note.length > 70) {
|
||||
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) => {
|
||||
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 {
|
||||
const data = await updateBuyer(login, slot);
|
||||
getWebsocket().emit("message", data);
|
||||
|
||||
@@ -66,13 +66,10 @@ router.post("/push/unsubscribe", async (req, res, next) => {
|
||||
} 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) => {
|
||||
try {
|
||||
const { login } = req.body;
|
||||
if (!login) {
|
||||
return res.status(400).json({ error: "Nebyl předán login" });
|
||||
}
|
||||
const login = getLogin(parseToken(req));
|
||||
const data = await addChoice(login, false, 'NEOBEDVAM', undefined, undefined);
|
||||
getWebsocket().emit("message", data);
|
||||
res.status(200).json({});
|
||||
|
||||
@@ -59,7 +59,6 @@ export async function getData(date?: Date, slot?: MealSlot): Promise<ClientData>
|
||||
SENKSERIKOVA: await getRestaurantMenu('SENKSERIKOVA', date),
|
||||
}
|
||||
}
|
||||
if (slot === MealSlot.EXTRA) clientData.slot = MealSlot.EXTRA;
|
||||
return clientData;
|
||||
}
|
||||
|
||||
@@ -531,9 +530,9 @@ export async function updateNote(login: string, trusted: boolean, note?: string,
|
||||
* @param time preferovaný čas odchodu
|
||||
* @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();
|
||||
let clientData = await getClientData(usedDate, slot);
|
||||
let clientData = await getClientData(usedDate);
|
||||
const found = Object.values(clientData.choices).find(location => login in location);
|
||||
// TODO validace, že se jedná o restauraci
|
||||
if (found) {
|
||||
@@ -545,7 +544,7 @@ export async function updateDepartureTime(login: string, time?: string, date?: D
|
||||
}
|
||||
found[login].departureTime = time;
|
||||
}
|
||||
await storage.setData(getDataKey(usedDate, slot), clientData);
|
||||
await storage.setData(getDataKey(usedDate), clientData);
|
||||
}
|
||||
return clientData;
|
||||
}
|
||||
@@ -581,5 +580,6 @@ export async function getClientData(date?: Date, slot?: MealSlot): Promise<Clien
|
||||
return {
|
||||
...clientData,
|
||||
todayDayIndex: getDayOfWeekIndex(getToday()),
|
||||
...(slot === MealSlot.EXTRA ? { slot: MealSlot.EXTRA } : {}),
|
||||
}
|
||||
}
|
||||
@@ -27,8 +27,9 @@ describe('MealSlot storage isolation', () => {
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
test('addChoice slot=extra writes only to _extra key, not to obed key', async () => {
|
||||
await addChoice('user1', false, LunchChoice.OBJEDNAVAM, undefined, TODAY, MealSlot.EXTRA);
|
||||
test('addChoice slot=extra writes only to _extra key, not to obed key, and returns slot=EXTRA', async () => {
|
||||
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_STR)).toBe(false);
|
||||
const extraData = mockStorageData.get(TODAY_EXTRA_STR);
|
||||
|
||||
Reference in New Issue
Block a user