{"version":3,"file":"api-notifications.mjs","sourceRoot":"","sources":["../../../src/NotificationServicesController/services/api-notifications.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,oBAAoB,EAAE,6CAAyC;AAMxE,OAAO,EAAE,WAAW,EAAE,2BAAuB;AAC7C,OAAO,EAAE,wBAAwB,EAAE,wCAAoC;AAWvE,MAAM,eAAe,GAAG;IACtB,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,wCAAwC;IAC7C,GAAG,EAAE,oCAAoC;CACZ,CAAC;AAEhC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAW,KAAK,EAAU,EAAE,CACtD,eAAe,CAAC,GAAG,CAAC,IAAI,eAAe,CAAC,GAAG,CAAC;AAE9C,MAAM,oBAAoB,GAAG;IAC3B,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,6CAA6C;IAClD,GAAG,EAAE,yCAAyC;CAC/C,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,MAAW,KAAK,EAAU,EAAE,CAC3D,oBAAoB,CAAC,GAAG,CAAC,IAAI,oBAAoB,CAAC,GAAG,CAAC;AAExD,uDAAuD;AACvD,MAAM,CAAC,MAAM,wCAAwC,GAAG,CACtD,MAAW,KAAK,EACR,EAAE,CAAC,GAAG,WAAW,CAAC,GAAG,CAAC,6BAA6B,CAAC;AAE9D,wEAAwE;AACxE,MAAM,CAAC,MAAM,kCAAkC,GAAG,CAAC,MAAW,KAAK,EAAU,EAAE,CAC7E,GAAG,WAAW,CAAC,GAAG,CAAC,uBAAuB,CAAC;AAE7C,gDAAgD;AAChD,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAAC,MAAW,KAAK,EAAU,EAAE,CACzE,GAAG,gBAAgB,CAAC,GAAG,CAAC,uBAAuB,CAAC;AAElD,8BAA8B;AAC9B,MAAM,CAAC,MAAM,0CAA0C,GAAG,CACxD,MAAW,KAAK,EACR,EAAE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;IAErB,mCAAmC;IACnC,MAAM,wBAAwB,GAAG,aAAa;QAC5C,EAAE,GAAG,CAAC,CAAC,YAAY,EAA0C,EAAE;QAC7D,IAAI,CAAC,YAAY,CAAC,iBAAiB,EAAE,CAAC;YACpC,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,IAAI,CAAC;YACH,OAAO,oBAAoB,CAAC,YAAY,CAAC,CAAC;QAC5C,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,IAAI,EAAqC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAEtE,OAAO,wBAAwB,IAAI,EAAE,CAAC;AACxC,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,WAAmB,EACnB,eAAyB,EACzB,MAAW,KAAK;IAEhB,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,OAAO;IACT,CAAC;IAID,MAAM,IAAI,GAAiB;QACzB,GAAG,EAAE,eAAe;KACrB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,WAAW,CACf,WAAW,EACX,0CAA0C,CAAC,GAAG,CAAC,EAC/C,MAAM,EACN,IAAI,CACL,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,KAAK,CAAC,sCAAsC,EAAE,KAAK,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport { toRawAPINotification } from '../../shared/to-raw-notification';\nimport type {\n  NormalisedAPINotification,\n  Schema,\n  UnprocessedRawNotification,\n} from '../types/notification-api';\nimport { makeApiCall } from '../utils/utils';\nimport { notificationsConfigCache } from './notification-config-cache';\n\nexport type NotificationTrigger = {\n  id: string;\n  chainId: string;\n  kind: string;\n  address: string;\n};\n\nexport type ENV = 'prd' | 'uat' | 'dev';\n\nconst TRIGGER_API_ENV = {\n  dev: 'https://trigger.dev-api.cx.metamask.io',\n  uat: 'https://trigger.uat-api.cx.metamask.io',\n  prd: 'https://trigger.api.cx.metamask.io',\n} satisfies Record<ENV, string>;\n\nexport const TRIGGER_API = (env: ENV = 'prd'): string =>\n  TRIGGER_API_ENV[env] ?? TRIGGER_API_ENV.prd;\n\nconst NOTIFICATION_API_ENV = {\n  dev: 'https://notification.dev-api.cx.metamask.io',\n  uat: 'https://notification.uat-api.cx.metamask.io',\n  prd: 'https://notification.api.cx.metamask.io',\n};\n\nexport const NOTIFICATION_API = (env: ENV = 'prd'): string =>\n  NOTIFICATION_API_ENV[env] ?? NOTIFICATION_API_ENV.prd;\n\n// Gets notification settings for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT = (\n  env: ENV = 'prd',\n): string => `${TRIGGER_API(env)}/api/v2/notifications/query`;\n\n// Used to create/update account notifications for each account provided\nexport const TRIGGER_API_NOTIFICATIONS_ENDPOINT = (env: ENV = 'prd'): string =>\n  `${TRIGGER_API(env)}/api/v2/notifications`;\n\n// Lists notifications for each account provided\nexport const NOTIFICATION_API_LIST_ENDPOINT = (env: ENV = 'prd'): string =>\n  `${NOTIFICATION_API(env)}/api/v3/notifications`;\n\n// Marks notifications as read\nexport const NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT = (\n  env: ENV = 'prd',\n): string => `${NOTIFICATION_API(env)}/api/v3/notifications/mark-as-read`;\n\n/**\n * fetches notification config (accounts enabled vs disabled)\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * NOTE the API will return addresses config with false if they have not been created before.\n * NOTE this is cached for 1s to prevent multiple update calls\n * @returns object of notification config, or null if missing\n */\nexport async function getNotificationsApiConfigCached(\n  bearerToken: string,\n  addresses: string[],\n  env: ENV = 'prd',\n): Promise<{ address: string; enabled: boolean }[]> {\n  if (addresses.length === 0) {\n    return [];\n  }\n\n  const normalizedAddresses = addresses.map((addr) => addr.toLowerCase());\n\n  const cached = notificationsConfigCache.get(normalizedAddresses);\n  if (cached) {\n    return cached;\n  }\n\n  type RequestBody = { address: string }[];\n  type Response = { address: string; enabled: boolean }[];\n  const body: RequestBody = normalizedAddresses.map((address) => ({ address }));\n  const apiResponse = await makeApiCall(\n    bearerToken,\n    TRIGGER_API_NOTIFICATIONS_QUERY_ENDPOINT(env),\n    'POST',\n    body,\n  )\n    .then<Response | null>((response) => (response.ok ? response.json() : null))\n    .catch(() => null);\n\n  const result = apiResponse ?? [];\n\n  if (result.length > 0) {\n    notificationsConfigCache.set(result);\n  }\n\n  return result;\n}\n\n/**\n * updates notifications for a given addresses\n *\n * @param bearerToken - jwt\n * @param addresses - list of addresses to check\n * @param env - the environment to use for the API call\n * @returns void\n */\nexport async function updateOnChainNotifications(\n  bearerToken: string,\n  addresses: { address: string; enabled: boolean }[],\n  env: ENV = 'prd',\n): Promise<void> {\n  if (addresses.length === 0) {\n    return;\n  }\n\n  const normalizedAddresses = addresses.map((item) => ({\n    ...item,\n    address: item.address.toLowerCase(),\n  }));\n\n  type RequestBody = { address: string; enabled: boolean }[];\n  const body: RequestBody = normalizedAddresses;\n  await makeApiCall(\n    bearerToken,\n    TRIGGER_API_NOTIFICATIONS_ENDPOINT(env),\n    'POST',\n    body,\n  )\n    .then(() => notificationsConfigCache.set(normalizedAddresses))\n    .catch(() => null);\n}\n\n/**\n * Fetches on-chain notifications for the given addresses\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param addresses - List of addresses\n * @param locale - to generate translated notifications\n * @param platform - filter notifications for specific platforms ('extension' | 'mobile')\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to an array of NormalisedAPINotification objects. If no notifications are enabled or an error occurs, it may return an empty array.\n */\nexport async function getAPINotifications(\n  bearerToken: string,\n  addresses: string[],\n  locale: string,\n  platform: 'extension' | 'mobile',\n  env: ENV = 'prd',\n): Promise<NormalisedAPINotification[]> {\n  if (addresses.length === 0) {\n    return [];\n  }\n\n  type RequestBody =\n    Schema.paths['/api/v3/notifications']['post']['requestBody']['content']['application/json'];\n  type APIResponse =\n    Schema.paths['/api/v3/notifications']['post']['responses']['200']['content']['application/json'];\n\n  const body: RequestBody = {\n    addresses: addresses.map((addr) => addr.toLowerCase()),\n    locale,\n    platform,\n  };\n  const notifications = await makeApiCall(\n    bearerToken,\n    NOTIFICATION_API_LIST_ENDPOINT(env),\n    'POST',\n    body,\n  )\n    .then<APIResponse | null>((response) =>\n      response.ok ? response.json() : null,\n    )\n    .catch(() => null);\n\n  // Transform and sort notifications\n  const transformedNotifications = notifications\n    ?.map((notification): UnprocessedRawNotification | undefined => {\n      if (!notification.notification_type) {\n        return undefined;\n      }\n\n      try {\n        return toRawAPINotification(notification);\n      } catch {\n        return undefined;\n      }\n    })\n    .filter((item): item is NormalisedAPINotification => Boolean(item));\n\n  return transformedNotifications ?? [];\n}\n\n/**\n * Marks the specified notifications as read.\n * This method sends a POST request to the notifications service to mark the provided notification IDs as read.\n * If the operation is successful, it completes without error. If the operation fails, it throws an error with details.\n *\n * @param bearerToken - The JSON Web Token used for authentication in the API call.\n * @param notificationIds - An array of notification IDs to be marked as read.\n * @param env - the environment to use for the API call\n * @returns A promise that resolves to void. The promise will reject if there's an error during the API call or if the response status is not 200.\n */\nexport async function markNotificationsAsRead(\n  bearerToken: string,\n  notificationIds: string[],\n  env: ENV = 'prd',\n): Promise<void> {\n  if (notificationIds.length === 0) {\n    return;\n  }\n\n  type ResponseBody =\n    Schema.paths['/api/v3/notifications/mark-as-read']['post']['requestBody']['content']['application/json'];\n  const body: ResponseBody = {\n    ids: notificationIds,\n  };\n\n  try {\n    await makeApiCall(\n      bearerToken,\n      NOTIFICATION_API_MARK_ALL_AS_READ_ENDPOINT(env),\n      'POST',\n      body,\n    );\n  } catch (error) {\n    log.error('Error marking notifications as read:', error);\n  }\n}\n"]}