change endpoint structure

This commit is contained in:
Joseph J Helfenbein
2025-01-25 08:23:53 -05:00
parent 4cc38b4860
commit 83be62bde2
3 changed files with 133 additions and 62 deletions

View File

@@ -1,9 +1,21 @@
import User from '../../models/User'; import { User } from '../../models/User';
import { connectDB } from '../../lib/utils'; import { connectDB } from '../../lib/utils';
import { NextResponse } from 'next/server';
export async function GET() {
console.log('Fetching patients...');
export default async (req, res) => {
await connectDB(); await connectDB();
try {
const patients = await User.find({ role: 'patient' }); const patients = await User.find({ role: 'patient' });
res.json(patients); console.log('Fetched patients:', patients);
}; return NextResponse.json(patients, { status: 200 });
} catch (error) {
console.error('Error fetching patients:', error);
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -1,13 +1,28 @@
import User from '../../../models/User'; import { User } from '../../../models/User';
import connectDB from '../../../lib/utils'; import { connectDB } from '../../../lib/utils';
import { NextResponse } from 'next/server';
export async function PUT(req) {
console.log('Processing patient update request...');
export default async (req, res) => {
await connectDB(); await connectDB();
const { email } = req.query; try {
const { medications, medicalConditions } = req.body; const url = new URL(req.url);
const email = url.searchParams.get('email');
console.log('Received request to update patient with email:', email); if (!email) {
console.log('No email provided');
return NextResponse.json(
{ message: 'Email is required' },
{ status: 400 }
);
}
const body = await req.json();
const { medications, medicalConditions } = body;
console.log('Updating patient with email:', email);
console.log('Medications:', medications); console.log('Medications:', medications);
console.log('Medical Conditions:', medicalConditions); console.log('Medical Conditions:', medicalConditions);
@@ -15,7 +30,10 @@ export default async (req, res) => {
if (!patient) { if (!patient) {
console.log('Patient not found for email:', email); console.log('Patient not found for email:', email);
return res.status(404).json({ message: 'Patient not found' }); return NextResponse.json(
{ message: 'Patient not found' },
{ status: 404 }
);
} }
patient.medications = medications; patient.medications = medications;
@@ -23,6 +41,12 @@ export default async (req, res) => {
await patient.save(); await patient.save();
console.log('Patient data updated successfully:', patient); console.log('Patient data updated successfully:', patient);
return NextResponse.json(patient, { status: 200 });
res.json(patient); } catch (error) {
}; console.error('Error updating patient data:', error);
return NextResponse.json(
{ message: 'Internal server error' },
{ status: 500 }
);
}
}

View File

@@ -1,14 +1,18 @@
import User from '../../models/User'; import { User } from '../../models/User';
import { connectDB } from '../../lib/utils'; import { connectDB } from '../../lib/utils';
import { NextResponse } from 'next/server';
export default async (req, res) => { export async function GET(req) {
const { userId } = req.query; const userId = req.nextUrl.searchParams.get('userId');
console.log('Received request with userId:', userId); console.log('Received GET request with userId:', userId);
if (!userId) { if (!userId) {
console.log('No userId provided'); console.error('No userId provided');
return res.status(401).json({ message: 'Unauthorized' }); return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
} }
await connectDB(); await connectDB();
@@ -16,23 +20,54 @@ export default async (req, res) => {
const user = await User.findOne({ id: userId }); const user = await User.findOne({ id: userId });
if (!user) { if (!user) {
console.log('User not found for userId:', userId); console.error('User not found for userId:', userId);
return res.status(404).json({ message: 'User not found' }); return NextResponse.json(
{ message: 'User not found' },
{ status: 404 }
);
} }
if (req.method === 'GET') {
console.log('Returning user data:', user); console.log('Returning user data:', user);
res.json(user); return NextResponse.json(user, { status: 200 });
} else if (req.method === 'PUT') { }
const { role } = req.body;
console.log('Updating user role to:', role); export async function PUT(req) {
const userId = req.nextUrl.searchParams.get('userId');
const { role } = await req.json();
console.log('Received PUT request with userId:', userId, 'and role:', role);
if (!userId) {
console.error('No userId provided');
return NextResponse.json(
{ message: 'Unauthorized' },
{ status: 401 }
);
}
await connectDB();
const user = await User.findOne({ id: userId });
if (!user) {
console.error('User not found for userId:', userId);
return NextResponse.json(
{ message: 'User not found' },
{ status: 404 }
);
}
if (!role) {
console.error('No role provided in request body');
return NextResponse.json(
{ message: 'Role is required' },
{ status: 400 }
);
}
user.role = role; user.role = role;
await user.save(); await user.save();
console.log('User role updated successfully');
res.json(user);
} else {
console.log('Method not allowed:', req.method);
res.status(405).json({ message: 'Method not allowed' });
}
};
console.log('User role updated successfully');
return NextResponse.json(user, { status: 200 });
}