import { Request, Response, NextFunction } from 'express'; import licenseService from '../services/licenseService'; import licenseRepository from '../repositories/licenseRepository'; import { PAGINATION } from '../utils/constants'; export class LicenseController { async validate(req: Request, res: Response, next: NextFunction) { try { const { licenseKey } = req.body; const result = await licenseService.validate(licenseKey); res.json({ success: true, data: result }); } catch (error) { next(error); } } async getUserLicense(req: Request, res: Response, next: NextFunction) { try { const license = await licenseService.getUserLicense(req.user!.userId); res.json({ success: true, data: license }); } catch (error) { next(error); } } async generate(req: Request, res: Response, next: NextFunction) { try { const { userId, plan, durationDays } = req.body; const license = await licenseService.generateLicense(userId, plan, durationDays); res.status(201).json({ success: true, data: license }); } catch (error) { next(error); } } async revoke(req: Request, res: Response, next: NextFunction) { try { const { id } = req.params; const result = await licenseService.revokeLicense(id); res.json({ success: true, data: result }); } catch (error) { next(error); } } async listAll(req: Request, res: Response, next: NextFunction) { try { const page = parseInt(req.query.page as string) || PAGINATION.defaultPage; const limit = Math.min( parseInt(req.query.limit as string) || PAGINATION.defaultLimit, PAGINATION.maxLimit ); const result = await licenseRepository.findAll(page, limit); res.json({ success: true, data: result }); } catch (error) { next(error); } } } export default new LicenseController();