@transcend-io/cli 8.37.1 → 8.37.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{api-keys-Bb2BbZQe.cjs → api-keys-Bvt2HbSv.cjs} +2 -2
- package/dist/{api-keys-Bb2BbZQe.cjs.map → api-keys-Bvt2HbSv.cjs.map} +1 -1
- package/dist/{app-C9jD-f87.cjs → app-CdWFyBYu.cjs} +18 -18
- package/dist/{app-C9jD-f87.cjs.map → app-CdWFyBYu.cjs.map} +1 -1
- package/dist/bin/bash-complete.cjs +1 -1
- package/dist/bin/cli.cjs +1 -1
- package/dist/bin/deprecated-command.cjs +1 -1
- package/dist/{code-scanning-4d0zlFxk.cjs → code-scanning-BZzwKEfY.cjs} +2 -2
- package/dist/{code-scanning-4d0zlFxk.cjs.map → code-scanning-BZzwKEfY.cjs.map} +1 -1
- package/dist/{command-XJ7XPQ04.cjs → command-DNcjQs8y.cjs} +2 -2
- package/dist/{command-XJ7XPQ04.cjs.map → command-DNcjQs8y.cjs.map} +1 -1
- package/dist/{consent-manager-CCyvzvY5.cjs → consent-manager-oip5m3XC.cjs} +2 -2
- package/dist/{consent-manager-CCyvzvY5.cjs.map → consent-manager-oip5m3XC.cjs.map} +1 -1
- package/dist/{constants-wkuhlP8d.cjs → constants-K6pQQtc7.cjs} +2 -2
- package/dist/{constants-wkuhlP8d.cjs.map → constants-K6pQQtc7.cjs.map} +1 -1
- package/dist/{cron-DfEGA7Rf.cjs → cron-lijiEqFA.cjs} +2 -2
- package/dist/{cron-DfEGA7Rf.cjs.map → cron-lijiEqFA.cjs.map} +1 -1
- package/dist/{data-inventory-C1eqZk1M.cjs → data-inventory-BKAQGjFN.cjs} +2 -2
- package/dist/{data-inventory-C1eqZk1M.cjs.map → data-inventory-BKAQGjFN.cjs.map} +1 -1
- package/dist/{dataFlowsToDataSilos-DXlFFHMV.cjs → dataFlowsToDataSilos-CnvG2jqy.cjs} +2 -2
- package/dist/{dataFlowsToDataSilos-DXlFFHMV.cjs.map → dataFlowsToDataSilos-CnvG2jqy.cjs.map} +1 -1
- package/dist/{impl-BOEoFzcB.cjs → impl-1-1sg4WF.cjs} +2 -2
- package/dist/{impl-BOEoFzcB.cjs.map → impl-1-1sg4WF.cjs.map} +1 -1
- package/dist/{impl-DwWoAbT_.cjs → impl-57HOh2c3.cjs} +2 -2
- package/dist/{impl-DwWoAbT_.cjs.map → impl-57HOh2c3.cjs.map} +1 -1
- package/dist/{impl-B_2CdctV.cjs → impl-58WnFNmn.cjs} +2 -2
- package/dist/{impl-B_2CdctV.cjs.map → impl-58WnFNmn.cjs.map} +1 -1
- package/dist/{impl-BOEjB3fo.cjs → impl-B4OVz7FC.cjs} +2 -2
- package/dist/{impl-BOEjB3fo.cjs.map → impl-B4OVz7FC.cjs.map} +1 -1
- package/dist/{impl-BfC5CRRX.cjs → impl-BFRrE04X.cjs} +2 -2
- package/dist/{impl-BfC5CRRX.cjs.map → impl-BFRrE04X.cjs.map} +1 -1
- package/dist/{impl-DqfyWyoV.cjs → impl-BSS_avMv.cjs} +2 -2
- package/dist/{impl-DqfyWyoV.cjs.map → impl-BSS_avMv.cjs.map} +1 -1
- package/dist/{impl-DNRsFfbU.cjs → impl-BVmw0mE4.cjs} +2 -2
- package/dist/{impl-DNRsFfbU.cjs.map → impl-BVmw0mE4.cjs.map} +1 -1
- package/dist/{impl-C6JjApDI.cjs → impl-Bf_hLViY.cjs} +2 -2
- package/dist/{impl-C6JjApDI.cjs.map → impl-Bf_hLViY.cjs.map} +1 -1
- package/dist/{impl-DxhyqjcY.cjs → impl-BkEg-Nm6.cjs} +2 -2
- package/dist/{impl-DxhyqjcY.cjs.map → impl-BkEg-Nm6.cjs.map} +1 -1
- package/dist/{impl-7LAuV25D.cjs → impl-Bmln6D88.cjs} +2 -2
- package/dist/{impl-7LAuV25D.cjs.map → impl-Bmln6D88.cjs.map} +1 -1
- package/dist/{impl-DhbV3bBZ.cjs → impl-BqIqzp40.cjs} +2 -2
- package/dist/{impl-DhbV3bBZ.cjs.map → impl-BqIqzp40.cjs.map} +1 -1
- package/dist/{impl-u8o3S8w2.cjs → impl-BszlCtcR.cjs} +2 -2
- package/dist/{impl-u8o3S8w2.cjs.map → impl-BszlCtcR.cjs.map} +1 -1
- package/dist/{impl-DetfC7CT.cjs → impl-BtuKKdl3.cjs} +2 -2
- package/dist/{impl-DetfC7CT.cjs.map → impl-BtuKKdl3.cjs.map} +1 -1
- package/dist/{impl-_QrpPIPw.cjs → impl-C2e4xVvX.cjs} +2 -2
- package/dist/{impl-_QrpPIPw.cjs.map → impl-C2e4xVvX.cjs.map} +1 -1
- package/dist/{impl-CyzGdwB1.cjs → impl-C65nk0G8.cjs} +2 -2
- package/dist/{impl-CyzGdwB1.cjs.map → impl-C65nk0G8.cjs.map} +1 -1
- package/dist/{impl-DjP2MJNK.cjs → impl-CCdxbRmg.cjs} +2 -2
- package/dist/{impl-DjP2MJNK.cjs.map → impl-CCdxbRmg.cjs.map} +1 -1
- package/dist/{impl-DmXYpp-M.cjs → impl-CMwmo2vR.cjs} +2 -2
- package/dist/{impl-DmXYpp-M.cjs.map → impl-CMwmo2vR.cjs.map} +1 -1
- package/dist/{impl-CR-wyJSg.cjs → impl-Cb64HwGx.cjs} +2 -2
- package/dist/{impl-CR-wyJSg.cjs.map → impl-Cb64HwGx.cjs.map} +1 -1
- package/dist/{impl-CV3axMeT.cjs → impl-CdfA8kxo.cjs} +2 -2
- package/dist/{impl-CV3axMeT.cjs.map → impl-CdfA8kxo.cjs.map} +1 -1
- package/dist/{impl-C-aKX3zu.cjs → impl-CkfOZzpI.cjs} +2 -2
- package/dist/{impl-C-aKX3zu.cjs.map → impl-CkfOZzpI.cjs.map} +1 -1
- package/dist/{impl-CKYwKeLz.cjs → impl-ClujxTb8.cjs} +2 -2
- package/dist/{impl-CKYwKeLz.cjs.map → impl-ClujxTb8.cjs.map} +1 -1
- package/dist/{impl-B04CctrY.cjs → impl-D-IWtHQi.cjs} +2 -2
- package/dist/{impl-B04CctrY.cjs.map → impl-D-IWtHQi.cjs.map} +1 -1
- package/dist/{impl-BGoAnVJu.cjs → impl-D9-ZQmJB.cjs} +2 -2
- package/dist/{impl-BGoAnVJu.cjs.map → impl-D9-ZQmJB.cjs.map} +1 -1
- package/dist/{impl-CmEsmnYZ.cjs → impl-DGel0ZLe.cjs} +2 -2
- package/dist/{impl-CmEsmnYZ.cjs.map → impl-DGel0ZLe.cjs.map} +1 -1
- package/dist/{impl-DHuguAlW.cjs → impl-DL2j8g1C.cjs} +2 -2
- package/dist/{impl-DHuguAlW.cjs.map → impl-DL2j8g1C.cjs.map} +1 -1
- package/dist/{impl-LgUGDTQK.cjs → impl-DSNgFKP_.cjs} +2 -2
- package/dist/{impl-LgUGDTQK.cjs.map → impl-DSNgFKP_.cjs.map} +1 -1
- package/dist/{impl-Dzq0t6mX.cjs → impl-DU85U1jO.cjs} +2 -2
- package/dist/{impl-Dzq0t6mX.cjs.map → impl-DU85U1jO.cjs.map} +1 -1
- package/dist/{impl-C4q9xHFr.cjs → impl-DV5f54rm.cjs} +2 -2
- package/dist/{impl-C4q9xHFr.cjs.map → impl-DV5f54rm.cjs.map} +1 -1
- package/dist/{impl-CLcnbVfj.cjs → impl-DXKJH0AZ.cjs} +2 -2
- package/dist/{impl-CLcnbVfj.cjs.map → impl-DXKJH0AZ.cjs.map} +1 -1
- package/dist/{impl-6TmoWv0o.cjs → impl-DbxzDk8h.cjs} +2 -2
- package/dist/{impl-6TmoWv0o.cjs.map → impl-DbxzDk8h.cjs.map} +1 -1
- package/dist/{impl-XyWPUpvw.cjs → impl-DhnCAbU-.cjs} +2 -2
- package/dist/{impl-XyWPUpvw.cjs.map → impl-DhnCAbU-.cjs.map} +1 -1
- package/dist/{impl-kxwq3OMk.cjs → impl-Dj2fTDNO.cjs} +2 -2
- package/dist/{impl-kxwq3OMk.cjs.map → impl-Dj2fTDNO.cjs.map} +1 -1
- package/dist/{impl-Cp7-Tctr.cjs → impl-KAorCmlT.cjs} +2 -2
- package/dist/{impl-Cp7-Tctr.cjs.map → impl-KAorCmlT.cjs.map} +1 -1
- package/dist/{impl-CnRqR4kw.cjs → impl-LMp29vxd.cjs} +2 -2
- package/dist/{impl-CnRqR4kw.cjs.map → impl-LMp29vxd.cjs.map} +1 -1
- package/dist/{impl-DC_YquN8.cjs → impl-MrsSr72p.cjs} +2 -2
- package/dist/{impl-DC_YquN8.cjs.map → impl-MrsSr72p.cjs.map} +1 -1
- package/dist/{impl-CgKn47V9.cjs → impl-SZp3iTUp.cjs} +2 -2
- package/dist/{impl-CgKn47V9.cjs.map → impl-SZp3iTUp.cjs.map} +1 -1
- package/dist/{impl-DrJj-l3s.cjs → impl-W6jE_UV0.cjs} +2 -2
- package/dist/{impl-DrJj-l3s.cjs.map → impl-W6jE_UV0.cjs.map} +1 -1
- package/dist/{impl-Dp3-sA6b.cjs → impl-XwC7A99P.cjs} +2 -2
- package/dist/{impl-Dp3-sA6b.cjs.map → impl-XwC7A99P.cjs.map} +1 -1
- package/dist/{impl-DQ8rr7Fv.cjs → impl-ebVxRYAc.cjs} +2 -2
- package/dist/{impl-DQ8rr7Fv.cjs.map → impl-ebVxRYAc.cjs.map} +1 -1
- package/dist/{impl-CsKfLxov.cjs → impl-k61p_VQY.cjs} +2 -2
- package/dist/{impl-CsKfLxov.cjs.map → impl-k61p_VQY.cjs.map} +1 -1
- package/dist/{impl-CqadSQOh.cjs → impl-kMebV10f.cjs} +2 -2
- package/dist/{impl-CqadSQOh.cjs.map → impl-kMebV10f.cjs.map} +1 -1
- package/dist/{impl-Dvoj_snk.cjs → impl-oYFKp06U.cjs} +2 -2
- package/dist/{impl-Dvoj_snk.cjs.map → impl-oYFKp06U.cjs.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +9 -9
- package/dist/{manual-enrichment-CzTpv-mM.cjs → manual-enrichment-C6h9gjY1.cjs} +2 -2
- package/dist/{manual-enrichment-CzTpv-mM.cjs.map → manual-enrichment-C6h9gjY1.cjs.map} +1 -1
- package/dist/{pooling-DA_LwUEp.cjs → pooling-DLEGcLtt.cjs} +5 -5
- package/dist/pooling-DLEGcLtt.cjs.map +1 -0
- package/dist/{preference-management-aOhuZCuE.cjs → preference-management-B36PQuMK.cjs} +2 -2
- package/dist/{preference-management-aOhuZCuE.cjs.map → preference-management-B36PQuMK.cjs.map} +1 -1
- package/dist/{syncConfigurationToTranscend-DuTZKIG8.cjs → syncConfigurationToTranscend-DKliAJhK.cjs} +2 -2
- package/dist/{syncConfigurationToTranscend-DuTZKIG8.cjs.map → syncConfigurationToTranscend-DKliAJhK.cjs.map} +1 -1
- package/dist/{uploadConsents-C9Pv8Awr.cjs → uploadConsents-MtgCk8B0.cjs} +2 -2
- package/dist/{uploadConsents-C9Pv8Awr.cjs.map → uploadConsents-MtgCk8B0.cjs.map} +1 -1
- package/package.json +1 -1
- package/dist/pooling-DA_LwUEp.cjs.map +0 -1
package/dist/{preference-management-aOhuZCuE.cjs.map → preference-management-B36PQuMK.cjs.map}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preference-management-aOhuZCuE.cjs","names":["extractErrorMessage","sleepPromise","t","PreferenceQueryResponseItem","chunk","cliProgress","map","PreferenceTopicType","splitCsvToList","difference","uniq","difference","uniq","inquirerConfirmBoolean","groupBy","difference","uniq","mapSeries","PreferenceTopicType","splitCsvToList","PreferenceTopicType","PreferenceTopicType","readCsv","t","keyBy","t","PreferenceQueryResponseItem","PreferenceUpdateItem","PreferenceStoreIdentifier","parseAttributesFromString","PersistedState","buildTranscendGraphQLClient","createSombraGotInstance","fetchAllPurposes","fetchAllPreferenceTopics","cliProgress","chunk","map","startOfUtcDay","DAY_MS","FIVE_MIN_MS","addDaysUtc","cliProgress","clampPageSize","pmap","map","chunk","readCsv"],"sources":["../src/lib/preference-management/withPreferenceRetry.ts","../src/lib/preference-management/types.ts","../src/lib/preference-management/getPreferencesForIdentifiers.ts","../src/lib/preference-management/getPreferenceUpdatesFromRow.ts","../src/lib/preference-management/parsePreferenceTimestampsFromCsv.ts","../src/lib/preference-management/parsePreferenceIdentifiersFromCsv.ts","../src/lib/preference-management/parsePreferenceAndPurposeValuesFromCsv.ts","../src/lib/preference-management/checkIfPendingPreferenceUpdatesAreNoOp.ts","../src/lib/preference-management/checkIfPendingPreferenceUpdatesCauseConflict.ts","../src/lib/preference-management/parsePreferenceManagementCsv.ts","../src/lib/preference-management/codecs.ts","../src/lib/preference-management/uploadPreferenceManagementPreferencesInteractive.ts","../src/lib/preference-management/transformPreferenceRecordToCsv.ts","../src/lib/preference-management/iterateConsentPages.ts","../src/lib/preference-management/pickConsentChunkMode.ts","../src/lib/preference-management/getComparisonTimeForRecord.ts","../src/lib/preference-management/discoverConsentWindow.ts","../src/lib/preference-management/buildConsentChunks.ts","../src/lib/preference-management/fetchConsentPreferencesChunked.ts","../src/lib/preference-management/fetchConsentPreferences.ts","../src/lib/preference-management/bulkDeletePreferenceRecords.ts"],"sourcesContent":["import colors from 'colors';\nimport { logger } from '../../logger';\nimport { extractErrorMessage, sleepPromise } from '../helpers';\n\n/**\n * Transient network / platform errors that merit a retry.\n * Keep this list short and specific to avoid masking real failures.\n */\nexport const RETRY_PREFERENCE_MSGS: string[] = [\n 'ENOTFOUND',\n 'ECONNRESET',\n 'ETIMEDOUT',\n '502 Bad Gateway',\n '504 Gateway Time-out',\n '429',\n 'Rate limit exceeded',\n 'Task timed out after',\n 'unknown request error',\n].map((s) => s.toLowerCase());\n\n/**\n * Options for retrying preference operations.\n */\nexport type RetryOptions = {\n /** Max attempts including the first try (default 5) */\n maxAttempts?: number;\n /** Initial backoff in ms (default 250) */\n baseDelayMs?: number;\n /** Optional custom predicate to decide if an error is retryable */\n isRetryable?: (err: unknown, message: string) => boolean;\n /** Optional hook to log on each retry */\n onRetry?: (attempt: number, err: unknown, message: string) => void;\n};\n\n/**\n * Run an async function with standardized retry behavior for preference operations.\n * Exponential backoff with jitter; only retries on known-transient messages.\n *\n * @param name - Name of the operation (for logging)\n * @param fn - Function to run\n * @param options - Retry options\n * @returns Result of the function\n */\nexport async function withPreferenceRetry<T>(\n name: string,\n fn: () => Promise<T>,\n {\n maxAttempts = 5,\n baseDelayMs = 250,\n isRetryable = (_err, msg) =>\n RETRY_PREFERENCE_MSGS.some((m) => msg.toLowerCase().includes(m)),\n onRetry,\n }: RetryOptions = {},\n): Promise<T> {\n let attempt = 0;\n // eslint-disable-next-line no-constant-condition\n while (true) {\n attempt += 1;\n try {\n return await fn();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n const msg: string = extractErrorMessage(err);\n const willRetry = attempt < maxAttempts && isRetryable(err, msg);\n if (!willRetry) {\n throw new Error(`${name} failed after ${attempt} attempt(s): ${msg}`);\n }\n onRetry?.(attempt, err, msg);\n\n const backoff = baseDelayMs * 2 ** (attempt - 1);\n const jitter = Math.floor(Math.random() * baseDelayMs);\n const delay = backoff + jitter;\n logger.warn(\n colors.yellow(\n `[retry] attempt ${attempt}/${\n maxAttempts - 1\n }; backing off ${delay}ms: ${msg}`,\n ),\n );\n await sleepPromise(delay);\n }\n }\n}\n","import { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport * as t from 'io-ts';\n\n/**\n * New response codec for the query endpoint\n */\nexport const ConsentPreferenceResponse = t.intersection([\n t.type({\n nodes: t.array(PreferenceQueryResponseItem),\n }),\n t.partial({\n /** Cursor for next page (opaque) */\n cursor: t.string,\n }),\n]);\n\n/**\n * Type override\n */\nexport type ConsentPreferenceResponse = t.TypeOf<\n typeof ConsentPreferenceResponse\n>;\n\n/** Identifier filter (new shape) */\nexport type PreferenceIdentifier = {\n /** e.g., \"email\", \"phone\" */\n name: string;\n /** identifier value */\n value: string;\n};\n\n/** Filter shape for the new query endpoint */\nexport type PreferencesQueryFilter = {\n /** Identifiers to filter by */\n identifiers?: PreferenceIdentifier[];\n /** Consent collection time */\n timestampBefore?: string;\n /** Consent collection time */\n timestampAfter?: string;\n /** System updatedAt time */\n system?: {\n /** Updated before this time */\n updatedBefore?: string;\n /** Updated after this time */\n updatedAfter?: string;\n };\n};\n\n/** Which dimension we chunk on */\nexport type ChunkMode = 'timestamp' | 'updated';\n","import { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport type { Got } from 'got';\nimport colors from 'colors';\nimport cliProgress from 'cli-progress';\nimport { chunk } from 'lodash-es';\nimport { decodeCodec } from '@transcend-io/type-utils';\nimport { map } from '../bluebird';\nimport { logger } from '../../logger';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { ConsentPreferenceResponse } from './types';\n\n/**\n * Grab the current consent preference values for a list of identifiers\n *\n * @param sombra - Backend to make API call to\n * @param options - Options\n * @returns Plaintext context information\n */\nexport async function getPreferencesForIdentifiers(\n sombra: Got,\n {\n identifiers,\n partitionKey,\n skipLogging = false,\n concurrency = 40,\n }: {\n /** The list of identifiers to look up */\n identifiers: {\n /** The value of the identifier */\n value: string;\n }[];\n /** The partition key to look up */\n partitionKey: string;\n /** Whether to skip logging */\n skipLogging?: boolean;\n /** Concurrency for requests (default 40) */\n concurrency?: number;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const results: PreferenceQueryResponseItem[] = [];\n const groupedIdentifiers = chunk(identifiers, 100);\n\n // create a new progress bar instance and use shades_classic theme\n const t0 = new Date().getTime();\n const progressBar = new cliProgress.SingleBar(\n {},\n cliProgress.Presets.shades_classic,\n );\n if (!skipLogging) {\n progressBar.start(identifiers.length, 0);\n }\n\n let total = 0;\n await map(\n groupedIdentifiers,\n async (group) => {\n const rawResult = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partitionKey}/query`, {\n json: {\n filter: { identifiers: group },\n limit: group.length,\n },\n })\n .json(),\n {\n onRetry: (attempt, _err, msg) => {\n logger.warn(\n colors.yellow(\n `[RETRY] group size=${group.length} partition=${partitionKey} attempt=${attempt}: ${msg}`,\n ),\n );\n },\n },\n );\n\n const result = decodeCodec(ConsentPreferenceResponse, rawResult);\n results.push(...result.nodes);\n total += group.length;\n progressBar.update(total);\n },\n {\n concurrency,\n },\n );\n\n progressBar.stop();\n const t1 = new Date().getTime();\n const totalTime = t1 - t0;\n\n if (!skipLogging) {\n // Log completion time\n logger.info(\n colors.green(`Completed download in \"${totalTime / 1000}\" seconds.`),\n );\n }\n\n return results;\n}\n","import {\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PurposeRowMapping } from './codecs';\nimport { apply } from '@transcend-io/type-utils';\nimport { PreferenceTopic } from '../graphql';\nimport { splitCsvToList } from '../requests';\n\n/**\n * Parse an arbitrary object to the Transcend PUT /v1/preference update shape\n * by using a mapping of column names to purpose/preference slugs.\n *\n * `columnToPurposeName` looks like:\n * {\n * 'my_purpose': { purpose: 'Marketing', preference: null, valueMapping: { 'true': true, 'false': false } },\n * 'has_topic_1': { purpose: 'Marketing', preference: 'BooleanPreference1', valueMapping: { 'true': true, 'false': false } },\n * 'has_topic_2': { purpose: 'Marketing', preference: 'SingleSelectPreference', valueMapping: { 'Option 1': 'Value1', 'Option 2': 'Value2' } }\n * }\n *\n * `row` looks like:\n * {\n * 'my_purpose': 'true',\n * 'has_topic_1': 'true',\n * 'has_topic_2': 'Option 1'\n * }\n *\n * OMISSION RULE:\n * - If `valueMapping[row[columnName]]`\n * returns `undefined` or `null`, we **omit** that column entirely (do not set purpose enabled, do not push a preference).\n * - For MultiSelect, **each token** is treated independently: tokens that map to `undefined|null` are skipped;\n * if all tokens are skipped, nothing is pushed.\n * - We still validate **types** for mapped values (e.g., boolean must map to boolean, select must map to string, etc.).\n *\n * NOTE:\n * - Final shape must have `enabled` for every purpose touched (enforced by `apply` below). If you omit all top-level purpose mappings,\n * but emit preferences, this will throw at the end. This preserves the existing “enabled required” contract.\n *\n * @param options - Options\n * @returns The parsed row\n */\nexport function getPreferenceUpdatesFromRow({\n row,\n columnToPurposeName,\n purposeSlugs,\n preferenceTopics,\n}: {\n /** Row to parse */\n row: Record<string, string>;\n /** Mapping from column name to parser config */\n columnToPurposeName: Record<string, PurposeRowMapping>;\n /** The set of allowed purpose slugs */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n}): {\n [k in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n} {\n // Create a result object to store the parsed preferences\n const result: {\n [k in string]: Partial<PreferenceStorePurposeResponse>;\n } = {};\n\n // Iterate over each column and map to the purpose or preference\n Object.entries(columnToPurposeName).forEach(\n ([columnName, { purpose, preference, valueMapping }]) => {\n // Ensure the purpose is valid\n if (!purposeSlugs.includes(purpose)) {\n throw new Error(\n `Invalid purpose slug: ${purpose}, expected: ${purposeSlugs.join(\n ', ',\n )}`,\n );\n }\n\n // The raw value from the CSV row for this column\n const rawValue = row[columnName];\n\n // Check if parsing a preference or just the top level purpose\n if (preference) {\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === preference && x.purpose.trackingType === purpose,\n );\n if (!preferenceTopic) {\n const allowedTopics = preferenceTopics\n .filter((x) => x.purpose.trackingType === purpose)\n .map((x) => x.slug);\n throw new Error(\n `Invalid preference slug: ${preference} for purpose: ${purpose}. ` +\n `Allowed preference slugs for purpose are: ${allowedTopics.join(\n ',',\n )}`,\n );\n }\n\n // Ensure destination array\n if (!result[purpose]) {\n result[purpose] = {\n preferences: [],\n };\n }\n if (!result[purpose].preferences) {\n result[purpose].preferences = [];\n }\n\n // handle each type of preference\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean: {\n const mappedValue = valueMapping[rawValue];\n // Throw error on missing mapping\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Purposefully missing mapping\n if (mappedValue === null || mappedValue === undefined) {\n return;\n }\n\n // Ensure boolean\n if (typeof mappedValue !== 'boolean') {\n throw new Error(\n `Invalid value for boolean preference: ${preference}, expected boolean, got: ${rawValue}`,\n );\n }\n result[purpose].preferences!.push({\n topic: preference,\n choice: { booleanValue: mappedValue },\n });\n break;\n }\n\n case PreferenceTopicType.Select: {\n const mappedValue = valueMapping[rawValue];\n // Throw error on missing mapping\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Omit if null\n if (mappedValue === null || mappedValue === undefined) {\n return;\n }\n\n // Ensure string\n if (typeof mappedValue !== 'string') {\n throw new Error(\n `Invalid value for select preference: ${preference}, expected string, got: ${rawValue}`,\n );\n }\n const trimmed = mappedValue.trim() || null;\n\n if (\n trimmed &&\n !preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .includes(trimmed)\n ) {\n throw new Error(\n `Invalid value for select preference: ${preference}, expected one of: ` +\n `${preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .join(', ')}, got: ${rawValue}`,\n );\n }\n\n result[purpose].preferences!.push({\n topic: preference,\n choice: { selectValue: trimmed },\n });\n break;\n }\n\n case PreferenceTopicType.MultiSelect: {\n if (typeof rawValue !== 'string') {\n throw new Error(\n `Invalid value for multi select preference: ${preference}, expected string, got: ${rawValue}`,\n );\n }\n\n // IMPORTANT: Do NOT rely on valueMapping[rawValue] for CSV.\n // Split and map per token with the new rule.\n const selectValues = splitCsvToList(rawValue)\n .map((token) => {\n const tokenMapped = valueMapping[token];\n // Throw error on missing mapping\n if (tokenMapped === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for multi select token \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Omit if null\n if (tokenMapped === null || tokenMapped === undefined) {\n return null;\n }\n\n // Ensure string\n if (typeof tokenMapped !== 'string') {\n throw new Error(\n `Invalid value for multi select preference: ${preference}, ` +\n `expected one of: ${preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .join(', ')}, got: ${token}`,\n );\n }\n return tokenMapped;\n })\n .filter((x): x is string => x !== null)\n .sort((a, b) => a.localeCompare(b));\n\n // Only push if at least one mapped token survived\n if (selectValues.length > 0) {\n result[purpose].preferences!.push({\n topic: preference,\n choice: { selectValues },\n });\n }\n break;\n }\n\n default:\n throw new Error(`Unknown preference type: ${preferenceTopic.type}`);\n }\n } else {\n // Top-level purpose (no preference)\n const mappedValue = valueMapping[rawValue];\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=∅) ${JSON.stringify(\n row,\n )}`,\n );\n }\n if (mappedValue === null) {\n return; // Omit if null\n }\n\n if (!result[purpose]) {\n // Top-level purpose: set enabled strictly from mapped boolean\n result[purpose] = { enabled: mappedValue === true };\n } else {\n // Preserve preferences; update enabled\n result[purpose].enabled = mappedValue === true;\n }\n }\n },\n );\n\n // Ensure that enabled is provided for any purpose that appears.\n // (This preserves the prior contract and existing tests.)\n return apply(result, (x, purposeName) => {\n if (typeof x.enabled !== 'boolean') {\n throw new Error(\n `No mapping provided for purpose.enabled=true/false value: ${purposeName}`,\n );\n }\n return {\n ...x,\n enabled: x.enabled!,\n };\n });\n}\n","import { uniq, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\n\nexport const NONE_PREFERENCE_MAP = '[NONE]';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse timestamps from a CSV list of preferences\n *\n * When timestamp is requested, this script\n * ensures that all rows have a valid timestamp.\n *\n * Error is throw if timestamp is missing\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceTimestampsFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n): Promise<FileMetadataState> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for timestamp\n const remainingColumnsForTimestamp = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...Object.keys(currentState.columnToPurposeName),\n ]);\n\n // Determine the timestamp column to work off of\n if (!currentState.timestampColum) {\n const { timestampName } = await inquirer.prompt<{\n /** timestamp name */\n timestampName: string;\n }>([\n {\n name: 'timestampName',\n message:\n 'Choose the column that will be used as the timestamp of last preference update',\n type: 'list',\n default:\n remainingColumnsForTimestamp.find((col) =>\n col.toLowerCase().includes('date'),\n ) ||\n remainingColumnsForTimestamp.find((col) =>\n col.toLowerCase().includes('time'),\n ) ||\n remainingColumnsForTimestamp[0],\n choices: [...remainingColumnsForTimestamp, NONE_PREFERENCE_MAP],\n },\n ]);\n currentState.timestampColum = timestampName;\n }\n logger.info(\n colors.magenta(`Using timestamp column \"${currentState.timestampColum}\"`),\n );\n\n // Validate that all rows have valid timestamp\n if (currentState.timestampColum !== NONE_PREFERENCE_MAP) {\n const timestampColumnsMissing = preferences\n .map((pref, ind) => (pref[currentState.timestampColum!] ? null : [ind]))\n .filter((x): x is number[] => !!x)\n .flat();\n if (timestampColumnsMissing.length > 0) {\n throw new Error(\n `The timestamp column \"${\n currentState.timestampColum\n }\" is missing a value for the following rows: ${timestampColumnsMissing.join(\n '\\n',\n )}`,\n );\n }\n logger.info(\n colors.magenta(\n `The timestamp column \"${currentState.timestampColum}\" is present for all row`,\n ),\n );\n }\n return currentState;\n}\n/* eslint-enable no-param-reassign */\n","import { uniq, groupBy, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\nimport { inquirerConfirmBoolean } from '../helpers';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse identifiers from a CSV list of preferences\n *\n * Ensures that all rows have a valid identifier\n * and that all identifiers are unique.\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceIdentifiersFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n): Promise<{\n /** The updated state */\n currentState: FileMetadataState;\n /** The updated preferences */\n preferences: Record<string, string>[];\n}> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for identifier\n const remainingColumnsForIdentifier = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...Object.keys(currentState.columnToPurposeName),\n ]);\n\n // Determine the identifier column to work off of\n if (!currentState.identifierColumn) {\n const { identifierName } = await inquirer.prompt<{\n /** Identifier name */\n identifierName: string;\n }>([\n {\n name: 'identifierName',\n message:\n 'Choose the column that will be used as the identifier to upload consent preferences by',\n type: 'list',\n default:\n remainingColumnsForIdentifier.find((col) =>\n col.toLowerCase().includes('email'),\n ) || remainingColumnsForIdentifier[0],\n choices: remainingColumnsForIdentifier,\n },\n ]);\n currentState.identifierColumn = identifierName;\n }\n logger.info(\n colors.magenta(\n `Using identifier column \"${currentState.identifierColumn}\"`,\n ),\n );\n\n // Validate that the identifier column is present for all rows and unique\n const identifierColumnsMissing = preferences\n .map((pref, ind) => (pref[currentState.identifierColumn!] ? null : [ind]))\n .filter((x): x is number[] => !!x)\n .flat();\n if (identifierColumnsMissing.length > 0) {\n const msg = `The identifier column \"${\n currentState.identifierColumn\n }\" is missing a value for the following rows: ${identifierColumnsMissing.join(\n ', ',\n )}`;\n logger.warn(colors.yellow(msg));\n\n // Ask user if they would like to skip rows missing an identifier\n const skip = await inquirerConfirmBoolean({\n message: 'Would you like to skip rows missing an identifier?',\n });\n if (!skip) {\n throw new Error(msg);\n }\n\n // Filter out rows missing an identifier\n const previous = preferences.length;\n preferences = preferences.filter(\n (pref) => pref[currentState.identifierColumn!],\n );\n logger.info(\n colors.yellow(\n `Skipped ${previous - preferences.length} rows missing an identifier`,\n ),\n );\n }\n logger.info(\n colors.magenta(\n `The identifier column \"${currentState.identifierColumn}\" is present for all rows`,\n ),\n );\n\n // Validate that all identifiers are unique\n const rowsByUserId = groupBy(preferences, currentState.identifierColumn);\n const duplicateIdentifiers = Object.entries(rowsByUserId).filter(\n ([, rows]) => rows.length > 1,\n );\n if (duplicateIdentifiers.length > 0) {\n const msg = `The identifier column \"${\n currentState.identifierColumn\n }\" has duplicate values for the following rows: ${duplicateIdentifiers\n .slice(0, 10)\n .map(([userId, rows]) => `${userId} (${rows.length})`)\n .join('\\n')}`;\n logger.warn(colors.yellow(msg));\n\n // Ask user if they would like to take the most recent update\n // for each duplicate identifier\n const skip = await inquirerConfirmBoolean({\n message: 'Would you like to automatically take the latest update?',\n });\n if (!skip) {\n throw new Error(msg);\n }\n preferences = Object.entries(rowsByUserId)\n .map(([, rows]) => {\n const sorted = rows.sort(\n (a, b) =>\n new Date(b[currentState.timestampColum!]).getTime() -\n new Date(a[currentState.timestampColum!]).getTime(),\n );\n return sorted[0];\n })\n .filter((x) => x);\n }\n\n return { currentState, preferences };\n}\n/* eslint-enable no-param-reassign */\n","import { uniq, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\nimport { mapSeries } from '../bluebird';\nimport { PreferenceTopic } from '../graphql';\nimport { PreferenceTopicType } from '@transcend-io/privacy-types';\nimport { splitCsvToList } from '../requests';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse out the purpose.enabled and preference values from a CSV file\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @param options - Options\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceAndPurposeValuesFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n {\n purposeSlugs,\n preferenceTopics,\n forceTriggerWorkflows,\n }: {\n /** The purpose slugs that are allowed to be updated */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n /** Force workflow triggers */\n forceTriggerWorkflows: boolean;\n },\n): Promise<FileMetadataState> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for identifier\n const otherColumns = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...(currentState.timestampColum ? [currentState.timestampColum] : []),\n ]);\n if (otherColumns.length === 0) {\n if (forceTriggerWorkflows) {\n return currentState;\n }\n throw new Error('No other columns to process');\n }\n\n // The purpose and preferences to map to\n const purposeNames = [\n ...purposeSlugs,\n ...preferenceTopics.map((x) => `${x.purpose.trackingType}->${x.slug}`),\n ];\n\n // Ensure all columns are accounted for\n await mapSeries(otherColumns, async (col) => {\n // Determine the unique values to map in this column\n const uniqueValues = uniq(preferences.map((x) => x[col]));\n\n // Map the column to a purpose\n let purposeMapping = currentState.columnToPurposeName[col];\n if (purposeMapping) {\n logger.info(\n colors.magenta(\n `Column \"${col}\" is associated with purpose \"${purposeMapping.purpose}\"`,\n ),\n );\n } else {\n const { purposeName } = await inquirer.prompt<{\n /** purpose name */\n purposeName: string;\n }>([\n {\n name: 'purposeName',\n message: `Choose the purpose that column ${col} is associated with`,\n type: 'list',\n default: purposeNames.find((x) => x.startsWith(purposeSlugs[0])),\n choices: purposeNames,\n },\n ]);\n const [purposeSlug, preferenceSlug] = purposeName.split('->');\n purposeMapping = {\n purpose: purposeSlug,\n preference: preferenceSlug || null,\n valueMapping: {},\n };\n }\n\n // map each value to the purpose value\n await mapSeries(uniqueValues, async (value) => {\n if (purposeMapping.valueMapping[value] !== undefined) {\n logger.info(\n colors.magenta(\n `Value \"${value}\" is associated with purpose value \"${purposeMapping.valueMapping[value]}\"`,\n ),\n );\n return;\n }\n // if preference is null, this column is just for the purpose\n if (purposeMapping.preference === null) {\n const { purposeValue } = await inquirer.prompt<{\n /** purpose value */\n purposeValue: boolean;\n }>([\n {\n name: 'purposeValue',\n message: `Choose the purpose value for value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'confirm',\n default: value !== 'false',\n },\n ]);\n purposeMapping.valueMapping[value] = purposeValue;\n }\n\n // if preference is not null, this column is for a specific preference\n if (purposeMapping.preference !== null) {\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === purposeMapping.preference,\n );\n if (!preferenceTopic) {\n logger.error(\n colors.red(\n `Preference topic \"${purposeMapping.preference}\" not found`,\n ),\n );\n return;\n }\n const preferenceOptions = preferenceTopic.preferenceOptionValues.map(\n ({ slug }) => slug,\n );\n\n if (preferenceTopic.type === PreferenceTopicType.Boolean) {\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n message:\n // eslint-disable-next-line max-len\n `Choose the preference value for \"${preferenceTopic.slug}\" value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'confirm',\n default: value !== 'false',\n },\n ]);\n purposeMapping.valueMapping[value] = preferenceValue;\n return;\n }\n\n if (preferenceTopic.type === PreferenceTopicType.Select) {\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n // eslint-disable-next-line max-len\n message: `Choose the preference value for \"${preferenceTopic.slug}\" value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'list',\n choices: preferenceOptions,\n default: preferenceOptions.find((x) => x === value),\n },\n ]);\n purposeMapping.valueMapping[value] = preferenceValue;\n return;\n }\n\n if (preferenceTopic.type === PreferenceTopicType.MultiSelect) {\n const parsedValues = splitCsvToList(value);\n // need to do this serially\n await mapSeries(parsedValues, async (parsedValue) => {\n // if we already have a value, skip re-processing it again\n if (purposeMapping.valueMapping[parsedValue] !== undefined) {\n return;\n }\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n // eslint-disable-next-line max-len\n message: `Choose the preference value for \"${preferenceTopic.slug}\" value \"${parsedValue}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'list',\n choices: preferenceOptions,\n default: preferenceOptions.find((x) => x === parsedValue),\n },\n ]);\n purposeMapping.valueMapping[parsedValue] = preferenceValue;\n });\n return;\n }\n\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n });\n\n currentState.columnToPurposeName[col] = purposeMapping;\n });\n\n return currentState;\n}\n/* eslint-enable no-param-reassign */\n","import {\n PreferenceQueryResponseItem,\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PreferenceTopic } from '../graphql';\n\n/**\n * Check if the pending set of updates are exactly the same as the current consent record.\n *\n * @param options - Options\n * @returns Whether the pending updates already exist in the preference store\n */\nexport function checkIfPendingPreferenceUpdatesAreNoOp({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n}: {\n /** The current consent record */\n currentConsentRecord: PreferenceQueryResponseItem;\n /** The pending updates */\n pendingUpdates: {\n [purposeName in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n };\n /** The preference topic configurations */\n preferenceTopics: PreferenceTopic[];\n}): boolean {\n // Check each update\n return Object.entries(pendingUpdates).every(\n ([purposeName, { preferences = [], enabled }]) => {\n // Ensure the purpose exists\n const currentPurpose = currentConsentRecord.purposes.find(\n (existingPurpose) => existingPurpose.purpose === purposeName,\n );\n\n // Ensure purpose.enabled is in sync\n // Also false if the purpose does not exist\n const enabledIsInSync =\n !!currentPurpose && currentPurpose.enabled === enabled;\n if (!enabledIsInSync) {\n return false;\n }\n\n // Compare the preferences are in sync\n return preferences.every(\n ({ topic, choice }) =>\n // ensure preferences exist on record\n currentPurpose.preferences &&\n currentPurpose.preferences.find((existingPreference) => {\n // find matching topic\n if (existingPreference.topic !== topic) {\n return false;\n }\n\n // Determine type of preference topic\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === topic && x.purpose.trackingType === purposeName,\n );\n if (!preferenceTopic) {\n throw new Error(`Could not find preference topic for ${topic}`);\n }\n\n // Handle comparison based on type\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean:\n return (\n existingPreference.choice.booleanValue === choice.booleanValue\n );\n case PreferenceTopicType.Select:\n return (\n existingPreference.choice.selectValue === choice.selectValue\n );\n case PreferenceTopicType.MultiSelect:\n // eslint-disable-next-line no-case-declarations\n const sortedCurrentValues = (\n existingPreference.choice.selectValues || []\n ).sort();\n // eslint-disable-next-line no-case-declarations\n const sortedNewValues = (choice.selectValues || []).sort();\n return (\n sortedCurrentValues.length === sortedNewValues.length &&\n sortedCurrentValues.every((x, i) => x === sortedNewValues[i])\n );\n default:\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n }),\n );\n },\n );\n}\n","import {\n PreferenceQueryResponseItem,\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PreferenceTopic } from '../graphql';\nimport { logger } from '../../logger';\n\n/**\n * Check if the pending set of updates will result in a change of\n * value to an existing purpose or preference in the preference store.\n *\n * @param options - Options\n * @returns True if conflict, false if no conflict and just adding new data for first time\n */\nexport function checkIfPendingPreferenceUpdatesCauseConflict({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n log,\n}: {\n /** The current consent record */\n currentConsentRecord: PreferenceQueryResponseItem;\n /** The pending updates */\n pendingUpdates: {\n [purposeName in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n };\n /** The preference topic configurations */\n preferenceTopics: PreferenceTopic[];\n /** Whether to log the conflict */\n log?: boolean;\n}): boolean {\n // Check if any update has conflict\n return !!Object.entries(pendingUpdates).find(\n ([purposeName, { preferences = [], enabled }]) => {\n // Ensure the purpose exists\n const currentPurpose = currentConsentRecord.purposes.find(\n (existingPurpose) => existingPurpose.purpose === purposeName,\n );\n\n // If no purpose exists, then it is not a conflict\n if (!currentPurpose) {\n if (log) {\n logger.warn(\n `No existing purpose found for ${purposeName} in consent record for ${currentConsentRecord.userId}.`,\n );\n }\n return false;\n }\n\n // If purpose.enabled value is off, this is a conflict\n if (currentPurpose.enabled !== enabled) {\n if (log) {\n logger.warn(\n `Purpose ${purposeName} enabled value conflict for user ${currentConsentRecord.userId}. ` +\n `Pending Value: ${enabled}, Current Value: ${currentPurpose.enabled}`,\n );\n }\n return true;\n }\n\n // Check if any preferences are out of sync\n return !!preferences.find(({ topic, choice }) => {\n // find matching topic\n const currentPreference = (currentPurpose.preferences || []).find(\n (existingPreference) => existingPreference.topic === topic,\n );\n\n // if no topic exists, no conflict\n if (!currentPreference) {\n if (log) {\n logger.warn(\n `No existing preference found for topic ${topic} in purpose ` +\n `${purposeName} for user ${currentConsentRecord.userId}.`,\n );\n }\n return false;\n }\n\n // Determine type of preference topic\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === topic && x.purpose.trackingType === purposeName,\n );\n if (!preferenceTopic) {\n throw new Error(`Could not find preference topic for ${topic}`);\n }\n\n // Handle comparison based on type\n let boolMatch: boolean;\n let selectMatch: boolean;\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean:\n boolMatch =\n currentPreference.choice.booleanValue !== choice.booleanValue;\n if (log) {\n logger.warn(\n `Preference topic ${topic} boolean value conflict for user ` +\n `${currentConsentRecord.userId}. Expected: ${choice.booleanValue}, ` +\n `Found: ${currentPreference.choice.booleanValue}`,\n );\n }\n return boolMatch;\n case PreferenceTopicType.Select:\n selectMatch =\n currentPreference.choice.selectValue !== choice.selectValue;\n if (log) {\n logger.warn(\n `Preference topic ${topic} select value conflict for user ` +\n `${currentConsentRecord.userId}. Expected: ${choice.selectValue}, ` +\n `Found: ${currentPreference.choice.selectValue}`,\n );\n }\n return selectMatch;\n case PreferenceTopicType.MultiSelect:\n // eslint-disable-next-line no-case-declarations\n const sortedCurrentValues = (\n currentPreference.choice.selectValues || []\n ).sort();\n // eslint-disable-next-line no-case-declarations\n const sortedNewValues = (choice.selectValues || []).sort();\n selectMatch =\n sortedCurrentValues.length !== sortedNewValues.length ||\n !sortedCurrentValues.every((x, i) => x === sortedNewValues[i]);\n if (log) {\n logger.warn(\n `Preference topic ${topic} multi-select value conflict for user ` +\n `${\n currentConsentRecord.userId\n }. Expected: ${sortedNewValues.join(\n ', ',\n )}, Found: ${sortedCurrentValues.join(', ')}`,\n );\n }\n return selectMatch;\n default:\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n });\n },\n );\n}\n","import { PersistedState } from '@transcend-io/persisted-state';\nimport type { Got } from 'got';\nimport { keyBy } from 'lodash-es';\nimport * as t from 'io-ts';\nimport colors from 'colors';\nimport { FileMetadataState, PreferenceState } from './codecs';\nimport { logger } from '../../logger';\nimport { readCsv } from '../requests';\nimport { getPreferencesForIdentifiers } from './getPreferencesForIdentifiers';\nimport { PreferenceTopic } from '../graphql';\nimport { getPreferenceUpdatesFromRow } from './getPreferenceUpdatesFromRow';\nimport { parsePreferenceTimestampsFromCsv } from './parsePreferenceTimestampsFromCsv';\nimport { parsePreferenceIdentifiersFromCsv } from './parsePreferenceIdentifiersFromCsv';\nimport { parsePreferenceAndPurposeValuesFromCsv } from './parsePreferenceAndPurposeValuesFromCsv';\nimport { checkIfPendingPreferenceUpdatesAreNoOp } from './checkIfPendingPreferenceUpdatesAreNoOp';\nimport { checkIfPendingPreferenceUpdatesCauseConflict } from './checkIfPendingPreferenceUpdatesCauseConflict';\n\n/**\n * Parse a file into the cache\n *\n *\n * @param options - Options\n * @param cache - The cache to store the parsed file in\n * @returns The cache with the parsed file\n */\nexport async function parsePreferenceManagementCsvWithCache(\n {\n file,\n sombra,\n purposeSlugs,\n preferenceTopics,\n partitionKey,\n skipExistingRecordCheck,\n forceTriggerWorkflows,\n }: {\n /** File to parse */\n file: string;\n /** The purpose slugs that are allowed to be updated */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n /** Sombra got instance */\n sombra: Got;\n /** Partition key */\n partitionKey: string;\n /** Whether to skip the check for existing records. SHOULD ONLY BE USED FOR INITIAL UPLOAD */\n skipExistingRecordCheck: boolean;\n /** Whether to force workflow triggers */\n forceTriggerWorkflows: boolean;\n },\n cache: PersistedState<typeof PreferenceState>,\n): Promise<void> {\n // Start the timer\n const t0 = new Date().getTime();\n\n // Get the current metadata\n const fileMetadata = cache.getValue('fileMetadata');\n\n // Read in the file\n logger.info(colors.magenta(`Reading in file: \"${file}\"`));\n let preferences = readCsv(file, t.record(t.string, t.string));\n\n // start building the cache, can use previous cache as well\n let currentState: FileMetadataState = {\n columnToPurposeName: {},\n pendingSafeUpdates: {},\n pendingConflictUpdates: {},\n skippedUpdates: {},\n // Load in the last fetched time\n ...((fileMetadata[file] || {}) as Partial<FileMetadataState>),\n lastFetchedAt: new Date().toISOString(),\n };\n\n // Validate that all timestamps are present in the file\n currentState = await parsePreferenceTimestampsFromCsv(\n preferences,\n currentState,\n );\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Validate that all identifiers are present and unique\n const result = await parsePreferenceIdentifiersFromCsv(\n preferences,\n currentState,\n );\n currentState = result.currentState;\n preferences = result.preferences;\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Ensure all other columns are mapped to purpose and preference\n // slug values\n currentState = await parsePreferenceAndPurposeValuesFromCsv(\n preferences,\n currentState,\n {\n preferenceTopics,\n purposeSlugs,\n forceTriggerWorkflows,\n },\n );\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Grab existing preference store records\n const identifiers = preferences.map(\n (pref) => pref[currentState.identifierColumn!],\n );\n const existingConsentRecords = skipExistingRecordCheck\n ? []\n : await getPreferencesForIdentifiers(sombra, {\n identifiers: identifiers.map((x) => ({ value: x })),\n partitionKey,\n });\n const consentRecordByIdentifier = keyBy(existingConsentRecords, 'userId');\n\n // Clear out previous updates\n currentState.pendingConflictUpdates = {};\n currentState.pendingSafeUpdates = {};\n currentState.skippedUpdates = {};\n\n // Process each row\n preferences.forEach((pref) => {\n // Grab unique Id for the user\n const userId = pref[currentState.identifierColumn!];\n\n // determine updates for user\n const pendingUpdates = getPreferenceUpdatesFromRow({\n row: pref,\n columnToPurposeName: currentState.columnToPurposeName,\n preferenceTopics,\n purposeSlugs,\n });\n\n // Grab current state of the update\n const currentConsentRecord = consentRecordByIdentifier[userId];\n if (forceTriggerWorkflows && !currentConsentRecord) {\n throw new Error(\n `No existing consent record found for user with id: ${userId}.\n When 'forceTriggerWorkflows' is set all the user identifiers should contain a consent record`,\n );\n }\n // Check if the update can be skipped\n // this is the case if a record exists, and the purpose\n // and preference values are all in sync\n if (\n currentConsentRecord &&\n checkIfPendingPreferenceUpdatesAreNoOp({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n }) &&\n !forceTriggerWorkflows\n ) {\n currentState.skippedUpdates[userId] = pref;\n return;\n }\n\n // Determine if there are any conflicts\n if (\n currentConsentRecord &&\n checkIfPendingPreferenceUpdatesCauseConflict({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n })\n ) {\n currentState.pendingConflictUpdates[userId] = {\n row: pref,\n record: currentConsentRecord,\n };\n return;\n }\n\n // Add to pending updates\n currentState.pendingSafeUpdates[userId] = pref;\n });\n\n // Read in the file\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n const t1 = new Date().getTime();\n logger.info(\n colors.green(\n `Successfully pre-processed file: \"${file}\" in ${(t1 - t0) / 1000}s`,\n ),\n );\n}\n","import {\n PreferenceQueryResponseItem,\n PreferenceStoreIdentifier,\n PreferenceUpdateItem,\n} from '@transcend-io/privacy-types';\nimport * as t from 'io-ts';\n\nexport const PurposeRowMapping = t.type({\n /**\n * The slug or trackingType of the purpose to map to\n *\n * e.g. `Marketing`\n */\n purpose: t.string,\n /**\n * If the column maps to a preference instead of a purpose\n * this is the slug of the purpose.\n *\n * null value indicates that this column maps to the true/false\n * value of the purpose\n */\n preference: t.union([t.string, t.null]),\n /**\n * The mapping between each row value and purpose/preference value.\n *\n * e.g. for a boolean preference or purpose\n * {\n * 'true': true,\n * 'false': false,\n * '': true,\n * }\n *\n * or for a single or multi select preference\n * {\n * '': true,\n * 'value1': 'Value1',\n * 'value2': 'Value2',\n * }\n */\n valueMapping: t.record(\n t.string,\n t.union([t.string, t.boolean, t.null, t.undefined]),\n ),\n});\n\n/** Override type */\nexport type PurposeRowMapping = t.TypeOf<typeof PurposeRowMapping>;\n\n/**\n * Mapping of column name to purpose row mapping.\n * This is used to map each column in the CSV to the relevant purpose and preference definitions in\n * transcend.\n */\nexport const ColumnPurposeMap = t.record(t.string, PurposeRowMapping);\n\n/** Override type */\nexport type ColumnPurposeMap = t.TypeOf<typeof ColumnPurposeMap>;\n\nexport const IdentifierMetadataForPreference = t.type({\n /** The identifier name */\n name: t.string,\n /** Is unique on preference store */\n isUniqueOnPreferenceStore: t.boolean,\n});\n\n/** Override type */\nexport type IdentifierMetadataForPreference = t.TypeOf<\n typeof IdentifierMetadataForPreference\n>;\n\n/**\n * Mapping of identifier name to the column name in the CSV file.\n * This is used to map each identifier name to the column in the CSV file.\n */\nexport const ColumnIdentifierMap = t.record(\n t.string,\n IdentifierMetadataForPreference,\n);\n\n/** Override type */\nexport type ColumnIdentifierMap = t.TypeOf<typeof ColumnIdentifierMap>;\n\n/** Mapping of a CSV column to a metadata key in the preference store. */\nexport const MetadataMapping = t.type({\n /** The metadata key name in the preference store */\n key: t.string,\n});\n\n/** Override type */\nexport type MetadataMapping = t.TypeOf<typeof MetadataMapping>;\n\n/** Record mapping CSV column names to metadata keys. */\nexport const ColumnMetadataMap = t.record(t.string, MetadataMapping);\n\n/** Override type */\nexport type ColumnMetadataMap = t.TypeOf<typeof ColumnMetadataMap>;\n\nexport const FileMetadataState = t.intersection([\n t.type({\n /**\n * Definition of how to map each column in the CSV to\n * the relevant purpose and preference definitions in transcend\n */\n columnToPurposeName: t.record(t.string, PurposeRowMapping),\n /** Last time the file was last parsed at */\n lastFetchedAt: t.string,\n /**\n * Mapping of userId to the rows in the file that need to be uploaded\n * These uploads are overwriting non-existent preferences and are safe\n */\n pendingSafeUpdates: t.record(t.string, t.record(t.string, t.string)),\n /**\n * Mapping of userId to the rows in the file that need to be uploaded\n * these records have conflicts with existing consent preferences\n */\n pendingConflictUpdates: t.record(\n t.string,\n t.type({\n record: PreferenceQueryResponseItem,\n row: t.record(t.string, t.string),\n }),\n ),\n /**\n * Mapping of userId to the rows in the file that can be skipped because\n * their preferences are already in the store\n */\n skippedUpdates: t.record(t.string, t.record(t.string, t.string)),\n }),\n t.partial({\n /** Determine which column name in file maps to consent record identifier to upload on */\n identifierColumn: t.string,\n /** Determine which column name in file maps to the timestamp */\n timestampColum: t.string,\n }),\n]);\n\n/** Override type */\nexport type FileMetadataState = t.TypeOf<typeof FileMetadataState>;\n\n/**\n * This is the type of the receipts that are stored in the file\n * that is used to track the state of the upload process.\n * It is used to resume the upload process from where it left off.\n * It is used to persist the state of the upload process across multiple runs.\n */\nexport const PreferenceUpdateMap = t.record(\n t.string,\n // This can either be true to indicate the record is pending\n // or it can be an object showing the object\n // We only return a fixed number of results to avoid\n // making the JSON file too large\n t.union([t.boolean, PreferenceUpdateItem]),\n);\n\n/** Override type */\nexport type PreferenceUpdateMap = t.TypeOf<typeof PreferenceUpdateMap>;\n\n/**\n * This is the type of the pending updates that are safe to run without\n * conflicts with existing consent preferences.\n *\n * Key is primaryKey of the record in the file.\n * The value is the row in the file that is safe to upload.\n */\nexport const PendingSafePreferenceUpdates = t.record(\n t.string,\n // This can either be true to indicate the record is safe\n // or it can be an object showing the object\n // We only return a fixed number of results to avoid\n // making the JSON file too large\n t.union([t.boolean, t.record(t.string, t.string)]),\n);\n\n/** Override type */\nexport type PendingSafePreferenceUpdates = t.TypeOf<\n typeof PendingSafePreferenceUpdates\n>;\n\n/**\n * These are the updates that failed to be uploaded to the API.\n */\nexport const FailingPreferenceUpdates = t.record(\n t.string,\n t.type({\n /** Time upload ran at */\n uploadedAt: t.string,\n /** Attempts to upload that resulted in an error */\n error: t.string,\n /** The update body */\n update: PreferenceUpdateItem,\n }),\n);\n\n/** Override type */\nexport type FailingPreferenceUpdates = t.TypeOf<\n typeof FailingPreferenceUpdates\n>;\n\n/**\n * This is the type of the pending updates that are in conflict with existing consent preferences.\n *\n * Key is primaryKey of the record in the file.\n * The value is the row in the file that is pending upload.\n */\nexport const PendingWithConflictPreferenceUpdates = t.record(\n t.string,\n // We always return the conflicts for investigation\n t.type({\n /** Record to be inserted to transcend v1/preferences API */\n record: PreferenceQueryResponseItem,\n /** The row in the file that is pending upload */\n row: t.record(t.string, t.string),\n }),\n);\n\n/** Override type */\nexport type PendingWithConflictPreferenceUpdates = t.TypeOf<\n typeof PendingWithConflictPreferenceUpdates\n>;\n\n/**\n * The set of preference updates that are skipped\n * Key is primaryKey and value is the row in the CSV\n * that is skipped.\n *\n * This is usually because the preferences are already in the store\n * or there are duplicate rows in the CSV file that are identical.\n */\nexport const SkippedPreferenceUpdates = t.record(\n t.string,\n t.record(t.string, t.string),\n);\n\n/** Override type */\nexport type SkippedPreferenceUpdates = t.TypeOf<\n typeof SkippedPreferenceUpdates\n>;\n\n/** Persist this data between runs of the script */\nexport const PreferenceState = t.type({\n /**\n * Store a cache of previous files read in\n */\n fileMetadata: t.record(t.string, FileMetadataState),\n /**\n * The set of successful uploads to Transcend\n * Mapping from userId to the upload metadata\n */\n failingUpdates: t.record(\n t.string,\n t.type({\n /** Time upload ran at */\n uploadedAt: t.string,\n /** Attempts to upload that resulted in an error */\n error: t.string,\n /** The update body */\n update: PreferenceUpdateItem,\n }),\n ),\n /**\n * The set of pending uploads to Transcend\n * Mapping from userId to the upload metadata\n */\n pendingUpdates: t.record(t.string, PreferenceUpdateItem),\n});\n\n/** Override type */\nexport type PreferenceState = t.TypeOf<typeof PreferenceState>;\n\nexport const DeletePreferenceRecordsInput = t.type({\n /** Array of consent preference records to delete */\n records: t.array(\n t.type({\n /** The anchor identifier to locate the consent record */\n anchorIdentifier: PreferenceStoreIdentifier,\n /** The ISO 8601 timestamp of when the deletion is requested */\n timestamp: t.string,\n }),\n ),\n});\n\n/** Override type */\nexport type DeletePreferenceRecordsInput = t.TypeOf<\n typeof DeletePreferenceRecordsInput\n>;\n\nexport const DeletePreferenceRecordsResponse = t.intersection([\n t.type({\n /** Array of results for each preference record deletion */\n records: t.array(\n t.intersection([\n t.type({\n /** Whether the deletion was successful */\n success: t.boolean,\n }),\n t.partial({\n /** An error message if the deletion failed */\n errorMessage: t.string,\n }),\n ]),\n ),\n /** The list of failed deletions with their respective errors */\n failures: t.array(\n t.type({\n /** The index of the failed update in the original request */\n index: t.number,\n /** The error message associated with the failure */\n error: t.string,\n }),\n ),\n }),\n t.partial({\n /** Any general errors that occurred during the operation */\n errors: t.array(t.string),\n }),\n]);\n\n/** Override type */\nexport type DeletePreferenceRecordsResponse = t.TypeOf<\n typeof DeletePreferenceRecordsResponse\n>;\n\n/** CLI CSV Row for deleting preference records */\nexport const DeletePreferenceRecordCliCsvRow = t.type({\n /** The name of the identifier type (e.g., email, userId) */\n name: t.string,\n /** The value of the identifier */\n value: t.string,\n});\n\n/** Override type */\nexport type DeletePreferenceRecordCliCsvRow = t.TypeOf<\n typeof DeletePreferenceRecordCliCsvRow\n>;\n","import {\n buildTranscendGraphQLClient,\n createSombraGotInstance,\n fetchAllPurposes,\n fetchAllPreferenceTopics,\n} from '../graphql';\nimport colors from 'colors';\nimport { map } from '../bluebird';\nimport { chunk } from 'lodash-es';\nimport { logger } from '../../logger';\nimport cliProgress from 'cli-progress';\nimport { parseAttributesFromString } from '../requests';\nimport { PersistedState } from '@transcend-io/persisted-state';\nimport { parsePreferenceManagementCsvWithCache } from './parsePreferenceManagementCsv';\nimport { PreferenceState } from './codecs';\nimport { PreferenceUpdateItem } from '@transcend-io/privacy-types';\nimport { apply } from '@transcend-io/type-utils';\nimport { NONE_PREFERENCE_MAP } from './parsePreferenceTimestampsFromCsv';\nimport { getPreferenceUpdatesFromRow } from './getPreferenceUpdatesFromRow';\n\n/**\n * Upload a set of consent preferences\n *\n * @param options - Options\n */\nexport async function uploadPreferenceManagementPreferencesInteractive({\n auth,\n sombraAuth,\n receiptFilepath,\n file,\n partition,\n isSilent = true,\n dryRun = false,\n skipWorkflowTriggers = false,\n skipConflictUpdates = false,\n skipExistingRecordCheck = false,\n attributes = [],\n transcendUrl,\n forceTriggerWorkflows = false,\n}: {\n /** The Transcend API key */\n auth: string;\n /** Sombra API key authentication */\n sombraAuth?: string;\n /** Partition key */\n partition: string;\n /** File where to store receipt and continue from where left off */\n receiptFilepath: string;\n /** The file to process */\n file: string;\n /** API URL for Transcend backend */\n transcendUrl: string;\n /** Whether to do a dry run */\n dryRun?: boolean;\n /** Whether to upload as isSilent */\n isSilent?: boolean;\n /** Attributes string pre-parse. In format Key:Value */\n attributes?: string[];\n /** Skip workflow triggers */\n skipWorkflowTriggers?: boolean;\n /**\n * When true, only update preferences that do not conflict with existing\n * preferences. When false, update all preferences in CSV based on timestamp.\n */\n skipConflictUpdates?: boolean;\n /** Whether to skip the check for existing records. SHOULD ONLY BE USED FOR INITIAL UPLOAD */\n skipExistingRecordCheck?: boolean;\n /** Whether to force trigger workflows */\n forceTriggerWorkflows?: boolean;\n}): Promise<void> {\n // Parse out the extra attributes to apply to all requests uploaded\n const parsedAttributes = parseAttributesFromString(attributes);\n\n // Create a new state file to store the requests from this run\n const preferenceState = new PersistedState(receiptFilepath, PreferenceState, {\n fileMetadata: {},\n failingUpdates: {},\n pendingUpdates: {},\n });\n const failingRequests = preferenceState.getValue('failingUpdates');\n const pendingRequests = preferenceState.getValue('pendingUpdates');\n let fileMetadata = preferenceState.getValue('fileMetadata');\n\n logger.info(\n colors.magenta(\n 'Restored cache, there are: \\n' +\n `${\n Object.values(failingRequests).length\n } failing requests to be retried\\n` +\n `${\n Object.values(pendingRequests).length\n } pending requests to be processed\\n` +\n `The following files are stored in cache and will be used:\\n${Object.keys(\n fileMetadata,\n )\n .map((x) => x)\n .join('\\n')}\\n` +\n `The following file will be processed: ${file}\\n`,\n ),\n );\n\n // Create GraphQL client to connect to Transcend backend\n const client = buildTranscendGraphQLClient(transcendUrl, auth);\n\n const [sombra, purposes, preferenceTopics] = await Promise.all([\n // Create sombra instance to communicate with\n createSombraGotInstance(transcendUrl, auth, sombraAuth),\n // get all purposes and topics\n fetchAllPurposes(client),\n fetchAllPreferenceTopics(client),\n ]);\n\n // Process the file\n await parsePreferenceManagementCsvWithCache(\n {\n file,\n purposeSlugs: purposes.map((x) => x.trackingType),\n preferenceTopics,\n sombra,\n partitionKey: partition,\n skipExistingRecordCheck,\n forceTriggerWorkflows,\n },\n preferenceState,\n );\n\n // Construct the pending updates\n const pendingUpdates: Record<string, PreferenceUpdateItem> = {};\n fileMetadata = preferenceState.getValue('fileMetadata');\n const metadata = fileMetadata[file];\n\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.pendingSafeUpdates).length\n } safe updates in ${file}`,\n ),\n );\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.pendingConflictUpdates).length\n } conflict updates in ${file}`,\n ),\n );\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.skippedUpdates).length\n } skipped updates in ${file}`,\n ),\n );\n\n // Update either safe updates only or safe + conflict\n Object.entries({\n ...metadata.pendingSafeUpdates,\n ...(skipConflictUpdates\n ? {}\n : apply(metadata.pendingConflictUpdates, ({ row }) => row)),\n }).forEach(([userId, update]) => {\n // Determine timestamp\n const timestamp =\n metadata.timestampColum === NONE_PREFERENCE_MAP\n ? new Date()\n : new Date(update[metadata.timestampColum!]);\n\n // Determine updates\n const updates = getPreferenceUpdatesFromRow({\n row: update,\n columnToPurposeName: metadata.columnToPurposeName,\n preferenceTopics,\n purposeSlugs: purposes.map((x) => x.trackingType),\n });\n pendingUpdates[userId] = {\n userId,\n partition,\n timestamp: timestamp.toISOString(),\n purposes: Object.entries(updates).map(([purpose, value]) => ({\n ...value,\n purpose,\n workflowSettings: {\n attributes: parsedAttributes,\n isSilent,\n skipWorkflowTrigger: skipWorkflowTriggers,\n ...(forceTriggerWorkflows\n ? { forceTriggerWorkflow: forceTriggerWorkflows }\n : {}),\n },\n })),\n };\n });\n await preferenceState.setValue(pendingUpdates, 'pendingUpdates');\n await preferenceState.setValue({}, 'failingUpdates');\n\n // Exist early if dry run\n if (dryRun) {\n logger.info(\n colors.green(\n `Dry run complete, exiting. ${\n Object.values(pendingUpdates).length\n } pending updates. Check file: ${receiptFilepath}`,\n ),\n );\n return;\n }\n\n logger.info(\n colors.magenta(\n `Uploading ${\n Object.values(pendingUpdates).length\n } preferences to partition: ${partition}`,\n ),\n );\n\n // Time duration\n const t0 = new Date().getTime();\n\n // create a new progress bar instance and use shades_classic theme\n const progressBar = new cliProgress.SingleBar(\n {},\n cliProgress.Presets.shades_classic,\n );\n\n // Build a GraphQL client\n let total = 0;\n const updatesToRun = Object.entries(pendingUpdates);\n const chunkedUpdates = chunk(updatesToRun, skipWorkflowTriggers ? 100 : 10);\n progressBar.start(updatesToRun.length, 0);\n await map(\n chunkedUpdates,\n async (currentChunk) => {\n // Make the request\n try {\n await sombra\n .put('v1/preferences', {\n json: {\n records: currentChunk.map(([, update]) => update),\n skipWorkflowTriggers,\n },\n })\n .json();\n } catch (err) {\n try {\n const parsed = JSON.parse(err?.response?.body || '{}');\n if (parsed.error) {\n logger.error(colors.red(`Error: ${parsed.error}`));\n }\n } catch (e) {\n // continue\n }\n logger.error(\n colors.red(\n `Failed to upload ${\n currentChunk.length\n } user preferences to partition ${partition}: ${\n err?.response?.body || err?.message\n }`,\n ),\n );\n const failingUpdates = preferenceState.getValue('failingUpdates');\n currentChunk.forEach(([userId, update]) => {\n failingUpdates[userId] = {\n uploadedAt: new Date().toISOString(),\n update,\n error: err?.response?.body || err?.message || 'Unknown error',\n };\n });\n await preferenceState.setValue(failingUpdates, 'failingUpdates');\n }\n\n total += currentChunk.length;\n progressBar.update(total);\n },\n {\n concurrency: 40,\n },\n );\n\n progressBar.stop();\n const t1 = new Date().getTime();\n const totalTime = t1 - t0;\n logger.info(\n colors.green(\n `Successfully uploaded ${\n updatesToRun.length\n } user preferences to partition ${partition} in \"${\n totalTime / 1000\n }\" seconds!`,\n ),\n );\n}\n","import type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\n\n/**\n * Transforms the output of the consent preferences query into a CSV-friendly format.\n *\n * @param input - The input object containing consent preferences data.\n * @param exportIdentifiersWithDelimiter - Delimiter to use when combining multiple identifier values.\n * @returns A record representing the transformed CSV output.\n */\nexport function transformPreferenceRecordToCsv(\n {\n identifiers = [],\n purposes = [],\n metadata = [],\n consentManagement = {},\n system = {\n decryptionStatus: 'DECRYPTED',\n },\n // keep other top-level fields as-is (e.g., partition, timestamp, metadataTimestamp)\n ...topLevel\n }: PreferenceQueryResponseItem,\n exportIdentifiersWithDelimiter: string,\n): Record<string, unknown> {\n // Start with: all other top-level fields + spread system and consentManagement\n const out: Record<string, unknown> = {\n ...topLevel,\n ...system,\n ...consentManagement,\n };\n\n // ── identifiers: each identifier.name -> CSV of values\n if (Array.isArray(identifiers)) {\n const byName = new Map<string, Set<string>>();\n for (const { name, value } of identifiers) {\n if (!byName.has(name)) byName.set(name, new Set());\n if (value) byName.get(name)!.add(value);\n }\n for (const [name, set] of byName.entries()) {\n out[name] = Array.from(set).join(exportIdentifiersWithDelimiter);\n }\n }\n\n // ── metadata: serialize as JSON\n if (Array.isArray(metadata)) {\n out.metadata = JSON.stringify(\n metadata.reduce((acc, { key, value }) => {\n acc[key] = value;\n return acc;\n }, {} as Record<string, string>),\n );\n }\n\n // ── purposes:\n // - purpose.slug column => true/false (enabled)\n // - for each preference: purpose.slug_preference.slug => bool | single | CSV (multi)\n if (Array.isArray(purposes)) {\n for (const { purpose, preferences, enabled } of purposes) {\n out[purpose] = Boolean(enabled);\n\n // nested preferences\n if (Array.isArray(preferences)) {\n for (const { topic, choice } of preferences) {\n const col = `${purpose}_${topic}`;\n\n let val: unknown = null;\n\n if (typeof choice.booleanValue === 'boolean') {\n val = choice.booleanValue;\n } else if (choice.selectValue) {\n val = choice.selectValue;\n } else if (Array.isArray(choice.selectValues)) {\n const vs = choice.selectValues.filter((v) => v.length > 0);\n val = vs.join(',');\n } else {\n // no pref value present -> null\n val = null;\n }\n\n out[col] = val;\n }\n }\n }\n }\n\n return out;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { ConsentPreferenceResponse, PreferencesQueryFilter } from './types';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { logger } from '../../logger';\n\n/**\n * Async generator over pages for a given filter\n *\n * @param sombra - Sombra Got instance\n * @param partition - Partition key\n * @param filter - Query filter\n * @param pageSize - Number of items per page\n * @yields Pages of PreferenceQueryResponseItem\n */\nexport async function* iterateConsentPages(\n sombra: Got,\n partition: string,\n filter: PreferencesQueryFilter,\n pageSize: number,\n): AsyncGenerator<PreferenceQueryResponseItem[], void, void> {\n let cursor: string | undefined;\n\n while (true) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const body: any = { limit: pageSize };\n if (filter && Object.keys(filter).length) body.filter = filter;\n if (cursor) body.cursor = cursor;\n\n const resp = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partition}/query`, {\n json: body,\n })\n .json(),\n {\n onRetry: (attempt, _error, message) => {\n logger.warn(\n colors.yellow(\n `Retry attempt ${attempt} for iterateConsentPages due to error: ${message}`,\n ),\n );\n },\n },\n );\n\n const { nodes, cursor: nextCursor } = decodeCodec(\n ConsentPreferenceResponse,\n resp,\n );\n if (!nodes?.length) break;\n\n yield nodes;\n\n if (!nextCursor) break;\n cursor = nextCursor;\n }\n}\n","import type { ChunkMode, PreferencesQueryFilter } from './types';\n\n/**\n * Decide which dimension to chunk on: 'timestamp' if timestamps provided, otherwise 'updated'\n *\n * @param filterBy - Filter to examine\n * @returns Chosen chunk mode\n */\nexport function pickConsentChunkMode(\n filterBy: PreferencesQueryFilter,\n): ChunkMode {\n const hasTimestamp = !!filterBy.timestampAfter || !!filterBy.timestampBefore;\n return hasTimestamp ? 'timestamp' : 'updated';\n}\n","import type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport type { ChunkMode } from './types';\n\n/**\n * Get the comparison instant for sorting based on the chosen dimension.\n *\n * @param mode - Chunking mode\n * @param item - Preference item\n * @returns date\n */\nexport function getComparisonTimeForRecord(\n mode: ChunkMode,\n item: PreferenceQueryResponseItem,\n): Date {\n if (mode === 'timestamp') {\n return new Date(item.timestamp);\n }\n // mode === 'updated'\n return item.system?.updatedAt ? new Date(item.system.updatedAt) : new Date();\n}\n","/* eslint-disable max-lines */\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { ChunkMode, PreferencesQueryFilter } from './types';\nimport { startOfUtcDay, DAY_MS } from '../helpers';\nimport { iterateConsentPages } from './iterateConsentPages';\nimport { logger } from '../../logger';\nimport { pickConsentChunkMode } from './pickConsentChunkMode';\nimport { getComparisonTimeForRecord } from './getComparisonTimeForRecord';\n\n/**\n * Get after/before bounds from filter for the given mode\n *\n * @param mode - Chunking mode\n * @param filterBy - Filter to examine\n * @returns after/before dates\n */\nexport function getBoundsFromConsentFilter(\n mode: ChunkMode,\n filterBy: PreferencesQueryFilter,\n): {\n /** After date */\n after?: Date;\n /** Before date */\n before?: Date;\n} {\n if (mode === 'timestamp') {\n return {\n after: filterBy.timestampAfter\n ? new Date(filterBy.timestampAfter)\n : undefined,\n before: filterBy.timestampBefore\n ? new Date(filterBy.timestampBefore)\n : undefined,\n };\n }\n const u = filterBy.system ?? {};\n return {\n after: u.updatedAfter ? new Date(u.updatedAfter) : undefined,\n before: u.updatedBefore ? new Date(u.updatedBefore) : undefined,\n };\n}\n\n/**\n * Merge base filter with a \"before\" bound (without mixing dimensions).\n *\n * @param mode - Chunking mode\n * @param base - Base filter to augment\n * @param beforeISO - ISO timestamp to apply as the exclusive *Before bound for the chosen dimension\n * @returns New filter with the appropriate *Before constraint applied\n */\nfunction withBeforeBound(\n mode: ChunkMode,\n base: PreferencesQueryFilter,\n beforeISO?: string,\n): PreferencesQueryFilter {\n if (mode === 'timestamp') {\n return {\n ...base,\n timestampBefore: beforeISO ?? base.timestampBefore,\n };\n }\n return {\n ...base,\n system: {\n ...(base.system || {}),\n ...(beforeISO ? { updatedBefore: beforeISO } : {}),\n },\n // ensure we don't mix dimensions\n timestampAfter: undefined,\n timestampBefore: undefined,\n };\n}\n\n/**\n * Fetch a single record (or null) with the given filter.\n *\n * @param sombra - Got instance configured for Sombra API\n * @param partition - Preference Store partition id\n * @param filter - Query filter to use (page size internally forced to 1)\n * @returns The first record or null if none\n */\nasync function fetchOne(\n sombra: Got,\n partition: string,\n filter: PreferencesQueryFilter,\n): Promise<PreferenceQueryResponseItem | null> {\n logger.info(\n colors.magenta(\n `Single-record probe with filter: ${JSON.stringify(filter)}`,\n ),\n );\n const it = iterateConsentPages(sombra, partition, filter, /* pageSize */ 1);\n const res = await it.next();\n if (res.done || !res.value || res.value.length === 0) {\n logger.info(colors.yellow('Probe result: no record'));\n return null;\n }\n const item = res.value[0]!;\n logger.info(\n colors.green(\n `Probe result: found record at ${getComparisonTimeForRecord(\n pickConsentChunkMode(filter),\n item,\n ).toISOString()}`,\n ),\n );\n return item;\n}\n\n/**\n * Robust earliest-day search (UTC):\n * 1) Anchor at the newest record (single-record probe).\n * 2) Exponential “jump back” using seeds (1d, 7d, 30d) then doubling (60d, 120d, 240d, …)\n * to cross into an empty region and establish a lower empty bound.\n * 3) **Exponential forward-from-empty**: gallop forward from the empty bound toward the last-found\n * to land close to the frontier quickly.\n * 4) Tighten with a short binary search on time using single-record probes.\n *\n * (Implementation note: preserves the public signature and docs while improving efficiency.)\n *\n * @param sombra - Sombra\n * @param opts - Options\n * @returns Earliest day with data (UTC start-of-day)\n */\nexport async function findEarliestDayWithData(\n sombra: Got,\n opts: {\n /** Partition */\n partition: string;\n /** Chunking mode */\n mode: ChunkMode;\n /** Base filter */\n baseFilter: PreferencesQueryFilter;\n /** Optional safety cap in days to avoid unbounded lookback (default ~10 years) */\n maxLookbackDays?: number;\n },\n): Promise<Date> {\n const { partition, mode, baseFilter, maxLookbackDays = 3650 } = opts;\n\n // 1) Find newest record (anchors our backtracking).\n const newest = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter),\n );\n if (!newest) {\n logger.info(\n colors.yellow('No records found; defaulting earliest day to today.'),\n );\n return startOfUtcDay(new Date());\n }\n const newestInstant = getComparisonTimeForRecord(mode, newest);\n logger.info(colors.green(`Newest instant: ${newestInstant.toISOString()}`));\n\n // 2) Exponential jump back to find an empty region.\n const seedSteps = [1, 7, 30]; // days\n let stepDaysIdx = 0;\n let stepMs = seedSteps[0] * DAY_MS;\n\n let lastFoundInstant = newestInstant; // last instant we *could* find a record before\n let emptyBeforeInstant: Date | null = null; // first bound that yielded no results\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const probeBound =\n stepDaysIdx < seedSteps.length\n ? new Date(newestInstant.getTime() - seedSteps[stepDaysIdx] * DAY_MS)\n : new Date(newestInstant.getTime() - stepMs);\n\n // stop if we exceeded lookback cap\n const daysSince =\n (startOfUtcDay(new Date()).getTime() -\n startOfUtcDay(probeBound).getTime()) /\n DAY_MS;\n if (daysSince > maxLookbackDays) {\n logger.warn(\n colors.yellow(\n `Exponential jump exceeded maxLookbackDays=${maxLookbackDays}. Using current bounds.`,\n ),\n );\n emptyBeforeInstant = probeBound;\n break;\n }\n\n logger.info(\n colors.magenta(\n `Probing before=${probeBound.toISOString()} (jump step ${\n stepDaysIdx < seedSteps.length\n ? `${seedSteps[stepDaysIdx]}d`\n : `${Math.round(stepMs / DAY_MS)}d`\n })…`,\n ),\n );\n\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, probeBound.toISOString()),\n );\n\n if (hit) {\n lastFoundInstant = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(\n `Found older record at ${lastFoundInstant.toISOString()} — continue jumping back.`,\n ),\n );\n // advance step\n if (stepDaysIdx < seedSteps.length - 1) {\n stepDaysIdx += 1;\n stepMs = seedSteps[stepDaysIdx] * DAY_MS;\n } else if (stepDaysIdx === seedSteps.length - 1) {\n stepDaysIdx += 1; // switch to doubling mode\n stepMs = seedSteps[seedSteps.length - 1] * 2 * DAY_MS; // start at 60d\n } else {\n stepMs *= 2;\n }\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // crossed into an empty zone — remember this bound\n emptyBeforeInstant = probeBound;\n logger.info(\n colors.green(\n `No record before ${probeBound.toISOString()} — established empty lower bound.`,\n ),\n );\n break;\n }\n\n // Guard: if for some reason empty bound wasn't set, synthesize one “just before” lastFound.\n if (!emptyBeforeInstant) {\n emptyBeforeInstant = new Date(lastFoundInstant.getTime() - DAY_MS);\n }\n\n // 3) Exponential forward-from-empty toward the found frontier.\n // This “gallop” reduces the span dramatically before binary search.\n // We keep moving the empty bound forward with exponentially growing steps\n // until we get a hit; then we shrink onto that hit instant.\n let lo = emptyBeforeInstant; // known EMPTY (no data before this bound)\n let hi = lastFoundInstant; // known FOUND (there is data before this instant)\n let fwdStep = Math.max(\n DAY_MS,\n Math.floor((hi.getTime() - lo.getTime()) / 64),\n ); // start small-ish\n logger.info(\n colors.magenta(\n `Exponential forward-from-empty start: empty=${lo.toISOString()} found=${hi.toISOString()} step=${Math.round(\n fwdStep / DAY_MS,\n )}d`,\n ),\n );\n\n // Do a few gallop iterations (bounded so we don't loop forever if distribution is dense)\n for (let i = 0; i < 8; i += 1) {\n const probe = new Date(lo.getTime() + fwdStep);\n if (probe.getTime() >= hi.getTime()) break;\n\n logger.info(\n colors.magenta(`Forward gallop probe before=${probe.toISOString()}…`),\n );\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, probe.toISOString()),\n );\n\n if (hit) {\n // We crossed into data — tighten hi to the actual hit instant.\n hi = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(\n `Gallop hit at ${hi.toISOString()} — tightening found bound. Next step halves.`,\n ),\n );\n fwdStep = Math.max(DAY_MS, Math.floor(fwdStep / 2));\n } else {\n // Still empty up to probe — advance lo and double the step.\n lo.setTime(probe.getTime());\n logger.info(\n colors.yellow(\n `Gallop miss — advancing empty bound to ${lo.toISOString()}. Next step doubles.`,\n ),\n );\n fwdStep = Math.min(hi.getTime() - lo.getTime(), fwdStep * 2);\n if (fwdStep < DAY_MS) fwdStep = DAY_MS;\n }\n\n if (hi.getTime() - lo.getTime() <= DAY_MS) break;\n }\n\n // 4) Finish with a short binary search between [lo (empty), hi (found)].\n while (hi.getTime() - lo.getTime() > DAY_MS) {\n const mid = new Date(\n lo.getTime() + Math.floor((hi.getTime() - lo.getTime()) / 2),\n );\n logger.info(colors.magenta(`Binary probe before=${mid.toISOString()}…`));\n\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, mid.toISOString()),\n );\n\n if (hit) {\n const when = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(`Binary probe found record at ${when.toISOString()}.`),\n );\n hi = when; // there is data before mid -> earliest could be even earlier\n } else {\n logger.info(colors.yellow('Binary probe found no record.'));\n lo = mid; // still empty -> move low up\n }\n }\n\n const earliestDay = startOfUtcDay(hi);\n logger.info(\n colors.green(\n `Earliest day (UTC) resolved to ${earliestDay.toISOString()} (instant ≈ ${hi.toISOString()}).`,\n ),\n );\n return earliestDay;\n}\n\n/**\n * Find latest day with data using exponential growth forward from earliest (UTC day math).\n *\n * (Implementation note: per your request, we now fetch a single newest record to infer the latest day.)\n *\n * @param sombra - Sombra\n * @param opts - Options\n * @returns Latest day with data\n */\nexport async function findLatestDayWithData(\n sombra: Got,\n opts: {\n /** Partition */\n partition: string;\n /** Chunking mode */\n mode: ChunkMode;\n /** Base filter */\n baseFilter: PreferencesQueryFilter;\n /** Earliest date */\n earliest: Date; // inclusive day start\n },\n): Promise<Date> {\n const { partition, mode, baseFilter } = opts;\n\n logger.info(colors.magenta('Latest-day discovery: probing newest record…'));\n const latest = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter),\n );\n if (!latest) {\n logger.info(\n colors.yellow('No records found at all; defaulting latest day to today.'),\n );\n return startOfUtcDay(new Date());\n }\n\n const when = getComparisonTimeForRecord(mode, latest);\n logger.info(colors.green(`Newest record instant is ${when.toISOString()}.`));\n\n const latestDay = startOfUtcDay(when);\n logger.info(\n colors.green(\n `Latest day (UTC) resolved to ${latestDay.toISOString()} from instant ${when.toISOString()}.`,\n ),\n );\n\n return latestDay;\n}\n/* eslint-enable max-lines */\n","import { FIVE_MIN_MS } from '../helpers';\nimport type { ChunkMode, PreferencesQueryFilter } from './types';\n\n/**\n * Build chunk windows by splitting [lower, upperExclusive) into up to `maxChunks`\n * equal-sized ranges, with a minimum chunk span of 5 minutes. Boundaries are snapped\n * once at the start to the nearest 5-minute boundary for stability.\n *\n * Each returned window is already \"half-open\" for an *inclusive* backend:\n * we subtract 1ms from the exclusive end so adjacent chunks do not overlap.\n *\n * Example (timestamp mode): [10:00, 12:00) → { after=10:00:00.000Z, before=11:59:59.999Z }\n *\n * @param mode - 'timestamp' or 'updated'\n * @param lower - Lower bound (inclusive)\n * @param upperExclusive - Upper bound (exclusive)\n * @param maxChunks - Maximum number of chunks to create\n * @returns Array of chunked preference query filters\n */\nexport function buildConsentChunks(\n mode: ChunkMode,\n lower: Date,\n upperExclusive: Date,\n maxChunks = 5000,\n): Array<PreferencesQueryFilter> {\n const totalMs = Math.max(0, upperExclusive.getTime() - lower.getTime());\n if (totalMs === 0) return [];\n\n // Snap only the starting boundary to the nearest 5-minute boundary.\n // We avoid re-snapping every step to prevent cumulative drift.\n const seriesStart = new Date(\n Math.floor(lower.getTime() / FIVE_MIN_MS) * FIVE_MIN_MS,\n );\n\n // Compute base chunk size (ceil to ensure ≤ maxChunks), enforced ≥ 5m.\n const rawChunkMs = Math.ceil(totalMs / Math.max(1, maxChunks));\n const chunkMs = Math.max(FIVE_MIN_MS, rawChunkMs);\n\n // Number of chunks needed to cover [seriesStart, upperExclusive)\n const count = Math.ceil(\n (upperExclusive.getTime() - seriesStart.getTime()) / chunkMs,\n );\n\n const chunks: PreferencesQueryFilter[] = [];\n\n for (let i = 0; i < count; i += 1) {\n const startMs = seriesStart.getTime() + i * chunkMs;\n const endExclusiveMs = Math.min(\n upperExclusive.getTime(),\n startMs + chunkMs,\n );\n\n // Convert exclusive end to inclusive end for an inclusive backend: -1ms.\n const endInclusiveMs = endExclusiveMs - 1;\n\n // Guard: in degenerate cases (shouldn’t happen with the math above), clamp.\n const safeEndMs = Math.max(startMs, endInclusiveMs);\n\n const afterIso = new Date(startMs).toISOString();\n const beforeIso = new Date(safeEndMs).toISOString();\n\n if (mode === 'timestamp') {\n chunks.push({\n timestampAfter: afterIso,\n timestampBefore: beforeIso,\n });\n } else {\n chunks.push({\n system: {\n updatedAfter: afterIso,\n updatedBefore: beforeIso,\n },\n });\n }\n }\n\n return chunks;\n}\n","import { map as pmap } from '../bluebird';\nimport type { Got } from 'got';\nimport cliProgress from 'cli-progress';\nimport colors from 'colors';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\n\nimport { PreferencesQueryFilter, ChunkMode } from './types';\nimport {\n findEarliestDayWithData,\n findLatestDayWithData,\n getBoundsFromConsentFilter,\n} from './discoverConsentWindow';\nimport { buildConsentChunks } from './buildConsentChunks';\nimport { addDaysUtc, clampPageSize } from '../helpers';\nimport { iterateConsentPages } from './iterateConsentPages';\nimport { logger } from '../../logger';\nimport { pickConsentChunkMode } from './pickConsentChunkMode';\n\n/**\n * Merge baseFilter with a window filter, taking care not to mix timestamp/updated fields improperly.\n *\n * @param mode - The chunking mode\n * @param base - The base filter\n * @param window - The per-chunk window filter\n * @returns merged filter\n */\nfunction mergeFilter(\n mode: ChunkMode,\n base: PreferencesQueryFilter,\n window: PreferencesQueryFilter,\n): PreferencesQueryFilter {\n if (mode === 'timestamp') {\n return {\n ...base,\n timestampAfter: window.timestampAfter ?? base.timestampAfter,\n timestampBefore: window.timestampBefore ?? base.timestampBefore,\n // ensure we don't pass `system.*` when chunking by timestamp\n system: undefined,\n };\n }\n // mode === 'updated'\n return {\n ...base,\n system: {\n ...(base.system || {}),\n ...(window.system?.updatedAfter\n ? { updatedAfter: window.system.updatedAfter }\n : {}),\n ...(window.system?.updatedBefore\n ? { updatedBefore: window.system.updatedBefore }\n : {}),\n },\n // Ensure we don't mix dimensions\n timestampAfter: undefined,\n timestampBefore: undefined,\n };\n}\n\n/**\n * High-level chunked fetch with progress bar.\n *\n * If an `onItems` callback is provided, pages are streamed to the callback\n * as they are fetched (no accumulation in memory). If no callback is provided,\n * the function returns all items (legacy behavior).\n *\n * @param sombra - Got instance\n * @param options - Options\n * @returns preference items (only if onItems is not provided)\n */\nexport async function fetchConsentPreferencesChunked(\n sombra: Got,\n {\n partition,\n filterBy = {},\n limit = 50,\n windowConcurrency = 25,\n maxChunks = 5000,\n maxLookbackDays = 3650,\n onItems,\n }: {\n /** Partition */\n partition: string;\n /** Filter by preferences */\n filterBy?: PreferencesQueryFilter;\n /** Limit number of results (page size) */\n limit?: number;\n /** Window concurrency */\n windowConcurrency?: number;\n /** Max chunks */\n maxChunks?: number; // up to N chunks; min 1 hour per chunk\n /** Max lookback days for discovering bounds */\n maxLookbackDays?: number;\n /** Optional streaming sink; if provided, items are not accumulated */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n onItems?: (items: PreferenceQueryResponseItem[]) => Promise<any> | any;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const mode: ChunkMode = pickConsentChunkMode(filterBy);\n logger.info(\n colors.magenta(\n `Fetching consent preferences in chunks by ${\n mode === 'timestamp' ? 'timestamp' : 'system.updatedAt'\n }...`,\n ),\n );\n\n // Resolve / discover bounds (UTC)\n let { after, before } = getBoundsFromConsentFilter(mode, filterBy);\n logger.info(\n colors.magenta(\n `Initial bounds: after=${after?.toISOString() ?? 'undefined'} before=${\n before?.toISOString() ?? 'undefined'\n }`,\n ),\n );\n\n if (!after || !before) {\n if (!after) {\n logger.info(\n colors.magenta(\n `Discovering earliest day with data for partition ${partition}...`,\n ),\n );\n after = await findEarliestDayWithData(sombra, {\n partition,\n mode,\n baseFilter: filterBy,\n maxLookbackDays,\n });\n logger.info(\n colors.green(\n `Discovered earliest day with data: ${after.toISOString()}`,\n ),\n );\n }\n if (!before) {\n logger.info(\n colors.magenta(\n `Discovering latest day with data for partition ${partition}...`,\n ),\n );\n const latestDay = await findLatestDayWithData(sombra, {\n partition,\n mode,\n baseFilter: filterBy,\n earliest: after,\n });\n // Exclusive upper bound = latest day start + 1 day (UTC)\n before = addDaysUtc(latestDay, 1);\n logger.info(\n colors.green(\n `Discovered latest day with data: ${latestDay.toISOString()}`,\n ),\n );\n }\n }\n\n logger.info(\n colors.green(\n `Final bounds (UTC): after=${after.toISOString()} before=${before.toISOString()}`,\n ),\n );\n\n // Build up to `maxChunks` chunks, min 1 hour each\n const chunks = buildConsentChunks(mode, after, before, maxChunks);\n\n logger.info(\n colors.magenta(\n `Fetching consent preferences from partition ${partition} in ${chunks.length} chunks...`,\n ),\n );\n\n // Progress bar over chunks (unordered):\n // - value = completed chunks (out-of-order OK)\n // - payload fetched = total records fetched\n const bar = new cliProgress.SingleBar(\n {\n format:\n 'Downloading [{bar}] {percentage}% | chunks {value}/{total} | fetched {fetched}',\n },\n cliProgress.Presets.shades_classic,\n );\n\n let completed = 0; // finished chunks (out-of-order)\n let fetched = 0; // raw records counter\n\n bar.start(chunks.length, 0, { fetched });\n\n const t0 = Date.now();\n const pageSize = clampPageSize(limit);\n\n // If we are streaming, do not accumulate everything in memory.\n const out: PreferenceQueryResponseItem[] = [];\n\n await pmap(\n chunks.map((windowFilter, idx) => ({ windowFilter, idx })),\n async ({ windowFilter }) => {\n const filter = mergeFilter(mode, filterBy, windowFilter);\n\n // Stream this chunk page-by-page\n for await (const page of iterateConsentPages(\n sombra,\n partition,\n filter,\n pageSize,\n )) {\n fetched += page.length;\n bar.update(completed, { fetched });\n\n if (onItems) {\n await onItems(page);\n } else {\n out.push(...page);\n }\n }\n\n completed += 1;\n bar.update(completed, { fetched });\n },\n { concurrency: Math.max(1, windowConcurrency) },\n );\n\n bar.update(completed, { fetched });\n bar.stop();\n\n logger.info(\n colors.green(\n `Fetched ${fetched} consent preference records from partition ${partition} in ${\n (Date.now() - t0) / 1000\n }s.`,\n ),\n );\n\n return onItems ? [] : out;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { ConsentPreferenceResponse, PreferencesQueryFilter } from './types';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { logger } from '../../logger';\n\n/**\n * Fetch consent preferences for the managed consent database (new query endpoint)\n *\n * Uses POST /v1/preferences/{partition}/query with cursor pagination.\n *\n * If `onItems` is provided, this streams pages to the callback and does not\n * accumulate results in memory. If omitted, the function returns all items.\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Query options\n * @returns All nodes (only when onItems is not provided)\n */\nexport async function fetchConsentPreferences(\n sombra: Got,\n {\n partition,\n filterBy = {},\n limit = 50,\n onItems,\n }: {\n /** Partition key to fetch (moved to URL path on new endpoint) */\n partition: string;\n /** Query filter (wrapped under \"filter\" in request body) */\n filterBy?: PreferencesQueryFilter;\n /** Number of users per page (1–50 per API spec) */\n limit?: number;\n /** Optional streaming sink; if provided, pages are not accumulated */\n onItems?: (items: PreferenceQueryResponseItem[]) => Promise<void> | void;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const collected: PreferenceQueryResponseItem[] = [];\n\n // Cursor-based pagination per new endpoint\n let cursor: string | undefined;\n\n // Build the filter payload, omitting empty filter\n const hasFilter =\n filterBy &&\n (Object.keys(filterBy).length > 0 ||\n (filterBy.system && Object.keys(filterBy.system).length > 0));\n\n // Enforce API max (defensive; backend also validates)\n const pageSize = Math.max(1, Math.min(50, limit ?? 50));\n\n // Keep fetching until no cursor is returned\n // (The API returns an opaque cursor string for the next page)\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const body: {\n /** Filter by user identifiers (new shape) */\n filter?: PreferencesQueryFilter;\n /** Cursor for pagination */\n cursor?: string;\n /** Number of records per page */\n limit: number;\n } = { limit: pageSize };\n\n if (hasFilter) {\n body.filter = filterBy;\n }\n if (cursor) {\n body.cursor = cursor;\n }\n\n const response = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partition}/query`, {\n json: body,\n })\n .json(),\n {\n onRetry: (attempt, _error, message) => {\n logger.warn(\n colors.yellow(\n `Retry attempt ${attempt} for fetchConsentPreferences due to error: ${message}`,\n ),\n );\n },\n },\n );\n\n const { nodes, cursor: nextCursor } = decodeCodec(\n ConsentPreferenceResponse,\n response,\n );\n\n if (!nodes || nodes.length === 0) {\n break;\n }\n\n if (onItems) {\n await onItems(nodes);\n } else {\n collected.push(...nodes);\n }\n\n if (!nextCursor) {\n break;\n }\n cursor = nextCursor;\n }\n\n return onItems ? [] : collected;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { logger } from '../../logger';\nimport {\n DeletePreferenceRecordCliCsvRow,\n DeletePreferenceRecordsResponse,\n} from './codecs';\nimport { readCsv } from '../requests';\nimport { chunk } from 'lodash-es';\nimport { map } from '../bluebird';\nimport { withPreferenceRetry } from './withPreferenceRetry';\n\ninterface FailedResult extends DeletePreferenceRecordCliCsvRow {\n /** Error message describing the failure */\n error: string;\n}\n\ninterface DeletePreferenceRecordsRepositoryOptions {\n /** The partition to delete from */\n partition: string;\n /** Chunk of identifiers to delete */\n identifierChunk: DeletePreferenceRecordCliCsvRow[];\n /** the timestamp for the deletion operation */\n timestamp: Date;\n}\n\n/**\n * Options for deleting preference records\n */\ntype DeletePreferenceRecordsOptions = Omit<\n DeletePreferenceRecordsRepositoryOptions,\n 'identifierChunk'\n> & {\n /** The file path to read CSV rows from */\n filePath: string;\n /** Maximum items to include in each deletion chunk */\n maxItemsInChunk: number;\n /** Maximum concurrency for deletion requests */\n maxConcurrency: number;\n};\n\n/**\n *\n * Delete a chunk of preference records\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Options for deletion\n * @param options.partition - The partition to delete from\n * @param options.identifierChunk - Chunk of identifiers to delete\n * @param options.timestamp - The timestamp for the deletion operation\n * @returns List of failed deletions\n */\nasync function deletePreferenceRecordsRepository(\n sombra: Got,\n {\n partition,\n identifierChunk: chunk,\n timestamp,\n }: DeletePreferenceRecordsRepositoryOptions,\n): Promise<FailedResult[]> {\n try {\n const response = await withPreferenceRetry(\n 'Delete Preference Records',\n () =>\n sombra\n .post(`v1/preferences/${partition}/delete`, {\n json: {\n records: chunk.map((record) => ({\n anchorIdentifier: record,\n timestamp: timestamp.toISOString(),\n })),\n },\n })\n .json(),\n {\n maxAttempts: 3,\n onRetry: (attempt, _err, msg) => {\n logger.warn(\n colors.yellow(\n `Attempt ${attempt} to delete preference records failed: ${msg}`,\n ),\n );\n },\n },\n );\n const { failures } = decodeCodec(DeletePreferenceRecordsResponse, response);\n if (failures.length > 0) {\n return failures.map(({ index, error }) => ({\n ...chunk[index],\n error,\n }));\n }\n return [];\n } catch (err) {\n return chunk.map((record) => ({\n ...record,\n error: (err as Error).message,\n }));\n }\n}\n\n/**\n * Delete consent preferences for the managed consent database (delete endpoint)\n *\n * Uses POST /v1/preferences/{partition}/delete.\n *\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Query options\n * @returns All nodes (only when onItems is not provided)\n */\nexport async function bulkDeletePreferenceRecords(\n sombra: Got,\n {\n partition,\n filePath,\n timestamp,\n maxItemsInChunk,\n maxConcurrency,\n }: DeletePreferenceRecordsOptions,\n): Promise<FailedResult[]> {\n const anchorIdentifiers = readCsv(filePath, DeletePreferenceRecordCliCsvRow);\n const chunks = chunk(anchorIdentifiers, maxItemsInChunk);\n\n const failedResults = await map(\n chunks,\n async (identifierChunk) => {\n const failedResults = await deletePreferenceRecordsRepository(sombra, {\n partition,\n identifierChunk,\n timestamp,\n });\n return failedResults;\n },\n { concurrency: maxConcurrency },\n );\n return failedResults.flat();\n}\n"],"mappings":"mbAQA,MAAa,EAAkC,CAC7C,YACA,aACA,YACA,kBACA,uBACA,MACA,sBACA,uBACA,wBACD,CAAC,IAAK,GAAM,EAAE,aAAa,CAAC,CAyB7B,eAAsB,EACpB,EACA,EACA,CACE,cAAc,EACd,cAAc,IACd,eAAe,EAAM,IACnB,EAAsB,KAAM,GAAM,EAAI,aAAa,CAAC,SAAS,EAAE,CAAC,CAClE,WACgB,EAAE,CACR,CACZ,IAAI,EAAU,EAEd,OAAa,CACX,GAAW,EACX,GAAI,CACF,OAAO,MAAM,GAAI,OAEV,EAAU,CACjB,IAAM,EAAcA,EAAAA,EAAoB,EAAI,CAE5C,GAAI,EADc,EAAU,GAAe,EAAY,EAAK,EAAI,EAE9D,MAAU,MAAM,GAAG,EAAK,gBAAgB,EAAQ,eAAe,IAAM,CAEvE,IAAU,EAAS,EAAK,EAAI,CAI5B,IAAM,EAFU,EAAc,IAAM,EAAU,GAC/B,KAAK,MAAM,KAAK,QAAQ,CAAG,EAAY,CAEtD,EAAA,EAAO,KACL,EAAA,QAAO,OACL,mBAAmB,EAAQ,GACzB,EAAc,EACf,gBAAgB,EAAM,MAAM,IAC9B,CACF,CACD,MAAMC,EAAAA,EAAa,EAAM,GCzE/B,MAAa,EAA4BC,EAAE,aAAa,CACtDA,EAAE,KAAK,CACL,MAAOA,EAAE,MAAMC,EAAAA,4BAA4B,CAC5C,CAAC,CACFD,EAAE,QAAQ,CAER,OAAQA,EAAE,OACX,CAAC,CACH,CAAC,CCIF,eAAsB,EACpB,EACA,CACE,cACA,eACA,cAAc,GACd,cAAc,IAcwB,CACxC,IAAM,EAAyC,EAAE,CAC3C,EAAqBE,EAAAA,GAAM,EAAa,IAAI,CAG5C,EAAK,IAAI,MAAM,CAAC,SAAS,CACzB,EAAc,IAAIC,EAAAA,QAAY,U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CAAwC,IAC5D,CACF,EAEJ,CACF,CAC0E,CAO3E,OANI,EAAS,OAAS,EACb,EAAS,KAAK,CAAE,QAAO,YAAa,CACzC,GAAG,EAAM,GACT,QACD,EAAE,CAEE,EAAE,OACF,EAAK,CACZ,OAAO,EAAM,IAAK,IAAY,CAC5B,GAAG,EACH,MAAQ,EAAc,QACvB,EAAE,EAcP,eAAsB,GACpB,EACA,CACE,YACA,WACA,YACA,kBACA,kBAEuB,CAgBzB,OAZsB,MAAMC,EAAAA,GAFbC,EAAAA,GADWC,EAAAA,GAAQ,EAAU,EAAgC,CACpC,EAAgB,CAItD,KAAO,IACiB,MAAM,EAAkC,EAAQ,CACpE,YACA,kBACA,YACD,CAAC,CAGJ,CAAE,YAAa,EAAgB,CAChC,EACoB,MAAM"}
|
|
1
|
+
{"version":3,"file":"preference-management-B36PQuMK.cjs","names":["extractErrorMessage","sleepPromise","t","PreferenceQueryResponseItem","chunk","cliProgress","map","PreferenceTopicType","splitCsvToList","difference","uniq","difference","uniq","inquirerConfirmBoolean","groupBy","difference","uniq","mapSeries","PreferenceTopicType","splitCsvToList","PreferenceTopicType","PreferenceTopicType","readCsv","t","keyBy","t","PreferenceQueryResponseItem","PreferenceUpdateItem","PreferenceStoreIdentifier","parseAttributesFromString","PersistedState","buildTranscendGraphQLClient","createSombraGotInstance","fetchAllPurposes","fetchAllPreferenceTopics","cliProgress","chunk","map","startOfUtcDay","DAY_MS","FIVE_MIN_MS","addDaysUtc","cliProgress","clampPageSize","pmap","map","chunk","readCsv"],"sources":["../src/lib/preference-management/withPreferenceRetry.ts","../src/lib/preference-management/types.ts","../src/lib/preference-management/getPreferencesForIdentifiers.ts","../src/lib/preference-management/getPreferenceUpdatesFromRow.ts","../src/lib/preference-management/parsePreferenceTimestampsFromCsv.ts","../src/lib/preference-management/parsePreferenceIdentifiersFromCsv.ts","../src/lib/preference-management/parsePreferenceAndPurposeValuesFromCsv.ts","../src/lib/preference-management/checkIfPendingPreferenceUpdatesAreNoOp.ts","../src/lib/preference-management/checkIfPendingPreferenceUpdatesCauseConflict.ts","../src/lib/preference-management/parsePreferenceManagementCsv.ts","../src/lib/preference-management/codecs.ts","../src/lib/preference-management/uploadPreferenceManagementPreferencesInteractive.ts","../src/lib/preference-management/transformPreferenceRecordToCsv.ts","../src/lib/preference-management/iterateConsentPages.ts","../src/lib/preference-management/pickConsentChunkMode.ts","../src/lib/preference-management/getComparisonTimeForRecord.ts","../src/lib/preference-management/discoverConsentWindow.ts","../src/lib/preference-management/buildConsentChunks.ts","../src/lib/preference-management/fetchConsentPreferencesChunked.ts","../src/lib/preference-management/fetchConsentPreferences.ts","../src/lib/preference-management/bulkDeletePreferenceRecords.ts"],"sourcesContent":["import colors from 'colors';\nimport { logger } from '../../logger';\nimport { extractErrorMessage, sleepPromise } from '../helpers';\n\n/**\n * Transient network / platform errors that merit a retry.\n * Keep this list short and specific to avoid masking real failures.\n */\nexport const RETRY_PREFERENCE_MSGS: string[] = [\n 'ENOTFOUND',\n 'ECONNRESET',\n 'ETIMEDOUT',\n '502 Bad Gateway',\n '504 Gateway Time-out',\n '429',\n 'Rate limit exceeded',\n 'Task timed out after',\n 'unknown request error',\n].map((s) => s.toLowerCase());\n\n/**\n * Options for retrying preference operations.\n */\nexport type RetryOptions = {\n /** Max attempts including the first try (default 5) */\n maxAttempts?: number;\n /** Initial backoff in ms (default 250) */\n baseDelayMs?: number;\n /** Optional custom predicate to decide if an error is retryable */\n isRetryable?: (err: unknown, message: string) => boolean;\n /** Optional hook to log on each retry */\n onRetry?: (attempt: number, err: unknown, message: string) => void;\n};\n\n/**\n * Run an async function with standardized retry behavior for preference operations.\n * Exponential backoff with jitter; only retries on known-transient messages.\n *\n * @param name - Name of the operation (for logging)\n * @param fn - Function to run\n * @param options - Retry options\n * @returns Result of the function\n */\nexport async function withPreferenceRetry<T>(\n name: string,\n fn: () => Promise<T>,\n {\n maxAttempts = 5,\n baseDelayMs = 250,\n isRetryable = (_err, msg) =>\n RETRY_PREFERENCE_MSGS.some((m) => msg.toLowerCase().includes(m)),\n onRetry,\n }: RetryOptions = {},\n): Promise<T> {\n let attempt = 0;\n // eslint-disable-next-line no-constant-condition\n while (true) {\n attempt += 1;\n try {\n return await fn();\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (err: any) {\n const msg: string = extractErrorMessage(err);\n const willRetry = attempt < maxAttempts && isRetryable(err, msg);\n if (!willRetry) {\n throw new Error(`${name} failed after ${attempt} attempt(s): ${msg}`);\n }\n onRetry?.(attempt, err, msg);\n\n const backoff = baseDelayMs * 2 ** (attempt - 1);\n const jitter = Math.floor(Math.random() * baseDelayMs);\n const delay = backoff + jitter;\n logger.warn(\n colors.yellow(\n `[retry] attempt ${attempt}/${\n maxAttempts - 1\n }; backing off ${delay}ms: ${msg}`,\n ),\n );\n await sleepPromise(delay);\n }\n }\n}\n","import { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport * as t from 'io-ts';\n\n/**\n * New response codec for the query endpoint\n */\nexport const ConsentPreferenceResponse = t.intersection([\n t.type({\n nodes: t.array(PreferenceQueryResponseItem),\n }),\n t.partial({\n /** Cursor for next page (opaque) */\n cursor: t.string,\n }),\n]);\n\n/**\n * Type override\n */\nexport type ConsentPreferenceResponse = t.TypeOf<\n typeof ConsentPreferenceResponse\n>;\n\n/** Identifier filter (new shape) */\nexport type PreferenceIdentifier = {\n /** e.g., \"email\", \"phone\" */\n name: string;\n /** identifier value */\n value: string;\n};\n\n/** Filter shape for the new query endpoint */\nexport type PreferencesQueryFilter = {\n /** Identifiers to filter by */\n identifiers?: PreferenceIdentifier[];\n /** Consent collection time */\n timestampBefore?: string;\n /** Consent collection time */\n timestampAfter?: string;\n /** System updatedAt time */\n system?: {\n /** Updated before this time */\n updatedBefore?: string;\n /** Updated after this time */\n updatedAfter?: string;\n };\n};\n\n/** Which dimension we chunk on */\nexport type ChunkMode = 'timestamp' | 'updated';\n","import { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport type { Got } from 'got';\nimport colors from 'colors';\nimport cliProgress from 'cli-progress';\nimport { chunk } from 'lodash-es';\nimport { decodeCodec } from '@transcend-io/type-utils';\nimport { map } from '../bluebird';\nimport { logger } from '../../logger';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { ConsentPreferenceResponse } from './types';\n\n/**\n * Grab the current consent preference values for a list of identifiers\n *\n * @param sombra - Backend to make API call to\n * @param options - Options\n * @returns Plaintext context information\n */\nexport async function getPreferencesForIdentifiers(\n sombra: Got,\n {\n identifiers,\n partitionKey,\n skipLogging = false,\n concurrency = 40,\n }: {\n /** The list of identifiers to look up */\n identifiers: {\n /** The value of the identifier */\n value: string;\n }[];\n /** The partition key to look up */\n partitionKey: string;\n /** Whether to skip logging */\n skipLogging?: boolean;\n /** Concurrency for requests (default 40) */\n concurrency?: number;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const results: PreferenceQueryResponseItem[] = [];\n const groupedIdentifiers = chunk(identifiers, 100);\n\n // create a new progress bar instance and use shades_classic theme\n const t0 = new Date().getTime();\n const progressBar = new cliProgress.SingleBar(\n {},\n cliProgress.Presets.shades_classic,\n );\n if (!skipLogging) {\n progressBar.start(identifiers.length, 0);\n }\n\n let total = 0;\n await map(\n groupedIdentifiers,\n async (group) => {\n const rawResult = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partitionKey}/query`, {\n json: {\n filter: { identifiers: group },\n limit: group.length,\n },\n })\n .json(),\n {\n onRetry: (attempt, _err, msg) => {\n logger.warn(\n colors.yellow(\n `[RETRY] group size=${group.length} partition=${partitionKey} attempt=${attempt}: ${msg}`,\n ),\n );\n },\n },\n );\n\n const result = decodeCodec(ConsentPreferenceResponse, rawResult);\n results.push(...result.nodes);\n total += group.length;\n progressBar.update(total);\n },\n {\n concurrency,\n },\n );\n\n progressBar.stop();\n const t1 = new Date().getTime();\n const totalTime = t1 - t0;\n\n if (!skipLogging) {\n // Log completion time\n logger.info(\n colors.green(`Completed download in \"${totalTime / 1000}\" seconds.`),\n );\n }\n\n return results;\n}\n","import {\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PurposeRowMapping } from './codecs';\nimport { apply } from '@transcend-io/type-utils';\nimport { PreferenceTopic } from '../graphql';\nimport { splitCsvToList } from '../requests';\n\n/**\n * Parse an arbitrary object to the Transcend PUT /v1/preference update shape\n * by using a mapping of column names to purpose/preference slugs.\n *\n * `columnToPurposeName` looks like:\n * {\n * 'my_purpose': { purpose: 'Marketing', preference: null, valueMapping: { 'true': true, 'false': false } },\n * 'has_topic_1': { purpose: 'Marketing', preference: 'BooleanPreference1', valueMapping: { 'true': true, 'false': false } },\n * 'has_topic_2': { purpose: 'Marketing', preference: 'SingleSelectPreference', valueMapping: { 'Option 1': 'Value1', 'Option 2': 'Value2' } }\n * }\n *\n * `row` looks like:\n * {\n * 'my_purpose': 'true',\n * 'has_topic_1': 'true',\n * 'has_topic_2': 'Option 1'\n * }\n *\n * OMISSION RULE:\n * - If `valueMapping[row[columnName]]`\n * returns `undefined` or `null`, we **omit** that column entirely (do not set purpose enabled, do not push a preference).\n * - For MultiSelect, **each token** is treated independently: tokens that map to `undefined|null` are skipped;\n * if all tokens are skipped, nothing is pushed.\n * - We still validate **types** for mapped values (e.g., boolean must map to boolean, select must map to string, etc.).\n *\n * NOTE:\n * - Final shape must have `enabled` for every purpose touched (enforced by `apply` below). If you omit all top-level purpose mappings,\n * but emit preferences, this will throw at the end. This preserves the existing “enabled required” contract.\n *\n * @param options - Options\n * @returns The parsed row\n */\nexport function getPreferenceUpdatesFromRow({\n row,\n columnToPurposeName,\n purposeSlugs,\n preferenceTopics,\n}: {\n /** Row to parse */\n row: Record<string, string>;\n /** Mapping from column name to parser config */\n columnToPurposeName: Record<string, PurposeRowMapping>;\n /** The set of allowed purpose slugs */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n}): {\n [k in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n} {\n // Create a result object to store the parsed preferences\n const result: {\n [k in string]: Partial<PreferenceStorePurposeResponse>;\n } = {};\n\n // Iterate over each column and map to the purpose or preference\n Object.entries(columnToPurposeName).forEach(\n ([columnName, { purpose, preference, valueMapping }]) => {\n // Ensure the purpose is valid\n if (!purposeSlugs.includes(purpose)) {\n throw new Error(\n `Invalid purpose slug: ${purpose}, expected: ${purposeSlugs.join(\n ', ',\n )}`,\n );\n }\n\n // The raw value from the CSV row for this column\n const rawValue = row[columnName];\n\n // Check if parsing a preference or just the top level purpose\n if (preference) {\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === preference && x.purpose.trackingType === purpose,\n );\n if (!preferenceTopic) {\n const allowedTopics = preferenceTopics\n .filter((x) => x.purpose.trackingType === purpose)\n .map((x) => x.slug);\n throw new Error(\n `Invalid preference slug: ${preference} for purpose: ${purpose}. ` +\n `Allowed preference slugs for purpose are: ${allowedTopics.join(\n ',',\n )}`,\n );\n }\n\n // Ensure destination array\n if (!result[purpose]) {\n result[purpose] = {\n preferences: [],\n };\n }\n if (!result[purpose].preferences) {\n result[purpose].preferences = [];\n }\n\n // handle each type of preference\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean: {\n const mappedValue = valueMapping[rawValue];\n // Throw error on missing mapping\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Purposefully missing mapping\n if (mappedValue === null || mappedValue === undefined) {\n return;\n }\n\n // Ensure boolean\n if (typeof mappedValue !== 'boolean') {\n throw new Error(\n `Invalid value for boolean preference: ${preference}, expected boolean, got: ${rawValue}`,\n );\n }\n result[purpose].preferences!.push({\n topic: preference,\n choice: { booleanValue: mappedValue },\n });\n break;\n }\n\n case PreferenceTopicType.Select: {\n const mappedValue = valueMapping[rawValue];\n // Throw error on missing mapping\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Omit if null\n if (mappedValue === null || mappedValue === undefined) {\n return;\n }\n\n // Ensure string\n if (typeof mappedValue !== 'string') {\n throw new Error(\n `Invalid value for select preference: ${preference}, expected string, got: ${rawValue}`,\n );\n }\n const trimmed = mappedValue.trim() || null;\n\n if (\n trimmed &&\n !preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .includes(trimmed)\n ) {\n throw new Error(\n `Invalid value for select preference: ${preference}, expected one of: ` +\n `${preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .join(', ')}, got: ${rawValue}`,\n );\n }\n\n result[purpose].preferences!.push({\n topic: preference,\n choice: { selectValue: trimmed },\n });\n break;\n }\n\n case PreferenceTopicType.MultiSelect: {\n if (typeof rawValue !== 'string') {\n throw new Error(\n `Invalid value for multi select preference: ${preference}, expected string, got: ${rawValue}`,\n );\n }\n\n // IMPORTANT: Do NOT rely on valueMapping[rawValue] for CSV.\n // Split and map per token with the new rule.\n const selectValues = splitCsvToList(rawValue)\n .map((token) => {\n const tokenMapped = valueMapping[token];\n // Throw error on missing mapping\n if (tokenMapped === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for multi select token \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=${preference})`,\n );\n }\n\n // Omit if null\n if (tokenMapped === null || tokenMapped === undefined) {\n return null;\n }\n\n // Ensure string\n if (typeof tokenMapped !== 'string') {\n throw new Error(\n `Invalid value for multi select preference: ${preference}, ` +\n `expected one of: ${preferenceTopic.preferenceOptionValues\n .map(({ slug }) => slug)\n .join(', ')}, got: ${token}`,\n );\n }\n return tokenMapped;\n })\n .filter((x): x is string => x !== null)\n .sort((a, b) => a.localeCompare(b));\n\n // Only push if at least one mapped token survived\n if (selectValues.length > 0) {\n result[purpose].preferences!.push({\n topic: preference,\n choice: { selectValues },\n });\n }\n break;\n }\n\n default:\n throw new Error(`Unknown preference type: ${preferenceTopic.type}`);\n }\n } else {\n // Top-level purpose (no preference)\n const mappedValue = valueMapping[rawValue];\n if (mappedValue === undefined && rawValue !== '') {\n throw new Error(\n `No preference mapping found for value \"${rawValue}\" in column ` +\n `\"${columnName}\" (purpose=${purpose}, preference=∅) ${JSON.stringify(\n row,\n )}`,\n );\n }\n if (mappedValue === null) {\n return; // Omit if null\n }\n\n if (!result[purpose]) {\n // Top-level purpose: set enabled strictly from mapped boolean\n result[purpose] = { enabled: mappedValue === true };\n } else {\n // Preserve preferences; update enabled\n result[purpose].enabled = mappedValue === true;\n }\n }\n },\n );\n\n // Ensure that enabled is provided for any purpose that appears.\n // (This preserves the prior contract and existing tests.)\n return apply(result, (x, purposeName) => {\n if (typeof x.enabled !== 'boolean') {\n throw new Error(\n `No mapping provided for purpose.enabled=true/false value: ${purposeName}`,\n );\n }\n return {\n ...x,\n enabled: x.enabled!,\n };\n });\n}\n","import { uniq, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\n\nexport const NONE_PREFERENCE_MAP = '[NONE]';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse timestamps from a CSV list of preferences\n *\n * When timestamp is requested, this script\n * ensures that all rows have a valid timestamp.\n *\n * Error is throw if timestamp is missing\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceTimestampsFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n): Promise<FileMetadataState> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for timestamp\n const remainingColumnsForTimestamp = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...Object.keys(currentState.columnToPurposeName),\n ]);\n\n // Determine the timestamp column to work off of\n if (!currentState.timestampColum) {\n const { timestampName } = await inquirer.prompt<{\n /** timestamp name */\n timestampName: string;\n }>([\n {\n name: 'timestampName',\n message:\n 'Choose the column that will be used as the timestamp of last preference update',\n type: 'list',\n default:\n remainingColumnsForTimestamp.find((col) =>\n col.toLowerCase().includes('date'),\n ) ||\n remainingColumnsForTimestamp.find((col) =>\n col.toLowerCase().includes('time'),\n ) ||\n remainingColumnsForTimestamp[0],\n choices: [...remainingColumnsForTimestamp, NONE_PREFERENCE_MAP],\n },\n ]);\n currentState.timestampColum = timestampName;\n }\n logger.info(\n colors.magenta(`Using timestamp column \"${currentState.timestampColum}\"`),\n );\n\n // Validate that all rows have valid timestamp\n if (currentState.timestampColum !== NONE_PREFERENCE_MAP) {\n const timestampColumnsMissing = preferences\n .map((pref, ind) => (pref[currentState.timestampColum!] ? null : [ind]))\n .filter((x): x is number[] => !!x)\n .flat();\n if (timestampColumnsMissing.length > 0) {\n throw new Error(\n `The timestamp column \"${\n currentState.timestampColum\n }\" is missing a value for the following rows: ${timestampColumnsMissing.join(\n '\\n',\n )}`,\n );\n }\n logger.info(\n colors.magenta(\n `The timestamp column \"${currentState.timestampColum}\" is present for all row`,\n ),\n );\n }\n return currentState;\n}\n/* eslint-enable no-param-reassign */\n","import { uniq, groupBy, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\nimport { inquirerConfirmBoolean } from '../helpers';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse identifiers from a CSV list of preferences\n *\n * Ensures that all rows have a valid identifier\n * and that all identifiers are unique.\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceIdentifiersFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n): Promise<{\n /** The updated state */\n currentState: FileMetadataState;\n /** The updated preferences */\n preferences: Record<string, string>[];\n}> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for identifier\n const remainingColumnsForIdentifier = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...Object.keys(currentState.columnToPurposeName),\n ]);\n\n // Determine the identifier column to work off of\n if (!currentState.identifierColumn) {\n const { identifierName } = await inquirer.prompt<{\n /** Identifier name */\n identifierName: string;\n }>([\n {\n name: 'identifierName',\n message:\n 'Choose the column that will be used as the identifier to upload consent preferences by',\n type: 'list',\n default:\n remainingColumnsForIdentifier.find((col) =>\n col.toLowerCase().includes('email'),\n ) || remainingColumnsForIdentifier[0],\n choices: remainingColumnsForIdentifier,\n },\n ]);\n currentState.identifierColumn = identifierName;\n }\n logger.info(\n colors.magenta(\n `Using identifier column \"${currentState.identifierColumn}\"`,\n ),\n );\n\n // Validate that the identifier column is present for all rows and unique\n const identifierColumnsMissing = preferences\n .map((pref, ind) => (pref[currentState.identifierColumn!] ? null : [ind]))\n .filter((x): x is number[] => !!x)\n .flat();\n if (identifierColumnsMissing.length > 0) {\n const msg = `The identifier column \"${\n currentState.identifierColumn\n }\" is missing a value for the following rows: ${identifierColumnsMissing.join(\n ', ',\n )}`;\n logger.warn(colors.yellow(msg));\n\n // Ask user if they would like to skip rows missing an identifier\n const skip = await inquirerConfirmBoolean({\n message: 'Would you like to skip rows missing an identifier?',\n });\n if (!skip) {\n throw new Error(msg);\n }\n\n // Filter out rows missing an identifier\n const previous = preferences.length;\n preferences = preferences.filter(\n (pref) => pref[currentState.identifierColumn!],\n );\n logger.info(\n colors.yellow(\n `Skipped ${previous - preferences.length} rows missing an identifier`,\n ),\n );\n }\n logger.info(\n colors.magenta(\n `The identifier column \"${currentState.identifierColumn}\" is present for all rows`,\n ),\n );\n\n // Validate that all identifiers are unique\n const rowsByUserId = groupBy(preferences, currentState.identifierColumn);\n const duplicateIdentifiers = Object.entries(rowsByUserId).filter(\n ([, rows]) => rows.length > 1,\n );\n if (duplicateIdentifiers.length > 0) {\n const msg = `The identifier column \"${\n currentState.identifierColumn\n }\" has duplicate values for the following rows: ${duplicateIdentifiers\n .slice(0, 10)\n .map(([userId, rows]) => `${userId} (${rows.length})`)\n .join('\\n')}`;\n logger.warn(colors.yellow(msg));\n\n // Ask user if they would like to take the most recent update\n // for each duplicate identifier\n const skip = await inquirerConfirmBoolean({\n message: 'Would you like to automatically take the latest update?',\n });\n if (!skip) {\n throw new Error(msg);\n }\n preferences = Object.entries(rowsByUserId)\n .map(([, rows]) => {\n const sorted = rows.sort(\n (a, b) =>\n new Date(b[currentState.timestampColum!]).getTime() -\n new Date(a[currentState.timestampColum!]).getTime(),\n );\n return sorted[0];\n })\n .filter((x) => x);\n }\n\n return { currentState, preferences };\n}\n/* eslint-enable no-param-reassign */\n","import { uniq, difference } from 'lodash-es';\nimport colors from 'colors';\nimport inquirer from 'inquirer';\nimport { FileMetadataState } from './codecs';\nimport { logger } from '../../logger';\nimport { mapSeries } from '../bluebird';\nimport { PreferenceTopic } from '../graphql';\nimport { PreferenceTopicType } from '@transcend-io/privacy-types';\nimport { splitCsvToList } from '../requests';\n\n/* eslint-disable no-param-reassign */\n\n/**\n * Parse out the purpose.enabled and preference values from a CSV file\n *\n * @param preferences - List of preferences\n * @param currentState - The current file metadata state for parsing this list\n * @param options - Options\n * @returns The updated file metadata state\n */\nexport async function parsePreferenceAndPurposeValuesFromCsv(\n preferences: Record<string, string>[],\n currentState: FileMetadataState,\n {\n purposeSlugs,\n preferenceTopics,\n forceTriggerWorkflows,\n }: {\n /** The purpose slugs that are allowed to be updated */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n /** Force workflow triggers */\n forceTriggerWorkflows: boolean;\n },\n): Promise<FileMetadataState> {\n // Determine columns to map\n const columnNames = uniq(preferences.map((x) => Object.keys(x)).flat());\n\n // Determine the columns that could potentially be used for identifier\n const otherColumns = difference(columnNames, [\n ...(currentState.identifierColumn ? [currentState.identifierColumn] : []),\n ...(currentState.timestampColum ? [currentState.timestampColum] : []),\n ]);\n if (otherColumns.length === 0) {\n if (forceTriggerWorkflows) {\n return currentState;\n }\n throw new Error('No other columns to process');\n }\n\n // The purpose and preferences to map to\n const purposeNames = [\n ...purposeSlugs,\n ...preferenceTopics.map((x) => `${x.purpose.trackingType}->${x.slug}`),\n ];\n\n // Ensure all columns are accounted for\n await mapSeries(otherColumns, async (col) => {\n // Determine the unique values to map in this column\n const uniqueValues = uniq(preferences.map((x) => x[col]));\n\n // Map the column to a purpose\n let purposeMapping = currentState.columnToPurposeName[col];\n if (purposeMapping) {\n logger.info(\n colors.magenta(\n `Column \"${col}\" is associated with purpose \"${purposeMapping.purpose}\"`,\n ),\n );\n } else {\n const { purposeName } = await inquirer.prompt<{\n /** purpose name */\n purposeName: string;\n }>([\n {\n name: 'purposeName',\n message: `Choose the purpose that column ${col} is associated with`,\n type: 'list',\n default: purposeNames.find((x) => x.startsWith(purposeSlugs[0])),\n choices: purposeNames,\n },\n ]);\n const [purposeSlug, preferenceSlug] = purposeName.split('->');\n purposeMapping = {\n purpose: purposeSlug,\n preference: preferenceSlug || null,\n valueMapping: {},\n };\n }\n\n // map each value to the purpose value\n await mapSeries(uniqueValues, async (value) => {\n if (purposeMapping.valueMapping[value] !== undefined) {\n logger.info(\n colors.magenta(\n `Value \"${value}\" is associated with purpose value \"${purposeMapping.valueMapping[value]}\"`,\n ),\n );\n return;\n }\n // if preference is null, this column is just for the purpose\n if (purposeMapping.preference === null) {\n const { purposeValue } = await inquirer.prompt<{\n /** purpose value */\n purposeValue: boolean;\n }>([\n {\n name: 'purposeValue',\n message: `Choose the purpose value for value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'confirm',\n default: value !== 'false',\n },\n ]);\n purposeMapping.valueMapping[value] = purposeValue;\n }\n\n // if preference is not null, this column is for a specific preference\n if (purposeMapping.preference !== null) {\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === purposeMapping.preference,\n );\n if (!preferenceTopic) {\n logger.error(\n colors.red(\n `Preference topic \"${purposeMapping.preference}\" not found`,\n ),\n );\n return;\n }\n const preferenceOptions = preferenceTopic.preferenceOptionValues.map(\n ({ slug }) => slug,\n );\n\n if (preferenceTopic.type === PreferenceTopicType.Boolean) {\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n message:\n // eslint-disable-next-line max-len\n `Choose the preference value for \"${preferenceTopic.slug}\" value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'confirm',\n default: value !== 'false',\n },\n ]);\n purposeMapping.valueMapping[value] = preferenceValue;\n return;\n }\n\n if (preferenceTopic.type === PreferenceTopicType.Select) {\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n // eslint-disable-next-line max-len\n message: `Choose the preference value for \"${preferenceTopic.slug}\" value \"${value}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'list',\n choices: preferenceOptions,\n default: preferenceOptions.find((x) => x === value),\n },\n ]);\n purposeMapping.valueMapping[value] = preferenceValue;\n return;\n }\n\n if (preferenceTopic.type === PreferenceTopicType.MultiSelect) {\n const parsedValues = splitCsvToList(value);\n // need to do this serially\n await mapSeries(parsedValues, async (parsedValue) => {\n // if we already have a value, skip re-processing it again\n if (purposeMapping.valueMapping[parsedValue] !== undefined) {\n return;\n }\n const { preferenceValue } = await inquirer.prompt<{\n /** purpose value */\n preferenceValue: boolean;\n }>([\n {\n name: 'preferenceValue',\n // eslint-disable-next-line max-len\n message: `Choose the preference value for \"${preferenceTopic.slug}\" value \"${parsedValue}\" associated with purpose \"${purposeMapping.purpose}\"`,\n type: 'list',\n choices: preferenceOptions,\n default: preferenceOptions.find((x) => x === parsedValue),\n },\n ]);\n purposeMapping.valueMapping[parsedValue] = preferenceValue;\n });\n return;\n }\n\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n });\n\n currentState.columnToPurposeName[col] = purposeMapping;\n });\n\n return currentState;\n}\n/* eslint-enable no-param-reassign */\n","import {\n PreferenceQueryResponseItem,\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PreferenceTopic } from '../graphql';\n\n/**\n * Check if the pending set of updates are exactly the same as the current consent record.\n *\n * @param options - Options\n * @returns Whether the pending updates already exist in the preference store\n */\nexport function checkIfPendingPreferenceUpdatesAreNoOp({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n}: {\n /** The current consent record */\n currentConsentRecord: PreferenceQueryResponseItem;\n /** The pending updates */\n pendingUpdates: {\n [purposeName in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n };\n /** The preference topic configurations */\n preferenceTopics: PreferenceTopic[];\n}): boolean {\n // Check each update\n return Object.entries(pendingUpdates).every(\n ([purposeName, { preferences = [], enabled }]) => {\n // Ensure the purpose exists\n const currentPurpose = currentConsentRecord.purposes.find(\n (existingPurpose) => existingPurpose.purpose === purposeName,\n );\n\n // Ensure purpose.enabled is in sync\n // Also false if the purpose does not exist\n const enabledIsInSync =\n !!currentPurpose && currentPurpose.enabled === enabled;\n if (!enabledIsInSync) {\n return false;\n }\n\n // Compare the preferences are in sync\n return preferences.every(\n ({ topic, choice }) =>\n // ensure preferences exist on record\n currentPurpose.preferences &&\n currentPurpose.preferences.find((existingPreference) => {\n // find matching topic\n if (existingPreference.topic !== topic) {\n return false;\n }\n\n // Determine type of preference topic\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === topic && x.purpose.trackingType === purposeName,\n );\n if (!preferenceTopic) {\n throw new Error(`Could not find preference topic for ${topic}`);\n }\n\n // Handle comparison based on type\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean:\n return (\n existingPreference.choice.booleanValue === choice.booleanValue\n );\n case PreferenceTopicType.Select:\n return (\n existingPreference.choice.selectValue === choice.selectValue\n );\n case PreferenceTopicType.MultiSelect:\n // eslint-disable-next-line no-case-declarations\n const sortedCurrentValues = (\n existingPreference.choice.selectValues || []\n ).sort();\n // eslint-disable-next-line no-case-declarations\n const sortedNewValues = (choice.selectValues || []).sort();\n return (\n sortedCurrentValues.length === sortedNewValues.length &&\n sortedCurrentValues.every((x, i) => x === sortedNewValues[i])\n );\n default:\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n }),\n );\n },\n );\n}\n","import {\n PreferenceQueryResponseItem,\n PreferenceStorePurposeResponse,\n PreferenceTopicType,\n} from '@transcend-io/privacy-types';\nimport { PreferenceTopic } from '../graphql';\nimport { logger } from '../../logger';\n\n/**\n * Check if the pending set of updates will result in a change of\n * value to an existing purpose or preference in the preference store.\n *\n * @param options - Options\n * @returns True if conflict, false if no conflict and just adding new data for first time\n */\nexport function checkIfPendingPreferenceUpdatesCauseConflict({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n log,\n}: {\n /** The current consent record */\n currentConsentRecord: PreferenceQueryResponseItem;\n /** The pending updates */\n pendingUpdates: {\n [purposeName in string]: Omit<PreferenceStorePurposeResponse, 'purpose'>;\n };\n /** The preference topic configurations */\n preferenceTopics: PreferenceTopic[];\n /** Whether to log the conflict */\n log?: boolean;\n}): boolean {\n // Check if any update has conflict\n return !!Object.entries(pendingUpdates).find(\n ([purposeName, { preferences = [], enabled }]) => {\n // Ensure the purpose exists\n const currentPurpose = currentConsentRecord.purposes.find(\n (existingPurpose) => existingPurpose.purpose === purposeName,\n );\n\n // If no purpose exists, then it is not a conflict\n if (!currentPurpose) {\n if (log) {\n logger.warn(\n `No existing purpose found for ${purposeName} in consent record for ${currentConsentRecord.userId}.`,\n );\n }\n return false;\n }\n\n // If purpose.enabled value is off, this is a conflict\n if (currentPurpose.enabled !== enabled) {\n if (log) {\n logger.warn(\n `Purpose ${purposeName} enabled value conflict for user ${currentConsentRecord.userId}. ` +\n `Pending Value: ${enabled}, Current Value: ${currentPurpose.enabled}`,\n );\n }\n return true;\n }\n\n // Check if any preferences are out of sync\n return !!preferences.find(({ topic, choice }) => {\n // find matching topic\n const currentPreference = (currentPurpose.preferences || []).find(\n (existingPreference) => existingPreference.topic === topic,\n );\n\n // if no topic exists, no conflict\n if (!currentPreference) {\n if (log) {\n logger.warn(\n `No existing preference found for topic ${topic} in purpose ` +\n `${purposeName} for user ${currentConsentRecord.userId}.`,\n );\n }\n return false;\n }\n\n // Determine type of preference topic\n const preferenceTopic = preferenceTopics.find(\n (x) => x.slug === topic && x.purpose.trackingType === purposeName,\n );\n if (!preferenceTopic) {\n throw new Error(`Could not find preference topic for ${topic}`);\n }\n\n // Handle comparison based on type\n let boolMatch: boolean;\n let selectMatch: boolean;\n switch (preferenceTopic.type) {\n case PreferenceTopicType.Boolean:\n boolMatch =\n currentPreference.choice.booleanValue !== choice.booleanValue;\n if (log) {\n logger.warn(\n `Preference topic ${topic} boolean value conflict for user ` +\n `${currentConsentRecord.userId}. Expected: ${choice.booleanValue}, ` +\n `Found: ${currentPreference.choice.booleanValue}`,\n );\n }\n return boolMatch;\n case PreferenceTopicType.Select:\n selectMatch =\n currentPreference.choice.selectValue !== choice.selectValue;\n if (log) {\n logger.warn(\n `Preference topic ${topic} select value conflict for user ` +\n `${currentConsentRecord.userId}. Expected: ${choice.selectValue}, ` +\n `Found: ${currentPreference.choice.selectValue}`,\n );\n }\n return selectMatch;\n case PreferenceTopicType.MultiSelect:\n // eslint-disable-next-line no-case-declarations\n const sortedCurrentValues = (\n currentPreference.choice.selectValues || []\n ).sort();\n // eslint-disable-next-line no-case-declarations\n const sortedNewValues = (choice.selectValues || []).sort();\n selectMatch =\n sortedCurrentValues.length !== sortedNewValues.length ||\n !sortedCurrentValues.every((x, i) => x === sortedNewValues[i]);\n if (log) {\n logger.warn(\n `Preference topic ${topic} multi-select value conflict for user ` +\n `${\n currentConsentRecord.userId\n }. Expected: ${sortedNewValues.join(\n ', ',\n )}, Found: ${sortedCurrentValues.join(', ')}`,\n );\n }\n return selectMatch;\n default:\n throw new Error(\n `Unknown preference topic type: ${preferenceTopic.type}`,\n );\n }\n });\n },\n );\n}\n","import { PersistedState } from '@transcend-io/persisted-state';\nimport type { Got } from 'got';\nimport { keyBy } from 'lodash-es';\nimport * as t from 'io-ts';\nimport colors from 'colors';\nimport { FileMetadataState, PreferenceState } from './codecs';\nimport { logger } from '../../logger';\nimport { readCsv } from '../requests';\nimport { getPreferencesForIdentifiers } from './getPreferencesForIdentifiers';\nimport { PreferenceTopic } from '../graphql';\nimport { getPreferenceUpdatesFromRow } from './getPreferenceUpdatesFromRow';\nimport { parsePreferenceTimestampsFromCsv } from './parsePreferenceTimestampsFromCsv';\nimport { parsePreferenceIdentifiersFromCsv } from './parsePreferenceIdentifiersFromCsv';\nimport { parsePreferenceAndPurposeValuesFromCsv } from './parsePreferenceAndPurposeValuesFromCsv';\nimport { checkIfPendingPreferenceUpdatesAreNoOp } from './checkIfPendingPreferenceUpdatesAreNoOp';\nimport { checkIfPendingPreferenceUpdatesCauseConflict } from './checkIfPendingPreferenceUpdatesCauseConflict';\n\n/**\n * Parse a file into the cache\n *\n *\n * @param options - Options\n * @param cache - The cache to store the parsed file in\n * @returns The cache with the parsed file\n */\nexport async function parsePreferenceManagementCsvWithCache(\n {\n file,\n sombra,\n purposeSlugs,\n preferenceTopics,\n partitionKey,\n skipExistingRecordCheck,\n forceTriggerWorkflows,\n }: {\n /** File to parse */\n file: string;\n /** The purpose slugs that are allowed to be updated */\n purposeSlugs: string[];\n /** The preference topics */\n preferenceTopics: PreferenceTopic[];\n /** Sombra got instance */\n sombra: Got;\n /** Partition key */\n partitionKey: string;\n /** Whether to skip the check for existing records. SHOULD ONLY BE USED FOR INITIAL UPLOAD */\n skipExistingRecordCheck: boolean;\n /** Whether to force workflow triggers */\n forceTriggerWorkflows: boolean;\n },\n cache: PersistedState<typeof PreferenceState>,\n): Promise<void> {\n // Start the timer\n const t0 = new Date().getTime();\n\n // Get the current metadata\n const fileMetadata = cache.getValue('fileMetadata');\n\n // Read in the file\n logger.info(colors.magenta(`Reading in file: \"${file}\"`));\n let preferences = readCsv(file, t.record(t.string, t.string));\n\n // start building the cache, can use previous cache as well\n let currentState: FileMetadataState = {\n columnToPurposeName: {},\n pendingSafeUpdates: {},\n pendingConflictUpdates: {},\n skippedUpdates: {},\n // Load in the last fetched time\n ...((fileMetadata[file] || {}) as Partial<FileMetadataState>),\n lastFetchedAt: new Date().toISOString(),\n };\n\n // Validate that all timestamps are present in the file\n currentState = await parsePreferenceTimestampsFromCsv(\n preferences,\n currentState,\n );\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Validate that all identifiers are present and unique\n const result = await parsePreferenceIdentifiersFromCsv(\n preferences,\n currentState,\n );\n currentState = result.currentState;\n preferences = result.preferences;\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Ensure all other columns are mapped to purpose and preference\n // slug values\n currentState = await parsePreferenceAndPurposeValuesFromCsv(\n preferences,\n currentState,\n {\n preferenceTopics,\n purposeSlugs,\n forceTriggerWorkflows,\n },\n );\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n\n // Grab existing preference store records\n const identifiers = preferences.map(\n (pref) => pref[currentState.identifierColumn!],\n );\n const existingConsentRecords = skipExistingRecordCheck\n ? []\n : await getPreferencesForIdentifiers(sombra, {\n identifiers: identifiers.map((x) => ({ value: x })),\n partitionKey,\n });\n const consentRecordByIdentifier = keyBy(existingConsentRecords, 'userId');\n\n // Clear out previous updates\n currentState.pendingConflictUpdates = {};\n currentState.pendingSafeUpdates = {};\n currentState.skippedUpdates = {};\n\n // Process each row\n preferences.forEach((pref) => {\n // Grab unique Id for the user\n const userId = pref[currentState.identifierColumn!];\n\n // determine updates for user\n const pendingUpdates = getPreferenceUpdatesFromRow({\n row: pref,\n columnToPurposeName: currentState.columnToPurposeName,\n preferenceTopics,\n purposeSlugs,\n });\n\n // Grab current state of the update\n const currentConsentRecord = consentRecordByIdentifier[userId];\n if (forceTriggerWorkflows && !currentConsentRecord) {\n throw new Error(\n `No existing consent record found for user with id: ${userId}.\n When 'forceTriggerWorkflows' is set all the user identifiers should contain a consent record`,\n );\n }\n // Check if the update can be skipped\n // this is the case if a record exists, and the purpose\n // and preference values are all in sync\n if (\n currentConsentRecord &&\n checkIfPendingPreferenceUpdatesAreNoOp({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n }) &&\n !forceTriggerWorkflows\n ) {\n currentState.skippedUpdates[userId] = pref;\n return;\n }\n\n // Determine if there are any conflicts\n if (\n currentConsentRecord &&\n checkIfPendingPreferenceUpdatesCauseConflict({\n currentConsentRecord,\n pendingUpdates,\n preferenceTopics,\n })\n ) {\n currentState.pendingConflictUpdates[userId] = {\n row: pref,\n record: currentConsentRecord,\n };\n return;\n }\n\n // Add to pending updates\n currentState.pendingSafeUpdates[userId] = pref;\n });\n\n // Read in the file\n fileMetadata[file] = currentState;\n await cache.setValue(fileMetadata, 'fileMetadata');\n const t1 = new Date().getTime();\n logger.info(\n colors.green(\n `Successfully pre-processed file: \"${file}\" in ${(t1 - t0) / 1000}s`,\n ),\n );\n}\n","import {\n PreferenceQueryResponseItem,\n PreferenceStoreIdentifier,\n PreferenceUpdateItem,\n} from '@transcend-io/privacy-types';\nimport * as t from 'io-ts';\n\nexport const PurposeRowMapping = t.type({\n /**\n * The slug or trackingType of the purpose to map to\n *\n * e.g. `Marketing`\n */\n purpose: t.string,\n /**\n * If the column maps to a preference instead of a purpose\n * this is the slug of the purpose.\n *\n * null value indicates that this column maps to the true/false\n * value of the purpose\n */\n preference: t.union([t.string, t.null]),\n /**\n * The mapping between each row value and purpose/preference value.\n *\n * e.g. for a boolean preference or purpose\n * {\n * 'true': true,\n * 'false': false,\n * '': true,\n * }\n *\n * or for a single or multi select preference\n * {\n * '': true,\n * 'value1': 'Value1',\n * 'value2': 'Value2',\n * }\n */\n valueMapping: t.record(\n t.string,\n t.union([t.string, t.boolean, t.null, t.undefined]),\n ),\n});\n\n/** Override type */\nexport type PurposeRowMapping = t.TypeOf<typeof PurposeRowMapping>;\n\n/**\n * Mapping of column name to purpose row mapping.\n * This is used to map each column in the CSV to the relevant purpose and preference definitions in\n * transcend.\n */\nexport const ColumnPurposeMap = t.record(t.string, PurposeRowMapping);\n\n/** Override type */\nexport type ColumnPurposeMap = t.TypeOf<typeof ColumnPurposeMap>;\n\nexport const IdentifierMetadataForPreference = t.type({\n /** The identifier name */\n name: t.string,\n /** Is unique on preference store */\n isUniqueOnPreferenceStore: t.boolean,\n});\n\n/** Override type */\nexport type IdentifierMetadataForPreference = t.TypeOf<\n typeof IdentifierMetadataForPreference\n>;\n\n/**\n * Mapping of identifier name to the column name in the CSV file.\n * This is used to map each identifier name to the column in the CSV file.\n */\nexport const ColumnIdentifierMap = t.record(\n t.string,\n IdentifierMetadataForPreference,\n);\n\n/** Override type */\nexport type ColumnIdentifierMap = t.TypeOf<typeof ColumnIdentifierMap>;\n\n/** Mapping of a CSV column to a metadata key in the preference store. */\nexport const MetadataMapping = t.type({\n /** The metadata key name in the preference store */\n key: t.string,\n});\n\n/** Override type */\nexport type MetadataMapping = t.TypeOf<typeof MetadataMapping>;\n\n/** Record mapping CSV column names to metadata keys. */\nexport const ColumnMetadataMap = t.record(t.string, MetadataMapping);\n\n/** Override type */\nexport type ColumnMetadataMap = t.TypeOf<typeof ColumnMetadataMap>;\n\nexport const FileMetadataState = t.intersection([\n t.type({\n /**\n * Definition of how to map each column in the CSV to\n * the relevant purpose and preference definitions in transcend\n */\n columnToPurposeName: t.record(t.string, PurposeRowMapping),\n /** Last time the file was last parsed at */\n lastFetchedAt: t.string,\n /**\n * Mapping of userId to the rows in the file that need to be uploaded\n * These uploads are overwriting non-existent preferences and are safe\n */\n pendingSafeUpdates: t.record(t.string, t.record(t.string, t.string)),\n /**\n * Mapping of userId to the rows in the file that need to be uploaded\n * these records have conflicts with existing consent preferences\n */\n pendingConflictUpdates: t.record(\n t.string,\n t.type({\n record: PreferenceQueryResponseItem,\n row: t.record(t.string, t.string),\n }),\n ),\n /**\n * Mapping of userId to the rows in the file that can be skipped because\n * their preferences are already in the store\n */\n skippedUpdates: t.record(t.string, t.record(t.string, t.string)),\n }),\n t.partial({\n /** Determine which column name in file maps to consent record identifier to upload on */\n identifierColumn: t.string,\n /** Determine which column name in file maps to the timestamp */\n timestampColum: t.string,\n }),\n]);\n\n/** Override type */\nexport type FileMetadataState = t.TypeOf<typeof FileMetadataState>;\n\n/**\n * This is the type of the receipts that are stored in the file\n * that is used to track the state of the upload process.\n * It is used to resume the upload process from where it left off.\n * It is used to persist the state of the upload process across multiple runs.\n */\nexport const PreferenceUpdateMap = t.record(\n t.string,\n // This can either be true to indicate the record is pending\n // or it can be an object showing the object\n // We only return a fixed number of results to avoid\n // making the JSON file too large\n t.union([t.boolean, PreferenceUpdateItem]),\n);\n\n/** Override type */\nexport type PreferenceUpdateMap = t.TypeOf<typeof PreferenceUpdateMap>;\n\n/**\n * This is the type of the pending updates that are safe to run without\n * conflicts with existing consent preferences.\n *\n * Key is primaryKey of the record in the file.\n * The value is the row in the file that is safe to upload.\n */\nexport const PendingSafePreferenceUpdates = t.record(\n t.string,\n // This can either be true to indicate the record is safe\n // or it can be an object showing the object\n // We only return a fixed number of results to avoid\n // making the JSON file too large\n t.union([t.boolean, t.record(t.string, t.string)]),\n);\n\n/** Override type */\nexport type PendingSafePreferenceUpdates = t.TypeOf<\n typeof PendingSafePreferenceUpdates\n>;\n\n/**\n * These are the updates that failed to be uploaded to the API.\n */\nexport const FailingPreferenceUpdates = t.record(\n t.string,\n t.type({\n /** Time upload ran at */\n uploadedAt: t.string,\n /** Attempts to upload that resulted in an error */\n error: t.string,\n /** The update body */\n update: PreferenceUpdateItem,\n }),\n);\n\n/** Override type */\nexport type FailingPreferenceUpdates = t.TypeOf<\n typeof FailingPreferenceUpdates\n>;\n\n/**\n * This is the type of the pending updates that are in conflict with existing consent preferences.\n *\n * Key is primaryKey of the record in the file.\n * The value is the row in the file that is pending upload.\n */\nexport const PendingWithConflictPreferenceUpdates = t.record(\n t.string,\n // We always return the conflicts for investigation\n t.type({\n /** Record to be inserted to transcend v1/preferences API */\n record: PreferenceQueryResponseItem,\n /** The row in the file that is pending upload */\n row: t.record(t.string, t.string),\n }),\n);\n\n/** Override type */\nexport type PendingWithConflictPreferenceUpdates = t.TypeOf<\n typeof PendingWithConflictPreferenceUpdates\n>;\n\n/**\n * The set of preference updates that are skipped\n * Key is primaryKey and value is the row in the CSV\n * that is skipped.\n *\n * This is usually because the preferences are already in the store\n * or there are duplicate rows in the CSV file that are identical.\n */\nexport const SkippedPreferenceUpdates = t.record(\n t.string,\n t.record(t.string, t.string),\n);\n\n/** Override type */\nexport type SkippedPreferenceUpdates = t.TypeOf<\n typeof SkippedPreferenceUpdates\n>;\n\n/** Persist this data between runs of the script */\nexport const PreferenceState = t.type({\n /**\n * Store a cache of previous files read in\n */\n fileMetadata: t.record(t.string, FileMetadataState),\n /**\n * The set of successful uploads to Transcend\n * Mapping from userId to the upload metadata\n */\n failingUpdates: t.record(\n t.string,\n t.type({\n /** Time upload ran at */\n uploadedAt: t.string,\n /** Attempts to upload that resulted in an error */\n error: t.string,\n /** The update body */\n update: PreferenceUpdateItem,\n }),\n ),\n /**\n * The set of pending uploads to Transcend\n * Mapping from userId to the upload metadata\n */\n pendingUpdates: t.record(t.string, PreferenceUpdateItem),\n});\n\n/** Override type */\nexport type PreferenceState = t.TypeOf<typeof PreferenceState>;\n\nexport const DeletePreferenceRecordsInput = t.type({\n /** Array of consent preference records to delete */\n records: t.array(\n t.type({\n /** The anchor identifier to locate the consent record */\n anchorIdentifier: PreferenceStoreIdentifier,\n /** The ISO 8601 timestamp of when the deletion is requested */\n timestamp: t.string,\n }),\n ),\n});\n\n/** Override type */\nexport type DeletePreferenceRecordsInput = t.TypeOf<\n typeof DeletePreferenceRecordsInput\n>;\n\nexport const DeletePreferenceRecordsResponse = t.intersection([\n t.type({\n /** Array of results for each preference record deletion */\n records: t.array(\n t.intersection([\n t.type({\n /** Whether the deletion was successful */\n success: t.boolean,\n }),\n t.partial({\n /** An error message if the deletion failed */\n errorMessage: t.string,\n }),\n ]),\n ),\n /** The list of failed deletions with their respective errors */\n failures: t.array(\n t.type({\n /** The index of the failed update in the original request */\n index: t.number,\n /** The error message associated with the failure */\n error: t.string,\n }),\n ),\n }),\n t.partial({\n /** Any general errors that occurred during the operation */\n errors: t.array(t.string),\n }),\n]);\n\n/** Override type */\nexport type DeletePreferenceRecordsResponse = t.TypeOf<\n typeof DeletePreferenceRecordsResponse\n>;\n\n/** CLI CSV Row for deleting preference records */\nexport const DeletePreferenceRecordCliCsvRow = t.type({\n /** The name of the identifier type (e.g., email, userId) */\n name: t.string,\n /** The value of the identifier */\n value: t.string,\n});\n\n/** Override type */\nexport type DeletePreferenceRecordCliCsvRow = t.TypeOf<\n typeof DeletePreferenceRecordCliCsvRow\n>;\n","import {\n buildTranscendGraphQLClient,\n createSombraGotInstance,\n fetchAllPurposes,\n fetchAllPreferenceTopics,\n} from '../graphql';\nimport colors from 'colors';\nimport { map } from '../bluebird';\nimport { chunk } from 'lodash-es';\nimport { logger } from '../../logger';\nimport cliProgress from 'cli-progress';\nimport { parseAttributesFromString } from '../requests';\nimport { PersistedState } from '@transcend-io/persisted-state';\nimport { parsePreferenceManagementCsvWithCache } from './parsePreferenceManagementCsv';\nimport { PreferenceState } from './codecs';\nimport { PreferenceUpdateItem } from '@transcend-io/privacy-types';\nimport { apply } from '@transcend-io/type-utils';\nimport { NONE_PREFERENCE_MAP } from './parsePreferenceTimestampsFromCsv';\nimport { getPreferenceUpdatesFromRow } from './getPreferenceUpdatesFromRow';\n\n/**\n * Upload a set of consent preferences\n *\n * @param options - Options\n */\nexport async function uploadPreferenceManagementPreferencesInteractive({\n auth,\n sombraAuth,\n receiptFilepath,\n file,\n partition,\n isSilent = true,\n dryRun = false,\n skipWorkflowTriggers = false,\n skipConflictUpdates = false,\n skipExistingRecordCheck = false,\n attributes = [],\n transcendUrl,\n forceTriggerWorkflows = false,\n}: {\n /** The Transcend API key */\n auth: string;\n /** Sombra API key authentication */\n sombraAuth?: string;\n /** Partition key */\n partition: string;\n /** File where to store receipt and continue from where left off */\n receiptFilepath: string;\n /** The file to process */\n file: string;\n /** API URL for Transcend backend */\n transcendUrl: string;\n /** Whether to do a dry run */\n dryRun?: boolean;\n /** Whether to upload as isSilent */\n isSilent?: boolean;\n /** Attributes string pre-parse. In format Key:Value */\n attributes?: string[];\n /** Skip workflow triggers */\n skipWorkflowTriggers?: boolean;\n /**\n * When true, only update preferences that do not conflict with existing\n * preferences. When false, update all preferences in CSV based on timestamp.\n */\n skipConflictUpdates?: boolean;\n /** Whether to skip the check for existing records. SHOULD ONLY BE USED FOR INITIAL UPLOAD */\n skipExistingRecordCheck?: boolean;\n /** Whether to force trigger workflows */\n forceTriggerWorkflows?: boolean;\n}): Promise<void> {\n // Parse out the extra attributes to apply to all requests uploaded\n const parsedAttributes = parseAttributesFromString(attributes);\n\n // Create a new state file to store the requests from this run\n const preferenceState = new PersistedState(receiptFilepath, PreferenceState, {\n fileMetadata: {},\n failingUpdates: {},\n pendingUpdates: {},\n });\n const failingRequests = preferenceState.getValue('failingUpdates');\n const pendingRequests = preferenceState.getValue('pendingUpdates');\n let fileMetadata = preferenceState.getValue('fileMetadata');\n\n logger.info(\n colors.magenta(\n 'Restored cache, there are: \\n' +\n `${\n Object.values(failingRequests).length\n } failing requests to be retried\\n` +\n `${\n Object.values(pendingRequests).length\n } pending requests to be processed\\n` +\n `The following files are stored in cache and will be used:\\n${Object.keys(\n fileMetadata,\n )\n .map((x) => x)\n .join('\\n')}\\n` +\n `The following file will be processed: ${file}\\n`,\n ),\n );\n\n // Create GraphQL client to connect to Transcend backend\n const client = buildTranscendGraphQLClient(transcendUrl, auth);\n\n const [sombra, purposes, preferenceTopics] = await Promise.all([\n // Create sombra instance to communicate with\n createSombraGotInstance(transcendUrl, auth, sombraAuth),\n // get all purposes and topics\n fetchAllPurposes(client),\n fetchAllPreferenceTopics(client),\n ]);\n\n // Process the file\n await parsePreferenceManagementCsvWithCache(\n {\n file,\n purposeSlugs: purposes.map((x) => x.trackingType),\n preferenceTopics,\n sombra,\n partitionKey: partition,\n skipExistingRecordCheck,\n forceTriggerWorkflows,\n },\n preferenceState,\n );\n\n // Construct the pending updates\n const pendingUpdates: Record<string, PreferenceUpdateItem> = {};\n fileMetadata = preferenceState.getValue('fileMetadata');\n const metadata = fileMetadata[file];\n\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.pendingSafeUpdates).length\n } safe updates in ${file}`,\n ),\n );\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.pendingConflictUpdates).length\n } conflict updates in ${file}`,\n ),\n );\n logger.info(\n colors.magenta(\n `Found ${\n Object.entries(metadata.skippedUpdates).length\n } skipped updates in ${file}`,\n ),\n );\n\n // Update either safe updates only or safe + conflict\n Object.entries({\n ...metadata.pendingSafeUpdates,\n ...(skipConflictUpdates\n ? {}\n : apply(metadata.pendingConflictUpdates, ({ row }) => row)),\n }).forEach(([userId, update]) => {\n // Determine timestamp\n const timestamp =\n metadata.timestampColum === NONE_PREFERENCE_MAP\n ? new Date()\n : new Date(update[metadata.timestampColum!]);\n\n // Determine updates\n const updates = getPreferenceUpdatesFromRow({\n row: update,\n columnToPurposeName: metadata.columnToPurposeName,\n preferenceTopics,\n purposeSlugs: purposes.map((x) => x.trackingType),\n });\n pendingUpdates[userId] = {\n userId,\n partition,\n timestamp: timestamp.toISOString(),\n purposes: Object.entries(updates).map(([purpose, value]) => ({\n ...value,\n purpose,\n workflowSettings: {\n attributes: parsedAttributes,\n isSilent,\n skipWorkflowTrigger: skipWorkflowTriggers,\n ...(forceTriggerWorkflows\n ? { forceTriggerWorkflow: forceTriggerWorkflows }\n : {}),\n },\n })),\n };\n });\n await preferenceState.setValue(pendingUpdates, 'pendingUpdates');\n await preferenceState.setValue({}, 'failingUpdates');\n\n // Exist early if dry run\n if (dryRun) {\n logger.info(\n colors.green(\n `Dry run complete, exiting. ${\n Object.values(pendingUpdates).length\n } pending updates. Check file: ${receiptFilepath}`,\n ),\n );\n return;\n }\n\n logger.info(\n colors.magenta(\n `Uploading ${\n Object.values(pendingUpdates).length\n } preferences to partition: ${partition}`,\n ),\n );\n\n // Time duration\n const t0 = new Date().getTime();\n\n // create a new progress bar instance and use shades_classic theme\n const progressBar = new cliProgress.SingleBar(\n {},\n cliProgress.Presets.shades_classic,\n );\n\n // Build a GraphQL client\n let total = 0;\n const updatesToRun = Object.entries(pendingUpdates);\n const chunkedUpdates = chunk(updatesToRun, skipWorkflowTriggers ? 100 : 10);\n progressBar.start(updatesToRun.length, 0);\n await map(\n chunkedUpdates,\n async (currentChunk) => {\n // Make the request\n try {\n await sombra\n .put('v1/preferences', {\n json: {\n records: currentChunk.map(([, update]) => update),\n skipWorkflowTriggers,\n },\n })\n .json();\n } catch (err) {\n try {\n const parsed = JSON.parse(err?.response?.body || '{}');\n if (parsed.error) {\n logger.error(colors.red(`Error: ${parsed.error}`));\n }\n } catch (e) {\n // continue\n }\n logger.error(\n colors.red(\n `Failed to upload ${\n currentChunk.length\n } user preferences to partition ${partition}: ${\n err?.response?.body || err?.message\n }`,\n ),\n );\n const failingUpdates = preferenceState.getValue('failingUpdates');\n currentChunk.forEach(([userId, update]) => {\n failingUpdates[userId] = {\n uploadedAt: new Date().toISOString(),\n update,\n error: err?.response?.body || err?.message || 'Unknown error',\n };\n });\n await preferenceState.setValue(failingUpdates, 'failingUpdates');\n }\n\n total += currentChunk.length;\n progressBar.update(total);\n },\n {\n concurrency: 40,\n },\n );\n\n progressBar.stop();\n const t1 = new Date().getTime();\n const totalTime = t1 - t0;\n logger.info(\n colors.green(\n `Successfully uploaded ${\n updatesToRun.length\n } user preferences to partition ${partition} in \"${\n totalTime / 1000\n }\" seconds!`,\n ),\n );\n}\n","import type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\n\n/**\n * Transforms the output of the consent preferences query into a CSV-friendly format.\n *\n * @param input - The input object containing consent preferences data.\n * @param exportIdentifiersWithDelimiter - Delimiter to use when combining multiple identifier values.\n * @returns A record representing the transformed CSV output.\n */\nexport function transformPreferenceRecordToCsv(\n {\n identifiers = [],\n purposes = [],\n metadata = [],\n consentManagement = {},\n system = {\n decryptionStatus: 'DECRYPTED',\n },\n // keep other top-level fields as-is (e.g., partition, timestamp, metadataTimestamp)\n ...topLevel\n }: PreferenceQueryResponseItem,\n exportIdentifiersWithDelimiter: string,\n): Record<string, unknown> {\n // Start with: all other top-level fields + spread system and consentManagement\n const out: Record<string, unknown> = {\n ...topLevel,\n ...system,\n ...consentManagement,\n };\n\n // ── identifiers: each identifier.name -> CSV of values\n if (Array.isArray(identifiers)) {\n const byName = new Map<string, Set<string>>();\n for (const { name, value } of identifiers) {\n if (!byName.has(name)) byName.set(name, new Set());\n if (value) byName.get(name)!.add(value);\n }\n for (const [name, set] of byName.entries()) {\n out[name] = Array.from(set).join(exportIdentifiersWithDelimiter);\n }\n }\n\n // ── metadata: serialize as JSON\n if (Array.isArray(metadata)) {\n out.metadata = JSON.stringify(\n metadata.reduce((acc, { key, value }) => {\n acc[key] = value;\n return acc;\n }, {} as Record<string, string>),\n );\n }\n\n // ── purposes:\n // - purpose.slug column => true/false (enabled)\n // - for each preference: purpose.slug_preference.slug => bool | single | CSV (multi)\n if (Array.isArray(purposes)) {\n for (const { purpose, preferences, enabled } of purposes) {\n out[purpose] = Boolean(enabled);\n\n // nested preferences\n if (Array.isArray(preferences)) {\n for (const { topic, choice } of preferences) {\n const col = `${purpose}_${topic}`;\n\n let val: unknown = null;\n\n if (typeof choice.booleanValue === 'boolean') {\n val = choice.booleanValue;\n } else if (choice.selectValue) {\n val = choice.selectValue;\n } else if (Array.isArray(choice.selectValues)) {\n const vs = choice.selectValues.filter((v) => v.length > 0);\n val = vs.join(',');\n } else {\n // no pref value present -> null\n val = null;\n }\n\n out[col] = val;\n }\n }\n }\n }\n\n return out;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { ConsentPreferenceResponse, PreferencesQueryFilter } from './types';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { logger } from '../../logger';\n\n/**\n * Async generator over pages for a given filter\n *\n * @param sombra - Sombra Got instance\n * @param partition - Partition key\n * @param filter - Query filter\n * @param pageSize - Number of items per page\n * @yields Pages of PreferenceQueryResponseItem\n */\nexport async function* iterateConsentPages(\n sombra: Got,\n partition: string,\n filter: PreferencesQueryFilter,\n pageSize: number,\n): AsyncGenerator<PreferenceQueryResponseItem[], void, void> {\n let cursor: string | undefined;\n\n while (true) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const body: any = { limit: pageSize };\n if (filter && Object.keys(filter).length) body.filter = filter;\n if (cursor) body.cursor = cursor;\n\n const resp = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partition}/query`, {\n json: body,\n })\n .json(),\n {\n onRetry: (attempt, _error, message) => {\n logger.warn(\n colors.yellow(\n `Retry attempt ${attempt} for iterateConsentPages due to error: ${message}`,\n ),\n );\n },\n },\n );\n\n const { nodes, cursor: nextCursor } = decodeCodec(\n ConsentPreferenceResponse,\n resp,\n );\n if (!nodes?.length) break;\n\n yield nodes;\n\n if (!nextCursor) break;\n cursor = nextCursor;\n }\n}\n","import type { ChunkMode, PreferencesQueryFilter } from './types';\n\n/**\n * Decide which dimension to chunk on: 'timestamp' if timestamps provided, otherwise 'updated'\n *\n * @param filterBy - Filter to examine\n * @returns Chosen chunk mode\n */\nexport function pickConsentChunkMode(\n filterBy: PreferencesQueryFilter,\n): ChunkMode {\n const hasTimestamp = !!filterBy.timestampAfter || !!filterBy.timestampBefore;\n return hasTimestamp ? 'timestamp' : 'updated';\n}\n","import type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport type { ChunkMode } from './types';\n\n/**\n * Get the comparison instant for sorting based on the chosen dimension.\n *\n * @param mode - Chunking mode\n * @param item - Preference item\n * @returns date\n */\nexport function getComparisonTimeForRecord(\n mode: ChunkMode,\n item: PreferenceQueryResponseItem,\n): Date {\n if (mode === 'timestamp') {\n return new Date(item.timestamp);\n }\n // mode === 'updated'\n return item.system?.updatedAt ? new Date(item.system.updatedAt) : new Date();\n}\n","/* eslint-disable max-lines */\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { ChunkMode, PreferencesQueryFilter } from './types';\nimport { startOfUtcDay, DAY_MS } from '../helpers';\nimport { iterateConsentPages } from './iterateConsentPages';\nimport { logger } from '../../logger';\nimport { pickConsentChunkMode } from './pickConsentChunkMode';\nimport { getComparisonTimeForRecord } from './getComparisonTimeForRecord';\n\n/**\n * Get after/before bounds from filter for the given mode\n *\n * @param mode - Chunking mode\n * @param filterBy - Filter to examine\n * @returns after/before dates\n */\nexport function getBoundsFromConsentFilter(\n mode: ChunkMode,\n filterBy: PreferencesQueryFilter,\n): {\n /** After date */\n after?: Date;\n /** Before date */\n before?: Date;\n} {\n if (mode === 'timestamp') {\n return {\n after: filterBy.timestampAfter\n ? new Date(filterBy.timestampAfter)\n : undefined,\n before: filterBy.timestampBefore\n ? new Date(filterBy.timestampBefore)\n : undefined,\n };\n }\n const u = filterBy.system ?? {};\n return {\n after: u.updatedAfter ? new Date(u.updatedAfter) : undefined,\n before: u.updatedBefore ? new Date(u.updatedBefore) : undefined,\n };\n}\n\n/**\n * Merge base filter with a \"before\" bound (without mixing dimensions).\n *\n * @param mode - Chunking mode\n * @param base - Base filter to augment\n * @param beforeISO - ISO timestamp to apply as the exclusive *Before bound for the chosen dimension\n * @returns New filter with the appropriate *Before constraint applied\n */\nfunction withBeforeBound(\n mode: ChunkMode,\n base: PreferencesQueryFilter,\n beforeISO?: string,\n): PreferencesQueryFilter {\n if (mode === 'timestamp') {\n return {\n ...base,\n timestampBefore: beforeISO ?? base.timestampBefore,\n };\n }\n return {\n ...base,\n system: {\n ...(base.system || {}),\n ...(beforeISO ? { updatedBefore: beforeISO } : {}),\n },\n // ensure we don't mix dimensions\n timestampAfter: undefined,\n timestampBefore: undefined,\n };\n}\n\n/**\n * Fetch a single record (or null) with the given filter.\n *\n * @param sombra - Got instance configured for Sombra API\n * @param partition - Preference Store partition id\n * @param filter - Query filter to use (page size internally forced to 1)\n * @returns The first record or null if none\n */\nasync function fetchOne(\n sombra: Got,\n partition: string,\n filter: PreferencesQueryFilter,\n): Promise<PreferenceQueryResponseItem | null> {\n logger.info(\n colors.magenta(\n `Single-record probe with filter: ${JSON.stringify(filter)}`,\n ),\n );\n const it = iterateConsentPages(sombra, partition, filter, /* pageSize */ 1);\n const res = await it.next();\n if (res.done || !res.value || res.value.length === 0) {\n logger.info(colors.yellow('Probe result: no record'));\n return null;\n }\n const item = res.value[0]!;\n logger.info(\n colors.green(\n `Probe result: found record at ${getComparisonTimeForRecord(\n pickConsentChunkMode(filter),\n item,\n ).toISOString()}`,\n ),\n );\n return item;\n}\n\n/**\n * Robust earliest-day search (UTC):\n * 1) Anchor at the newest record (single-record probe).\n * 2) Exponential “jump back” using seeds (1d, 7d, 30d) then doubling (60d, 120d, 240d, …)\n * to cross into an empty region and establish a lower empty bound.\n * 3) **Exponential forward-from-empty**: gallop forward from the empty bound toward the last-found\n * to land close to the frontier quickly.\n * 4) Tighten with a short binary search on time using single-record probes.\n *\n * (Implementation note: preserves the public signature and docs while improving efficiency.)\n *\n * @param sombra - Sombra\n * @param opts - Options\n * @returns Earliest day with data (UTC start-of-day)\n */\nexport async function findEarliestDayWithData(\n sombra: Got,\n opts: {\n /** Partition */\n partition: string;\n /** Chunking mode */\n mode: ChunkMode;\n /** Base filter */\n baseFilter: PreferencesQueryFilter;\n /** Optional safety cap in days to avoid unbounded lookback (default ~10 years) */\n maxLookbackDays?: number;\n },\n): Promise<Date> {\n const { partition, mode, baseFilter, maxLookbackDays = 3650 } = opts;\n\n // 1) Find newest record (anchors our backtracking).\n const newest = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter),\n );\n if (!newest) {\n logger.info(\n colors.yellow('No records found; defaulting earliest day to today.'),\n );\n return startOfUtcDay(new Date());\n }\n const newestInstant = getComparisonTimeForRecord(mode, newest);\n logger.info(colors.green(`Newest instant: ${newestInstant.toISOString()}`));\n\n // 2) Exponential jump back to find an empty region.\n const seedSteps = [1, 7, 30]; // days\n let stepDaysIdx = 0;\n let stepMs = seedSteps[0] * DAY_MS;\n\n let lastFoundInstant = newestInstant; // last instant we *could* find a record before\n let emptyBeforeInstant: Date | null = null; // first bound that yielded no results\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const probeBound =\n stepDaysIdx < seedSteps.length\n ? new Date(newestInstant.getTime() - seedSteps[stepDaysIdx] * DAY_MS)\n : new Date(newestInstant.getTime() - stepMs);\n\n // stop if we exceeded lookback cap\n const daysSince =\n (startOfUtcDay(new Date()).getTime() -\n startOfUtcDay(probeBound).getTime()) /\n DAY_MS;\n if (daysSince > maxLookbackDays) {\n logger.warn(\n colors.yellow(\n `Exponential jump exceeded maxLookbackDays=${maxLookbackDays}. Using current bounds.`,\n ),\n );\n emptyBeforeInstant = probeBound;\n break;\n }\n\n logger.info(\n colors.magenta(\n `Probing before=${probeBound.toISOString()} (jump step ${\n stepDaysIdx < seedSteps.length\n ? `${seedSteps[stepDaysIdx]}d`\n : `${Math.round(stepMs / DAY_MS)}d`\n })…`,\n ),\n );\n\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, probeBound.toISOString()),\n );\n\n if (hit) {\n lastFoundInstant = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(\n `Found older record at ${lastFoundInstant.toISOString()} — continue jumping back.`,\n ),\n );\n // advance step\n if (stepDaysIdx < seedSteps.length - 1) {\n stepDaysIdx += 1;\n stepMs = seedSteps[stepDaysIdx] * DAY_MS;\n } else if (stepDaysIdx === seedSteps.length - 1) {\n stepDaysIdx += 1; // switch to doubling mode\n stepMs = seedSteps[seedSteps.length - 1] * 2 * DAY_MS; // start at 60d\n } else {\n stepMs *= 2;\n }\n // eslint-disable-next-line no-continue\n continue;\n }\n\n // crossed into an empty zone — remember this bound\n emptyBeforeInstant = probeBound;\n logger.info(\n colors.green(\n `No record before ${probeBound.toISOString()} — established empty lower bound.`,\n ),\n );\n break;\n }\n\n // Guard: if for some reason empty bound wasn't set, synthesize one “just before” lastFound.\n if (!emptyBeforeInstant) {\n emptyBeforeInstant = new Date(lastFoundInstant.getTime() - DAY_MS);\n }\n\n // 3) Exponential forward-from-empty toward the found frontier.\n // This “gallop” reduces the span dramatically before binary search.\n // We keep moving the empty bound forward with exponentially growing steps\n // until we get a hit; then we shrink onto that hit instant.\n let lo = emptyBeforeInstant; // known EMPTY (no data before this bound)\n let hi = lastFoundInstant; // known FOUND (there is data before this instant)\n let fwdStep = Math.max(\n DAY_MS,\n Math.floor((hi.getTime() - lo.getTime()) / 64),\n ); // start small-ish\n logger.info(\n colors.magenta(\n `Exponential forward-from-empty start: empty=${lo.toISOString()} found=${hi.toISOString()} step=${Math.round(\n fwdStep / DAY_MS,\n )}d`,\n ),\n );\n\n // Do a few gallop iterations (bounded so we don't loop forever if distribution is dense)\n for (let i = 0; i < 8; i += 1) {\n const probe = new Date(lo.getTime() + fwdStep);\n if (probe.getTime() >= hi.getTime()) break;\n\n logger.info(\n colors.magenta(`Forward gallop probe before=${probe.toISOString()}…`),\n );\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, probe.toISOString()),\n );\n\n if (hit) {\n // We crossed into data — tighten hi to the actual hit instant.\n hi = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(\n `Gallop hit at ${hi.toISOString()} — tightening found bound. Next step halves.`,\n ),\n );\n fwdStep = Math.max(DAY_MS, Math.floor(fwdStep / 2));\n } else {\n // Still empty up to probe — advance lo and double the step.\n lo.setTime(probe.getTime());\n logger.info(\n colors.yellow(\n `Gallop miss — advancing empty bound to ${lo.toISOString()}. Next step doubles.`,\n ),\n );\n fwdStep = Math.min(hi.getTime() - lo.getTime(), fwdStep * 2);\n if (fwdStep < DAY_MS) fwdStep = DAY_MS;\n }\n\n if (hi.getTime() - lo.getTime() <= DAY_MS) break;\n }\n\n // 4) Finish with a short binary search between [lo (empty), hi (found)].\n while (hi.getTime() - lo.getTime() > DAY_MS) {\n const mid = new Date(\n lo.getTime() + Math.floor((hi.getTime() - lo.getTime()) / 2),\n );\n logger.info(colors.magenta(`Binary probe before=${mid.toISOString()}…`));\n\n const hit = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter, mid.toISOString()),\n );\n\n if (hit) {\n const when = getComparisonTimeForRecord(mode, hit);\n logger.info(\n colors.green(`Binary probe found record at ${when.toISOString()}.`),\n );\n hi = when; // there is data before mid -> earliest could be even earlier\n } else {\n logger.info(colors.yellow('Binary probe found no record.'));\n lo = mid; // still empty -> move low up\n }\n }\n\n const earliestDay = startOfUtcDay(hi);\n logger.info(\n colors.green(\n `Earliest day (UTC) resolved to ${earliestDay.toISOString()} (instant ≈ ${hi.toISOString()}).`,\n ),\n );\n return earliestDay;\n}\n\n/**\n * Find latest day with data using exponential growth forward from earliest (UTC day math).\n *\n * (Implementation note: per your request, we now fetch a single newest record to infer the latest day.)\n *\n * @param sombra - Sombra\n * @param opts - Options\n * @returns Latest day with data\n */\nexport async function findLatestDayWithData(\n sombra: Got,\n opts: {\n /** Partition */\n partition: string;\n /** Chunking mode */\n mode: ChunkMode;\n /** Base filter */\n baseFilter: PreferencesQueryFilter;\n /** Earliest date */\n earliest: Date; // inclusive day start\n },\n): Promise<Date> {\n const { partition, mode, baseFilter } = opts;\n\n logger.info(colors.magenta('Latest-day discovery: probing newest record…'));\n const latest = await fetchOne(\n sombra,\n partition,\n withBeforeBound(mode, baseFilter),\n );\n if (!latest) {\n logger.info(\n colors.yellow('No records found at all; defaulting latest day to today.'),\n );\n return startOfUtcDay(new Date());\n }\n\n const when = getComparisonTimeForRecord(mode, latest);\n logger.info(colors.green(`Newest record instant is ${when.toISOString()}.`));\n\n const latestDay = startOfUtcDay(when);\n logger.info(\n colors.green(\n `Latest day (UTC) resolved to ${latestDay.toISOString()} from instant ${when.toISOString()}.`,\n ),\n );\n\n return latestDay;\n}\n/* eslint-enable max-lines */\n","import { FIVE_MIN_MS } from '../helpers';\nimport type { ChunkMode, PreferencesQueryFilter } from './types';\n\n/**\n * Build chunk windows by splitting [lower, upperExclusive) into up to `maxChunks`\n * equal-sized ranges, with a minimum chunk span of 5 minutes. Boundaries are snapped\n * once at the start to the nearest 5-minute boundary for stability.\n *\n * Each returned window is already \"half-open\" for an *inclusive* backend:\n * we subtract 1ms from the exclusive end so adjacent chunks do not overlap.\n *\n * Example (timestamp mode): [10:00, 12:00) → { after=10:00:00.000Z, before=11:59:59.999Z }\n *\n * @param mode - 'timestamp' or 'updated'\n * @param lower - Lower bound (inclusive)\n * @param upperExclusive - Upper bound (exclusive)\n * @param maxChunks - Maximum number of chunks to create\n * @returns Array of chunked preference query filters\n */\nexport function buildConsentChunks(\n mode: ChunkMode,\n lower: Date,\n upperExclusive: Date,\n maxChunks = 5000,\n): Array<PreferencesQueryFilter> {\n const totalMs = Math.max(0, upperExclusive.getTime() - lower.getTime());\n if (totalMs === 0) return [];\n\n // Snap only the starting boundary to the nearest 5-minute boundary.\n // We avoid re-snapping every step to prevent cumulative drift.\n const seriesStart = new Date(\n Math.floor(lower.getTime() / FIVE_MIN_MS) * FIVE_MIN_MS,\n );\n\n // Compute base chunk size (ceil to ensure ≤ maxChunks), enforced ≥ 5m.\n const rawChunkMs = Math.ceil(totalMs / Math.max(1, maxChunks));\n const chunkMs = Math.max(FIVE_MIN_MS, rawChunkMs);\n\n // Number of chunks needed to cover [seriesStart, upperExclusive)\n const count = Math.ceil(\n (upperExclusive.getTime() - seriesStart.getTime()) / chunkMs,\n );\n\n const chunks: PreferencesQueryFilter[] = [];\n\n for (let i = 0; i < count; i += 1) {\n const startMs = seriesStart.getTime() + i * chunkMs;\n const endExclusiveMs = Math.min(\n upperExclusive.getTime(),\n startMs + chunkMs,\n );\n\n // Convert exclusive end to inclusive end for an inclusive backend: -1ms.\n const endInclusiveMs = endExclusiveMs - 1;\n\n // Guard: in degenerate cases (shouldn’t happen with the math above), clamp.\n const safeEndMs = Math.max(startMs, endInclusiveMs);\n\n const afterIso = new Date(startMs).toISOString();\n const beforeIso = new Date(safeEndMs).toISOString();\n\n if (mode === 'timestamp') {\n chunks.push({\n timestampAfter: afterIso,\n timestampBefore: beforeIso,\n });\n } else {\n chunks.push({\n system: {\n updatedAfter: afterIso,\n updatedBefore: beforeIso,\n },\n });\n }\n }\n\n return chunks;\n}\n","import { map as pmap } from '../bluebird';\nimport type { Got } from 'got';\nimport cliProgress from 'cli-progress';\nimport colors from 'colors';\nimport type { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\n\nimport { PreferencesQueryFilter, ChunkMode } from './types';\nimport {\n findEarliestDayWithData,\n findLatestDayWithData,\n getBoundsFromConsentFilter,\n} from './discoverConsentWindow';\nimport { buildConsentChunks } from './buildConsentChunks';\nimport { addDaysUtc, clampPageSize } from '../helpers';\nimport { iterateConsentPages } from './iterateConsentPages';\nimport { logger } from '../../logger';\nimport { pickConsentChunkMode } from './pickConsentChunkMode';\n\n/**\n * Merge baseFilter with a window filter, taking care not to mix timestamp/updated fields improperly.\n *\n * @param mode - The chunking mode\n * @param base - The base filter\n * @param window - The per-chunk window filter\n * @returns merged filter\n */\nfunction mergeFilter(\n mode: ChunkMode,\n base: PreferencesQueryFilter,\n window: PreferencesQueryFilter,\n): PreferencesQueryFilter {\n if (mode === 'timestamp') {\n return {\n ...base,\n timestampAfter: window.timestampAfter ?? base.timestampAfter,\n timestampBefore: window.timestampBefore ?? base.timestampBefore,\n // ensure we don't pass `system.*` when chunking by timestamp\n system: undefined,\n };\n }\n // mode === 'updated'\n return {\n ...base,\n system: {\n ...(base.system || {}),\n ...(window.system?.updatedAfter\n ? { updatedAfter: window.system.updatedAfter }\n : {}),\n ...(window.system?.updatedBefore\n ? { updatedBefore: window.system.updatedBefore }\n : {}),\n },\n // Ensure we don't mix dimensions\n timestampAfter: undefined,\n timestampBefore: undefined,\n };\n}\n\n/**\n * High-level chunked fetch with progress bar.\n *\n * If an `onItems` callback is provided, pages are streamed to the callback\n * as they are fetched (no accumulation in memory). If no callback is provided,\n * the function returns all items (legacy behavior).\n *\n * @param sombra - Got instance\n * @param options - Options\n * @returns preference items (only if onItems is not provided)\n */\nexport async function fetchConsentPreferencesChunked(\n sombra: Got,\n {\n partition,\n filterBy = {},\n limit = 50,\n windowConcurrency = 25,\n maxChunks = 5000,\n maxLookbackDays = 3650,\n onItems,\n }: {\n /** Partition */\n partition: string;\n /** Filter by preferences */\n filterBy?: PreferencesQueryFilter;\n /** Limit number of results (page size) */\n limit?: number;\n /** Window concurrency */\n windowConcurrency?: number;\n /** Max chunks */\n maxChunks?: number; // up to N chunks; min 1 hour per chunk\n /** Max lookback days for discovering bounds */\n maxLookbackDays?: number;\n /** Optional streaming sink; if provided, items are not accumulated */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n onItems?: (items: PreferenceQueryResponseItem[]) => Promise<any> | any;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const mode: ChunkMode = pickConsentChunkMode(filterBy);\n logger.info(\n colors.magenta(\n `Fetching consent preferences in chunks by ${\n mode === 'timestamp' ? 'timestamp' : 'system.updatedAt'\n }...`,\n ),\n );\n\n // Resolve / discover bounds (UTC)\n let { after, before } = getBoundsFromConsentFilter(mode, filterBy);\n logger.info(\n colors.magenta(\n `Initial bounds: after=${after?.toISOString() ?? 'undefined'} before=${\n before?.toISOString() ?? 'undefined'\n }`,\n ),\n );\n\n if (!after || !before) {\n if (!after) {\n logger.info(\n colors.magenta(\n `Discovering earliest day with data for partition ${partition}...`,\n ),\n );\n after = await findEarliestDayWithData(sombra, {\n partition,\n mode,\n baseFilter: filterBy,\n maxLookbackDays,\n });\n logger.info(\n colors.green(\n `Discovered earliest day with data: ${after.toISOString()}`,\n ),\n );\n }\n if (!before) {\n logger.info(\n colors.magenta(\n `Discovering latest day with data for partition ${partition}...`,\n ),\n );\n const latestDay = await findLatestDayWithData(sombra, {\n partition,\n mode,\n baseFilter: filterBy,\n earliest: after,\n });\n // Exclusive upper bound = latest day start + 1 day (UTC)\n before = addDaysUtc(latestDay, 1);\n logger.info(\n colors.green(\n `Discovered latest day with data: ${latestDay.toISOString()}`,\n ),\n );\n }\n }\n\n logger.info(\n colors.green(\n `Final bounds (UTC): after=${after.toISOString()} before=${before.toISOString()}`,\n ),\n );\n\n // Build up to `maxChunks` chunks, min 1 hour each\n const chunks = buildConsentChunks(mode, after, before, maxChunks);\n\n logger.info(\n colors.magenta(\n `Fetching consent preferences from partition ${partition} in ${chunks.length} chunks...`,\n ),\n );\n\n // Progress bar over chunks (unordered):\n // - value = completed chunks (out-of-order OK)\n // - payload fetched = total records fetched\n const bar = new cliProgress.SingleBar(\n {\n format:\n 'Downloading [{bar}] {percentage}% | chunks {value}/{total} | fetched {fetched}',\n },\n cliProgress.Presets.shades_classic,\n );\n\n let completed = 0; // finished chunks (out-of-order)\n let fetched = 0; // raw records counter\n\n bar.start(chunks.length, 0, { fetched });\n\n const t0 = Date.now();\n const pageSize = clampPageSize(limit);\n\n // If we are streaming, do not accumulate everything in memory.\n const out: PreferenceQueryResponseItem[] = [];\n\n await pmap(\n chunks.map((windowFilter, idx) => ({ windowFilter, idx })),\n async ({ windowFilter }) => {\n const filter = mergeFilter(mode, filterBy, windowFilter);\n\n // Stream this chunk page-by-page\n for await (const page of iterateConsentPages(\n sombra,\n partition,\n filter,\n pageSize,\n )) {\n fetched += page.length;\n bar.update(completed, { fetched });\n\n if (onItems) {\n await onItems(page);\n } else {\n out.push(...page);\n }\n }\n\n completed += 1;\n bar.update(completed, { fetched });\n },\n { concurrency: Math.max(1, windowConcurrency) },\n );\n\n bar.update(completed, { fetched });\n bar.stop();\n\n logger.info(\n colors.green(\n `Fetched ${fetched} consent preference records from partition ${partition} in ${\n (Date.now() - t0) / 1000\n }s.`,\n ),\n );\n\n return onItems ? [] : out;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { PreferenceQueryResponseItem } from '@transcend-io/privacy-types';\nimport { ConsentPreferenceResponse, PreferencesQueryFilter } from './types';\nimport { withPreferenceRetry } from './withPreferenceRetry';\nimport { logger } from '../../logger';\n\n/**\n * Fetch consent preferences for the managed consent database (new query endpoint)\n *\n * Uses POST /v1/preferences/{partition}/query with cursor pagination.\n *\n * If `onItems` is provided, this streams pages to the callback and does not\n * accumulate results in memory. If omitted, the function returns all items.\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Query options\n * @returns All nodes (only when onItems is not provided)\n */\nexport async function fetchConsentPreferences(\n sombra: Got,\n {\n partition,\n filterBy = {},\n limit = 50,\n onItems,\n }: {\n /** Partition key to fetch (moved to URL path on new endpoint) */\n partition: string;\n /** Query filter (wrapped under \"filter\" in request body) */\n filterBy?: PreferencesQueryFilter;\n /** Number of users per page (1–50 per API spec) */\n limit?: number;\n /** Optional streaming sink; if provided, pages are not accumulated */\n onItems?: (items: PreferenceQueryResponseItem[]) => Promise<void> | void;\n },\n): Promise<PreferenceQueryResponseItem[]> {\n const collected: PreferenceQueryResponseItem[] = [];\n\n // Cursor-based pagination per new endpoint\n let cursor: string | undefined;\n\n // Build the filter payload, omitting empty filter\n const hasFilter =\n filterBy &&\n (Object.keys(filterBy).length > 0 ||\n (filterBy.system && Object.keys(filterBy.system).length > 0));\n\n // Enforce API max (defensive; backend also validates)\n const pageSize = Math.max(1, Math.min(50, limit ?? 50));\n\n // Keep fetching until no cursor is returned\n // (The API returns an opaque cursor string for the next page)\n // eslint-disable-next-line no-constant-condition\n while (true) {\n const body: {\n /** Filter by user identifiers (new shape) */\n filter?: PreferencesQueryFilter;\n /** Cursor for pagination */\n cursor?: string;\n /** Number of records per page */\n limit: number;\n } = { limit: pageSize };\n\n if (hasFilter) {\n body.filter = filterBy;\n }\n if (cursor) {\n body.cursor = cursor;\n }\n\n const response = await withPreferenceRetry(\n 'Preference Query',\n () =>\n sombra\n .post(`v1/preferences/${partition}/query`, {\n json: body,\n })\n .json(),\n {\n onRetry: (attempt, _error, message) => {\n logger.warn(\n colors.yellow(\n `Retry attempt ${attempt} for fetchConsentPreferences due to error: ${message}`,\n ),\n );\n },\n },\n );\n\n const { nodes, cursor: nextCursor } = decodeCodec(\n ConsentPreferenceResponse,\n response,\n );\n\n if (!nodes || nodes.length === 0) {\n break;\n }\n\n if (onItems) {\n await onItems(nodes);\n } else {\n collected.push(...nodes);\n }\n\n if (!nextCursor) {\n break;\n }\n cursor = nextCursor;\n }\n\n return onItems ? [] : collected;\n}\n","import { decodeCodec } from '@transcend-io/type-utils';\nimport colors from 'colors';\nimport type { Got } from 'got';\nimport { logger } from '../../logger';\nimport {\n DeletePreferenceRecordCliCsvRow,\n DeletePreferenceRecordsResponse,\n} from './codecs';\nimport { readCsv } from '../requests';\nimport { chunk } from 'lodash-es';\nimport { map } from '../bluebird';\nimport { withPreferenceRetry } from './withPreferenceRetry';\n\ninterface FailedResult extends DeletePreferenceRecordCliCsvRow {\n /** Error message describing the failure */\n error: string;\n}\n\ninterface DeletePreferenceRecordsRepositoryOptions {\n /** The partition to delete from */\n partition: string;\n /** Chunk of identifiers to delete */\n identifierChunk: DeletePreferenceRecordCliCsvRow[];\n /** the timestamp for the deletion operation */\n timestamp: Date;\n}\n\n/**\n * Options for deleting preference records\n */\ntype DeletePreferenceRecordsOptions = Omit<\n DeletePreferenceRecordsRepositoryOptions,\n 'identifierChunk'\n> & {\n /** The file path to read CSV rows from */\n filePath: string;\n /** Maximum items to include in each deletion chunk */\n maxItemsInChunk: number;\n /** Maximum concurrency for deletion requests */\n maxConcurrency: number;\n};\n\n/**\n *\n * Delete a chunk of preference records\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Options for deletion\n * @param options.partition - The partition to delete from\n * @param options.identifierChunk - Chunk of identifiers to delete\n * @param options.timestamp - The timestamp for the deletion operation\n * @returns List of failed deletions\n */\nasync function deletePreferenceRecordsRepository(\n sombra: Got,\n {\n partition,\n identifierChunk: chunk,\n timestamp,\n }: DeletePreferenceRecordsRepositoryOptions,\n): Promise<FailedResult[]> {\n try {\n const response = await withPreferenceRetry(\n 'Delete Preference Records',\n () =>\n sombra\n .post(`v1/preferences/${partition}/delete`, {\n json: {\n records: chunk.map((record) => ({\n anchorIdentifier: record,\n timestamp: timestamp.toISOString(),\n })),\n },\n })\n .json(),\n {\n maxAttempts: 3,\n onRetry: (attempt, _err, msg) => {\n logger.warn(\n colors.yellow(\n `Attempt ${attempt} to delete preference records failed: ${msg}`,\n ),\n );\n },\n },\n );\n const { failures } = decodeCodec(DeletePreferenceRecordsResponse, response);\n if (failures.length > 0) {\n return failures.map(({ index, error }) => ({\n ...chunk[index],\n error,\n }));\n }\n return [];\n } catch (err) {\n return chunk.map((record) => ({\n ...record,\n error: (err as Error).message,\n }));\n }\n}\n\n/**\n * Delete consent preferences for the managed consent database (delete endpoint)\n *\n * Uses POST /v1/preferences/{partition}/delete.\n *\n *\n * @param sombra - Sombra instance (must include auth headers)\n * @param options - Query options\n * @returns All nodes (only when onItems is not provided)\n */\nexport async function bulkDeletePreferenceRecords(\n sombra: Got,\n {\n partition,\n filePath,\n timestamp,\n maxItemsInChunk,\n maxConcurrency,\n }: DeletePreferenceRecordsOptions,\n): Promise<FailedResult[]> {\n const anchorIdentifiers = readCsv(filePath, DeletePreferenceRecordCliCsvRow);\n const chunks = chunk(anchorIdentifiers, maxItemsInChunk);\n\n const failedResults = await map(\n chunks,\n async (identifierChunk) => {\n const failedResults = await deletePreferenceRecordsRepository(sombra, {\n partition,\n identifierChunk,\n timestamp,\n });\n return failedResults;\n },\n { concurrency: maxConcurrency },\n );\n return failedResults.flat();\n}\n"],"mappings":"mbAQA,MAAa,EAAkC,CAC7C,YACA,aACA,YACA,kBACA,uBACA,MACA,sBACA,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