@simmalugnt-se/payload-consent 0.0.0-stage → 0.2.0

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +21 -0
  3. package/README.md +242 -2
  4. package/dist/admin/ScanPanel.d.ts +3 -0
  5. package/dist/admin/ScanPanel.js +232 -0
  6. package/dist/admin/ServicesPanel.d.ts +11 -0
  7. package/dist/admin/ServicesPanel.js +136 -0
  8. package/dist/block.d.ts +6 -0
  9. package/dist/block.js +27 -0
  10. package/dist/defaults.d.ts +56 -0
  11. package/dist/defaults.js +137 -0
  12. package/dist/evaluate.d.ts +106 -0
  13. package/dist/evaluate.js +101 -0
  14. package/dist/evidence.d.ts +12 -0
  15. package/dist/evidence.js +114 -0
  16. package/dist/exports/admin.d.ts +2 -0
  17. package/dist/exports/admin.js +2 -0
  18. package/dist/exports/client.d.ts +37 -0
  19. package/dist/exports/client.js +180 -0
  20. package/dist/exports/rsc.d.ts +1 -0
  21. package/dist/exports/rsc.js +1 -0
  22. package/dist/global.d.ts +14 -0
  23. package/dist/global.js +317 -0
  24. package/dist/index.d.ts +13 -0
  25. package/dist/index.js +75 -0
  26. package/dist/integrations.d.ts +40 -0
  27. package/dist/integrations.js +32 -0
  28. package/dist/notify.d.ts +15 -0
  29. package/dist/notify.js +86 -0
  30. package/dist/records.d.ts +15 -0
  31. package/dist/records.js +198 -0
  32. package/dist/scans.d.ts +37 -0
  33. package/dist/scans.js +424 -0
  34. package/dist/schedule.d.ts +17 -0
  35. package/dist/schedule.js +18 -0
  36. package/dist/services.d.ts +8 -0
  37. package/dist/services.js +258 -0
  38. package/dist/site-config.d.ts +128 -0
  39. package/dist/site-config.js +204 -0
  40. package/dist/styles/admin.css +202 -0
  41. package/dist/styles/consent.css +301 -0
  42. package/dist/types.d.ts +65 -0
  43. package/dist/types.js +17 -0
  44. package/dist/widget/ConsentScanWidget.d.ts +8 -0
  45. package/dist/widget/ConsentScanWidget.js +99 -0
  46. package/package.json +76 -3
