← 中文资源

简体中文版本

通过备用链路由通知

对每条通知,主通道成功就保持原通道,失败则按备用链升级到下一个通道。

阅读英文原文

任务

按通道结果判断:成功即记录投递通道;失败则取备用链中该通道的下一个,链尾失败则回到链首。

契约

  • 输入:JSON 对象:{ notifications: [{ id, channel, ok }], fallbackChain: string[] }
  • 输出:JSON 对象数组:[{ id, deliveredVia, escalated }]

样例

  1. 输入 {"notifications":[{"id":"n1","channel":"email","ok":true},{"id":"n2","channel":"email","ok":false}],"fallbackChain":["email","sms","pager"]} -> 期望 [{"id":"n1","deliveredVia":"email","escalated":false},{"id":"n2","deliveredVia":"sms","escalated":true}]。n2 在 email 失败后升级到下一个通道。
  2. 输入 {"notifications":[{"id":"n3","channel":"pager","ok":false}],"fallbackChain":["email","sms","pager"]} -> 期望 [{"id":"n3","deliveredVia":"email","escalated":true}]。失败通道位于链尾时,备用链从第一项重新开始。

参考实现

function solve({ notifications, fallbackChain }) {
      return notifications.map((notification) => {
        if (notification.ok) return { id: notification.id, deliveredVia: notification.channel, escalated: false };
        const next = fallbackChain.find((channel) => channel !== notification.channel) ?? null;
        return { id: notification.id, deliveredVia: next, escalated: next !== null };
      });
    }

为什么有效

通知管道在某个通道劣化时会静默失败。显式的升级逻辑能把实际投递通道纳入审计轨迹。

绝不能把失败的通道报告为投递目标;备用链为空说明什么都没投递出去。