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
71 changes: 71 additions & 0 deletions src/docs/schemas/delivery.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,76 @@ export const deliverySchemas = {
}
}
}
},
DeliveryOrderHistoryResponse: {
type: "object",
properties: {
content: {
type: "array",
items: {
type: "object",
properties: {
id_delivery_assignment: { type: "integer", example: 1 },
assignment_status: {
type: "string",
enum: ["PENDING", "ACCEPTED", "REJECTED", "DELIVERED"],
example: "DELIVERED"
},
assignment_sequence: { type: "integer", example: 1 },
created_at: { type: "string", format: "date-time" },
order: {
type: "object",
properties: {
id_order: { type: "integer", example: 100 },
order_status: { type: "string", example: "DELIVERED" },
total: { type: "number", example: 150000 },
shipping_cost: { type: "number", example: 10000 },
created_at: { type: "string", format: "date-time" },
user: {
type: "object",
properties: {
id_user: { type: "integer", example: 1 },
name: { type: "string", example: "Juan Pérez" }
}
},
store: {
type: "object",
properties: {
id_store: { type: "integer", example: 1 },
name: { type: "string", example: "Mi Comercio" }
}
}
}
}
}
}
},
total_elements: { type: "integer", example: 50 },
total_pages: { type: "integer", example: 3 },
page: { type: "integer", example: 1 },
size: { type: "integer", example: 20 }
}
},
QueryValidationErrorResponse: {
type: "object",
properties: {
error: {
type: "object",
properties: {
code: { type: "integer", example: 400 },
message: { type: "string", example: "Datos inválidos" },
details: {
type: "array",
items: {
type: "object",
properties: {
field: { type: "string", example: "period" },
message: { type: "string", example: "Invalid enum value" }
}
}
}
}
}
}
}
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//delivery.assignments.controller.js
import { ZodError } from 'zod';
import { createAssignmentSchema, respondToAssignmentSchema } from './delivery-assignments.validation.js';
import { createAssignmentSchema, respondToAssignmentSchema, deliveryOrderHistoryQuerySchema } from './delivery-assignments.validation.js';
import {
createAssignmentService,
getAssignmentByIdService,
Expand All @@ -10,6 +10,7 @@ import {
getAcceptedAssignmentService,
completeAssignmentService,
respondToAssignmentService,
getDeliveryOrderHistoryService,
} from './delivery-assignments.service.js';

// Crear asignación
Expand Down Expand Up @@ -182,4 +183,27 @@ export const respondToAssignment = async (req, res) => {
error: { code: error.status || 500, message: error.message }
});
}
};

export const getDeliveryOrderHistory = async (req, res) => {
try {
const { id } = req.params;
const validFilters = deliveryOrderHistoryQuerySchema.parse(req.query);
const result = await getDeliveryOrderHistoryService(
id,
req.user.id_user,
validFilters,
req.pagination
);
return res.status(200).json(result);
} catch (error) {
if (error instanceof ZodError) {
return res.status(400).json({
error: { code: 400, message: "Datos inválidos", details: error.issues }
});
}
return res.status(error.status || 500).json({
error: { code: error.status || 500, message: error.message }
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
getAcceptedAssignment,
completeAssignment,
respondToAssignment,
getDeliveryOrderHistory,
} from "./delivery-assignments.controller.js";
import { parsePagination } from "../../../middlewares/pagination.middleware.js";

const router = Router();

Expand Down Expand Up @@ -96,4 +98,88 @@ router.post("/:id/complete", authenticate, requireRole(ROLES.DELIVERY), complete
*/
router.post("/orders/:orderId/delivery-response", authenticate, requireRole(ROLES.DELIVERY), respondToAssignment);

/**
* @swagger
* /api/assignments/{id}/orders:
* get:
* summary: Historial de pedidos del delivery
* description: Retorna el historial de pedidos asignados al delivery con filtros opcionales de período, estado, ID de pedido y nombre de usuario.
* tags: [Deliveries]
* security:
* - cookieAuth: []
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: integer
* description: ID del delivery
* - in: query
* name: period
* schema:
* type: string
* enum: [7d, 15d, 1m, all]
* description: Período de tiempo a filtrar
* - in: query
* name: assignment_status
* schema:
* type: string
* enum: [PENDING, ACCEPTED, REJECTED, DELIVERED]
* description: Estado de la asignación
* - in: query
* name: orderId
* schema:
* type: integer
* description: ID del pedido a buscar
* - in: query
* name: userName
* schema:
* type: string
* description: Nombre del usuario que realizó el pedido
* - in: query
* name: page
* schema:
* type: integer
* default: 1
* description: Página actual
* - in: query
* name: limit
* schema:
* type: integer
* default: 20
* description: Resultados por página (máximo 100)
* responses:
* 200:
* description: Historial de pedidos obtenido correctamente
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DeliveryOrderHistoryResponse'
* 400:
* description: Parámetros inválidos
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/QueryValidationErrorResponse'
* 401:
* description: No autenticado
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/AuthErrorResponse'
* 403:
* description: No tiene permisos para ver este historial
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DeliveryResponseForbidden'
* 404:
* description: Delivery no encontrado
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DeliveryStatusNotFoundResponse'
*/
router.get("/:id/orders", authenticate, requireRole(ROLES.DELIVERY), parsePagination, getDeliveryOrderHistory);

export default router;
Loading
Loading