Skip to content

Commit

Permalink
Merge pull request #12 from YouthCatalyst/TECH-174-1
Browse files Browse the repository at this point in the history
TECH-174: Enhancement on Getting Cal.com User based on Availability
  • Loading branch information
valenciusap17 authored Sep 1, 2024
2 parents 3bdf298 + 54a0bad commit bf2e1c4
Showing 1 changed file with 20 additions and 49 deletions.
69 changes: 20 additions & 49 deletions apps/web/pages/api/availabilities/mentor-by-availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import prisma from "@calcom/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const getMentorsSchema = z
.object({
startTime: z.date().or(z.string()),
endTime: z.date().or(z.string()),
startTime: z.string(),
endTime: z.string(),
take: z.number().optional(),
skip: z.number().optional(),
})
Expand All @@ -17,44 +17,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
const { startTime, endTime, take, skip } = getMentorsSchema.parse(req.query);

if (req.method === "GET") {
const parsedStartTime = new Date(startTime as string);
const parsedEndTime = new Date(endTime as string);

const selectedUsers = await prisma.user.findMany({
where: {
schedules: {
some: {
availability: {
some: {
OR: [
...(startTime
? [
{
startTime: {
lte: parsedStartTime,
},
endTime: {
gte: parsedStartTime,
},
},
]
: []),
...(endTime
? [
{
startTime: {
lte: parsedEndTime,
},
endTime: {
gte: parsedEndTime,
},
},
]
: []),
].filter(Boolean),
},
},
},
emailVerified: {
not: null,
},
},
select: {
Expand All @@ -75,17 +41,22 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
returnDateOverrides: true,
},
})
).map(({ busy, dateRanges, oooExcludedDateRanges, timeZone, datesOutOfOffice }, index) => {
const currentUser = selectedUsers[index];
return {
timeZone,
dateRanges,
oooExcludedDateRanges,
busy,
user: currentUser,
datesOutOfOffice,
};
});
)
.map(({ busy, dateRanges, oooExcludedDateRanges, timeZone, datesOutOfOffice }, index) => {
const currentUser = selectedUsers[index];

if (dateRanges.length == 0) return null;

return {
timeZone,
dateRanges,
oooExcludedDateRanges,
busy,
user: currentUser,
datesOutOfOffice,
};
})
.filter((item) => item !== null);

res.status(200).json({
mentors: allUsersAvailability,
Expand Down

0 comments on commit bf2e1c4

Please sign in to comment.