import { logger } from '@goodgo/logger'; import { NotFoundError } from '../../errors/http-error'; import { featureRepository } from './feature.repository'; /** * EN: Service for managing features in the system * VI: Service để quản lý các features trong hệ thống */ export class FeatureService { /** * EN: Create a new feature * VI: Tạo một feature mới */ async create(data: { name: string; title?: string; description?: string; config?: Record; tags?: string[] }) { logger.info('Creating feature / Tạo feature', { data }); const feature = await featureRepository.create(data); logger.info('Feature created successfully / Feature đã được tạo thành công', { featureId: feature.id }); return feature; } /** * EN: Get all features * VI: Lấy tất cả features */ async findAll() { logger.info('Fetching all features / Lấy tất cả features'); const features = await featureRepository.findAll({ orderBy: { createdAt: 'desc' }, }); logger.info(`Retrieved ${features.length} features / Đã lấy ${features.length} features`); return features; } /** * EN: Get feature by ID * VI: Lấy feature theo ID */ async findById(id: string) { logger.info('Fetching feature by ID / Lấy feature theo ID', { id }); const feature = await featureRepository.findById(id); if (!feature) { logger.warn('Feature not found / Không tìm thấy feature', { id }); throw new NotFoundError('Feature', { id }); } logger.info('Feature retrieved successfully / Feature đã được lấy thành công', { id }); return feature; } /** * EN: Get feature by name * VI: Lấy feature theo tên */ async findByName(name: string) { logger.info('Fetching feature by name / Lấy feature theo tên', { name }); const feature = await featureRepository.findByName(name); if (!feature) { logger.warn('Feature not found / Không tìm thấy feature', { name }); return null; } logger.info('Feature retrieved successfully / Feature đã được lấy thành công', { name }); return feature; } /** * EN: Update feature * VI: Cập nhật feature */ async update(id: string, data: Partial<{ title?: string; description?: string; config?: Record; enabled?: boolean; tags?: string[] }>) { logger.info('Updating feature / Cập nhật feature', { id, data }); const feature = await featureRepository.update(id, data); logger.info('Feature updated successfully / Feature đã được cập nhật thành công', { id }); return feature; } /** * EN: Delete feature * VI: Xóa feature */ async delete(id: string) { logger.info('Deleting feature / Xóa feature', { id }); await featureRepository.delete(id); logger.info('Feature deleted successfully / Feature đã được xóa thành công', { id }); return true; } /** * EN: Toggle feature enabled/disabled status * VI: Bật/tắt trạng thái feature */ async toggle(id: string) { logger.info('Toggling feature status / Chuyển đổi trạng thái feature', { id }); const updatedFeature = await featureRepository.toggleEnabled(id); logger.info(`Feature ${updatedFeature.enabled ? 'enabled' : 'disabled'} / Feature đã được ${updatedFeature.enabled ? 'bật' : 'tắt'}`, { id }); return updatedFeature; } }