{"version":3,"sources":["../src/NotificationServicesController/services/feature-announcements.ts"],"sourcesContent":["import { documentToHtmlString } from '@contentful/rich-text-html-renderer';\nimport type { Entry, Asset } from 'contentful';\nimport log from 'loglevel';\n\nimport { TRIGGER_TYPES } from '../constants/notification-schema';\nimport { processFeatureAnnouncement } from '../processors/process-feature-announcement';\nimport type { FeatureAnnouncementRawNotification } from '../types/feature-announcement/feature-announcement';\nimport type {\n  ImageFields,\n  TypeFeatureAnnouncement,\n} from '../types/feature-announcement/type-feature-announcement';\nimport type { TypeExtensionLinkFields } from '../types/feature-announcement/type-links';\nimport type { INotification } from '../types/notification/notification';\n\nconst DEFAULT_SPACE_ID = ':space_id';\nconst DEFAULT_ACCESS_TOKEN = ':access_token';\nconst DEFAULT_CLIENT_ID = ':client_id';\nexport const FEATURE_ANNOUNCEMENT_API = `https://cdn.contentful.com/spaces/${DEFAULT_SPACE_ID}/environments/master/entries`;\nexport const FEATURE_ANNOUNCEMENT_URL = `${FEATURE_ANNOUNCEMENT_API}?access_token=${DEFAULT_ACCESS_TOKEN}&content_type=productAnnouncement&include=10&fields.clients=${DEFAULT_CLIENT_ID}`;\n\ntype Env = {\n  spaceId: string;\n  accessToken: string;\n  platform: string;\n};\n\n/**\n * Contentful API Response Shape\n */\nexport type ContentfulResult = {\n  includes?: {\n    // eslint-disable-next-line @typescript-eslint/naming-convention\n    Entry?: Entry[];\n    // eslint-disable-next-line @typescript-eslint/naming-convention\n    Asset?: Asset[];\n  };\n  items?: TypeFeatureAnnouncement[];\n};\n\nconst fetchFromContentful = async (\n  url: string,\n  retries = 3,\n  retryDelay = 1000,\n): Promise<ContentfulResult | null> => {\n  let lastError: Error | null = null;\n\n  for (let i = 0; i < retries; i++) {\n    try {\n      const response = await fetch(url);\n      if (!response.ok) {\n        throw new Error(`Fetch failed with status: ${response.status}`);\n      }\n      return await response.json();\n    } catch (error) {\n      if (error instanceof Error) {\n        lastError = error;\n      }\n      if (i < retries - 1) {\n        await new Promise((resolve) => setTimeout(resolve, retryDelay));\n      }\n    }\n  }\n\n  log.error(\n    `Error fetching from Contentful after ${retries} retries:`,\n    lastError,\n  );\n  return null;\n};\n\nconst fetchFeatureAnnouncementNotifications = async (\n  env: Env,\n): Promise<FeatureAnnouncementRawNotification[]> => {\n  const url = FEATURE_ANNOUNCEMENT_URL.replace(DEFAULT_SPACE_ID, env.spaceId)\n    .replace(DEFAULT_ACCESS_TOKEN, env.accessToken)\n    .replace(DEFAULT_CLIENT_ID, env.platform);\n  const data = await fetchFromContentful(url);\n\n  if (!data) {\n    return [];\n  }\n\n  const findIncludedItem = (sysId: string) => {\n    const item =\n      data?.includes?.Entry?.find((i: Entry) => i?.sys?.id === sysId) ||\n      data?.includes?.Asset?.find((i: Asset) => i?.sys?.id === sysId);\n    return item ? item?.fields : null;\n  };\n\n  const contentfulNotifications = data?.items ?? [];\n  const rawNotifications: FeatureAnnouncementRawNotification[] =\n    contentfulNotifications.map((n: TypeFeatureAnnouncement) => {\n      const { fields } = n;\n      const imageFields = fields.image\n        ? (findIncludedItem(fields.image.sys.id) as ImageFields['fields'])\n        : undefined;\n      const extensionLinkFields = fields.extensionLink\n        ? (findIncludedItem(\n            fields.extensionLink.sys.id,\n          ) as TypeExtensionLinkFields['fields'])\n        : undefined;\n\n      const notification: FeatureAnnouncementRawNotification = {\n        type: TRIGGER_TYPES.FEATURES_ANNOUNCEMENT,\n        createdAt: new Date(n.sys.createdAt).toString(),\n        data: {\n          id: fields.id,\n          category: fields.category,\n          title: fields.title,\n          longDescription: documentToHtmlString(fields.longDescription),\n          shortDescription: fields.shortDescription,\n          image: {\n            title: imageFields?.title,\n            description: imageFields?.description,\n            url: imageFields?.file?.url ?? '',\n          },\n          extensionLink: extensionLinkFields && {\n            extensionLinkText: extensionLinkFields?.extensionLinkText,\n            extensionLinkRoute: extensionLinkFields?.extensionLinkRoute,\n          },\n        },\n      };\n\n      return notification;\n    });\n\n  return rawNotifications;\n};\n\n/**\n * Gets Feature Announcement from our services\n * @param env - environment for feature announcements\n * @returns Raw Feature Announcements\n */\nexport async function getFeatureAnnouncementNotifications(\n  env: Env,\n): Promise<INotification[]> {\n  const rawNotifications = await fetchFeatureAnnouncementNotifications(env);\n  const notifications = rawNotifications.map((notification) =>\n    processFeatureAnnouncement(notification),\n  );\n\n  return notifications;\n}\n"],"mappings":";;;;;AAAA,SAAS,4BAA4B;AAErC,OAAO,SAAS;AAYhB,IAAM,mBAAmB;AACzB,IAAM,uBAAuB;AAC7B,IAAM,oBAAoB;AACnB,IAAM,2BAA2B,qCAAqC,gBAAgB;AACtF,IAAM,2BAA2B,GAAG,wBAAwB,iBAAiB,oBAAoB,+DAA+D,iBAAiB;AAqBxL,IAAM,sBAAsB,OAC1B,KACA,UAAU,GACV,aAAa,QACwB;AACrC,MAAI,YAA0B;AAE9B,WAAS,IAAI,GAAG,IAAI,SAAS,KAAK;AAChC,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,GAAG;AAChC,UAAI,CAAC,SAAS,IAAI;AAChB,cAAM,IAAI,MAAM,6BAA6B,SAAS,MAAM,EAAE;AAAA,MAChE;AACA,aAAO,MAAM,SAAS,KAAK;AAAA,IAC7B,SAAS,OAAO;AACd,UAAI,iBAAiB,OAAO;AAC1B,oBAAY;AAAA,MACd;AACA,UAAI,IAAI,UAAU,GAAG;AACnB,cAAM,IAAI,QAAQ,CAAC,YAAY,WAAW,SAAS,UAAU,CAAC;AAAA,MAChE;AAAA,IACF;AAAA,EACF;AAEA,MAAI;AAAA,IACF,wCAAwC,OAAO;AAAA,IAC/C;AAAA,EACF;AACA,SAAO;AACT;AAEA,IAAM,wCAAwC,OAC5C,QACkD;AAClD,QAAM,MAAM,yBAAyB,QAAQ,kBAAkB,IAAI,OAAO,EACvE,QAAQ,sBAAsB,IAAI,WAAW,EAC7C,QAAQ,mBAAmB,IAAI,QAAQ;AAC1C,QAAM,OAAO,MAAM,oBAAoB,GAAG;AAE1C,MAAI,CAAC,MAAM;AACT,WAAO,CAAC;AAAA,EACV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