updated orders route

This commit is contained in:
Juthatip McDevitt 2024-05-26 09:49:28 -05:00
parent c1c840bf7e
commit f198267ac9

View file

@ -1,9 +1,32 @@
import mongoose from "mongoose";
import { getServerSession } from "next-auth"
import { authOptions } from "../auth/[...nextauth]/route"
import { UserInfo } from "../models/UserInfo"
import { Order } from "../models/Order"
export async function GET(){
export async function GET(req){
mongoose.connect(process.env.MONGO_URL);
const session = await getServerSession(authOptions);
const userEmail = session?.user?.email;
const url = new URL(req.url);
const _id = url.searchParams.get('_id')
if(_id){
return Response.json( await Order.findById(_id));
}
let isAdmin = false;
if(userEmail){
const userInfo = await UserInfo.findOne({email:userEmail});
if(userInfo){
const isAdmin = userInfo.admin;
}
}
if(isAdmin){
return Response.json( await Order.find() )
}
if(userEmail){
return Response.json( await Order.find({userEmail}) )
}
}