package/dist/global.js ADDED
@@ -0,0 +1,317 @@
1
+ import { defaultTexts, OPTIONAL_CATEGORIES } from "./defaults.js";
2
+ import { CONSENT_GLOBAL } from "./types.js";
3
+ export const categoryLabels = {
4
+ necessary: { en: "Necessary", sv: "Nödvändiga" },
5
+ functionality: { en: "Functionality", sv: "Funktionalitet" },
6
+ experience: { en: "Experience", sv: "Upplevelse" },
7
+ measurement: { en: "Statistics", sv: "Statistik" },
8
+ marketing: { en: "Marketing", sv: "Marknadsföring" },
9
+ };
10
+ const hasOptional = (data) => Boolean(data?.categories?.length);
11
+ /**
12
+ * A localized text prefilled with the suggestion for the language being edited. Not required: an
13
+ * empty text shows the suggestion, and adding a text later needs no value for saved documents.
14
+ */
15
+ function text(name, label, suggestion, extra = {}) {
16
+ return {
17
+ name,
18
+ type: extra.textarea ? "textarea" : "text",
19
+ label,
20
+ localized: true,
21
+ defaultValue: ({ locale }) => suggestion(defaultTexts(locale)),
22
+ admin: {
23
+ ...(extra.width ? { width: extra.width } : {}),
24
+ ...(extra.description ? { description: extra.description } : {}),
25
+ ...(extra.condition ? { condition: (data) => extra.condition?.(data) ?? true } : {}),
26
+ },
27
+ };
28
+ }
29
+ const BUTTON_WIDTH = "33%";
30
+ const withChoices = {
31
+ description: {
32
+ en: "Shown when visitors can choose optional categories.",
33
+ sv: "Visas när besökaren kan välja valfria kategorier.",
34
+ },
35
+ condition: hasOptional,
36
+ };
37
+ const necessaryOnly = {
38
+ description: {
39
+ en: "Shown when the site only uses necessary storage.",
40
+ sv: "Visas när webbplatsen bara använder nödvändig lagring.",
41
+ },
42
+ condition: (data) => !hasOptional(data),
43
+ };
44
+ export function consentGlobal(options, { usersSlug, schedule }) {
45
+ const time = `${String(schedule.hour).padStart(2, "0")}:${String(schedule.minute).padStart(2, "0")}`;
46
+ const legal = options.legalPages;
47
+ const global = {
48
+ slug: CONSENT_GLOBAL,
49
+ label: { en: "Cookies", sv: "Cookies" },
50
+ admin: { group: options.group ?? { en: "Settings", sv: "Inställningar" } },
51
+ access: { read: () => true, ...options.access },
52
+ fields: [
53
+ {
54
+ type: "tabs",
55
+ tabs: [
56
+ {
57
+ label: { en: "Banner", sv: "Banner" },
58
+ description: {
59
+ en: "What visitors see before they choose. Accept and reject are always shown side by side.",
60
+ sv: "Det besökaren ser innan hen har valt. Godkänn och avvisa visas alltid sida vid sida.",
61
+ },
62
+ fields: [
63
+ text("bannerTitle", { en: "Title", sv: "Rubrik" }, (t) => t.bannerTitle),
64
+ text("bannerDescription", { en: "Description", sv: "Beskrivning" }, (t) => t.bannerDescription, { textarea: true, ...withChoices }),
65
+ text("bannerDescriptionNecessaryOnly", { en: "Description", sv: "Beskrivning" }, (t) => t.bannerDescriptionNecessaryOnly, { textarea: true, ...necessaryOnly }),
66
+ {
67
+ name: "buttons",
68
+ type: "group",
69
+ label: { en: "Buttons", sv: "Knappar" },
70
+ fields: [
71
+ {
72
+ type: "row",
73
+ fields: [
74
+ text("acceptAll", { en: "Accept all", sv: "Godkänn alla" }, (t) => t.buttons.acceptAll, { condition: hasOptional, width: BUTTON_WIDTH }),
75
+ text("acceptNecessaryOnly", { en: "Accept", sv: "Godkänn" }, (t) => t.buttons.acceptNecessaryOnly, { condition: necessaryOnly.condition, width: BUTTON_WIDTH }),
76
+ text("rejectAll", { en: "Reject optional", sv: "Avvisa valfria" }, (t) => t.buttons.rejectAll, { condition: hasOptional, width: BUTTON_WIDTH }),
77
+ ],
78
+ },
79
+ {
80
+ type: "row",
81
+ fields: [
82
+ text("customize", { en: "Settings", sv: "Inställningar" }, (t) => t.buttons.customize, { width: BUTTON_WIDTH }),
83
+ text("save", { en: "Save choices", sv: "Spara val" }, (t) => t.buttons.save, {
84
+ width: BUTTON_WIDTH,
85
+ }),
86
+ text("close", { en: "Close", sv: "Stäng" }, (t) => t.buttons.close, {
87
+ width: BUTTON_WIDTH,
88
+ }),
89
+ ],
90
+ },
91
+ ],
92
+ },
93
+ {
94
+ name: "dialog",
95
+ type: "group",
96
+ label: { en: "Settings dialog", sv: "Inställningsdialog" },
97
+ fields: [
98
+ text("title", { en: "Title", sv: "Rubrik" }, (t) => t.dialog.title),
99
+ text("description", { en: "Description", sv: "Beskrivning" }, (t) => t.dialog.description, { textarea: true, ...withChoices }),
100
+ text("descriptionNecessaryOnly", { en: "Description", sv: "Beskrivning" }, (t) => t.dialog.descriptionNecessaryOnly, { textarea: true, ...necessaryOnly }),
101
+ ],
102
+ },
103
+ {
104
+ name: "embeds",
105
+ type: "group",
106
+ label: { en: "Embedded content", sv: "Inbäddat innehåll" },
107
+ admin: {
108
+ description: {
109
+ en: "Shown in place of a video, map or other embed until the visitor allows its category. {category} becomes the category's title.",
110
+ sv: "Visas i stället för en video, karta eller annan inbäddning tills besökaren tillåter dess kategori. {category} blir kategorins rubrik.",
111
+ },
112
+ },
113
+ fields: [
114
+ {
115
+ type: "row",
116
+ fields: [
117
+ text("title", { en: "Text", sv: "Text" }, (t) => t.embeds.title, {
118
+ width: "66%",
119
+ }),
120
+ text("button", { en: "Button", sv: "Knapp" }, (t) => t.embeds.button, {
121
+ width: "34%",
122
+ }),
123
+ ],
124
+ },
125
+ ],
126
+ },
127
+ {
128
+ name: "categoryTexts",
129
+ type: "group",
130
+ label: { en: "Categories", sv: "Kategorier" },
131
+ fields: ["necessary", ...OPTIONAL_CATEGORIES].map((category) => ({
132
+ name: category,
133
+ type: "group",
134
+ label: categoryLabels[category],
135
+ admin: category === "necessary"
136
+ ? {}
137
+ : {
138
+ condition: (data) => Boolean(data?.categories?.includes(category)),
139
+ },
140
+ fields: [
141
+ text("title", { en: "Title", sv: "Rubrik" }, (t) => t.categoryTexts[category].title),
142
+ text("description", { en: "Description", sv: "Beskrivning" }, (t) => t.categoryTexts[category].description, { textarea: true }),
143
+ ],
144
+ })),
145
+ },
146
+ ],
147
+ },
148
+ {
149
+ label: { en: "Services", sv: "Tjänster" },
150
+ description: {
151
+ en: "Every service that stores something in the visitor's browser or that the site contacts. The banner's categories and the cookie table follow this list.",
152
+ sv: "Varje tjänst som sparar något i besökarens webbläsare eller som webbplatsen kontaktar. Bannerns kategorier och cookietabellen följer listan.",
153
+ },
154
+ fields: [
155
+ {
156
+ name: "servicesPanel",
157
+ type: "ui",
158
+ admin: {
159
+ components: { Field: "@simmalugnt-se/payload-consent/admin#ServicesPanel" },
160
+ },
161
+ },
162
+ ],
163
+ },
164
+ {
165
+ label: { en: "Scan", sv: "Skanning" },
166
+ description: {
167
+ en: "Scans the published site as a new visitor, after rejecting and after accepting, and compares what it finds with the Cookie services.",
168
+ sv: "Skannar den publicerade webbplatsen som ny besökare, efter avvisa och efter godkänn, och jämför det som hittas med Cookietjänsterna.",
169
+ },
170
+ fields: [
171
+ {
172
+ name: "scanUrl",
173
+ type: "text",
174
+ label: { en: "Site address", sv: "Webbplatsens adress" },
175
+ admin: {
176
+ placeholder: "https://",
177
+ description: {
178
+ en: "The published site's start page. Empty: the site's own address (serverURL).",
179
+ sv: "Den publicerade webbplatsens startsida. Tom: webbplatsens egen adress (serverURL).",
180
+ },
181
+ },
182
+ },
183
+ {
184
+ name: "scheduledScans",
185
+ type: "checkbox",
186
+ defaultValue: true,
187
+ label: { en: "Scan every month", sv: "Skanna varje månad" },
188
+ admin: {
189
+ description: {
190
+ en: `On day ${schedule.day} of each month, the first time the host's cron runs Payload's jobs after ${time} server time.`,
191
+ sv: `Den ${schedule.day}:e varje månad, första gången värdens cron kör Payloads jobb efter kl. ${time} servertid.`,
192
+ },
193
+ },
194
+ },
195
+ {
196
+ name: "notify",
197
+ type: "relationship",
198
+ relationTo: usersSlug,
199
+ hasMany: true,
200
+ label: { en: "Email about scans", sv: "Mejla om skanningar" },
201
+ admin: {
202
+ description: {
203
+ en: "Gets an email when a scan fails, finds something to handle or finds something new.",
204
+ sv: "Får ett mejl när en skanning misslyckas, hittar något att åtgärda eller hittar något nytt.",
205
+ },
206
+ },
207
+ },
208
+ {
209
+ name: "scanPanel",
210
+ type: "ui",
211
+ admin: {
212
+ components: { Field: "@simmalugnt-se/payload-consent/admin#ScanPanel" },
213
+ },
214
+ },
215
+ {
216
+ name: "scanLinks",
217
+ type: "ui",
218
+ admin: {
219
+ components: { Field: "@simmalugnt-se/payload-consent/admin#ScanLinks" },
220
+ },
221
+ },
222
+ ],
223
+ },
224
+ ...(options.records === false
225
+ ? []
226
+ : [
227
+ {
228
+ label: { en: "Consent log", sv: "Samtyckeslogg" },
229
+ description: {
230
+ en: "Every choice a visitor saves, with the policy version they chose under and what it showed them. Nobody can change or delete a record.",
231
+ sv: "Varje val en besökare sparar, med policyversionen valet gjordes under och vad den visade. Ingen kan ändra eller ta bort en post.",
232
+ },
233
+ fields: [
234
+ {
235
+ name: "consentLog",
236
+ type: "ui",
237
+ admin: {
238
+ components: {
239
+ Field: "@simmalugnt-se/payload-consent/admin#ConsentLogPanel",
240
+ },
241
+ },
242
+ },
243
+ ],
244
+ },
245
+ ]),
246
+ {
247
+ label: { en: "Settings", sv: "Inställningar" },
248
+ fields: [
249
+ {
250
+ name: "enabled",
251
+ type: "checkbox",
252
+ label: { en: "Show the cookie banner", sv: "Visa cookiebannern" },
253
+ defaultValue: true,
254
+ },
255
+ {
256
+ name: "categories",
257
+ type: "select",
258
+ hasMany: true,
259
+ label: { en: "Optional categories", sv: "Valfria kategorier" },
260
+ admin: {
261
+ readOnly: true,
262
+ description: {
263
+ en: "Follows the categories of the Cookie services. With none, the banner only informs and has an Okay button.",
264
+ sv: "Följer kategorierna under Cookietjänster. Utan valfria kategorier informerar bannern bara och har en Okej-knapp.",
265
+ },
266
+ },
267
+ options: OPTIONAL_CATEGORIES.map((value) => ({
268
+ value,
269
+ label: categoryLabels[value],
270
+ })),
271
+ },
272
+ {
273
+ name: "googleConsentMode",
274
+ type: "radio",
275
+ defaultValue: "basic",
276
+ label: { en: "Google Consent Mode", sv: "Google Consent Mode" },
277
+ options: [
278
+ {
279
+ value: "basic",
280
+ label: {
281
+ en: "Basic: nothing loads before consent",
282
+ sv: "Basic: inget laddas före samtycke",
283
+ },
284
+ },
285
+ {
286
+ value: "advanced",
287
+ label: {
288
+ en: "Advanced: Google and similar tags load at once in a denied state",
289
+ sv: "Advanced: Google och liknande taggar laddas direkt i nekat läge",
290
+ },
291
+ },
292
+ ],
293
+ admin: {
294
+ description: {
295
+ en: "Applies to the services the plugin loads. Advanced sends cookieless measurements to the provider before the visitor has chosen; pick it only when that has been assessed as acceptable.",
296
+ sv: "Gäller tjänsterna som pluginet laddar. Advanced skickar mätningar utan cookies till leverantören innan besökaren har valt; välj det bara när det har bedömts som godtagbart.",
297
+ },
298
+ },
299
+ },
300
+ ...(legal
301
+ ? ["privacyPolicy", "cookiePolicy"].map((name) => ({
302
+ name,
303
+ type: "relationship",
304
+ relationTo: legal.collection,
305
+ label: name === "privacyPolicy"
306
+ ? { en: "Privacy policy", sv: "Integritetspolicy" }
307
+ : { en: "Cookie policy", sv: "Cookiepolicy" },
308
+ }))
309
+ : []),
310
+ ],
311
+ },
312
+ ],
313
+ },
314
+ ],
315
+ };
316
+ return options.overrideGlobal ? options.overrideGlobal(global) : global;
317
+ }
@@ -0,0 +1,13 @@
1
+ import type { Plugin } from "payload";
2
+ import { type ConsentPluginOptions } from "./types.ts";
3
+ export { cookieTableBlock } from "./block.ts";
4
+ export { CATEGORIES, type Category, defaultTexts, OPTIONAL_CATEGORIES } from "./defaults.ts";
5
+ export { evaluateScan, type Finding, type FindingStatus, type ScanResultState, type ScanState, } from "./evaluate.ts";
6
+ export { INTEGRATIONS, type IntegrationKey } from "./integrations.ts";
7
+ export { notifyScan, type ScanOutcome } from "./notify.ts";
8
+ export { type ConsentRecordInput, parseRecord } from "./records.ts";
9
+ export { scanStates, startScan } from "./scans.ts";
10
+ export { nextScan, type ScanSchedule, scanSchedule } from "./schedule.ts";
11
+ export { type ConsentConfig, type ConsentDocument, type CookieTableData, getConsentConfig, getCookieTable, policyVersion, type ServiceDocument, toConsentConfig, toCookieTable, } from "./site-config.ts";
12
+ export { CONSENT_GLOBAL, CONSENT_POLICIES, CONSENT_RECORDS, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, type ConsentPluginOptions, SCAN_TASK, SCAN_WIDGET, } from "./types.ts";
13
+ export declare function consentPlugin(options?: ConsentPluginOptions): Plugin;
package/dist/index.js ADDED
@@ -0,0 +1,75 @@
1
+ import { evidenceCollection } from "./evidence.js";
2
+ import { consentGlobal } from "./global.js";
3
+ import { policiesCollection, recordsCollection } from "./records.js";
4
+ import { scansCollection, scanTask } from "./scans.js";
5
+ import { scanSchedule } from "./schedule.js";
6
+ import { servicesCollection } from "./services.js";
7
+ import { CONSENT_GLOBAL, CONSENT_POLICIES, CONSENT_RECORDS, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, SCAN_WIDGET, } from "./types.js";
8
+ export { cookieTableBlock } from "./block.js";
9
+ export { CATEGORIES, defaultTexts, OPTIONAL_CATEGORIES } from "./defaults.js";
10
+ export { evaluateScan, } from "./evaluate.js";
11
+ export { INTEGRATIONS } from "./integrations.js";
12
+ export { notifyScan } from "./notify.js";
13
+ export { parseRecord } from "./records.js";
14
+ export { scanStates, startScan } from "./scans.js";
15
+ export { nextScan, scanSchedule } from "./schedule.js";
16
+ export { getConsentConfig, getCookieTable, policyVersion, toConsentConfig, toCookieTable, } from "./site-config.js";
17
+ export { CONSENT_GLOBAL, CONSENT_POLICIES, CONSENT_RECORDS, CONSENT_SCAN_EVIDENCE, CONSENT_SCANS, CONSENT_SERVICES, SCAN_TASK, SCAN_WIDGET, } from "./types.js";
18
+ const widget = {
19
+ slug: SCAN_WIDGET,
20
+ Component: "@simmalugnt-se/payload-consent/rsc#ConsentScanWidget",
21
+ label: { en: "Cookie scan", sv: "Cookieskanning" },
22
+ minWidth: "small",
23
+ };
24
+ export function consentPlugin(options = {}) {
25
+ return (config) => {
26
+ if (options.disabled)
27
+ return config;
28
+ if (config.globals?.some((global) => global.slug === CONSENT_GLOBAL))
29
+ throw new Error(`The ${CONSENT_GLOBAL} global slug is already in use`);
30
+ for (const slug of [
31
+ CONSENT_SERVICES,
32
+ CONSENT_RECORDS,
33
+ CONSENT_POLICIES,
34
+ CONSENT_SCANS,
35
+ CONSENT_SCAN_EVIDENCE,
36
+ ])
37
+ if (config.collections?.some((collection) => collection.slug === slug))
38
+ throw new Error(`The ${slug} collection slug is already in use`);
39
+ if (options.legalPages &&
40
+ !config.collections?.some((collection) => collection.slug === options.legalPages?.collection))
41
+ throw new Error(`Unknown legal pages collection: ${options.legalPages.collection}`);
42
+ // The site's own address spreads monthly scans over the month; the secret when it has none.
43
+ const schedule = scanSchedule(config.serverURL || config.secret || CONSENT_GLOBAL);
44
+ const usersSlug = config.admin?.user ?? "users";
45
+ return {
46
+ ...config,
47
+ collections: [
48
+ ...(config.collections ?? []),
49
+ servicesCollection(options),
50
+ ...(options.records === false
51
+ ? []
52
+ : [recordsCollection(options), policiesCollection(options)]),
53
+ scansCollection(options),
54
+ evidenceCollection(options),
55
+ ],
56
+ globals: [...(config.globals ?? []), consentGlobal(options, { usersSlug, schedule })],
57
+ // The monthly scan runs through Payload's jobs; added with or without a scanner, so the
58
+ // schema does not depend on the environment.
59
+ jobs: {
60
+ ...config.jobs,
61
+ tasks: [...(config.jobs?.tasks ?? []), scanTask(options, schedule)],
62
+ },
63
+ custom: { ...config.custom, consent: options, consentSchedule: schedule },
64
+ admin: {
65
+ ...config.admin,
66
+ // What the Scan tab needs to know in the browser; the token stays on the server.
67
+ custom: { ...config.admin?.custom, consent: { scanner: Boolean(options.scanner) } },
68
+ dashboard: {
69
+ ...config.admin?.dashboard,
70
+ widgets: [...(config.admin?.dashboard?.widgets ?? []), widget],
71
+ },
72
+ },
73
+ };
74
+ };
75
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * The c15t script integrations an editor can pick for a service, from `@c15t/scripts` 2.2.0. A
3
+ * picked integration is loaded by c15t and follows the visitor's choice. The client imports each
4
+ * one, so the list stays short: the services our sites use, not the whole registry.
5
+ */
6
+ export declare const INTEGRATIONS: readonly [{
7
+ readonly key: "googleTagManager";
8
+ readonly label: "Google Tag Manager";
9
+ readonly idLabel: "Container ID (GTM-…)";
10
+ }, {
11
+ readonly key: "googleTag";
12
+ readonly label: "Google Analytics 4 / Google tag";
13
+ readonly idLabel: "Measurement ID (G-…)";
14
+ }, {
15
+ readonly key: "metaPixel";
16
+ readonly label: "Meta Pixel";
17
+ readonly idLabel: "Pixel ID";
18
+ }, {
19
+ readonly key: "linkedinInsights";
20
+ readonly label: "LinkedIn Insight Tag";
21
+ readonly idLabel: "Partner ID";
22
+ }, {
23
+ readonly key: "tiktokPixel";
24
+ readonly label: "TikTok Pixel";
25
+ readonly idLabel: "Pixel ID";
26
+ }, {
27
+ readonly key: "microsoftUet";
28
+ readonly label: "Microsoft Ads (UET)";
29
+ readonly idLabel: "Tag ID";
30
+ }, {
31
+ readonly key: "microsoftClarity";
32
+ readonly label: "Microsoft Clarity";
33
+ readonly idLabel: "Project ID";
34
+ }, {
35
+ readonly key: "hotjar";
36
+ readonly label: "Hotjar";
37
+ readonly idLabel: "Site ID";
38
+ }];
39
+ export type IntegrationKey = (typeof INTEGRATIONS)[number]["key"];
40
+ export declare const isIntegration: (value: unknown) => value is IntegrationKey;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The c15t script integrations an editor can pick for a service, from `@c15t/scripts` 2.2.0. A
3
+ * picked integration is loaded by c15t and follows the visitor's choice. The client imports each
4
+ * one, so the list stays short: the services our sites use, not the whole registry.
5
+ */
6
+ export const INTEGRATIONS = [
7
+ {
8
+ key: "googleTagManager",
9
+ label: "Google Tag Manager",
10
+ idLabel: "Container ID (GTM-…)",
11
+ },
12
+ {
13
+ key: "googleTag",
14
+ label: "Google Analytics 4 / Google tag",
15
+ idLabel: "Measurement ID (G-…)",
16
+ },
17
+ { key: "metaPixel", label: "Meta Pixel", idLabel: "Pixel ID" },
18
+ {
19
+ key: "linkedinInsights",
20
+ label: "LinkedIn Insight Tag",
21
+ idLabel: "Partner ID",
22
+ },
23
+ { key: "tiktokPixel", label: "TikTok Pixel", idLabel: "Pixel ID" },
24
+ { key: "microsoftUet", label: "Microsoft Ads (UET)", idLabel: "Tag ID" },
25
+ {
26
+ key: "microsoftClarity",
27
+ label: "Microsoft Clarity",
28
+ idLabel: "Project ID",
29
+ },
30
+ { key: "hotjar", label: "Hotjar", idLabel: "Site ID" },
31
+ ];
32
+ export const isIntegration = (value) => INTEGRATIONS.some((integration) => integration.key === value);
@@ -0,0 +1,15 @@
1
+ import type { Payload } from "payload";
2
+ import type { ScanSummary } from "./evaluate.ts";
3
+ export type ScanOutcome = {
4
+ url: string;
5
+ status: "done" | "failed";
6
+ error?: string | null;
7
+ summary?: ScanSummary | null;
8
+ };
9
+ /** A scan worth an email: it failed, found something to handle, or found something new. */
10
+ export declare function needsNotice(outcome: ScanOutcome): boolean;
11
+ /**
12
+ * Emails the users picked under Cookies → Scan about a finished scan, through the host's
13
+ * email adapter, when it needs a look. Admin's default language picks the text.
14
+ */
15
+ export declare function notifyScan(payload: Payload, outcome: ScanOutcome): Promise<void>;
package/dist/notify.js ADDED
@@ -0,0 +1,86 @@
1
+ import { CONSENT_GLOBAL } from "./types.js";
2
+ /** A scan worth an email: it failed, found something to handle, or found something new. */
3
+ export function needsNotice(outcome) {
4
+ if (outcome.status === "failed")
5
+ return true;
6
+ const summary = outcome.summary;
7
+ return Boolean(summary && (summary.action || summary.unknown || summary.new));
8
+ }
9
+ const COPY = {
10
+ en: {
11
+ subject: (o, host) => o.status === "failed"
12
+ ? `Cookie scan of ${host} failed`
13
+ : `Cookie scan of ${host}: ${count(o.summary, "en")}`,
14
+ failed: (o) => `The cookie scan of ${o.url} failed: ${o.error ?? "no reason given"}.`,
15
+ done: (o) => `The cookie scan of ${o.url} found ${count(o.summary, "en")}.`,
16
+ open: "Open Cookies in Admin",
17
+ why: "You get this because you are chosen under Cookies → Scan.",
18
+ },
19
+ sv: {
20
+ subject: (o, host) => o.status === "failed"
21
+ ? `Cookieskanningen av ${host} misslyckades`
22
+ : `Cookieskanning av ${host}: ${count(o.summary, "sv")}`,
23
+ failed: (o) => `Cookieskanningen av ${o.url} misslyckades: ${o.error ?? "ingen orsak angiven"}.`,
24
+ done: (o) => `Cookieskanningen av ${o.url} hittade ${count(o.summary, "sv")}.`,
25
+ open: "Öppna Cookies i Admin",
26
+ why: "Du får det här för att du är vald under Cookies → Skanning.",
27
+ },
28
+ };
29
+ function count(summary, language) {
30
+ const s = summary ?? { action: 0, unknown: 0, ok: 0, new: 0 };
31
+ const parts = language === "sv"
32
+ ? [
33
+ s.action && `${s.action} som kräver åtgärd`,
34
+ s.unknown && `${s.unknown} som inte finns bland Cookietjänsterna`,
35
+ s.new && `${s.new} nya sedan förra skanningen`,
36
+ ]
37
+ : [
38
+ s.action && `${s.action} needing action`,
39
+ s.unknown && `${s.unknown} not in the Cookie services`,
40
+ s.new && `${s.new} new since the last scan`,
41
+ ];
42
+ return (parts.filter(Boolean).join(", ") ||
43
+ (language === "sv" ? "inget att åtgärda" : "nothing to handle"));
44
+ }
45
+ const escape = (value) => value.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c] ?? c);
46
+ /**
47
+ * Emails the users picked under Cookies → Scan about a finished scan, through the host's
48
+ * email adapter, when it needs a look. Admin's default language picks the text.
49
+ */
50
+ export async function notifyScan(payload, outcome) {
51
+ if (!needsNotice(outcome))
52
+ return;
53
+ const global = (await payload.findGlobal({
54
+ slug: CONSENT_GLOBAL,
55
+ depth: 1,
56
+ overrideAccess: true,
57
+ }));
58
+ const to = (global.notify ?? [])
59
+ .map((user) => (typeof user === "object" ? user?.email : undefined))
60
+ .filter((email) => Boolean(email));
61
+ if (!to.length)
62
+ return;
63
+ const language = payload.config.i18n.fallbackLanguage === "sv" ? "sv" : "en";
64
+ const copy = COPY[language];
65
+ const host = (() => {
66
+ try {
67
+ return new URL(outcome.url).host;
68
+ }
69
+ catch {
70
+ return outcome.url;
71
+ }
72
+ })();
73
+ const text = outcome.status === "failed" ? copy.failed(outcome) : copy.done(outcome);
74
+ const link = `${payload.config.serverURL}${payload.config.routes.admin}/globals/${CONSENT_GLOBAL}`;
75
+ try {
76
+ await payload.sendEmail({
77
+ to,
78
+ subject: copy.subject(outcome, host),
79
+ text: `${text}\n\n${copy.open}: ${link}\n\n${copy.why}`,
80
+ html: `<p>${escape(text)}</p><p><a href="${escape(link)}">${copy.open}</a></p><p><small>${copy.why}</small></p>`,
81
+ });
82
+ }
83
+ catch (error) {
84
+ payload.logger.error({ err: error, msg: "payload-consent: could not email the scan result" });
85
+ }
86
+ }
@@ -0,0 +1,15 @@
1
+ import type { CollectionConfig } from "payload";
2
+ import { type Category } from "./defaults.ts";
3
+ import { type ConsentPluginOptions } from "./types.ts";
4
+ /** What the site posts after a visitor saves a choice. */
5
+ export type ConsentRecordInput = {
6
+ subjectId: string;
7
+ choices: Partial<Record<Category, boolean>>;
8
+ policyVersion: string;
9
+ locale: string;
10
+ path?: string;
11
+ };
12
+ /** Checks a posted record; returns why it is refused, or the record. */
13
+ export declare function parseRecord(body: unknown): ConsentRecordInput | string;
14
+ export declare function recordsCollection(options: ConsentPluginOptions): CollectionConfig;
15
+ export declare function policiesCollection(options: ConsentPluginOptions): CollectionConfig;