mirror of
https://github.com/andwati/order-service.git
synced 2026-10-18 02:12:53 +00:00
added get users admin only endpoint
This commit is contained in:
@@ -7,6 +7,7 @@ import { errorHandler } from "./middleware/error.middleware.js";
|
||||
import authRoutes from "./routes/auth.routes.js";
|
||||
import productRoutes from "./routes/product.routes.js";
|
||||
import orderRoutes from "./routes/order.routes.js";
|
||||
import userRoutes from "./routes/user.routes.js";
|
||||
import docsRoutes from "./routes/docs.routes.js";
|
||||
|
||||
const app: Application = express();
|
||||
@@ -25,6 +26,7 @@ app.get("/health", (req, res) => {
|
||||
app.use("/auth", authRoutes);
|
||||
app.use("/products", productRoutes);
|
||||
app.use("/orders", orderRoutes);
|
||||
app.use("/users", userRoutes);
|
||||
// swagger ui
|
||||
app.use("/docs", docsRoutes);
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
import { UserService } from "../services/user.service.js";
|
||||
|
||||
export class UserController {
|
||||
static async getAll(_req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const users = await UserService.getAll();
|
||||
res.status(200).json(users);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,29 @@
|
||||
import type { ZodSchema } from "zod";
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
|
||||
interface ParsedRequest {
|
||||
body?: Record<string, unknown>;
|
||||
params?: Record<string, string>;
|
||||
query?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const validate =
|
||||
(schema: ZodSchema) => (req: Request, _res: Response, next: NextFunction) => {
|
||||
try {
|
||||
schema.parse({
|
||||
const parsed = schema.parse({
|
||||
body: req.body,
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
});
|
||||
}) as ParsedRequest;
|
||||
|
||||
// Update request with parsed values (including defaults)
|
||||
if (parsed.body) req.body = parsed.body;
|
||||
if (parsed.params) req.params = parsed.params;
|
||||
if (parsed.query) req.query = parsed.query as Record<string, string>;
|
||||
|
||||
next();
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ import { UserModel } from "../models/user.model.js";
|
||||
import type { UserDocument } from "../models/user.model.js";
|
||||
|
||||
export class UserRepository {
|
||||
static findAll(): Promise<UserDocument[]> {
|
||||
return UserModel.find().select("-passwordHash");
|
||||
}
|
||||
|
||||
static findByEmail(email: string): Promise<UserDocument | null> {
|
||||
return UserModel.findOne({ email });
|
||||
}
|
||||
|
||||
@@ -13,22 +13,52 @@ const router: Router = Router();
|
||||
* tags:
|
||||
* - Auth
|
||||
* summary: Register a new user
|
||||
* security: []
|
||||
* requestBody:
|
||||
* required: true
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* required:
|
||||
* - email
|
||||
* - password
|
||||
* properties:
|
||||
* email:
|
||||
* type: string
|
||||
* format: email
|
||||
* description: User's email address
|
||||
* password:
|
||||
* type: string
|
||||
* minLength: 8
|
||||
* description: User's password (minimum 8 characters)
|
||||
* role:
|
||||
* type: string
|
||||
* enum: [customer, admin]
|
||||
* default: customer
|
||||
* description: Account type (defaults to customer)
|
||||
* responses:
|
||||
* 201:
|
||||
* description: User created
|
||||
* description: User created successfully
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The user ID
|
||||
* email:
|
||||
* type: string
|
||||
* description: The user's email
|
||||
* role:
|
||||
* type: string
|
||||
* enum: [customer, admin]
|
||||
* description: The user's role
|
||||
* 400:
|
||||
* description: Invalid input
|
||||
* 409:
|
||||
* description: Email already registered
|
||||
*/
|
||||
router.post(
|
||||
"/register",
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { Router } from "express";
|
||||
import { UserController } from "../controllers/user.controller.js";
|
||||
import { authMiddleware } from "../middleware/auth.middleware.js";
|
||||
import { requireRole } from "../middleware/role.middleware.js";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
/**
|
||||
* @openapi
|
||||
* /users:
|
||||
* get:
|
||||
* tags:
|
||||
* - Users
|
||||
* summary: Get all users (Admin only)
|
||||
* description: Retrieve a list of all registered users. This endpoint is restricted to administrators only.
|
||||
* security:
|
||||
* - BearerAuth: []
|
||||
* responses:
|
||||
* 200:
|
||||
* description: List of users retrieved successfully
|
||||
* content:
|
||||
* application/json:
|
||||
* schema:
|
||||
* type: array
|
||||
* items:
|
||||
* type: object
|
||||
* properties:
|
||||
* id:
|
||||
* type: string
|
||||
* description: The user ID
|
||||
* email:
|
||||
* type: string
|
||||
* description: The user's email
|
||||
* role:
|
||||
* type: string
|
||||
* enum: [customer, admin]
|
||||
* description: The user's role
|
||||
* createdAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* description: Account creation date
|
||||
* updatedAt:
|
||||
* type: string
|
||||
* format: date-time
|
||||
* description: Last update date
|
||||
* 401:
|
||||
* description: Unauthorized - Missing or invalid token
|
||||
* 403:
|
||||
* description: Forbidden - Admin access required
|
||||
*/
|
||||
router.get("/", authMiddleware, requireRole("admin"), UserController.getAll);
|
||||
|
||||
export default router;
|
||||
@@ -0,0 +1,14 @@
|
||||
import { UserRepository } from "../repositories/user.repo.js";
|
||||
|
||||
export class UserService {
|
||||
static async getAll() {
|
||||
const users = await UserRepository.findAll();
|
||||
return users.map((user) => ({
|
||||
id: user._id,
|
||||
email: user.email,
|
||||
role: user.role,
|
||||
createdAt: user.createdAt,
|
||||
updatedAt: user.updatedAt,
|
||||
}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user