Repository navigation
Endpoint para crear categoría como admin #116
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,62 @@ import { | |
| getOfferProductPrice | ||
| } from "../../../lib/product-pricing.js"; | ||
| import { PAGINATION } from "../../../utils/contants/pagination.constant.js"; | ||
|
|
||
|
|
||
| const normalizeAdminCategoryName = (name) => { | ||
| if (typeof name !== "string" || name.trim().length === 0) { | ||
| throw new ValidationError("El nombre de la categoría no puede estar vacío"); | ||
| } | ||
|
|
||
| const normalizedName = name.trim(); | ||
| if (normalizedName.length > 100) { | ||
| throw new ValidationError("El nombre de la categoría no puede superar 100 caracteres"); | ||
| } | ||
|
|
||
| return normalizedName; | ||
| }; | ||
|
|
||
| export const createAdminProductCategoryService = async (name) => { | ||
| const normalizedName = normalizeAdminCategoryName(name); | ||
|
|
||
| const existingCategory = await prisma.productCategories.findFirst({ | ||
| where: { | ||
| name: { | ||
| equals: normalizedName, | ||
| mode: "insensitive" | ||
| }, | ||
| status: true | ||
| }, | ||
| select: { id_product_category: true } | ||
| }); | ||
|
|
||
| if (existingCategory) { | ||
| throw new ValidationError("Ya existe una categoría con ese nombre"); | ||
| } | ||
|
|
||
| const createdCategory = await prisma.productCategories.create({ | ||
| data: { | ||
| name: normalizedName, | ||
| visible: true, | ||
| status: true | ||
| }, | ||
| select: { | ||
| id_product_category: true, | ||
| name: true, | ||
| visible: true, | ||
| status: true, | ||
| created_at: true | ||
| } | ||
| }); | ||
|
Comment on lines
+23
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Misma condición de carrera (TOCTOU) que en el servicio de commerce.
🤖 Prompt for AI Agents |
||
|
|
||
| return { | ||
| id: createdCategory.id_product_category, | ||
| name: createdCategory.name, | ||
| visible: createdCategory.visible, | ||
| status: createdCategory.status, | ||
| createdAt: createdCategory.created_at | ||
| }; | ||
| }; | ||
|
|
||
| export const getAdminProductCategoryService = async (id) => { | ||
| const category = await prisma.productCategories.findUnique({ | ||
| where: { id_product_category: id }, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Status HTTP inconsistente con el flujo de commerce para el mismo error.
En
createCategoryRequestService(commerce) el duplicado se lanza comoConflictError→ 409, acá lo lanzan comoValidationError→ 400 con el mismo significado semántico ("ya existe una categoría con ese nombre"). Los clientes reciben códigos diferentes para la misma clase de error. Sugiero usarConflictErrortambién acá para consistencia de API (y ajustar el test deadmin-category.test.jslínea 153 que espera 400).🔧 Cambio propuesto
📝 Committable suggestion
🤖 Prompt for AI Agents