286 lines
11 KiB
JavaScript
286 lines
11 KiB
JavaScript
"use strict";
|
|
/**
|
|
* TicketController - REST API for post-sale tickets
|
|
*
|
|
* Endpoints para gestión de tickets de garantía.
|
|
*
|
|
* @module Quality
|
|
* @routes /api/tickets
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createTicketController = createTicketController;
|
|
const express_1 = require("express");
|
|
const ticket_service_1 = require("../services/ticket.service");
|
|
const post_sale_ticket_entity_1 = require("../entities/post-sale-ticket.entity");
|
|
const ticket_assignment_entity_1 = require("../entities/ticket-assignment.entity");
|
|
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
|
|
const auth_service_1 = require("../../auth/services/auth.service");
|
|
const user_entity_1 = require("../../core/entities/user.entity");
|
|
const tenant_entity_1 = require("../../core/entities/tenant.entity");
|
|
const refresh_token_entity_1 = require("../../auth/entities/refresh-token.entity");
|
|
function createTicketController(dataSource) {
|
|
const router = (0, express_1.Router)();
|
|
// Repositories
|
|
const ticketRepo = dataSource.getRepository(post_sale_ticket_entity_1.PostSaleTicket);
|
|
const assignmentRepo = dataSource.getRepository(ticket_assignment_entity_1.TicketAssignment);
|
|
const userRepository = dataSource.getRepository(user_entity_1.User);
|
|
const tenantRepository = dataSource.getRepository(tenant_entity_1.Tenant);
|
|
const refreshTokenRepository = dataSource.getRepository(refresh_token_entity_1.RefreshToken);
|
|
// Services
|
|
const service = new ticket_service_1.TicketService(ticketRepo, assignmentRepo);
|
|
const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
|
|
const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
|
|
// Helper for service context
|
|
const getContext = (req) => {
|
|
if (!req.tenantId) {
|
|
throw new Error('Tenant ID is required');
|
|
}
|
|
return {
|
|
tenantId: req.tenantId,
|
|
userId: req.user?.sub,
|
|
};
|
|
};
|
|
/**
|
|
* GET /api/tickets
|
|
* List tickets with filters
|
|
*/
|
|
router.get('/', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const filters = {};
|
|
if (req.query.loteId)
|
|
filters.loteId = req.query.loteId;
|
|
if (req.query.derechohabienteId)
|
|
filters.derechohabienteId = req.query.derechohabienteId;
|
|
if (req.query.category)
|
|
filters.category = req.query.category;
|
|
if (req.query.priority)
|
|
filters.priority = req.query.priority;
|
|
if (req.query.status)
|
|
filters.status = req.query.status;
|
|
if (req.query.slaBreached)
|
|
filters.slaBreached = req.query.slaBreached === 'true';
|
|
if (req.query.assignedTo)
|
|
filters.assignedTo = req.query.assignedTo;
|
|
if (req.query.dateFrom)
|
|
filters.dateFrom = new Date(req.query.dateFrom);
|
|
if (req.query.dateTo)
|
|
filters.dateTo = new Date(req.query.dateTo);
|
|
const page = parseInt(req.query.page) || 1;
|
|
const limit = Math.min(parseInt(req.query.limit) || 20, 100);
|
|
const result = await service.findWithFilters(getContext(req), filters, page, limit);
|
|
res.status(200).json({ success: true, data: result.data, pagination: result.meta });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* GET /api/tickets/sla-stats
|
|
* Get SLA statistics
|
|
*/
|
|
router.get('/sla-stats', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const stats = await service.getSlaStats(getContext(req));
|
|
res.status(200).json({ success: true, data: stats });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* GET /api/tickets/:id
|
|
* Get ticket with details
|
|
*/
|
|
router.get('/:id', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.findWithDetails(getContext(req), req.params.id);
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets
|
|
* Create new ticket
|
|
*/
|
|
router.post('/', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.create(getContext(req), {
|
|
loteId: req.body.loteId,
|
|
derechohabienteId: req.body.derechohabienteId,
|
|
category: req.body.category,
|
|
title: req.body.title,
|
|
description: req.body.description,
|
|
photoUrl: req.body.photoUrl,
|
|
contactName: req.body.contactName,
|
|
contactPhone: req.body.contactPhone,
|
|
});
|
|
res.status(201).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/:id/assign
|
|
* Assign ticket to technician
|
|
*/
|
|
router.post('/:id/assign', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.assign(getContext(req), req.params.id, {
|
|
technicianId: req.body.technicianId,
|
|
scheduledDate: req.body.scheduledDate ? new Date(req.body.scheduledDate) : undefined,
|
|
scheduledTime: req.body.scheduledTime,
|
|
});
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/:id/start
|
|
* Start work on ticket
|
|
*/
|
|
router.post('/:id/start', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa', 'technician'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.startWork(getContext(req), req.params.id);
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/:id/resolve
|
|
* Resolve ticket
|
|
*/
|
|
router.post('/:id/resolve', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa', 'technician'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.resolve(getContext(req), req.params.id, {
|
|
resolutionNotes: req.body.resolutionNotes,
|
|
resolutionPhotoUrl: req.body.resolutionPhotoUrl,
|
|
});
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/:id/close
|
|
* Close ticket with satisfaction rating
|
|
*/
|
|
router.post('/:id/close', authMiddleware.authenticate, async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.close(getContext(req), req.params.id, req.body.rating, req.body.comment);
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/:id/cancel
|
|
* Cancel ticket
|
|
*/
|
|
router.post('/:id/cancel', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const ticket = await service.cancel(getContext(req), req.params.id, req.body.reason);
|
|
if (!ticket) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
|
|
return;
|
|
}
|
|
res.status(200).json({ success: true, data: ticket });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
/**
|
|
* POST /api/tickets/check-sla
|
|
* Check and update SLA breaches
|
|
*/
|
|
router.post('/check-sla', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality'), async (req, res, next) => {
|
|
try {
|
|
const tenantId = req.tenantId;
|
|
if (!tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
const breached = await service.checkSlaBreaches(getContext(req));
|
|
res.status(200).json({ success: true, data: { breachedTickets: breached } });
|
|
}
|
|
catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
return router;
|
|
}
|
|
//# sourceMappingURL=ticket.controller.js.map
|