fix(ops): 修复企微报告表零日期导致无法保存
早期 INSERT 未写 updated_at,MySQL 落成 0000-00-00,Prisma 读行失败。
This commit is contained in:
@@ -61,6 +61,7 @@ type ReportRow = WecomReportPush;
|
||||
@Injectable()
|
||||
export class AdminWecomReportsService implements OnModuleInit {
|
||||
private readonly logger = new Logger(AdminWecomReportsService.name);
|
||||
private defaultsReady = false;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
@@ -77,11 +78,36 @@ export class AdminWecomReportsService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 早期 INSERT IGNORE 未写 updated_at,Prisma db push 的列又无默认值,
|
||||
* MySQL 会落成 0000-00-00,后续 Prisma 读行即报 invalid datetime。
|
||||
*/
|
||||
private async repairZeroDatetimes(): Promise<void> {
|
||||
await this.prisma.$executeRawUnsafe(`
|
||||
UPDATE wecom_report_push
|
||||
SET created_at = CURRENT_TIMESTAMP(3)
|
||||
WHERE CAST(created_at AS CHAR) REGEXP '^0000|-00-'
|
||||
`);
|
||||
await this.prisma.$executeRawUnsafe(`
|
||||
UPDATE wecom_report_push
|
||||
SET updated_at = CURRENT_TIMESTAMP(3)
|
||||
WHERE CAST(updated_at AS CHAR) REGEXP '^0000|-00-'
|
||||
`);
|
||||
await this.prisma.$executeRawUnsafe(`
|
||||
UPDATE wecom_report_push
|
||||
SET last_sent_at = NULL
|
||||
WHERE last_sent_at IS NOT NULL AND CAST(last_sent_at AS CHAR) REGEXP '^0000|-00-'
|
||||
`);
|
||||
}
|
||||
|
||||
async ensureDefaults(): Promise<void> {
|
||||
if (this.defaultsReady) return;
|
||||
await this.repairZeroDatetimes();
|
||||
for (const seed of KIND_SEED) {
|
||||
await this.prisma.wecomReportPush.upsert({
|
||||
where: { kind: seed.kind },
|
||||
create: {
|
||||
const existing = await this.prisma.wecomReportPush.findUnique({ where: { kind: seed.kind } });
|
||||
if (existing) continue;
|
||||
await this.prisma.wecomReportPush.create({
|
||||
data: {
|
||||
kind: seed.kind,
|
||||
name: seed.name,
|
||||
webhookUrl: WECOM_STORE_AUDIT_PLACEHOLDER_WEBHOOK,
|
||||
@@ -91,9 +117,9 @@ export class AdminWecomReportsService implements OnModuleInit {
|
||||
sendWeekday: 1,
|
||||
sendMonthDay: 1,
|
||||
},
|
||||
update: {},
|
||||
});
|
||||
}
|
||||
this.defaultsReady = true;
|
||||
}
|
||||
|
||||
parseKind(raw: string): WecomReportKind {
|
||||
|
||||
Reference in New Issue
Block a user