Skip to content
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
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,5 @@ model CustomDay {
endTime DateTime
title String?
description String?
capacity Int @default(10)
}
3 changes: 2 additions & 1 deletion src/app/api/customDay/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ export const fetchApi = async (
};

export const addCustomDay = async (customDay: CreateCustomDayInput) => {
const { date, startTime, endTime, title, description } = customDay;
const { date, startTime, endTime, title, description, capacity } = customDay;
return fetchApi("/api/customDay", "POST", {
date: date.toISOString().split("T")[0],
startTime,
endTime,
title,
description,
capacity,
});
};

Expand Down
5 changes: 3 additions & 2 deletions src/app/api/customDay/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const prisma = new PrismaClient();

export const POST = async (request: NextRequest) => {
try {
const { date, startTime, endTime, title, description } =
const { date, startTime, endTime, title, description, capacity } =
await request.json();

const normalizedDate = new Date(date);
Expand All @@ -24,7 +24,7 @@ export const POST = async (request: NextRequest) => {
if (existing) {
result = await prisma.customDay.update({
where: { id: existing.id },
data: { startTime, endTime, title, description },
data: { startTime, endTime, title, description, capacity },
});
} else {
result = await prisma.customDay.create({
Expand All @@ -34,6 +34,7 @@ export const POST = async (request: NextRequest) => {
endTime,
title,
description,
capacity,
},
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/api/organization/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export const getOrganizations = async () => {
return fetchApi(url, "GET");
};

export const getOrganizationsByDate = async (date: Date) => {
const isoDate = date.toISOString().split("T")[0];
const url = `/api/organization?date=${isoDate}`;
return fetchApi(url, "GET");
};

export const deleteOrganization = async (organizationId: string) => {
const url = `/api/organization?id=${organizationId}`;
return fetchApi(url, "DELETE");
Expand Down
60 changes: 59 additions & 1 deletion src/app/api/organization/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ export const GET = async (request: NextRequest) => {
const { searchParams } = new URL(request.url);

const id: string | undefined = searchParams.get("id") || undefined;
const date: string | undefined = searchParams.get("date") || undefined;

try {
if (id) {
const fetchedOrganization = await prisma.organization.findUnique({
where: { id },
include: { volunteerSessions: true, users: true },
include: {
volunteerSessions: true,
users: {
include: {
volunteerSessions: true,
},
},
},
});

if (!fetchedOrganization) {
Expand All @@ -77,6 +85,56 @@ export const GET = async (request: NextRequest) => {
);
}

if (date) {
try {
const [year, month, day] = date.split("-").map(Number);
const start = new Date(year, month - 1, day);
start.setHours(0, 0, 0, 0);
const end = new Date(start);
end.setDate(start.getDate() + 1);

const orgsWithSlots = await prisma.organization.findMany({
where: {
timeSlots: {
some: {
date: {
gte: start,
lt: end,
},
},
},
},
include: {
timeSlots: {
where: {
date: {
gte: start,
lt: end,
},
},
},
},
});

return NextResponse.json(
{
code: "SUCCESS",
data: orgsWithSlots,
},
{ status: 200 }
);
} catch (error) {
console.error("Error:", error);
return NextResponse.json(
{
code: "ERROR",
message: error,
},
{ status: 404 }
);
}
}

const fetchedOrganizations = await prisma.organization.findMany({
include: {
users: true,
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/password/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const POST = async (request: NextRequest) => {
const codeString = Math.floor(Math.random() * 10000)
.toString()
.padStart(4, "6");
const expire = new Date(Date.now() + 1000 * 60);
const expire = new Date(Date.now() + 1000 * 60 * 15);

await prisma.code.update({
where: { userId: user.id },
Expand Down
1 change: 0 additions & 1 deletion src/app/api/timeSlot/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const fetchApi = async (
export const addTimeSlot = async (
timeSlot: CreateTimeSlotInput,
groupSignupInfo?: {
eventTitle: string;
date: string;
startTime: string;
endTime: string;
Expand Down
1 change: 1 addition & 0 deletions src/app/api/timeSlot/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const POST = async (request: NextRequest) => {
durationHours: timeSlot.durationHours,
date: new Date(timeSlot.date),
approved: timeSlot.approved,
numVolunteers: timeSlot.numVolunteers,
},
});

Expand Down
5 changes: 4 additions & 1 deletion src/app/api/user/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type CreateVolunteerDetailsInput = Omit<
VolunteerDetails,
"id" | "user" | "userId"
>;
type UserUpdateInput = Omit<User, "volunteerDetails" | "volunteerSessions"> & {
organizationName?: string;
};

/**
* Sends an HTTP request to the specified endpoint with the provided method and body.
Expand Down Expand Up @@ -76,7 +79,7 @@ export const deleteUser = async (userID: string) => {
};

export const updateUser = async (
user: User,
user: UserUpdateInput,
volunteerDetails?: VolunteerDetails
) => {
return fetchApi("/api/user", "PATCH", { user, volunteerDetails });
Expand Down
27 changes: 23 additions & 4 deletions src/app/api/user/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ export const GET = async (request: NextRequest) => {
try {
const fetchedUser = await prisma.user.findUnique({
where: id ? { id } : { email },
include: { volunteerDetails: true, volunteerSessions: true },
include: {
volunteerDetails: true,
volunteerSessions: true,
organization: true,
},
});

if (!fetchedUser) {
Expand Down Expand Up @@ -347,13 +351,28 @@ export const PATCH = async (request: NextRequest) => {
volunteerDetails: _,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
volunteerSessions: __,
...userWithoutIdAndRelations
// eslint-disable-next-line @typescript-eslint/no-unused-vars
organizationId: ___,
organizationName,
...userWithoutRelations
} = user;

const organization =
organizationName?.trim() !== ""
? await prisma.organization.findFirst({
where: {
normalizedName: organizationName.trim().toLowerCase(),
},
})
: null;

const updatedUser = await prisma.user.update({
where: { id },
data: {
...userWithoutIdAndRelations,
...userWithoutRelations,
...(organization
? { organization: { connect: { id: organization.id } } }
: { organization: { disconnect: true } }),
volunteerDetails: volunteerDetails
? {
update: {
Expand Down Expand Up @@ -383,7 +402,7 @@ export const PATCH = async (request: NextRequest) => {
{ status: 200 }
);
} catch (error) {
console.error("Error:", error);
console.error("Error updating user:", error);
return NextResponse.json(
{
code: "ERROR",
Expand Down
7 changes: 5 additions & 2 deletions src/app/api/volunteerSession/route.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ export const addVolunteerSession = async (
return fetchApi("/api/volunteerSession", "POST", { volunteerSession });
};

export const updateVolunteerSession = async (userId: string) => {
return fetchApi("/api/volunteerSession", "PATCH", { userId });
export const updateVolunteerSession = async (params: {
userId?: string;
organizationId?: string;
}) => {
return fetchApi("/api/volunteerSession", "PATCH", params);
};

export const getVolunteerSessions = async (userId: string) => {
Expand Down
Loading