fix(alerts): проверять правила и группы до замены конфигурации
This commit is contained in:
1 parent
3556a378f2
commit
6093442bcf
10 files changed
+61
-10
No files matched your search
@@ -1,15 +1,12 @@
|
||||
import {z} from "zod"
|
||||
import {alertsResponseSchema,alertsRulesReceiptSchema,telegramSettingsReceiptSchema,telegramTestReceiptSchema,putRulesSchema,putTelegramSchema,testTelegramSchema,type PutRulesBody,type PutTelegramBody,type TestTelegramBody} from "@mmapp/contracts/alerts"
|
||||
import {normalizeAlertsConfig,alertsResponseSchema,alertsRulesReceiptSchema,telegramSettingsReceiptSchema,telegramTestReceiptSchema,putTelegramSchema,testTelegramSchema,type PutRulesBody,type PutTelegramBody,type TestTelegramBody} from "@mmapp/contracts/alerts"
|
||||
import {requestJson} from "./http-client"
|
||||
type Options={signal?:AbortSignal}
|
||||
const signal=(options?:Options)=>{const active=options?.signal?AbortSignal.any([options.signal,AbortSignal.timeout(30_000)]):AbortSignal.timeout(30_000);active.throwIfAborted();return active}
|
||||
function parse<S extends z.ZodType>(schema:S,raw:unknown,message:string):z.output<S>{const result=schema.safeParse(raw);if(!result.success)throw Error(message);return result.data}
|
||||
export async function getAlerts(baseUrl:string,options?:Options){return parse(alertsResponseSchema,await requestJson<unknown>(baseUrl,"/api/alerts",{signal:signal(options),cache:"no-store"}),"Данные оповещений не подтверждены сервером")}
|
||||
export async function saveAlertRules(baseUrl:string,payload:PutRulesBody,options?:Options){
|
||||
const body=parse(putRulesSchema,payload,"Проверьте правила и группы оповещений")
|
||||
const ids=new Set(body.rules.map(rule=>rule.id));if(ids.size!==body.rules.length)throw Error("Повторяющиеся ID правил")
|
||||
if(body.groups){const groups=new Set(body.groups.map(group=>group.id));if(groups.size!==body.groups.length||body.rules.some(rule=>rule.groupId?.trim()&&!groups.has(rule.groupId.trim())))throw Error("Повторяющиеся группы или неизвестная группа правила")}
|
||||
for(const rule of body.rules){if(!(rule.targets?.length?rule.targets:[rule.target??""]).some(value=>value.trim())||!(rule.conditions?.length?rule.conditions:[rule.condition??""]).some(value=>value.trim()))throw Error("Нужны непустые объекты и условия правила")}
|
||||
const body=normalizeAlertsConfig(payload)
|
||||
const receipt=parse(alertsRulesReceiptSchema,await requestJson<unknown>(baseUrl,"/api/alerts/rules",{method:"PUT",body:JSON.stringify(body),signal:signal(options)}),"Сохранение правил не подтверждено сервером")
|
||||
const same=(a:unknown,b:unknown)=>JSON.stringify(a)===JSON.stringify(b)
|
||||
if(receipt.rules.length!==body.rules.length||body.rules.some(rule=>{
|
||||
|
||||
Reference in new issue
Block a user