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
20 changes: 19 additions & 1 deletion src/modules/admin/stores/admin-stores.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { approveStoreService, getPendingStoresService } from "./admin-stores.service.js";
import { approveStoreService, getPendingStoresService, rejectStoreService } from "./admin-stores.service.js";

export const approveStore = async (req, res, next) => {
try {
Expand All @@ -25,3 +25,21 @@ export const getPendingStores = async (req, res, next) => {
next(error);
}
};

export const rejectStore = async (req, res, next) => {
try {
const { id } = req.params;
const { reason } = req.body;
const store = await rejectStoreService(id, reason);
return res.status(200).json({
success: true,
message: "Comercio rechazado exitosamente",
data: store
});
} catch (error) {
if (error.status) {
return res.status(error.status).json({ message: error.message });
}
next(error);
}
};
5 changes: 4 additions & 1 deletion src/modules/admin/stores/admin-stores.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Router } from "express";
import authenticate from "../../../config/jwt.config.js";
import { requireRole } from "../../../middlewares/auth.middleware.js";
import { parsePagination } from "../../../middlewares/pagination.middleware.js";
import { approveStore, getPendingStores } from "./admin-stores.controller.js";
import { approveStore, getPendingStores, rejectStore } from "./admin-stores.controller.js";
import { ROLES } from "../../../utils/contants/roles.constant.js";

const router = Router();
Expand All @@ -13,4 +13,7 @@ router.get("/pending", authenticate, requireRole(ROLES.ADMIN), parsePagination,
// PATCH /api/admin/stores/:id/approve — aprobar un comercio
router.patch("/:id/approve", authenticate, requireRole(ROLES.ADMIN), approveStore);

// PATCH /api/admin/stores/:id/reject — rechaza un comercio
router.patch("/:id/reject", authenticate, requireRole(ROLES.ADMIN), rejectStore);

export { router as adminStoresRoutes };
48 changes: 48 additions & 0 deletions src/modules/admin/stores/admin-stores.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,51 @@ export const getPendingStoresService = async (pagination) => {
}
};
};

/**
* Rechaza un comercio cambiando su store_status de INACTIVE a SUSPENDED.
* También envía una notificación al usuario con el motivo del rechazo.
*/
export const rejectStoreService = async (storeId, reason) => {
const id = Number(storeId);
if (!Number.isInteger(id) || id <= 0) {
throw { status: 400, message: "ID de comercio inválido" };
}
if (!reason || typeof reason !== "string" || !reason.trim()) {
throw { status: 400, message: "El motivo de rechazo es requerido" };
}

const store = await prisma.stores.findUnique({
where: { id_store: id },
select: { id_store: true, name: true, store_status: true, status: true, fk_user: true }
});

if (!store || !store.status) {
throw { status: 404, message: "Comercio no encontrado" };
}

if (store.store_status !== "INACTIVE") {
throw { status: 400, message: "El comercio no está pendiente de aprobación" };
}

await prisma.$transaction([
prisma.stores.update({
where: { id_store: id },
data: { store_status: "SUSPENDED" }
}),
prisma.notifications.create({
data: {
fk_user: store.fk_user,
title: "Solicitud de comercio rechazada",
message: `Tu solicitud para registrar el comercio "${store.name}" ha sido rechazada. Motivo: ${reason}`,
}
})
]);

const updated = await prisma.stores.findUnique({
where: { id_store: id },
select: { id_store: true, name: true, store_status: true, status: true }
});

return updated;
};
1 change: 1 addition & 0 deletions src/modules/commerce/commerces/store.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const STORE_RESPONSE_SELECT = {
instagram_url: true,
tiktok_url: true,
status: true,
store_status: true,
created_at: true,
updated_at: true,
user: {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/report/product-report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe("updateProductReportService", () => {

expect(prisma.productReports.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { id_product_report: 1 },
where: expect.objectContaining({ id_product_report: 1 }),
data: expect.objectContaining({ report_status: "IN_PROGRESS" }),
})
);
Expand Down
Loading