Skip to content

Commit

Permalink
Fixes cli command for consent upload and adds example (#316)
Browse files Browse the repository at this point in the history
* Fixes filter by

* Consent push fix
  • Loading branch information
michaelfarrell76 authored Mar 22, 2024
1 parent b7831f7 commit 094248f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2079,9 +2079,12 @@ Each row in the CSV must include:
| timestamp | Timestamp for when consent was collected for that user | string - timestamp | N/A | true |
| purposes | JSON map from consent purpose name -> boolean indicating whether user has opted in or out of that purpose | {[k in string]: boolean } | {} | false |
| confirmed | Whether consent preferences have been explicitly confirmed or inferred | boolean | true | false |
| updated | Time consent preferences were last updated | string - timestamp | N/A | false |
| updated | Has the consent been updated (including no-change confirmation) since default resolution | boolean | N/A | false |
| prompted | Whether or not the UI has been shown to the end-user (undefined in older versions of airgap.js) | boolean | N/A | false |
| usp | US Privacy string | string - USP | N/A | false |

An sample CSV can be found [here](./examples/preference-upload.csv).

#### Authentication

In order to use this cli, you will first need to follow [this guide](https://docs.transcend.io/docs/consent/reference/managed-consent-database#authenticate-a-user's-consent) in order
Expand Down
2 changes: 2 additions & 0 deletions examples/preference-upload.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
userId,timestamp,purposes,confirmed,updated,usp
[email protected],2024-03-11T19:32:31.707Z,"{""Analytics"":true,""SaleOfInfo"":true,""Advertising"":true,""Essential"":true,""Functional"":true,""TestConsent"":true,""VisibleNotConfigurable"":true}",true,true,"1YYY"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Transcend Inc.",
"name": "@transcend-io/cli",
"description": "Small package containing useful typescript utilities.",
"version": "4.132.2",
"version": "4.132.3",
"homepage": "https://github.com/transcend-io/cli",
"repository": {
"type": "git",
Expand Down
100 changes: 84 additions & 16 deletions src/consent-manager/uploadConsentPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import { map } from 'bluebird';
import { createConsentToken } from './createConsentToken';
import { logger } from '../logger';
import cliProgress from 'cli-progress';
import { decodeCodec } from '@transcend-io/type-utils';

export const USP_STRING_REGEX = /^[0-9][Y|N]([Y|N])[Y|N]$/;

export const PurposeMap = t.record(
t.string,
t.union([t.boolean, t.literal('Auto')]),
);

export const ManagedConsentDatabaseConsentPreference = t.intersection([
t.type({
/** User ID */
Expand All @@ -17,14 +23,21 @@ export const ManagedConsentDatabaseConsentPreference = t.intersection([
timestamp: t.string,
}),
t.partial({
/** Purpose map */
purposes: t.record(t.string, t.union([t.boolean, t.literal('Auto')])),
/**
* Purpose map
* This is a JSON object with keys as purpose names and values as booleans or 'Auto'
*/
purposes: t.string,
/** Was tracking consent confirmed by the user? If this is false, the consent was resolved from defaults & is not yet confirmed */
confirmed: t.boolean,
/** Time updated */
updated: t.boolean,
/** Whether or not the UI has been shown to the end-user (undefined in older versions of airgap.js) */
prompted: t.boolean,
confirmed: t.union([t.literal('true'), t.literal('false')]),
/**
* Has the consent been updated (including no-change confirmation) since default resolution
*/
updated: t.union([t.literal('true'), t.literal('false')]),
/**
* Whether or not the UI has been shown to the end-user (undefined in older versions of airgap.js)
*/
prompted: t.union([t.literal('true'), t.literal('false')]),
/** US Privacy (USP) String */
usp: t.string,
}),
Expand Down Expand Up @@ -78,6 +91,33 @@ export async function uploadConsentPreferences({
);
}

// Ensure purpose maps are valid
const invalidPurposeMaps = preferences
.map(
(pref, ind) =>
[pref, ind] as [ManagedConsentDatabaseConsentPreference, number],
)
.filter(([pref]) => {
if (!pref.purposes) {
return false;
}
try {
decodeCodec(PurposeMap, pref.purposes);
return false;
} catch {
return true;
}
});
if (invalidPurposeMaps.length > 0) {
throw new Error(
`Received invalid purpose maps: ${JSON.stringify(
invalidPurposeMaps,
null,
2,
)}`,
);
}

// Ensure usp or preferences are provided
const invalidInputs = preferences.filter(
(pref) => !pref.usp && !pref.purposes,
Expand Down Expand Up @@ -111,7 +151,14 @@ export async function uploadConsentPreferences({
progressBar.start(preferences.length, 0);
await map(
preferences,
async ({ userId, confirmed = true, purposes, ...consent }) => {
async ({
userId,
confirmed = 'true',
updated,
prompted,
purposes,
...consent
}) => {
const token = createConsentToken(
userId,
base64EncryptionKey,
Expand All @@ -127,19 +174,40 @@ export async function uploadConsentPreferences({
token,
partition,
consent: {
confirmed,
purposes:
purposes || (consent.usp ? { SaleOfInfo: saleStatus === 'Y' } : {}),
confirmed: confirmed === 'true',
purposes: purposes
? decodeCodec(PurposeMap, purposes)
: consent.usp
? { SaleOfInfo: saleStatus === 'Y' }
: {},
...(updated ? { updated: updated === 'true' } : {}),
...(prompted ? { prompted: prompted === 'true' } : {}),
...consent,
},
};

// Make the request
await transcendConsentApi
.post('sync', {
json: input,
})
.json();
try {
await transcendConsentApi
.post('sync', {
json: input,
})
.json();
} catch (err) {
try {
const parsed = JSON.parse(err?.response?.body || '{}');
if (parsed.error) {
logger.error(colors.red(`Error: ${parsed.error}`));
}
} catch (e) {
// continue
}
throw new Error(
`Received an error from server: ${
err?.response?.body || err?.message
}`,
);
}

total += 1;
progressBar.update(total);
Expand Down

0 comments on commit 094248f

Please sign in to comment.