sto-k-odnomu/backend/src/admin/feature-flags/admin-feature-flags.service.ts
2026-01-10 23:49:42 +03:00

64 lines
1.6 KiB
TypeScript

import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { UpdateFeatureFlagDto } from './dto/update-feature-flag.dto';
@Injectable()
export class AdminFeatureFlagsService {
constructor(private prisma: PrismaService) {}
async findAll() {
const flags = await this.prisma.featureFlag.findMany({
orderBy: { key: 'asc' },
});
return flags;
}
async findOne(key: string) {
const flag = await this.prisma.featureFlag.findUnique({
where: { key },
});
if (!flag) {
throw new NotFoundException(`Feature flag with key "${key}" not found`);
}
return flag;
}
async update(key: string, dto: UpdateFeatureFlagDto) {
try {
const flag = await this.prisma.featureFlag.update({
where: { key },
data: {
enabled: dto.enabled,
description: dto.description,
},
});
return flag;
} catch (error) {
if (error.code === 'P2025') {
throw new NotFoundException(`Feature flag with key "${key}" not found`);
}
throw new BadRequestException(`Failed to update feature flag: ${error.message}`);
}
}
async upsert(key: string, dto: UpdateFeatureFlagDto & { description?: string }) {
const flag = await this.prisma.featureFlag.upsert({
where: { key },
update: {
enabled: dto.enabled,
description: dto.description,
},
create: {
key,
enabled: dto.enabled,
description: dto.description || null,
},
});
return flag;
}
}