-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
211 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import mongoose from "mongoose"; | ||
import dbConnect from "@/lib/dbConnect"; | ||
import { headers } from "next/headers"; | ||
import { extractBearerFromHeaders, validateToken } from "@/lib/auth"; | ||
import { Order } from "@/model/order"; | ||
import { NextResponse } from "next/server"; | ||
|
||
/** | ||
* Retrieve whether order is paid or not | ||
* @param req | ||
* @param params | ||
* @constructor | ||
*/ | ||
export async function GET(req: Request, { params }: { params: { id: string } }) { | ||
await dbConnect(); | ||
|
||
// Get the ID from the URL | ||
const id = params.id | ||
|
||
// Check if the ID is valid and ObjectId | ||
if (!id || !mongoose.isValidObjectId(id)) { | ||
return NextResponse.json({ | ||
message: 'The ID is not valid.' | ||
}, { status: 400 }) | ||
} | ||
|
||
// Find the order by ID | ||
const order = await Order.findById(id); | ||
if (!order) { | ||
return NextResponse.json({ | ||
message: 'The Order was not found.' | ||
}, { status: 404 }) | ||
} | ||
|
||
return Response.json({ | ||
isPaid: order.isPaid | ||
}) | ||
} | ||
|
||
/** | ||
* Set order as paid | ||
* @param req | ||
* @param params | ||
* @constructor | ||
*/ | ||
export async function PUT(req: Request, { params }: { params: { id: string } }) { | ||
await dbConnect(); | ||
|
||
// Authenticate the user | ||
const headersList = headers() | ||
if (!await validateToken(extractBearerFromHeaders(headersList))) { | ||
return new Response('Unauthorized', { status: 401 }); | ||
} | ||
|
||
// Get the order details from the request body | ||
const id = params.id | ||
const { isPaid } = await req.json() | ||
|
||
// Check if the ID is valid and ObjectId | ||
if (!id || !mongoose.isValidObjectId(id)) { | ||
return NextResponse.json({ | ||
message: 'The ID is not valid.' | ||
}, { status: 400 }) | ||
} | ||
|
||
|
||
try { | ||
// Find the order by ID | ||
const foundOrder = await Order.findById(id); | ||
|
||
if (!foundOrder) { | ||
return NextResponse.json({ | ||
message: 'The Order was not found.' | ||
}, { status: 404 }) | ||
} | ||
|
||
// Update the order status | ||
foundOrder.isPaid = isPaid | ||
|
||
// Save the updated order | ||
await foundOrder.save(); | ||
|
||
// console.log('Order updated:', foundOrder) | ||
return Response.json(foundOrder); | ||
} catch (error) { | ||
console.error(`Error setting order as paid`) | ||
return new Response(`Error setting order as paid`, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.