import type { NotificationClassification, NotificationHandlingMode, NotificationSourceMatcher, PushNotificationPayload, } from "../../types"; const DEFAULT_SOURCE_MATCHER: Required = { sourceKeys: ["source", "sdk_source", "vendor", "provider"], sourceValues: ["martech"], campaignIdKeys: ["sirrus_campaign_id", "sirrusCampaignId"], templateIdKeys: ["sirrus_template_id", "sirrusTemplateId"], }; const getRawValue = ( payload: PushNotificationPayload, key: string ): string | undefined => { const rawPayloadValue = payload.rawPayload?.[key]; const dataValue = payload.data?.[key]; if (typeof rawPayloadValue === "string") { return rawPayloadValue; } if (typeof dataValue === "string") { return dataValue; } return undefined; }; const hasTaggedValue = ( payload: PushNotificationPayload, keys: string[] ): { key: string; value: string } | null => { for (const key of keys) { const value = getRawValue(payload, key); if (value) { return { key, value }; } } return null; }; export class NotificationClassifier { constructor( private readonly handlingMode: NotificationHandlingMode, private readonly sourceMatcher: NotificationSourceMatcher = {} ) {} classify(payload: PushNotificationPayload): NotificationClassification { if (this.handlingMode === "all") { return { matched: true, ownership: "sirrus", reason: "Notification handling mode is set to all.", }; } const matcher = { ...DEFAULT_SOURCE_MATCHER, ...this.sourceMatcher, }; for (const key of matcher.sourceKeys) { const value = getRawValue(payload, key)?.toLowerCase(); if (value && matcher.sourceValues.includes(value)) { return { matched: true, ownership: "sirrus", reason: `Matched Martech source marker from "${key}".`, }; } } if (hasTaggedValue(payload, matcher.campaignIdKeys)) { return { matched: true, ownership: "sirrus", reason: "Matched campaign identifier.", }; } if (hasTaggedValue(payload, matcher.templateIdKeys)) { return { matched: true, ownership: "sirrus", reason: "Matched template identifier.", }; } if (payload.rawPayload || payload.data) { return { matched: false, ownership: "external", reason: "Notification payload did not include a Martech marker.", }; } return { matched: false, ownership: "unknown", reason: "Notification payload did not contain structured metadata.", }; } }