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

Fix POST/event #100

Merged
merged 3 commits into from
Oct 18, 2023
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
18 changes: 7 additions & 11 deletions src/database/event-db.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { modelOptions, prop } from "@typegoose/typegoose";
import { ObjectId } from "mongodb";

import Constants from "../constants.js";
import { GenericEventFormat } from "../services/event/event-formats.js";
Expand Down Expand Up @@ -56,13 +55,10 @@ class BaseEvent {
@prop({ required: true })
public isAsync: boolean;

constructor(baseEvent: GenericEventFormat, setId: boolean = true) {
const id: string = new ObjectId().toHexString();
if (setId) {
this.eventId = id;
}
this.description = baseEvent.description;
constructor(baseEvent: GenericEventFormat) {
this.eventId = baseEvent.eventId;
this.name = baseEvent.name;
this.description = baseEvent.description;
this.startTime = baseEvent.startTime;
this.endTime = baseEvent.endTime;
this.locations = baseEvent.locations;
Expand Down Expand Up @@ -100,8 +96,8 @@ export class PublicEvent extends BaseEvent {
@prop({ required: true })
public points: number;

constructor(baseEvent: GenericEventFormat, setId: boolean = true) {
super(baseEvent, setId);
constructor(baseEvent: GenericEventFormat) {
super(baseEvent);
this.eventType = baseEvent.publicEventType ?? "OTHER";
this.isPrivate = baseEvent.isPrivate ?? false;
this.displayOnStaffCheckIn = baseEvent.displayOnStaffCheckIn ?? false;
Expand All @@ -111,8 +107,8 @@ export class PublicEvent extends BaseEvent {
}

export class StaffEvent extends BaseEvent {
constructor(baseEvent: GenericEventFormat, setId: boolean = true) {
super(baseEvent, setId);
constructor(baseEvent: GenericEventFormat) {
super(baseEvent);
this.eventType = baseEvent.staffEventType ?? "OTHER";
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/services/event/event-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ eventsRouter.post("/", strongJwtVerification, async (req: Request, res: Response
const eventId: string = crypto.randomBytes(Constants.EVENT_BYTES_GEN).toString("hex");
const isStaffEvent: boolean = eventFormat.isStaff;
const metadata: EventMetadata = new EventMetadata(eventId, isStaffEvent, eventFormat.endTime);

console.log(eventId);
// Populate the new eventFormat object with the needed params
eventFormat._id = new ObjectId().toString();
eventFormat.eventId = eventId;
Expand All @@ -378,12 +378,14 @@ eventsRouter.post("/", strongJwtVerification, async (req: Request, res: Response
return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" });
}
const event: StaffEvent = new StaffEvent(eventFormat);
console.log(event, metadata);
newEvent = await Models.StaffEvent.create(event);
} else {
if (!isValidPublicFormat(eventFormat)) {
return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" });
}
const event: PublicEvent = new PublicEvent(eventFormat);
console.log(event, metadata);
newEvent = await Models.PublicEvent.create(event);
}
await Models.EventMetadata.create(metadata);
Expand Down Expand Up @@ -612,14 +614,15 @@ eventsRouter.put("/", strongJwtVerification, async (req: Request, res: Response)
return res.status(Constants.BAD_REQUEST).send({ message: "InvalidParams" });
}

const event: StaffEvent = new StaffEvent(eventFormat, false);
const event: StaffEvent = new StaffEvent(eventFormat);
const updatedEvent: StaffEvent | null = await Models.StaffEvent.findOneAndUpdate({ eventId: eventId }, event);
return res.status(Constants.SUCCESS).send(updatedEvent);
} else {
if (!isValidPublicFormat(eventFormat)) {
return res.status(Constants.BAD_REQUEST).send({ message: "InvalidParams" });
}
const event: PublicEvent = new PublicEvent(eventFormat, false);

const event: PublicEvent = new PublicEvent(eventFormat);
const updatedEvent: PublicEvent | null = await Models.PublicEvent.findOneAndUpdate({ eventId: eventId }, event);
return res.status(Constants.SUCCESS).send(updatedEvent);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/staff/staff-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ staffRouter.post("/attendance/", strongJwtVerification, async (req: Request, res
return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" });
}

const metadata: EventMetadata | null = await Models.EventMetadata.findById(eventId);
const metadata: EventMetadata | null = await Models.EventMetadata.findOne({ eventId: eventId });

if (!metadata) {
return res.status(Constants.BAD_REQUEST).send({ error: "EventNotFound" });
Expand Down
Loading