mirror of
https://github.com/andwati/order-service.git
synced 2026-11-02 17:22:15 +00:00
fixed 403 errror on order creation
This commit is contained in:
+2
-1
@@ -10,7 +10,8 @@
|
||||
"lint": "eslint . --ext .ts",
|
||||
"lint:fix": "eslint . --ext .ts --fix",
|
||||
"prettier": "prettier --check .",
|
||||
"prettier:fix": "prettier --write ."
|
||||
"prettier:fix": "prettier --write .",
|
||||
"seed": "tsc && node dist/scripts/seed.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
|
||||
export function requireRole(role: "admin" | "customer") {
|
||||
export function requireRole(roles: "admin" | "customer" | ("admin" | "customer")[]) {
|
||||
const allowedRoles = Array.isArray(roles) ? roles : [roles];
|
||||
|
||||
return (req: Request, _res: Response, next: NextFunction) => {
|
||||
if (!req.user) {
|
||||
return next({
|
||||
@@ -9,7 +11,7 @@ export function requireRole(role: "admin" | "customer") {
|
||||
});
|
||||
}
|
||||
|
||||
if (req.user.role !== role) {
|
||||
if (!allowedRoles.includes(req.user.role as "admin" | "customer")) {
|
||||
return next({
|
||||
statusCode: 403,
|
||||
message: "Forbidden",
|
||||
|
||||
@@ -44,7 +44,7 @@ const router: Router = Router();
|
||||
router.post(
|
||||
"/",
|
||||
authMiddleware,
|
||||
requireRole("customer"),
|
||||
requireRole(["customer", "admin"]),
|
||||
validate(createOrderSchema),
|
||||
OrderController.create,
|
||||
);
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { connectDB } from "../config/db.js";
|
||||
import { ProductModel } from "../models/product.model.js";
|
||||
import mongoose from "mongoose";
|
||||
import { logger } from "../utils/logger.js";
|
||||
|
||||
const PRODUCTS = [
|
||||
{
|
||||
name: "Wireless Mouse",
|
||||
price: 2500, // $25.00
|
||||
stock: 100,
|
||||
},
|
||||
{
|
||||
name: "Mechanical Keyboard",
|
||||
price: 12000, // $120.00
|
||||
stock: 50,
|
||||
},
|
||||
{
|
||||
name: "USB-C Hub",
|
||||
price: 4500, // $45.00
|
||||
stock: 75,
|
||||
},
|
||||
{
|
||||
name: "Monitor Stand",
|
||||
price: 3500, // $35.00
|
||||
stock: 200,
|
||||
},
|
||||
{
|
||||
name: "Gaming Headset",
|
||||
price: 8000, // $80.00
|
||||
stock: 30,
|
||||
}
|
||||
];
|
||||
|
||||
async function seed() {
|
||||
try {
|
||||
logger.info("Connecting to database...");
|
||||
await connectDB();
|
||||
logger.info("Connected to database.");
|
||||
|
||||
logger.info("Clearing products...");
|
||||
await ProductModel.deleteMany({});
|
||||
logger.info("Products cleared.");
|
||||
|
||||
logger.info("Seeding products...");
|
||||
await ProductModel.insertMany(PRODUCTS);
|
||||
logger.info("Products seeded successfully.");
|
||||
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
logger.error("Error seeding database:", { error });
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await mongoose.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
seed();
|
||||
Reference in New Issue
Block a user