Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notif Fixes #199

Merged
merged 10 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions src/database/attendee-db.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { prop } from "@typegoose/typegoose";

export class AttendeeMetadata {
@prop({ required: true })
public userId: string;

constructor(id: string) {
this.userId = id;
}
}

export class AttendeeProfile {
@prop({ required: true })
public userId: string;
Expand Down
4 changes: 1 addition & 3 deletions src/database/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import mongoose, { Model } from "mongoose";
import { getModelForClass } from "@typegoose/typegoose";

import { AuthInfo } from "./auth-db.js";
import { AttendeeFollowing, AttendeeMetadata, AttendeeProfile } from "./attendee-db.js";
import { AttendeeFollowing, AttendeeProfile } from "./attendee-db.js";
import { AdmissionDecision } from "./admission-db.js";
import { MentorOfficeHours } from "./mentor-db.js";
import { Event, EventAttendance, EventFollowers } from "./event-db.js";
Expand Down Expand Up @@ -31,7 +31,6 @@ export enum Group {

// Collections for each database, where models will be stored
enum AttendeeCollection {
METADATA = "metadata",
PROFILE = "profile",
FOLLOWING = "following",
}
Expand Down Expand Up @@ -96,7 +95,6 @@ function getModel<T>(of: AnyParamConstructor<any>, group: Group, collection: str
// Define models
export default class Models {
// Attendee
static AttendeeMetadata: Model<AttendeeMetadata> = getModel(AttendeeMetadata, Group.ATTENDEE, AttendeeCollection.METADATA);
static AttendeeProfile: Model<AttendeeProfile> = getModel(AttendeeProfile, Group.ATTENDEE, AttendeeCollection.PROFILE);
static AttendeeFollowing: Model<AttendeeFollowing> = getModel(
AttendeeFollowing,
Expand Down
20 changes: 18 additions & 2 deletions src/services/notification/notification-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ import { RouterError } from "../../middleware/error-handler.js";
import { StatusCode } from "status-code-enum";
import Models from "../../database/models.js";
import { JwtPayload } from "../auth/auth-models.js";
import { hasAdminPerms } from "../auth/auth-lib.js";
import { hasAdminPerms, hasStaffPerms } from "../auth/auth-lib.js";
import { NotificationSendFormat, isValidNotificationSendFormat } from "./notification-formats.js";
import { StaffShift } from "database/staff-db.js";
import { NotificationsMiddleware } from "../../middleware/fcm.js";

const notificationsRouter: Router = Router();

notificationsRouter.get("/", strongJwtVerification, async (_: Request, res: Response, next: NextFunction) => {
const payload: JwtPayload = res.locals.payload as JwtPayload;

if (!hasStaffPerms(payload)) {
return next(new RouterError(StatusCode.ClientErrorForbidden, "Forbidden"));
}

const notifs = await Models.NotificationMessages.find();

return res.status(StatusCode.SuccessOK).send(notifs ?? []);
});

// register your current device token as associated with your userId
notificationsRouter.post("/", strongJwtVerification, async (req: Request, res: Response, next: NextFunction) => {
const payload: JwtPayload = res.locals.payload as JwtPayload;
Expand Down Expand Up @@ -67,7 +79,11 @@ notificationsRouter.post(
targetUserIds = targetUserIds.concat(staffUserIds);
}

// TODO: WAIT ON FOOD WAVE STUFF
if (sendRequest.foodWave) {
const foodwaves = await Models.AttendeeProfile.find({ foodWave: sendRequest.foodWave });
const foodUserIds = foodwaves.map((x) => x.userId);
targetUserIds = targetUserIds.concat(foodUserIds);
}

const messageTemplate = {
notification: {
Expand Down
7 changes: 1 addition & 6 deletions src/services/profile/profile-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { AttendeeMetadata, AttendeeProfile } from "database/attendee-db.js";
import { AttendeeProfile } from "database/attendee-db.js";
import { StatusCode } from "status-code-enum";
import Config from "../../config.js";
import Models from "../../database/models.js";
Expand All @@ -15,10 +15,6 @@ const TESTER_USER = {
foodWave: 1,
} satisfies AttendeeProfile;

const TESTER_METADATA = {
userId: TESTER.id,
} satisfies AttendeeMetadata;

const TESTER_USER_2 = {
userId: "tester2",
displayName: TESTER.name + "2",
Expand Down Expand Up @@ -51,7 +47,6 @@ const profile: AttendeeProfile = {

beforeEach(async () => {
await Models.AttendeeProfile.create(TESTER_USER);
await Models.AttendeeMetadata.create(TESTER_METADATA);
await Models.AttendeeProfile.create(TESTER_USER_2);
await Models.AttendeeProfile.create(TESTER_USER_3);
});
Expand Down
7 changes: 2 additions & 5 deletions src/services/profile/profile-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextFunction, Response } from "express-serve-static-core";
import Config from "../../config.js";
import { Avatars } from "../../config.js";
import { isValidLimit, updatePoints, updateCoins } from "./profile-lib.js";
import { AttendeeMetadata, AttendeeProfile } from "../../database/attendee-db.js";
import { AttendeeProfile } from "../../database/attendee-db.js";
import { RegistrationApplication } from "../../database/registration-db.js";

import Models from "../../database/models.js";
Expand Down Expand Up @@ -272,9 +272,7 @@ profileRouter.post("/", strongJwtVerification, async (req: Request, res: Respons

// Create a metadata object, and return it
try {
const profileMetadata: AttendeeMetadata = new AttendeeMetadata(profile.userId);
const newProfile = await Models.AttendeeProfile.create(profile);
await Models.AttendeeMetadata.create(profileMetadata);
return res.status(StatusCode.SuccessOK).send(newProfile);
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -304,9 +302,8 @@ profileRouter.delete("/", strongJwtVerification, async (_: Request, res: Respons
const decodedData: JwtPayload = res.locals.payload as JwtPayload;

const attendeeProfileDeleteResponse: DeleteResult = await Models.AttendeeProfile.deleteOne({ userId: decodedData.id });
const attendeeMetadataDeleteResponse: DeleteResult = await Models.AttendeeMetadata.deleteOne({ userId: decodedData.id });

if (attendeeMetadataDeleteResponse.deletedCount == 0 || attendeeProfileDeleteResponse.deletedCount == 0) {
if (attendeeProfileDeleteResponse.deletedCount == 0) {
return next(new RouterError(StatusCode.ClientErrorNotFound, "AttendeeNotFound"));
}
return res.status(StatusCode.SuccessOK).send({ success: true });
Expand Down
Loading