79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { PrismaService } from '../infrastructure/persistence/prisma/prisma.service';
|
|
|
|
@Injectable()
|
|
export class PrePlantingConfigService {
|
|
private readonly logger = new Logger(PrePlantingConfigService.name);
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
async getConfig(): Promise<{
|
|
isActive: boolean;
|
|
activatedAt: Date | null;
|
|
}> {
|
|
const config = await this.prisma.prePlantingConfig.findFirst({
|
|
orderBy: { updatedAt: 'desc' },
|
|
});
|
|
|
|
if (!config) {
|
|
return { isActive: false, activatedAt: null };
|
|
}
|
|
|
|
return {
|
|
isActive: config.isActive,
|
|
activatedAt: config.activatedAt,
|
|
};
|
|
}
|
|
|
|
async updateConfig(
|
|
isActive: boolean,
|
|
updatedBy?: string,
|
|
): Promise<{
|
|
isActive: boolean;
|
|
activatedAt: Date | null;
|
|
}> {
|
|
const existing = await this.prisma.prePlantingConfig.findFirst({
|
|
orderBy: { updatedAt: 'desc' },
|
|
});
|
|
|
|
const activatedAt = isActive ? new Date() : null;
|
|
|
|
if (existing) {
|
|
const updated = await this.prisma.prePlantingConfig.update({
|
|
where: { id: existing.id },
|
|
data: {
|
|
isActive,
|
|
activatedAt: isActive ? (existing.activatedAt || activatedAt) : existing.activatedAt,
|
|
updatedBy: updatedBy || null,
|
|
},
|
|
});
|
|
|
|
this.logger.log(
|
|
`[PRE-PLANTING] Config updated: isActive=${updated.isActive} by ${updatedBy || 'unknown'}`,
|
|
);
|
|
|
|
return {
|
|
isActive: updated.isActive,
|
|
activatedAt: updated.activatedAt,
|
|
};
|
|
}
|
|
|
|
const created = await this.prisma.prePlantingConfig.create({
|
|
data: {
|
|
isActive,
|
|
activatedAt,
|
|
updatedBy: updatedBy || null,
|
|
},
|
|
});
|
|
|
|
this.logger.log(
|
|
`[PRE-PLANTING] Config created: isActive=${created.isActive} by ${updatedBy || 'unknown'}`,
|
|
);
|
|
|
|
return {
|
|
isActive: created.isActive,
|
|
activatedAt: created.activatedAt,
|
|
};
|
|
}
|
|
}
|