updated orders route
This commit is contained in:
parent
c1c840bf7e
commit
f198267ac9
1 changed files with 24 additions and 1 deletions
|
@ -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}) )
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue