feat(ai): add system settings schema delta and resilience fallback try-catch
CI / CD Pipeline / build (push) Successful in 5m15s
CI / CD Pipeline / deploy (push) Successful in 4m38s

This commit is contained in:
2026-05-22 10:14:27 +07:00
parent 2e50bcaa58
commit b5425d8b20
4 changed files with 78 additions and 8 deletions
+16 -8
View File
@@ -1,6 +1,7 @@
// File: src/modules/ai/ai-settings.service.ts
// Change Log
// - 2026-05-21: เพิ่ม service สำหรับอ่าน/เขียน AI feature toggle พร้อม Redis cache.
// - 2026-05-22: เพิ่ม try-catch ใน getAiFeaturesEnabled() เพื่อความยืดหยุ่นในกรณีที่ฐานข้อมูลยังไม่ได้อัปเกรดตาราง system_settings
import { Injectable, Logger } from '@nestjs/common';
import { InjectRedis } from '@nestjs-modules/ioredis';
@@ -26,14 +27,21 @@ export class AiSettingsService {
/** อ่านสถานะเปิด/ปิด AI features โดยใช้ Redis cache ก่อน DB */
async getAiFeaturesEnabled(): Promise<boolean> {
const cachedValue = await this.getCachedValue();
if (cachedValue !== null) return cachedValue === 'true';
const setting = await this.settingRepo.findOne({
where: { settingKey: AI_FEATURES_ENABLED_KEY },
});
const enabled = setting ? setting.settingValue === 'true' : true;
await this.setCachedValue(enabled);
return enabled;
try {
const cachedValue = await this.getCachedValue();
if (cachedValue !== null) return cachedValue === 'true';
const setting = await this.settingRepo.findOne({
where: { settingKey: AI_FEATURES_ENABLED_KEY },
});
const enabled = setting ? setting.settingValue === 'true' : true;
await this.setCachedValue(enabled);
return enabled;
} catch (error: unknown) {
this.logger.error(
`Failed to retrieve AI features enabled setting from database: ${this.toMessage(error)}`
);
return true;
}
}
/** อัปเดตสถานะ AI features ใน transaction แล้ว invalid cache หลัง DB สำเร็จ */