SearchModule mit GET /search?q= über Bienenstände, Völker, Königinnen, Aufgaben und Honigchargen. Kombinierbare Filter (Status, Standort, Bienenstand) in den bestehenden List-Endpunkten.
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
import { AuditLogService } from '../audit/audit-log.service';
|
|
import { Apiary, ApiaryStatus } from '../../generated/prisma';
|
|
import { CreateApiaryDto } from './dto/create-apiary.dto';
|
|
import { UpdateApiaryDto } from './dto/update-apiary.dto';
|
|
|
|
export interface ApiaryFilter {
|
|
status?: ApiaryStatus;
|
|
location?: string;
|
|
}
|
|
|
|
@Injectable()
|
|
export class ApiariesService {
|
|
constructor(
|
|
private readonly prisma: PrismaService,
|
|
private readonly auditLog: AuditLogService,
|
|
) {}
|
|
|
|
async findAll(filter: ApiaryFilter = {}): Promise<Apiary[]> {
|
|
return this.prisma.apiary.findMany({
|
|
where: {
|
|
status: filter.status,
|
|
location: filter.location
|
|
? { contains: filter.location, mode: 'insensitive' }
|
|
: undefined,
|
|
},
|
|
orderBy: { name: 'asc' },
|
|
});
|
|
}
|
|
|
|
async findById(id: string): Promise<Apiary> {
|
|
const apiary = await this.prisma.apiary.findUnique({ where: { id } });
|
|
if (!apiary) {
|
|
throw new NotFoundException('Apiary not found');
|
|
}
|
|
return apiary;
|
|
}
|
|
|
|
async create(dto: CreateApiaryDto, actingUserId: string): Promise<Apiary> {
|
|
const apiary = await this.prisma.apiary.create({ data: { ...dto } });
|
|
await this.auditLog.record({
|
|
entityType: 'Apiary',
|
|
entityId: apiary.id,
|
|
action: 'CREATE',
|
|
userId: actingUserId,
|
|
afterData: apiary,
|
|
});
|
|
return apiary;
|
|
}
|
|
|
|
async update(
|
|
id: string,
|
|
dto: UpdateApiaryDto,
|
|
actingUserId: string,
|
|
): Promise<Apiary> {
|
|
const before = await this.findById(id);
|
|
const apiary = await this.prisma.apiary.update({
|
|
where: { id },
|
|
data: { ...dto },
|
|
});
|
|
await this.auditLog.record({
|
|
entityType: 'Apiary',
|
|
entityId: id,
|
|
action: 'UPDATE',
|
|
userId: actingUserId,
|
|
beforeData: before,
|
|
afterData: apiary,
|
|
});
|
|
return apiary;
|
|
}
|
|
|
|
async deactivate(id: string, actingUserId: string): Promise<Apiary> {
|
|
const before = await this.findById(id);
|
|
const apiary = await this.prisma.apiary.update({
|
|
where: { id },
|
|
data: { status: 'INACTIVE', deactivatedAt: new Date() },
|
|
});
|
|
await this.auditLog.record({
|
|
entityType: 'Apiary',
|
|
entityId: id,
|
|
action: 'DEACTIVATE',
|
|
userId: actingUserId,
|
|
beforeData: before,
|
|
afterData: apiary,
|
|
});
|
|
return apiary;
|
|
}
|
|
}
|