Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Juanito98 committed Nov 5, 2024
1 parent df458c9 commit f2840f6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/lib/calendly.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { UserAvailability } from "@/types/mentor.schema";
import { TTLCache } from "./cache";
import type { JSONValue } from "@/types/json";

const CALENDLY_API_BASE_URL = "https://api.calendly.com";

Expand Down Expand Up @@ -28,7 +29,7 @@ async function getUserUri(token: string): Promise<string> {
};
const response = await fetch(`${CALENDLY_API_BASE_URL}/users/me`, options);
if (response.status === 429) {
throw Error("Calendly RareLimit");
throw Error("Calendly RateLimit");
}
const json = await response.json();
if (response.status !== 200) {
Expand Down Expand Up @@ -72,7 +73,7 @@ async function getEventUri(
options,
);
if (response.status === 429) {
throw Error("Calendly RareLimit");
throw Error("Calendly RateLimit");
}
const json = await response.json();
if (response.status !== 200) {
Expand Down Expand Up @@ -146,7 +147,7 @@ async function getAvailableStartTimes({
options,
);
if (response.status === 429) {
throw Error("Calendly RareLimit");
throw Error("Calendly RateLimit");
}
const json = await response.json();
if (response.status !== 200) {
Expand Down Expand Up @@ -208,8 +209,7 @@ export async function getEventOrInvitee({
}: {
token: string;
url: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}): Promise<any> {
}): Promise<JSONValue> {
const options = {
method: "GET",
headers: {
Expand All @@ -218,7 +218,7 @@ export async function getEventOrInvitee({
};
const response = await fetch(url, options);
if (response.status === 429) {
throw Error("Calendly RareLimit");
throw Error("Calendly RateLimit");
}
const json = await response.json();
if (response.status !== 200) {
Expand Down
47 changes: 26 additions & 21 deletions src/pages/api/mentoria/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { getEventOrInvitee } from "@/lib/calendly";
import { getAccessToken } from "@/lib/oauth";
import { OauthProvider } from "@prisma/client";
import type { JSONValue } from "@/types/json";

async function scheduleMentoriaHandler(
req: NextApiRequest,
Expand All @@ -26,48 +27,52 @@ async function scheduleMentoriaHandler(
calendlyPayload,
} = body;

let eventPayload: JSONValue = calendlyPayload.event;
let inviteePayload: JSONValue = calendlyPayload.invitee;
let meetingTime = meetingTimeOpt || null;
try {
const token = await getAccessToken(volunteerAuthId, OauthProvider.CALENDLY);
const eventPayload = await getEventOrInvitee({
eventPayload = await getEventOrInvitee({
token,
url: calendlyPayload.event.uri,
});
calendlyPayload.event = eventPayload;
if (eventPayload.start_time) {
if (
eventPayload !== null &&
typeof eventPayload === "object" &&
"start_time" in eventPayload &&
typeof eventPayload.start_time === "string"
) {
meetingTime = eventPayload.start_time;
}
const inviteePayload = await getEventOrInvitee({
inviteePayload = await getEventOrInvitee({
token,
url: calendlyPayload.invitee.uri,
});
calendlyPayload.invitee = inviteePayload;
} catch (e) {
console.error(e);
}

meetingTime = meetingTime || new Date(Date.now()).toISOString();

await prisma.mentoria.upsert({
where: {
volunteerParticipationId_contestantParticipantId_meetingTime: {
try {
await prisma.mentoria.create({
data: {
volunteerParticipationId,
contestantParticipantId,
status: "SCHEDULED",
meetingTime,
metadata: {
calendly: {
event: eventPayload,
invitee: inviteePayload,
},
},
},
},
update: {},
create: {
volunteerParticipationId,
contestantParticipantId,
status: "SCHEDULED",
meetingTime,
metadata: {
calendly: calendlyPayload,
},
},
});

});
} catch (e) {
console.error("scheduleMentoriaHandler: could not store mentoria", e);
return res.status(500).json({ message: "Internal server error" });
}
return res.status(200).json({ success: true });
}

Expand Down
11 changes: 10 additions & 1 deletion src/pages/mentorias.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ export const getServerSideProps: GetServerSideProps<{
const ofmi = await findMostRecentOfmi();
const participation = await findParticipation(ofmi, email);

if (participation === null) {
return {
redirect: {
destination: "/",
permanent: false,
},
};
}

return {
props: {
ofmiId: ofmi.id,
ofmiEdition: ofmi.edition,
contestantParticipantId: participation?.contestantParticipantId || null,
contestantParticipantId: participation.contestantParticipantId,
},
};
};
7 changes: 7 additions & 0 deletions src/types/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type JSONPrimitive = string | number | boolean | null | undefined;

export type JSONValue =
| JSONPrimitive
| {
[key: string]: JSONValue;
};

0 comments on commit f2840f6

Please sign in to comment.