Fix errors

This commit is contained in:
Gabriel Brown 2024-10-21 11:04:29 -05:00
parent 4c3688b065
commit fc8f26dd0b
4 changed files with 20 additions and 10 deletions

View File

@ -9,14 +9,17 @@ export const POST = async (request: NextRequest) => {
const middlewareResponse = await middleware(request); const middlewareResponse = await middleware(request);
if (middlewareResponse) return middlewareResponse; if (middlewareResponse) return middlewareResponse;
try { try {
const url = new URL(request.url); const body = await request.json() as {
const userId = Number.parseInt(url.searchParams.get('userId') ?? ''); userId: number;
const partnerId = Number.parseInt(url.searchParams.get('partnerId') ?? ''); targetUserId: number;
if (!userId || !partnerId || isNaN(userId) || isNaN(partnerId)) };
const { userId, targetUserId } = body;
console.log('User ID:', userId, 'Partner ID:', targetUserId);
if (!userId || !targetUserId || isNaN(userId) || isNaN(targetUserId))
return NextResponse.json( return NextResponse.json(
{ message: 'Missing userId or partnerId' }, { status: 400 } { message: 'Missing userId or targetUserId' }, { status: 400 }
); );
const relationshipData: RelationshipData | null = await createRelationshipRequest(userId, partnerId); const relationshipData: RelationshipData | null = await createRelationshipRequest(userId, targetUserId);
if (!relationshipData) { if (!relationshipData) {
return NextResponse.json( return NextResponse.json(
{ message: 'Error creating relationship request' }, { status: 500 } { message: 'Error creating relationship request' }, { status: 500 }

View File

@ -9,9 +9,12 @@ export const POST = async (request: NextRequest) => {
const middlewareResponse = await middleware(request); const middlewareResponse = await middleware(request);
if (middlewareResponse) return middlewareResponse; if (middlewareResponse) return middlewareResponse;
try { try {
const url = new URL(request.url); const body = await request.json() as {
const userId = Number.parseInt(url.searchParams.get('userId') ?? ''); userId: number;
const status = url.searchParams.get('status'); status: string;
};
const { userId, status } = body;
console.log('User ID:', userId, 'Status:', status);
if (!userId || !status || isNaN(userId)) if (!userId || !status || isNaN(userId))
return NextResponse.json( return NextResponse.json(
{ message: 'Missing userId or status' }, { status: 400 } { message: 'Missing userId or status' }, { status: 400 }
@ -21,7 +24,7 @@ export const POST = async (request: NextRequest) => {
await updateRelationshipStatus(userId, theStatus); await updateRelationshipStatus(userId, theStatus);
if (!relationshipData || relationshipData === undefined) { if (!relationshipData || relationshipData === undefined) {
return NextResponse.json( return NextResponse.json(
{ message: 'Error updating relationship status' }, { status: 500 } { message: 'Error updating relationship status.' }, { status: 500 }
); );
} }
return NextResponse.json(relationshipData); return NextResponse.json(relationshipData);

View File

@ -226,6 +226,10 @@ export const updateRelationshipStatus = async (
throw new Error('Relationship not found'); throw new Error('Relationship not found');
} }
const relationship = relationships[0] as Relationship; const relationship = relationships[0] as Relationship;
// Everything above is just getting info
if (userId === relationship.requestorId) {
throw new Error('The requestor cannot accept the relationship they requested.');
}
if (status === 'accepted') { if (status === 'accepted') {
await db.update(schema.relationships) await db.update(schema.relationships)
.set({ isAccepted: true }) .set({ isAccepted: true })