{"version":3,"file":"cancelPrivacyRequests-CMIaonIz.mjs","names":[],"sources":["../src/lib/requests/cancelPrivacyRequests.ts"],"sourcesContent":["import { RequestAction, RequestStatus } from '@transcend-io/privacy-types';\nimport {\n  buildTranscendGraphQLClient,\n  fetchAllTemplates,\n  makeGraphQLRequest,\n  type Template,\n} from '@transcend-io/sdk';\nimport { map } from '@transcend-io/utils';\nimport cliProgress from 'cli-progress';\nimport colors from 'colors';\n\nimport { DEFAULT_TRANSCEND_API } from '../../constants.js';\nimport { logger } from '../../logger.js';\nimport {\n  UPDATE_PRIVACY_REQUEST,\n  fetchAllRequests,\n  CANCEL_PRIVACY_REQUEST,\n} from '../graphql/index.js';\n\n/**\n * Cancel a set of privacy requests\n *\n * @param options - Options\n * @returns The number of requests canceled\n */\nexport async function cancelPrivacyRequests({\n  requestActions,\n  cancellationTitle,\n  auth,\n  requestIds,\n  silentModeBefore,\n  createdAtBefore,\n  createdAtAfter,\n  updatedAtBefore,\n  updatedAtAfter,\n  statuses = [\n    RequestStatus.Compiling,\n    RequestStatus.RequestMade,\n    RequestStatus.Delayed,\n    RequestStatus.Approving,\n    RequestStatus.Secondary,\n    RequestStatus.Enriching,\n    RequestStatus.Waiting,\n    RequestStatus.SecondaryApproving,\n  ],\n  concurrency = 50,\n  transcendUrl = DEFAULT_TRANSCEND_API,\n}: {\n  /** The request actions that should be restarted */\n  requestActions: RequestAction[];\n  /** Transcend API key authentication */\n  auth: string;\n  /** Concurrency limit for approving */\n  concurrency?: number;\n  /** The request statuses to cancel */\n  statuses?: RequestStatus[];\n  /** The set of privacy requests to cancel */\n  requestIds?: string[];\n  /** Mark these requests as silent mode if they were created before this date */\n  silentModeBefore?: Date;\n  /** Filter for requests created before this date */\n  createdAtBefore?: Date;\n  /** Filter for requests created after this date */\n  createdAtAfter?: Date;\n  /** Filter for requests updated before this date */\n  updatedAtBefore?: Date;\n  /** Filter for requests updated after this date */\n  updatedAtAfter?: Date;\n  /** API URL for Transcend backend */\n  transcendUrl?: string;\n  /** The email template to use when canceling the requests */\n  cancellationTitle?: string;\n}): Promise<number> {\n  // Find all requests made before createdAt that are in a removing data state\n  const client = buildTranscendGraphQLClient(transcendUrl, auth);\n\n  // Time duration\n  const t0 = new Date().getTime();\n  // create a new progress bar instance and use shades_classic theme\n  const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);\n\n  // Grab the template with that title\n  let cancelationTemplate: Template | undefined;\n  if (cancellationTitle) {\n    const matchingTemplates = await fetchAllTemplates(client, {\n      logger,\n      filterBy: { title: cancellationTitle },\n    });\n    const exactTitleMatch = matchingTemplates.find(\n      (template) => template.title === cancellationTitle,\n    );\n    if (!exactTitleMatch) {\n      throw new Error(`Failed to find a template with title: \"${cancellationTitle}\"`);\n    }\n    cancelationTemplate = exactTitleMatch;\n  }\n\n  // Pull in the requests\n  const allRequests = await fetchAllRequests(client, {\n    actions: requestActions,\n    createdAtBefore,\n    createdAtAfter,\n    updatedAtBefore,\n    updatedAtAfter,\n    statuses,\n    requestIds,\n  });\n\n  // Notify Transcend\n  logger.info(\n    colors.magenta(\n      `Canceling \"${allRequests.length}\" requests${\n        cancelationTemplate ? ` Using template: ${cancelationTemplate.title}` : ''\n      }.`,\n    ),\n  );\n\n  let total = 0;\n  progressBar.start(allRequests.length, 0);\n  await map(\n    allRequests,\n    async (requestToCancel) => {\n      // update request to silent mode if silentModeBefore is defined\n      // and the request was created before silentModeBefore\n      if (silentModeBefore && new Date(silentModeBefore) > new Date(requestToCancel.createdAt)) {\n        await makeGraphQLRequest(client, UPDATE_PRIVACY_REQUEST, {\n          variables: {\n            input: {\n              id: requestToCancel.id,\n              isSilent: true,\n            },\n          },\n          logger,\n        });\n      }\n\n      // cancel the request\n      await makeGraphQLRequest(client, CANCEL_PRIVACY_REQUEST, {\n        variables: {\n          input: {\n            requestId: requestToCancel.id,\n            ...(cancelationTemplate\n              ? {\n                  subject: `Re: ${cancelationTemplate.subject.defaultMessage}`,\n                  template: cancelationTemplate.template.defaultMessage,\n                }\n              : {}),\n          },\n        },\n        logger,\n      });\n\n      total += 1;\n      progressBar.update(total);\n    },\n    { concurrency },\n  );\n\n  progressBar.stop();\n  const t1 = new Date().getTime();\n  const totalTime = t1 - t0;\n\n  logger.info(\n    colors.green(`Successfully canceled ${total} requests in \"${totalTime / 1000}\" seconds!`),\n  );\n  return allRequests.length;\n}\n"],"mappings":"0bAyBA,eAAsB,EAAsB,CAC1C,iBACA,oBACA,OACA,aACA,mBACA,kBACA,iBACA,kBACA,iBACA,WAAW,CACT,EAAc,UACd,EAAc,YACd,EAAc,QACd,EAAc,UACd,EAAc,UACd,EAAc,UACd,EAAc,QACd,EAAc,mBACf,CACD,cAAc,GACd,eAAe,GA0BG,CAElB,IAAM,EAAS,EAA4B,EAAc,EAAK,CAGxD,EAAK,IAAI,MAAM,CAAC,SAAS,CAEzB,EAAc,IAAI,EAAY,UAAU,EAAE,CAAE,EAAY,QAAQ,eAAe,CAGjF,EACJ,GAAI,EAAmB,CAKrB,IAAM,GAAkB,MAJQ,EAAkB,EAAQ,CACxD,SACA,SAAU,CAAE,MAAO,EAAmB,CACvC,CAAC,EACwC,KACvC,GAAa,EAAS,QAAU,EAClC,CACD,GAAI,CAAC,EACH,MAAU,MAAM,0CAA0C,EAAkB,GAAG,CAEjF,EAAsB,EAIxB,IAAM,EAAc,MAAM,EAAiB,EAAQ,CACjD,QAAS,EACT,kBACA,iBACA,kBACA,iBACA,WACA,aACD,CAAC,CAGF,EAAO,KACL,EAAO,QACL,cAAc,EAAY,OAAO,YAC/B,EAAsB,oBAAoB,EAAoB,QAAU,GACzE,GACF,CACF,CAED,IAAI,EAAQ,EACZ,EAAY,MAAM,EAAY,OAAQ,EAAE,CACxC,MAAM,EACJ,EACA,KAAO,IAAoB,CAGrB,GAAoB,IAAI,KAAK,EAAiB,CAAG,IAAI,KAAK,EAAgB,UAAU,EACtF,MAAM,EAAmB,EAAQ,EAAwB,CACvD,UAAW,CACT,MAAO,CACL,GAAI,EAAgB,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