Skip to content

Commit

Permalink
Merge pull request #244 from UTDallasEPICS/integration
Browse files Browse the repository at this point in the history
integration->main
  • Loading branch information
devAgant authored Dec 5, 2024
2 parents 78d8b27 + 40182e8 commit 0853f79
Show file tree
Hide file tree
Showing 42 changed files with 1,300 additions and 228 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ yarn-error.log*
# local env files
.env*.local
.env
.env*.test

# vercel
.vercel
Expand All @@ -42,3 +43,6 @@ postgres-data
/prisma/customerQueries.js
# Package Lock
package-lock.json

#Cypress env
cypress.env.json
68 changes: 68 additions & 0 deletions app/api/addVolunteer/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

interface VolunteerRequestBody {
firstName: string;
lastName: string;
email: string;
phone: string;
}

export async function POST(req: Request) {
if (req.method !== 'POST') {
return Response.json({
status: 405,
message: 'Method Not Allowed',
});
}

try {
const { firstName, lastName, email, phone } = (await req.json()) as VolunteerRequestBody;

const existingVolunteer = await prisma.volunteer.findFirst({
where: {
OR: [{ email: email }, { phone: phone }],
},
});

if (existingVolunteer) {
// Return a specific error message if the email or phone exists
if (existingVolunteer.email === email) {
return Response.json({
status: 400,
message: 'Volunteer not added! Email already exists',
});
} else if (existingVolunteer.phone === phone) {
return Response.json({
status: 400,
message: 'Volunteer not added! Phone number already exists',
});
}
}

const volunteer = await prisma.volunteer.create({
data: {
firstName: firstName,
lastName: lastName,
email: email,
phone: phone,
rides: {
create: [],
},
},
});

return Response.json({
status: 200,
message: `${volunteer} created successfully!`,
volunteer: volunteer,
});
} catch (error) {
console.error('Error creating volunteer:', error);
return Response.json({
status: 500,
message: 'Internal Server Error',
});
}
}
122 changes: 78 additions & 44 deletions app/api/createCustomerAccount/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/*
** Route to handle customer creation request
*/
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();
Expand All @@ -9,52 +6,89 @@ interface UserRequestBody {
customerEmail: string;
firstName: string;
lastName: string;
middleName?: string;
customerPhone?: string;
streetAddress?: string;
city?: string;
state?: string;
customerZipCode?: number;
birthdate?: string;
}

