diff --git a/package-lock.json b/package-lock.json index a109d94..003ae76 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "backend2", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 99fb3bc..2d9d18c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backend2", - "version": "1.0.0", + "version": "1.0.1", "description": "", "main": "src/index.js", "type": "module", diff --git a/src/attendence.ts b/src/attendence.ts index 182d404..5aed945 100644 --- a/src/attendence.ts +++ b/src/attendence.ts @@ -1,7 +1,15 @@ import { Job, scheduleJob } from "node-schedule"; +interface IAttendence { + auto: { + _id: string; + hour?: string + }[]; + notes: string; +} + class Attendence { - private attendence = new Map(); + private attendence = new Map(); private job: Job constructor () { this.job = scheduleJob("0 0 * * *", () => { @@ -9,18 +17,22 @@ class Attendence { }) } - setRoom (room: string, att: {_id: string, hour?: string}[]) { + setRoom (room: string, att: IAttendence) { this.attendence.set(room, att) } + clearRoom (room: string) { + this.attendence.delete(room) + } + getRoom (room: string) { return this.attendence.get(room) } summary () { - var summary: {room: string, hours: string[]}[] = [] + var summary: {room: string, hours: string[], notes: string}[] = [] this.attendence.forEach((v, k) => { - summary.push({room: k, hours: v.map(i => i.hour)}) + summary.push({room: k, hours: v.auto.map(i => i.hour), notes: v.notes}) }) return summary } diff --git a/src/index.ts b/src/index.ts index 48e8a2e..7c448d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -54,8 +54,8 @@ app.use(passport.session()) //#region Passport strategies initialization passport.use("normal",new LocalStrategy(async function verify(uname,pass,done) { let query = await User.findOne({uname: uname.toLowerCase()}) - if (query.locked == true) return done(null, false) if (query) { + if (query.locked == true) return done(null, false) if (await bcrypt.compare(pass, query.pass)) { return done(null, query) } else done(null, false) diff --git a/src/notif.ts b/src/notif.ts index 7841a72..f330b8f 100644 --- a/src/notif.ts +++ b/src/notif.ts @@ -1,7 +1,6 @@ -import { PushSubscription, RequestOptions, VapidKeys, sendNotification } from "web-push"; +import { PushSubscription, RequestOptions, VapidKeys, WebPushError, sendNotification } from "web-push"; import Notification from "./schemas/Notification"; import vapidKeys from "./vapidKeys"; -import { Types } from "mongoose"; import { IUser } from "./schemas/User"; export class NotifcationHelper { @@ -26,12 +25,23 @@ export class NotifcationHelper { result = await sendNotification(v, message, this.options) count++ } catch (error) { - if (error.statusCode == 410) { - console.log("GONE") - await Notification.findOneAndDelete({endpoint: v.endpoint, keys: v.keys}) - subslen-- + if (error instanceof WebPushError) { + switch (error.statusCode) { + case 410: + console.log("GONE") + await Notification.findOneAndDelete({endpoint: v.endpoint, keys: v.keys}) + subslen-- + break; + case 404: + console.warn("NOT FOUND", error.message) + await Notification.findOneAndDelete(v) + subslen-- + break; + default: + console.log(error) + break; + } } - else console.log(error) } } return {sent: count, possible: subslen} diff --git a/src/routes/api/admin/clean.ts b/src/routes/api/admin/clean.ts index 6967fb8..57deec4 100644 --- a/src/routes/api/admin/clean.ts +++ b/src/routes/api/admin/clean.ts @@ -82,6 +82,11 @@ cleanRouter.post('/attendence/:room', async (req, res) => { res.send({status: 200}) }) +cleanRouter.delete('/attendence/:room', async (req, res) => { + attendence.clearRoom(req.params.room) + res.send({status: 200}) +}) + cleanRouter.get('/attendenceSummary', async (req, res) => { res.send(attendence.summary()) }) diff --git a/src/routes/api/admin/keys.ts b/src/routes/api/admin/keys.ts index 15d4e13..106c57b 100644 --- a/src/routes/api/admin/keys.ts +++ b/src/routes/api/admin/keys.ts @@ -2,7 +2,7 @@ import { Router } from "express"; import capability, { Features } from "@/capability"; import Key from "@schemas/Key"; import usettings from "@/usettings"; -import User from "@schemas/User"; +import User, { IUser } from "@schemas/User"; import { Perms, adminPerm } from "@/utility"; const keysRouter = Router() @@ -11,7 +11,7 @@ keysRouter.use(capability.mw(Features.Key)) keysRouter.use(adminPerm(Perms.Key)) keysRouter.get("/", async (req, res) => { - var keys = await Key.find({}, {}, {sort: {borrow: -1}}).populate("whom", {uname: 1, _id: 1, room: 1}) + var keys = await Key.find({}, undefined, {sort: {borrow: -1}}).populate & {_id: string}>({path: "whom", select: { _id: 1, uname: 1, room: 1}}) res.send(keys) }) diff --git a/src/routes/api/appRouter.ts b/src/routes/api/appRouter.ts index 8df0c4a..72543d9 100644 --- a/src/routes/api/appRouter.ts +++ b/src/routes/api/appRouter.ts @@ -5,7 +5,7 @@ import Menu from "@schemas/Menu"; import Vote from "@schemas/Vote"; import { vote } from "@/pipelines/vote"; import capability, { Features } from "@/capability"; -import Key from "@schemas/Key"; +import Key, { IKey } from "@schemas/Key"; import usettings from "@/usettings"; import Grade from "@schemas/Grade"; import { createHash } from "node:crypto"; @@ -58,7 +58,7 @@ appRouter.post("/menu/:timestamp", capability.mw(Features.Menu), async (req, res }) appRouter.get("/keys", capability.mw(Features.Key), async (req, res) => { - var keys = await Key.find({tb: {$exists: false}}, {_id: 0, room: 1, whom: 0}, {sort: {room: 1}}) + var keys = await Key.find>({tb: {$exists: false}}, {room: 1}, {sort: {room: 1}}) var occ = keys.map(x=>x.room) var all = usettings.settings.keyrooms var free = all.filter(x=>!occ.includes(x)).sort().map(x => { diff --git a/src/schemas/Key.ts b/src/schemas/Key.ts index d24044f..bf28f62 100644 --- a/src/schemas/Key.ts +++ b/src/schemas/Key.ts @@ -1,6 +1,6 @@ import { Schema, Types, model } from "mongoose" -interface IKey { +export interface IKey { room: string; whom: Types.ObjectId; borrow: Date;