accounts page

This commit is contained in:
Joseph J Helfenbein
2025-01-25 06:13:43 -05:00
parent 1db24c5ceb
commit 1f4c828b30
12 changed files with 388 additions and 38 deletions

16
src/app/api/layout.tsx Normal file
View File

@@ -0,0 +1,16 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}

9
src/app/api/patients.js Normal file
View File

@@ -0,0 +1,9 @@
import User from '../../models/User';
import { connectDB } from '../../lib/utils';
export default async (req, res) => {
await connectDB();
const patients = await User.find({ role: 'patient' });
res.json(patients);
};

View File

@@ -0,0 +1,21 @@
import User from '../../../models/User';
import connectDB from '../../../lib/utils';
export default async (req, res) => {
await connectDB();
const { email } = req.query;
const { medications, medicalConditions } = req.body;
const patient = await User.findOne({ email });
if (!patient) {
return res.status(404).json({ message: 'Patient not found' });
}
patient.medications = medications;
patient.medicalConditions = medicalConditions;
await patient.save();
res.json(patient);
};

29
src/app/api/user.js Normal file
View File

@@ -0,0 +1,29 @@
import { getAuth } from '@clerk/nextjs/server';
import User from '../../models/User';
import { connectDB } from '../../lib/utils';
export default async (req, res) => {
await connectDB();
const { userId } = getAuth(req);
if (!userId) {
return res.status(401).json({ message: 'Unauthorized' });
}
const user = await User.findOne({ id: userId });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
if (req.method === 'GET') {
res.json(user);
} else if (req.method === 'PUT') {
const { role } = req.body;
user.role = role;
await user.save();
res.json(user);
} else {
res.status(405).json({ message: 'Method not allowed' });
}
};

View File

@@ -1,42 +1,10 @@
import { User } from '../../../models/User';
import { NextResponse } from 'next/server';
import { Webhook } from 'svix';
import mongoose from "mongoose";
import { connectDB } from '../../../lib/utils';
const CLERK_WEBHOOK_SECRET = process.env.CLERK_WEBHOOK_SECRET;
const DATABASE_URL = process.env.MONGO_URI;
let cached = global.mongoose;
if (!cached) {
cached = global.mongoose = { conn: null, promise: null };
}
async function connectDB() {
if (cached.conn) {
return cached.conn;
}
if (!cached.promise) {
const opts = {
bufferCommands: false,
};
try {
cached.promise = mongoose.connect(DATABASE_URL, opts).then((mongoose) => {
console.log('MongoDB connected successfully');
return mongoose;
});
} catch (error) {
console.error('Error connecting to MongoDB:', error.message);
throw new Error('Error connecting to MongoDB');
}
}
cached.conn = await cached.promise;
return cached.conn;
}
export async function POST(req) {
console.log('Received request:', req);
@@ -106,6 +74,7 @@ export async function POST(req) {
}
user = new User({
id,
name,
email,
role: 'patient',