export async function POST(req: Request) {
if (req.method !== 'POST') {
return Response.json({
status: 405,
message: 'Method Not Allowed',
try {
if (req.method !== 'POST') {
return new Response(
JSON.stringify({
status: 405,
message: 'Method Not Allowed',
}),
{ status: 405 }
);
}

const body = (await req.json()) as UserRequestBody;

// Validation: Ensure required fields are provided
const { customerEmail, firstName, lastName } = body;
if (!customerEmail || !firstName || !lastName) {
return new Response(
JSON.stringify({
status: 400,
message: 'Missing required fields: customerEmail, firstName, or lastName',
}),
{ status: 400 }
);
}

const existingCustomer = await prisma.customer.findUnique({
where: {
customerEmail,
},
});
}

const { customerEmail, firstName, lastName } = (await req.json()) as UserRequestBody;

// searches for the customer with email, first name, and last name
const existingCustomer = await prisma.customer.findFirst({
where: {
customerEmail,
firstName,
lastName,
},
});

if (existingCustomer) {
// if the customer already exists
return Response.json({
status: 409,
message: 'Customer already exists',
if (existingCustomer) {
return new Response(
JSON.stringify({
status: 409,
message: 'Customer already exists',
}),
{ status: 409 }
);
}

const newCustomer = await prisma.customer.create({
data: {
customerEmail,
firstName,
lastName,
middleName: body.middleName || null,
customerPhone: body.customerPhone || '0000000000',
streetAddress: body.streetAddress || 'N/A',
city: body.city || 'N/A',
state: body.state || 'N/A',
customerZipCode: body.customerZipCode || 0,
birthdate: body.birthdate ? new Date(body.birthdate).toISOString() : null,
},
});
}

//otherwise, a new customer is created
await prisma.customer.create({
data: {
customerEmail,
firstName,
middleName: 'dummy middle name',
lastName,
customerPhone: '0000000000',
streetAddress: 'dummy street address',
city: 'dummy city',
state: 'dummy state',
birthdate: new Date('2022-09-27T18:00:00.000Z').toISOString(),
},
});

return Response.json({
status: 200,
message: 'Customer created successfully',
});
return new Response(
JSON.stringify({
status: 201,
message: 'Customer created successfully',
data: newCustomer,
}),
{ status: 201 }
);
} catch (error) {
console.error('Error creating customer:', error);
return new Response(
JSON.stringify({
status: 500,
message: 'Internal Server Error',
error: (error as Error).message,
}),
{ status: 500 }
);
}
}
14 changes: 7 additions & 7 deletions app/api/createRide/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ export async function POST(req: Request) {
const time = new Date();

const [hours, minutes] = startTime.split(':').map(Number);
time.setHours(hours, minutes, 0, 0); // Set the time on the current date
time.setHours(hours, minutes, 0, 0);

await prisma.ride.create({
data: {
customerName: clientName.valueOf(),
time: time,
startTime: time,
endTime: time,
volunteerID: 1,
customerID: 1,
date: '2024-01-15T00:00:00Z',
endLocation: '456 Wonderland Plaza',
customerPhone: phoneNumber.valueOf(),
startLocation: address.valueOf(),
},
});

return Response.json({
status: 200,
message: 'Customer created successfully',
});
}
50 changes: 50 additions & 0 deletions app/api/deleteVolunteer/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

interface DeleteVolunteerParams {
id: number;
}

export async function DELETE(req: Request) {
if (req.method !== 'DELETE') {
return Response.json({
status: 405,
message: 'Method Not Allowed',
});
}

try {
const { id } = (await req.json()) as DeleteVolunteerParams;

const existingVolunteer = await prisma.volunteer.findUnique({
where: {
VolunteerID: id,
},
});

if (!existingVolunteer) {
return Response.json({
status: 404,
message: 'Volunteer not found',
});
}

await prisma.volunteer.delete({
where: {
VolunteerID: id,
},
});

return Response.json({
status: 200,
message: 'Volunteer deleted successfully',
});
} catch (error) {
console.error('Error deleting volunteer:', error);
return Response.json({
status: 500,
message: 'Internal Server Error',
});
}
}
93 changes: 93 additions & 0 deletions app/api/editVolunteer/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

interface EditVolunteerParams {
id: number;
firstName: string;
lastName: string;
email: string;
phone: string;
}

export async function PUT(req: Request) {
if (req.method !== 'PUT') {
return Response.json({
status: 405,
message: 'Method Not Allowed',
});
}

try {
const { id, firstName, lastName, email, phone } = (await req.json()) as EditVolunteerParams;

const existingVolunteer = await prisma.volunteer.findUnique({
where: {
VolunteerID: id,
},
});

if (!existingVolunteer) {
return Response.json({
status: 404,
message: 'Volunteer not found',
});
}

const existingEmailVolunteer = await prisma.volunteer.findFirst({
where: {
email: email,
NOT: {
VolunteerID: id,
},
},
});

if (existingEmailVolunteer && existingEmailVolunteer.VolunteerID !== id) {
return Response.json({
status: 409,
message: 'Email is already in use by another volunteer',
});
}

const existingPhoneVolunteer = await prisma.volunteer.findFirst({
where: {
phone: phone,
NOT: {
VolunteerID: id,
},
},
});

if (existingPhoneVolunteer && existingPhoneVolunteer.VolunteerID !== id) {
return Response.json({
status: 409,
message: 'Phone number is already in use by another volunteer',
});
}

const updatedVolunteer = await prisma.volunteer.update({
where: {
VolunteerID: id,
},
data: {
firstName,
lastName,
email,
phone,
},
});

return Response.json({
status: 200,
message: 'Volunteer updated successfully',
volunteer: updatedVolunteer,
});
} catch (error) {
console.error('Error updating volunteer:', error);
return Response.json({
status: 500,
message: 'Internal Server Error',
});
}
}
Loading

0 comments on commit 0853f79

Please sign in to comment.