Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
winzamark123 committed Nov 7, 2024
1 parent 64672f9 commit da80234
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 61 deletions.
42 changes: 22 additions & 20 deletions app/(api)/_datalib/userToEvent/getUserToEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@
import { NextResponse } from 'next/server';
import { getDatabase } from '@utils/mongodb/mongoClient.mjs';
import { HttpError } from '@utils/response/Errors';
import { ObjectId } from 'mongodb';

export const GetUserToEvent = async (query: object = {}) => {
try {
const db = await getDatabase();

const userEvents = await db.collection('user_to_event').aggregate([
{
$match: query,
},
{
$lookup: {
from: 'events',
localField: 'event_id',
foreignField: '_id',
as: 'events',
const userEvents = await db
.collection('user_to_event')
.aggregate([
{
$match: query,
},
},
{
$lookup: {
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users',
{
$lookup: {
from: 'events',
localField: 'event_id',
foreignField: '_id',
as: 'events',
},
},
},
]).toArray();
{
$lookup: {
from: 'users',
localField: 'user_id',
foreignField: '_id',
as: 'users',
},
},
])
.toArray();

return NextResponse.json(
{ ok: true, body: userEvents, error: null },
Expand Down
25 changes: 14 additions & 11 deletions app/(api)/_datalib/userToEvent/updateUserToEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
NotFoundError,
} from '@utils/response/Errors';

export const updateUserToEvent = async (userId: string, eventId: string, body: object) => {
export const updateUserToEvent = async (
userId: string,
eventId: string,
body: object
) => {
try {
// FOR TESTING ONLY delete later
console.log('Received userId:', userId);
Expand All @@ -28,24 +32,23 @@ export const updateUserToEvent = async (userId: string, eventId: string, body: o
const db = await getDatabase();

// Update user details if the user ID is provided
const userUpdateResult = await db.collection('teams').updateOne(
{ _id: user_id },
{ $set: parsedBody }
);
const userUpdateResult = await db
.collection('users')
.updateOne({ _id: user_id }, { $set: parsedBody });

// Update event details if the event ID is provided
const eventUpdateResult = await db.collection('events').updateOne(
{ _id: event_id },
{ $set: parsedBody }
);
const eventUpdateResult = await db
.collection('events')
.updateOne({ _id: event_id }, { $set: parsedBody });

// Check if either the user or event was updated
const userUpdated = userUpdateResult.modifiedCount > 0;
const eventUpdated = eventUpdateResult.modifiedCount > 0;


if (!userUpdated && !eventUpdated) {
throw new NotFoundError(`No user with id: ${userId} or event with id: ${eventId} found.`);
throw new NotFoundError(
`No user with id: ${userId} or event with id: ${eventId} found.`
);
}

// Success response
Expand Down
21 changes: 1 addition & 20 deletions app/(api)/api/user-to-event/[id]/delete.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// DELETE handler
import { NextRequest, NextResponse } from 'next/server';
import { NextRequest } from 'next/server';
import { DeleteUserToEvent } from '@datalib/userToEvent/deleteUserToEvent';

export async function DELETE(
Expand All @@ -9,22 +9,3 @@ export async function DELETE(
// Return the response from DeleteUserToEvent
return DeleteUserToEvent(params.id);
}

/* export async function DELETE(
request: NextRequest,
{ params }: { params: { id: string } }
) {
const { id } = params;
// Create the query object to pass to DeleteUserToEvent
const query = { _id: id };
const result = await DeleteUserToEvent(query);
// Check if the result was successful or returned an error
if (result.ok) {
return NextResponse.json(result, { status: 200 });
} else {
return NextResponse.json(result, { status: 400 });
}
} */
22 changes: 16 additions & 6 deletions app/(api)/api/user-to-event/[id]/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
import { NextRequest, NextResponse } from 'next/server';
import { GetUserToEvent } from '@datalib/userToEvent/getUserToEvent';

export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
export async function GET(
req: NextRequest,
{ params }: { params: { id: string } }
) {
const { id } = params;
const idType = req.headers.get("idType") || "user_id"; // Default to "user_id" if no header is provided
const idType = req.headers.get('idType') || 'user_id'; // Default to "user_id" if no header is provided

// Validate idType to ensure it's either 'user_id' or 'event_id'
if (idType !== 'user_id' && idType !== 'event_id') {
return NextResponse.json(
{ ok: false, body: null, error: 'Invalid idType provided. Use "user_id" or "event_id".' },
{
ok: false,
body: null,
error: 'Invalid idType provided. Use "user_id" or "event_id".',
},
{ status: 400 }
);
}

const result = await GetUserToEvent({ id, idType: idType as 'user_id' | 'event_id' });
return result;
const result = await GetUserToEvent({
id,
idType: idType as 'user_id' | 'event_id',
});
return result;
}
7 changes: 5 additions & 2 deletions app/(api)/api/user-to-event/[id]/put.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { NextRequest, NextResponse } from 'next/server';
import { NextRequest } from 'next/server';
import { updateUserToEvent } from '@datalib/userToEvent/updateUserToEvent';

export async function PUT(request: NextRequest, { params }: { params: { userId: string; eventId: string } }) {
export async function PUT(
request: NextRequest,
{ params }: { params: { userId: string; eventId: string } }
) {
const { userId, eventId } = params;
const body = await request.json();
return updateUserToEvent(userId, eventId, body);
Expand Down
1 change: 0 additions & 1 deletion app/(api)/api/user-to-event/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { NextRequest } from 'next/server';
import getQueries from '@utils/request/getQueries';
import { GetUserToEvent } from '@datalib/userToEvent/getUserToEvent'; // Adjust the import path as needed
Expand Down
1 change: 0 additions & 1 deletion app/(api)/api/user-to-event/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ import { DELETE } from './[id]/delete';
// const POST = authenticated(post);
// const GET = authenticated(get);


export { POST, GET, /*PUT, */ DELETE };

0 comments on commit da80234

Please sign in to comment.