Skip to content

Commit

Permalink
Merge pull request #124 from ReflectionsProjections/dev/riyap/filtering2
Browse files Browse the repository at this point in the history
Dev/riyap/filtering2
  • Loading branch information
divyack2 authored Aug 17, 2024
2 parents 0f43670 + 9993b9b commit 8011fd4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
34 changes: 26 additions & 8 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,37 @@ registrationRouter.get("/", RoleChecker([]), async (req, res, next) => {
}
});

// Get attendees based on a partial filter in body
registrationRouter.post(
"/filter",
RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE]),
RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE], true),
async (req, res, next) => {
try {
const filterData = RegistrationFilterValidator.parse(req.body);
const projection = Object.assign({}, ...filterData.projection);
const attendees = await Database.REGISTRATION.find(
filterData.filter,
{ ...projection, hasSubmitted: 1 }
const { graduations, majors, jobInterests } =
RegistrationFilterValidator.parse(req.body);

const query = {
hasSubmitted: true,
...(graduations && { graduation: { $in: graduations } }),
...(majors && { major: { $in: majors } }),
...(jobInterests && {
jobInterest: { $elemMatch: { $in: jobInterests } },
}),
};

const projection = {
userId: 1,
name: 1,
major: 1,
graduation: 1,
jobInterest: 1,
};

const registrants = await Database.REGISTRATION.find(
query,
projection
);
return res.status(StatusCodes.OK).json(attendees);

return res.status(StatusCodes.OK).json({ registrants });
} catch (error) {
next(error);
}
Expand Down
10 changes: 3 additions & 7 deletions src/services/registration/registration-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ const RegistrationSchema = new mongoose.Schema({
hasSubmitted: { type: Boolean, default: false },
});

// Partial schema for attendee filter
const PartialRegistrationValidator = RegistrationValidator.partial();

const RegistrationFilterValidator = z.object({
filter: PartialRegistrationValidator,
projection: z.array(
z.record(PartialRegistrationValidator.keyof(), z.number().min(1).max(1))
),
graduations: z.array(z.string()).optional(),
majors: z.array(z.string()).optional(),
jobInterests: z.array(z.string()).optional(),
});

export {
Expand Down

0 comments on commit 8011fd4

Please sign in to comment.