Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Categories" ADD COLUMN "icon" VARCHAR(500);
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ model Products {
model Categories {
id_category Int @id @default(autoincrement())
name String @db.VarChar(100)
icon String? @db.VarChar(500)
status Boolean @default(true)
visible Boolean @default(true)
created_at DateTime @default(now()) @db.Timestamptz
Expand Down
2 changes: 1 addition & 1 deletion src/modules/admin/categories/admin-category.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PAGINATION } from "../../../constants/pagination.constant.js";

export const createAdminProductCategory = async (req, res, next) => {
try {
const result = await createAdminProductCategoryService(req.body?.name);
const result = await createAdminProductCategoryService(req.body?.name, req.body?.icon);
return res.status(201).json(result);
} catch (error) {
next(error);
Expand Down
18 changes: 17 additions & 1 deletion src/modules/admin/categories/admin-category.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
return normalizedName;
};

export const createAdminProductCategoryService = async (name) => {
export const createAdminProductCategoryService = async (name, icon = null) => {
const normalizedName = normalizeAdminCategoryName(name);

const existingCategory = await prisma.categories.findFirst({
Expand All @@ -49,12 +49,14 @@
const createdCategory = await prisma.categories.create({
data: {
name: normalizedName,
icon: icon ?? null,
visible: true,
status: true
},
select: {
id_category: true,
name: true,
icon: true,
visible: true,
status: true,
created_at: true
Expand All @@ -64,6 +66,7 @@
return {
id: createdCategory.id_category,
name: createdCategory.name,
icon: createdCategory.icon,
visible: createdCategory.visible,
status: createdCategory.status,
createdAt: createdCategory.created_at
Expand All @@ -76,6 +79,7 @@
select: {
id_category: true,
name: true,
icon: true,
status: true,
visible: true,
created_at: true,
Expand All @@ -89,6 +93,7 @@
return {
id: category.id_category,
name: category.name,
icon: category.icon,
status: category.status,
visible: category.visible,
productCount: category._count.product_categories,
Expand Down Expand Up @@ -124,6 +129,7 @@
select: {
id_category: true,
name: true,
icon: true,
status: true,
visible: true,
_count: { select: { product_categories: true } }
Expand All @@ -135,6 +141,7 @@
data: categories.map((c) => ({
id: c.id_category,
name: c.name,
icon: c.icon,
status: c.status,
visible: c.visible,
productCount: c._count.product_categories
Expand Down Expand Up @@ -310,7 +317,7 @@
const isPlainObject = (value) =>
value !== null && typeof value === "object" && !Array.isArray(value);

const normalizeUpdatePayload = (payload) => {

Check failure on line 320 in src/modules/admin/categories/admin-category.service.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=CrisNAC_BackendMarketplace&issues=AZ44SQQ-55rYU4SfFZzJ&open=AZ44SQQ-55rYU4SfFZzJ&pullRequest=157
if (!isPlainObject(payload)) {
throw new ValidationError("Body inválido");
}
Expand Down Expand Up @@ -339,6 +346,13 @@
}
data.visible = payload.visible;
}

if (payload.icon !== undefined) {
if (payload.icon !== null && typeof payload.icon !== "string") {
throw new ValidationError("icon debe ser texto o null");
}
data.icon = payload.icon ?? null;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

if (Object.keys(data).length === 0) {
throw new ValidationError("Debe enviar al menos uno: name o visible");
Expand All @@ -365,6 +379,7 @@
select: {
id_category: true,
name: true,
icon: true,
visible: true,
created_at: true,
updated_at: true
Expand All @@ -374,6 +389,7 @@
return {
id: updated.id_category,
name: updated.name,
icon: updated.icon,
visible: updated.visible,
createdAt: updated.created_at,
updatedAt: updated.updated_at
Expand Down
Loading