Skip to content

Commit

Permalink
add 3 endpts for favs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Jun 24, 2024
1 parent db3c351 commit 37898fe
Show file tree
Hide file tree
Showing 7 changed files with 4,496 additions and 4,405 deletions.
6 changes: 3 additions & 3 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
cd /home/ubuntu/rp-api
yarn build
#!/bin/bash
cd /home/ubuntu/rp-api
yarn build
22 changes: 11 additions & 11 deletions scripts/install_dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash
cd /home/ubuntu/rp-api
sudo yarn

sudo pm2 describe appname 2>&1 /dev/null
RUNNING=$?


if [ "${RUNNING}" -eq 0 ]; then
sudo pm2 start build/app.js --name RP_API -i 2 --wait-ready --listen-timeout 10000
fi;
#!/bin/bash
cd /home/ubuntu/rp-api
sudo yarn

sudo pm2 describe appname 2>&1 /dev/null
RUNNING=$?


if [ "${RUNNING}" -eq 0 ]; then
sudo pm2 start build/app.js --name RP_API -i 2 --wait-ready --listen-timeout 10000
fi;
6 changes: 3 additions & 3 deletions scripts/reload_server.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
udo pm2 reload RP_API!/bin/bash
cd /home/ubuntu/rp-api
sudo pm2 reload RP_API
udo pm2 reload RP_API!/bin/bash
cd /home/ubuntu/rp-api
sudo pm2 reload RP_API
86 changes: 86 additions & 0 deletions src/services/attendees/attendee-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,92 @@ dotenv.config();

const attendeeRouter = Router();

// Favorite an event for an attendee
attendeeRouter.post(
"/favoriteEvents/:eventId",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = req.params;

const attendee = await Database.ATTENDEES.findOne({ userId });

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

await Database.ATTENDEES.updateOne(
{ userId: userId },
{ $addToSet: { favorites: eventId } }
);

return res.status(StatusCodes.OK).json(attendee);
} catch (error) {
next(error);
}
}
);

// Unfavorite an event for an attendee
attendeeRouter.delete(
"/favoriteEvents/:eventId",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = req.params;

const attendee = await Database.ATTENDEES.findOne({ userId });

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

await Database.ATTENDEES.updateOne(
{ userId: userId },
{ $pull: { favorites: eventId } }
);

return res.status(StatusCodes.OK).json(attendee);
} catch (error) {
next(error);
}
}
);

// Get favorite events for an attendee
attendeeRouter.get(
"/favoriteEvents",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;

const attendee = await Database.ATTENDEES.findOne({
userId,
}).populate("favorites");

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

return res.status(StatusCodes.OK).json(attendee.favorites);
} catch (error) {
next(error);
}
}
);

// Create a new attendee
attendeeRouter.post("/", async (req, res, next) => {
try {
Expand Down
3 changes: 3 additions & 0 deletions src/services/attendees/attendee-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import mongoose from "mongoose";
import { StringDecoder } from "string_decoder";

Check failure on line 2 in src/services/attendees/attendee-schema.ts

View workflow job for this annotation

GitHub Actions / build

'StringDecoder' is declared but its value is never read.

Check failure on line 2 in src/services/attendees/attendee-schema.ts

View workflow job for this annotation

GitHub Actions / lint

'StringDecoder' is defined but never used
import { z } from "zod";

// Zod schema for attendee
Expand All @@ -19,6 +20,7 @@ const AttendeeValidator = z.object({
dayFour: z.boolean(),
dayFive: z.boolean(),
}),
favorites: z.array(z.string()).optional(),
});

// Mongoose schema for attendee
Expand All @@ -45,6 +47,7 @@ const AttendeeSchema = new mongoose.Schema({
),
default: () => ({}),
},
favorites: [{ type: String }],
});

export { AttendeeSchema, AttendeeValidator };
4 changes: 3 additions & 1 deletion src/services/events/events-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Schema } from "mongoose";
import mongoose, { Schema } from "mongoose";
import { z } from "zod";
import { v4 as uuidv4 } from "uuid";

Expand Down Expand Up @@ -70,3 +70,5 @@ export const EventSchema = new Schema({
enum: EventType.Values,
},
});

export const Event = mongoose.model("Event", EventSchema);
Loading

0 comments on commit 37898fe

Please sign in to comment.