- Add ServiceContext interface to base.service.ts - Fix admin module: audit-log, backup, cost-center, system-setting - Define ServiceContext and PaginatedResult locally - Convert services from extends BaseService to standalone - Change PaginatedResult from meta format to flat format - Fix controllers to use flat pagination format - Fix bidding module: bid, bid-budget, opportunity - Change PaginatedResult from meta format to flat format - Fix controllers to use flat pagination format Modules with remaining errors: finance, payment-terminals, estimates, mcp, reports, progress, budgets, ai, hse (563 errors total) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
267 lines
9.4 KiB
TypeScript
267 lines
9.4 KiB
TypeScript
/**
|
|
* OpportunityController - Controller de Oportunidades
|
|
*
|
|
* Endpoints REST para gestión del pipeline de oportunidades.
|
|
*
|
|
* @module Bidding
|
|
*/
|
|
|
|
import { Router, Request, Response, NextFunction } from 'express';
|
|
import { DataSource } from 'typeorm';
|
|
import { OpportunityService, CreateOpportunityDto, UpdateOpportunityDto, OpportunityFilters } from '../services/opportunity.service';
|
|
import { AuthMiddleware } from '../../auth/middleware/auth.middleware';
|
|
import { AuthService } from '../../auth/services/auth.service';
|
|
import { Opportunity, OpportunityStatus } from '../entities/opportunity.entity';
|
|
import { User } from '../../core/entities/user.entity';
|
|
import { Tenant } from '../../core/entities/tenant.entity';
|
|
import { RefreshToken } from '../../auth/entities/refresh-token.entity';
|
|
import { ServiceContext } from '../../../shared/services/base.service';
|
|
|
|
export function createOpportunityController(dataSource: DataSource): Router {
|
|
const router = Router();
|
|
|
|
// Repositorios
|
|
const opportunityRepository = dataSource.getRepository(Opportunity);
|
|
const userRepository = dataSource.getRepository(User);
|
|
const tenantRepository = dataSource.getRepository(Tenant);
|
|
const refreshTokenRepository = dataSource.getRepository(RefreshToken);
|
|
|
|
// Servicios
|
|
const opportunityService = new OpportunityService(opportunityRepository);
|
|
const authService = new AuthService(userRepository, tenantRepository, refreshTokenRepository as any);
|
|
const authMiddleware = new AuthMiddleware(authService, dataSource);
|
|
|
|
// Helper para crear contexto
|
|
const getContext = (req: Request): ServiceContext => {
|
|
if (!req.tenantId) {
|
|
throw new Error('Tenant ID is required');
|
|
}
|
|
return {
|
|
tenantId: req.tenantId,
|
|
userId: req.user?.sub,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* GET /opportunities
|
|
*/
|
|
router.get('/', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const page = parseInt(req.query.page as string) || 1;
|
|
const limit = Math.min(parseInt(req.query.limit as string) || 20, 100);
|
|
|
|
const filters: OpportunityFilters = {};
|
|
if (req.query.status) {
|
|
const statuses = (req.query.status as string).split(',') as OpportunityStatus[];
|
|
filters.status = statuses.length === 1 ? statuses[0] : statuses;
|
|
}
|
|
if (req.query.source) filters.source = req.query.source as any;
|
|
if (req.query.projectType) filters.projectType = req.query.projectType as any;
|
|
if (req.query.priority) filters.priority = req.query.priority as any;
|
|
if (req.query.assignedToId) filters.assignedToId = req.query.assignedToId as string;
|
|
if (req.query.clientName) filters.clientName = req.query.clientName as string;
|
|
if (req.query.state) filters.state = req.query.state as string;
|
|
if (req.query.dateFrom) filters.dateFrom = new Date(req.query.dateFrom as string);
|
|
if (req.query.dateTo) filters.dateTo = new Date(req.query.dateTo as string);
|
|
if (req.query.minValue) filters.minValue = parseFloat(req.query.minValue as string);
|
|
if (req.query.maxValue) filters.maxValue = parseFloat(req.query.maxValue as string);
|
|
if (req.query.search) filters.search = req.query.search as string;
|
|
|
|
const result = await opportunityService.findWithFilters(getContext(req), filters, page, limit);
|
|
|
|
res.status(200).json({
|
|
success: true,
|
|
data: result.data,
|
|
pagination: { total: result.total, page: result.page, limit: result.limit, totalPages: result.totalPages },
|
|
});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /opportunities/pipeline
|
|
*/
|
|
router.get('/pipeline', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const pipeline = await opportunityService.getPipeline(getContext(req));
|
|
res.status(200).json({ success: true, data: pipeline });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /opportunities/upcoming-deadlines
|
|
*/
|
|
router.get('/upcoming-deadlines', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const days = parseInt(req.query.days as string) || 7;
|
|
const opportunities = await opportunityService.getUpcomingDeadlines(getContext(req), days);
|
|
res.status(200).json({ success: true, data: opportunities });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /opportunities/stats
|
|
*/
|
|
router.get('/stats', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const year = req.query.year ? parseInt(req.query.year as string) : undefined;
|
|
const stats = await opportunityService.getStats(getContext(req), year);
|
|
res.status(200).json({ success: true, data: stats });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /opportunities/:id
|
|
*/
|
|
router.get('/:id', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const opportunity = await opportunityService.findById(getContext(req), req.params.id);
|
|
if (!opportunity) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
|
|
return;
|
|
}
|
|
|
|
res.status(200).json({ success: true, data: opportunity });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /opportunities
|
|
*/
|
|
router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const dto: CreateOpportunityDto = req.body;
|
|
if (!dto.code || !dto.name || !dto.source || !dto.projectType || !dto.clientName || !dto.identificationDate) {
|
|
res.status(400).json({
|
|
error: 'Bad Request',
|
|
message: 'code, name, source, projectType, clientName, and identificationDate are required',
|
|
});
|
|
return;
|
|
}
|
|
|
|
const opportunity = await opportunityService.create(getContext(req), dto);
|
|
res.status(201).json({ success: true, data: opportunity });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* PUT /opportunities/:id
|
|
*/
|
|
router.put('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const dto: UpdateOpportunityDto = req.body;
|
|
const opportunity = await opportunityService.update(getContext(req), req.params.id, dto);
|
|
|
|
if (!opportunity) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
|
|
return;
|
|
}
|
|
|
|
res.status(200).json({ success: true, data: opportunity });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* POST /opportunities/:id/status
|
|
*/
|
|
router.post('/:id/status', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const { status, reason } = req.body;
|
|
if (!status) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'status is required' });
|
|
return;
|
|
}
|
|
|
|
const opportunity = await opportunityService.changeStatus(getContext(req), req.params.id, status, reason);
|
|
|
|
if (!opportunity) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
|
|
return;
|
|
}
|
|
|
|
res.status(200).json({ success: true, data: opportunity });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* DELETE /opportunities/:id
|
|
*/
|
|
router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
|
|
try {
|
|
if (!req.tenantId) {
|
|
res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
|
|
return;
|
|
}
|
|
|
|
const deleted = await opportunityService.softDelete(getContext(req), req.params.id);
|
|
if (!deleted) {
|
|
res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
|
|
return;
|
|
}
|
|
|
|
res.status(200).json({ success: true, message: 'Opportunity deleted' });
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
}
|
|
|
|
export default createOpportunityController;
|