Repository navigation
Add getCategoryById function for ADMIN role #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| export const adminCategorySchemas = { | ||
| AdminCategoryResponse: { | ||
| type: "object", | ||
| properties: { | ||
| id: { type: "integer", example: 1 }, | ||
| name: { type: "string", example: "Electrónica" }, | ||
| status: { type: "boolean", example: true }, | ||
| productCount: { type: "integer", example: 42 }, | ||
| createdAt: { type: "string", format: "date-time" }, | ||
| updatedAt: { type: "string", format: "date-time" } | ||
| } | ||
| } | ||
| }; |
This file contains hidden or 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
Empty file.
Empty file.
This file contains hidden or 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 hidden or 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,12 @@ | ||
| import { validateId } from "../../../lib/validators.js"; | ||
| import { getAdminProductCategoryService } from "./admin-category.service.js"; | ||
|
|
||
| export const getAdminProductCategory = async (req, res, next) => { | ||
| try { | ||
| const id = validateId(req.params.id); | ||
| const result = await getAdminProductCategoryService(id); | ||
| return res.status(200).json(result); | ||
| } catch (error) { | ||
| next(error); | ||
| } | ||
| }; |
This file contains hidden or 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,41 @@ | ||
| import { Router } from "express"; | ||
| import authenticate from "../../../config/jwt.config.js"; | ||
| import { requireRole } from "../../../middlewares/auth.middleware.js"; | ||
| import { getAdminProductCategory } from "./admin-category.controller.js"; | ||
| import { ROLES } from "../../../utils/contants/roles.constant.js"; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| /** | ||
| * @swagger | ||
| * /api/admin/categories/{id}: | ||
| * get: | ||
| * summary: Ver categoría de productos (Admin) | ||
| * tags: [Admin] | ||
| * security: | ||
| * - cookieAuth: [] | ||
| * parameters: | ||
| * - in: path | ||
| * name: id | ||
| * required: true | ||
| * schema: | ||
| * type: integer | ||
| * responses: | ||
| * 200: | ||
| * description: Categoría encontrada | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * $ref: '#/components/schemas/AdminCategoryResponse' | ||
| * 400: | ||
| * description: ID inválido | ||
| * 401: | ||
| * description: No autenticado | ||
| * 403: | ||
| * description: Sin permisos de administrador | ||
| * 404: | ||
| * description: Categoría no encontrada | ||
| */ | ||
| router.get("/:id", authenticate, requireRole(ROLES.ADMIN), getAdminProductCategory); | ||
|
|
||
| export { router as adminCategoryRoutes }; | ||
This file contains hidden or 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,27 @@ | ||
| import { prisma } from "../../../lib/prisma.js"; | ||
| import { NotFoundError } from "../../../lib/errors.js"; | ||
|
|
||
| export const getAdminProductCategoryService = async (id) => { | ||
| const category = await prisma.productCategories.findUnique({ | ||
| where: { id_product_category: id }, | ||
| select: { | ||
| id_product_category: true, | ||
| name: true, | ||
| status: true, | ||
| created_at: true, | ||
| updated_at: true, | ||
| _count: { select: { products: true } } | ||
| } | ||
| }); | ||
|
|
||
| if (!category) throw new NotFoundError("Categoría no encontrada"); | ||
|
|
||
| return { | ||
| id: category.id_product_category, | ||
| name: category.name, | ||
| status: category.status, | ||
| productCount: category._count.products, | ||
| createdAt: category.created_at, | ||
| updatedAt: category.updated_at | ||
| }; | ||
| }; |
This file contains hidden or 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,2 @@ | ||
| export * from "./categories/admin-category.routes.js"; | ||
| export * from "./users/admin-users.routes.js"; |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,3 @@ | ||
| export * from "./product-image.routes.js"; | ||
| export * from "./user-image.routes.js"; | ||
| export * from "./store-image.routes.js"; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,143 @@ | ||
| import { vi, describe, it, expect, beforeEach } from "vitest"; | ||
| import request from "supertest"; | ||
| import app from "../../../src/app.js"; | ||
| import { prisma } from "../../../src/lib/prisma.js"; | ||
|
|
||
| // ─── MOCKS ──────────────────────────────────────────────────────── | ||
| vi.mock("../../../src/lib/prisma.js", () => ({ | ||
| prisma: { | ||
| productCategories: { findUnique: vi.fn() } | ||
| } | ||
| })); | ||
|
|
||
| vi.mock("../../../src/config/jwt.config.js", () => ({ | ||
| default: (req, res, next) => { | ||
| const role = req.headers["x-test-role"]; | ||
|
|
||
| if (!role) { | ||
| return res.status(401).json({ errors: { auth: { message: "No autenticado" } } }); | ||
| } | ||
|
|
||
| const users = { | ||
| admin: { id_user: 1, role: "ADMIN" }, | ||
| seller: { id_user: 10, role: "SELLER" }, | ||
| customer: { id_user: 2, role: "CUSTOMER" }, | ||
| }; | ||
|
|
||
| req.user = users[role] ?? null; | ||
|
|
||
| if (!req.user) { | ||
| return res.status(401).json({ errors: { auth: { message: "No autenticado" } } }); | ||
| } | ||
|
|
||
| next(); | ||
| } | ||
| })); | ||
|
|
||
| // ─── HELPERS ────────────────────────────────────────────────────── | ||
| const asRole = (req, role) => req.set("x-test-role", role); | ||
|
|
||
| const mockCategory = { | ||
| id_product_category: 1, | ||
| name: "Electrónica", | ||
| status: true, | ||
| created_at: new Date().toISOString(), | ||
| updated_at: new Date().toISOString(), | ||
| _count: { products: 5 } | ||
| }; | ||
|
|
||
| // ─── GET /api/admin/categories/:id ──────────────────────────────── | ||
| describe("GET /api/admin/categories/:id", () => { | ||
| beforeEach(() => vi.resetAllMocks()); | ||
|
|
||
| it("devuelve 401 cuando no hay token", async () => { | ||
| const res = await request(app).get("/api/admin/categories/1"); | ||
|
|
||
| expect(res.status).toBe(401); | ||
| }); | ||
|
|
||
| it("devuelve 403 cuando el usuario no es ADMIN", async () => { | ||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/1"), | ||
| "seller" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(403); | ||
| }); | ||
|
|
||
| it("devuelve 403 cuando el usuario es CUSTOMER", async () => { | ||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/1"), | ||
| "customer" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(403); | ||
| }); | ||
|
|
||
| it("devuelve 400 cuando el id no es un entero válido", async () => { | ||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/abc"), | ||
| "admin" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| }); | ||
|
|
||
| it("devuelve 404 cuando la categoría no existe", async () => { | ||
| prisma.productCategories.findUnique.mockResolvedValue(null); | ||
|
|
||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/999"), | ||
| "admin" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(404); | ||
| }); | ||
|
|
||
| it("devuelve 200 con la categoría y productCount", async () => { | ||
| prisma.productCategories.findUnique.mockResolvedValue(mockCategory); | ||
|
|
||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/1"), | ||
| "admin" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body).toMatchObject({ | ||
| id: 1, | ||
| name: "Electrónica", | ||
| status: true, | ||
| productCount: 5 | ||
| }); | ||
| }); | ||
|
|
||
| it("devuelve la categoría aunque status sea false (admin ve todo)", async () => { | ||
| prisma.productCategories.findUnique.mockResolvedValue({ | ||
| ...mockCategory, | ||
| status: false | ||
| }); | ||
|
|
||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/1"), | ||
| "admin" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body.status).toBe(false); | ||
| }); | ||
|
|
||
| it("retorna productCount en 0 cuando la categoría no tiene productos", async () => { | ||
| prisma.productCategories.findUnique.mockResolvedValue({ | ||
| ...mockCategory, | ||
| _count: { products: 0 } | ||
| }); | ||
|
|
||
| const res = await asRole( | ||
| request(app).get("/api/admin/categories/1"), | ||
| "admin" | ||
| ); | ||
|
|
||
| expect(res.status).toBe(200); | ||
| expect(res.body.productCount).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.