@jsb188/mday 1.1.10 → 1.1.12
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/package.json +140 -54
- package/src/constants/agentMemory.ts +123 -0
- package/src/constants/agentNote.ts +212 -0
- package/src/constants/aiChat.ts +10 -0
- package/src/constants/billing.ts +126 -0
- package/src/constants/campaign.ts +236 -0
- package/src/constants/contact.ts +72 -0
- package/src/constants/csvExport.ts +133 -0
- package/src/constants/dataTable.ts +21 -21
- package/src/constants/dataTableFieldTypes.generated.ts +59 -0
- package/src/constants/docAccess.ts +33 -0
- package/src/constants/dt.ts +234 -0
- package/src/constants/email.ts +125 -7
- package/src/constants/fieldDoc.ts +104 -0
- package/src/constants/graphqlReactivity.ts +196 -0
- package/src/constants/integration.ts +8 -9
- package/src/constants/log.ts +14 -408
- package/src/constants/logDisplay/README.md +88 -0
- package/src/constants/logDisplay/defaults.ts +39 -0
- package/src/constants/logDisplay/index.ts +27 -0
- package/src/constants/logDisplay/operations/arable.ts +16 -0
- package/src/constants/logDisplay/operations/farmersMarket.ts +10 -0
- package/src/constants/logDisplay/operations/foodManufacturing.ts +13 -0
- package/src/constants/logDisplay/shared/accounting.ts +13 -0
- package/src/constants/logDisplay/shared/interactions.ts +13 -0
- package/src/constants/logDisplay/shared/rackTemperatures.ts +42 -0
- package/src/constants/logTaxonomy.generated.ts +2207 -0
- package/src/constants/noteDoc.ts +77 -0
- package/src/constants/organization.ts +18 -21
- package/src/constants/pageRoute.ts +23 -0
- package/src/constants/qrCode.ts +30 -0
- package/src/constants/sheet.ts +126 -14
- package/src/constants/sms.ts +18 -0
- package/src/constants/square.ts +6 -6
- package/src/constants/storage.ts +80 -7
- package/src/constants/usage.ts +82 -0
- package/src/constants/workflowSteps.ts +60 -0
- package/src/schemas/editLogEntry.ts +836 -780
- package/src/schemas/inboundContact.ts +60 -55
- package/src/schemas/logsQuery.ts +55 -49
- package/src/types/account.d.ts +4 -1
- package/src/types/agentMemory.d.ts +98 -0
- package/src/types/agentNote.d.ts +443 -0
- package/src/types/billing.d.ts +537 -0
- package/src/types/campaign.d.ts +1533 -0
- package/src/types/contact.d.ts +15 -0
- package/src/types/csvExport.d.ts +213 -0
- package/src/types/dataAnalysis.d.ts +44 -0
- package/src/types/dataTable.d.ts +115 -403
- package/src/types/definitionElement.d.ts +35 -0
- package/src/types/docAccess.d.ts +96 -0
- package/src/types/dt.d.ts +257 -0
- package/src/types/email.d.ts +184 -24
- package/src/types/emailReplyReport.d.ts +76 -0
- package/src/types/fieldDoc.d.ts +702 -0
- package/src/types/fieldDocContract.d.ts +90 -0
- package/src/types/inboundContact.d.ts +10 -1
- package/src/types/log.d.ts +254 -295
- package/src/types/logDisplay.types.ts +85 -0
- package/src/types/organization.d.ts +91 -43
- package/src/types/pageRoute.d.ts +48 -0
- package/src/types/qrCode.d.ts +96 -0
- package/src/types/richText.d.ts +81 -0
- package/src/types/sheet.d.ts +678 -129
- package/src/types/sms.d.ts +38 -0
- package/src/types/storage.d.ts +449 -129
- package/src/types/usage.d.ts +106 -0
- package/src/types/workflow.d.ts +315 -54
- package/src/utils/agentMemory.ts +15 -0
- package/src/utils/agentNote.ts +850 -0
- package/src/utils/aiChat.ts +43 -0
- package/src/utils/aiChatWork.ts +64 -0
- package/src/utils/billing.ts +37 -0
- package/src/utils/campaign.ts +1174 -0
- package/src/utils/campaignAI.ts +72 -0
- package/src/utils/campaignAccess.ts +239 -0
- package/src/utils/campaignEmail.ts +601 -0
- package/src/utils/campaignEmailCompiler.ts +372 -0
- package/src/utils/campaignEmailImages.ts +60 -0
- package/src/utils/campaignEmailStyles.ts +544 -0
- package/src/utils/campaignLifecycle.ts +104 -0
- package/src/utils/campaignPreview.ts +285 -0
- package/src/utils/contact.ts +50 -0
- package/src/utils/csvExport.ts +19 -0
- package/src/utils/dataTable.ts +95 -618
- package/src/utils/dataTableFieldTypeRegistry.ts +106 -0
- package/src/utils/dataTableRelativeDate.ts +447 -0
- package/src/utils/definitionElement.ts +84 -0
- package/src/utils/docAccess.ts +383 -0
- package/src/utils/documentName.ts +53 -0
- package/src/utils/email.ts +317 -51
- package/src/utils/emailReplyReport.test.ts +187 -0
- package/src/utils/emailReplyReport.ts +143 -0
- package/src/utils/fieldDoc.ts +157 -0
- package/src/utils/fieldDocAnswers.ts +128 -0
- package/src/utils/fieldDocDefaults.ts +36 -0
- package/src/utils/fieldDocProgram.ts +36 -0
- package/src/utils/fieldDocSummary.ts +30 -0
- package/src/utils/log.ts +276 -192
- package/src/utils/logDisplay.helpers.ts +177 -0
- package/src/utils/logDisplay.ts +87 -0
- package/src/utils/logRegistryValue.ts +90 -0
- package/src/utils/logStoreRead.ts +262 -0
- package/src/utils/logTaxonomy.helpers.ts +17 -0
- package/src/utils/logTaxonomy.ts +76 -0
- package/src/utils/logTaxonomyDisplay.ts +395 -0
- package/src/utils/noteDoc.ts +609 -0
- package/src/utils/organization.ts +337 -84
- package/src/utils/pageRoute.ts +71 -0
- package/src/utils/qrCode.helpers.ts +189 -0
- package/src/utils/qrCode.test.ts +519 -0
- package/src/utils/qrCode.ts +273 -0
- package/src/utils/richTextHTML.ts +385 -0
- package/src/utils/sheet.ts +199 -107
- package/src/utils/sheetAxisMove.ts +616 -0
- package/src/utils/sheetBootstrap.ts +1402 -0
- package/src/utils/sheetCellLiteral.ts +101 -0
- package/src/utils/sheetCommand.ts +358 -0
- package/src/utils/sheetDesign.ts +705 -0
- package/src/utils/sheetExport.ts +186 -0
- package/src/utils/sheetFeatureReference.ts +183 -0
- package/src/utils/sheetFormulaArgs.ts +168 -0
- package/src/utils/sheetFormulaCompile.ts +475 -0
- package/src/utils/sheetFormulaConditionalAggregate.ts +254 -0
- package/src/utils/sheetFormulaCriteria.ts +656 -0
- package/src/utils/sheetFormulaDate.ts +444 -0
- package/src/utils/sheetFormulaEvaluation.ts +899 -0
- package/src/utils/sheetFormulaEvaluator.ts +997 -0
- package/src/utils/sheetFormulaFunctionHandlers.ts +403 -0
- package/src/utils/sheetFormulaLogical.ts +116 -0
- package/src/utils/sheetFormulaLookup.ts +488 -0
- package/src/utils/sheetFormulaMath.ts +48 -0
- package/src/utils/sheetFormulaParser.ts +729 -0
- package/src/utils/sheetFormulaRange.ts +330 -0
- package/src/utils/sheetFormulaReference.ts +703 -0
- package/src/utils/sheetFormulaReferenceAggregates.ts +313 -0
- package/src/utils/sheetFormulaReferenceDates.ts +237 -0
- package/src/utils/sheetFormulaReferenceLogical.ts +330 -0
- package/src/utils/sheetFormulaReferenceLookup.ts +341 -0
- package/src/utils/sheetFormulaReferenceMath.ts +95 -0
- package/src/utils/sheetFormulaReferenceRewrite.ts +220 -0
- package/src/utils/sheetFormulaReferenceText.ts +462 -0
- package/src/utils/sheetFormulaSyntax.ts +1027 -0
- package/src/utils/sheetFormulaText.ts +430 -0
- package/src/utils/sheetJournal.ts +200 -0
- package/src/utils/sheetNumberFormat.ts +1012 -0
- package/src/utils/sheetPreview.ts +68 -0
- package/src/utils/sheetSequence.ts +118 -0
- package/src/utils/sheetSnapshot.ts +739 -0
- package/src/utils/sheetStructureEdit.ts +914 -0
- package/src/utils/sheetStructureReferences.ts +389 -0
- package/src/utils/square.ts +22 -12
- package/src/utils/storage.ts +198 -0
- package/src/utils/table-route.ts +127 -11
- package/src/utils/time.ts +12 -0
- package/src/utils/workflow.ts +334 -40
- package/src/utils/workflowLogFingerprint.ts +302 -0
- package/src/utils/workflowLogRevisionSnapshot.ts +424 -0
- package/src/utils/workflowProgress.ts +280 -0
- package/src/utils/workflowTriggers.ts +61 -0
- package/tests/agentNote.test.ts +395 -0
- package/tests/campaignAI.test.ts +119 -0
- package/tests/campaignAccess.test.ts +239 -0
- package/tests/campaignEmail.test.ts +441 -0
- package/tests/campaignLifecycle.test.ts +134 -0
- package/tests/campaignPublication.test.ts +309 -0
- package/tests/dataTableFieldTypeRegistry.test.ts +111 -0
- package/tests/dataTableRelativeDate.test.ts +412 -0
- package/tests/docAccess.test.ts +525 -0
- package/tests/editLogEntry.test.ts +112 -0
- package/tests/email.test.ts +39 -0
- package/tests/fieldDocProgram.test.ts +114 -0
- package/tests/logDisplay.fixtures.ts +67 -0
- package/tests/logDisplay.test.ts +227 -0
- package/tests/noteDoc.test.ts +514 -0
- package/tests/richTextHTML.test.ts +185 -0
- package/tests/sheetAxisMove.test.ts +352 -0
- package/tests/sheetBootstrap.test.ts +1967 -0
- package/tests/sheetFeatureReference.test.ts +22 -0
- package/tests/sheetFormulaCriteria.test.ts +269 -0
- package/tests/sheetFormulaEvaluator.test.ts +468 -0
- package/tests/sheetFormulaFunctions.test.ts +538 -0
- package/tests/sheetFormulaLanguage.test.ts +361 -0
- package/tests/sheetFormulaParity.fixtures.ts +872 -0
- package/tests/sheetFormulaParity.test.ts +188 -0
- package/tests/sheetFormulaRange.test.ts +167 -0
- package/tests/sheetFormulaReference.test.ts +78 -0
- package/tests/sheetFormulaReferenceRewrite.test.ts +117 -0
- package/tests/sheetFormulaTextDate.test.ts +512 -0
- package/tests/sheetSequence.test.ts +22 -0
- package/tests/sheetSnapshot.test.ts +134 -0
- package/tests/sheetStructureEdit.test.ts +537 -0
- package/tests/sheetStructureReferences.test.ts +589 -0
- package/tests/workflowLifecycleStatus.test.ts +60 -0
- package/tests/workflowLogFingerprint.test.ts +203 -0
- package/tests/workflowLogRevisionSnapshot.test.ts +96 -0
- package/tests/workflowProgress.test.ts +180 -0
- package/tests/workflowTriggers.test.ts +29 -0
- package/tsconfig.json +10 -10
- package/vitest.config.ts +8 -0
- package/src/constants/log-activityTexts.ts +0 -235
- package/src/constants/product.ts +0 -42
- package/src/constants/report.ts +0 -44
- package/src/schemas/organization.ts +0 -65
- package/src/schemas/productsQuery.ts +0 -53
- package/src/schemas/reportsQuery.ts +0 -20
- package/src/types/product.d.ts +0 -84
- package/src/types/report.d.ts +0 -290
- package/src/utils/product-calEvent.ts +0 -455
- package/src/utils/report.ts +0 -158
|
@@ -0,0 +1,1174 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CAMPAIGN_REPLY_TO_AGENT_DOMAINS,
|
|
3
|
+
DISABLED_INBOXES,
|
|
4
|
+
DO_NOT_REPLY_EMAIL_DOMAINS,
|
|
5
|
+
DOMAIN_TO_INBOX,
|
|
6
|
+
EMAIL_BASE_DOMAINS,
|
|
7
|
+
OPERATION_INBOX_DOMAINS,
|
|
8
|
+
} from '@jsb188/app/constants/agent.ts';
|
|
9
|
+
import type { DraftSchema } from '@jsb188/app/utils/schema.ts';
|
|
10
|
+
import { isEmail } from '@jsb188/app/utils/string.ts';
|
|
11
|
+
import {
|
|
12
|
+
CAMPAIGN_CHOICE_ELEMENT_TYPES,
|
|
13
|
+
CAMPAIGN_DRAFT_KEY_PREFIX,
|
|
14
|
+
CAMPAIGN_ELEMENT_TYPES,
|
|
15
|
+
CAMPAIGN_ELEMENT_VISIBLE_WHEN_OPERATORS,
|
|
16
|
+
CAMPAIGN_EMAIL_TEXT_LIMITS,
|
|
17
|
+
CAMPAIGN_FORM_BACKGROUND_FILL_STYLES,
|
|
18
|
+
CAMPAIGN_FORM_COLORS,
|
|
19
|
+
CAMPAIGN_FORM_DEFAULT_BACKGROUND_FILL_STYLE,
|
|
20
|
+
CAMPAIGN_FORM_NEUTRAL_COLOR,
|
|
21
|
+
CAMPAIGN_INSTRUCTIONS_MAX_LENGTH,
|
|
22
|
+
CAMPAIGN_LONG_TEXT_ANSWER_MAX_LENGTH,
|
|
23
|
+
CAMPAIGN_PUBLISH_STATUS_ENUMS,
|
|
24
|
+
CAMPAIGN_RESPONSE_TEXT_ANSWERS_MAX_LENGTH,
|
|
25
|
+
CAMPAIGN_SHORT_TEXT_ANSWER_MAX_LENGTH,
|
|
26
|
+
CAMPAIGN_WEB_FORM_RESULT_FIXED_COLUMN_KEYS,
|
|
27
|
+
} from '../constants/campaign.ts';
|
|
28
|
+
import { MAX_PUBLIC_FORM_GENERATION_FILES } from '../constants/storage.ts';
|
|
29
|
+
import type {
|
|
30
|
+
CampaignAnswerObj,
|
|
31
|
+
CampaignAnswersObj,
|
|
32
|
+
CampaignDefinitionSnapshotElementObj,
|
|
33
|
+
CampaignDefinitionSnapshotObj,
|
|
34
|
+
CampaignDraft,
|
|
35
|
+
CampaignElementData,
|
|
36
|
+
CampaignElementOptionObj,
|
|
37
|
+
CampaignElementSettingsGQL,
|
|
38
|
+
CampaignElementSettingsObj,
|
|
39
|
+
CampaignElementTypeEnum,
|
|
40
|
+
CampaignElementValidationObj,
|
|
41
|
+
CampaignElementVisibleWhenObj,
|
|
42
|
+
CampaignElementVisibleWhenOperatorEnum,
|
|
43
|
+
CampaignElementVisibleWhenValuesObj,
|
|
44
|
+
CampaignFormBackgroundFillStyleEnum,
|
|
45
|
+
CampaignKindEnum,
|
|
46
|
+
CampaignPublishStatusEnum,
|
|
47
|
+
CampaignStatusEnum,
|
|
48
|
+
CampaignTemplateType,
|
|
49
|
+
EmailCampaignSettingsObj,
|
|
50
|
+
FormCampaignSettingsObj,
|
|
51
|
+
} from '../types/campaign.d.ts';
|
|
52
|
+
import {
|
|
53
|
+
getCampaignEmailBlockStorageSettings,
|
|
54
|
+
getCampaignEmailDesignSettings,
|
|
55
|
+
getCampaignEmailRowStorageSettings,
|
|
56
|
+
} from './campaignEmail.ts';
|
|
57
|
+
import { getCampaignFormWithoutAIQuestions, isCampaignAIQuestion } from './campaignAI.ts';
|
|
58
|
+
|
|
59
|
+
export type CampaignFormPublicationErrorCode =
|
|
60
|
+
| 'EMPTY_CHOICE_OPTION'
|
|
61
|
+
| 'EMPTY_QUESTION_LABEL'
|
|
62
|
+
| 'EMPTY_SECTION'
|
|
63
|
+
| 'INSUFFICIENT_CHOICE_OPTIONS'
|
|
64
|
+
| 'INVALID_QUESTION_VALIDATION'
|
|
65
|
+
| 'NO_QUESTIONS'
|
|
66
|
+
| 'RESERVED_QUESTION_KEY'
|
|
67
|
+
| 'UNSUPPORTED_FILE_QUESTION';
|
|
68
|
+
|
|
69
|
+
export interface CampaignFormPublicationError {
|
|
70
|
+
code: CampaignFormPublicationErrorCode;
|
|
71
|
+
elementKey?: string;
|
|
72
|
+
sectionKey?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/*
|
|
76
|
+
* Return whether one lifecycle state can be the source of a publish command.
|
|
77
|
+
*/
|
|
78
|
+
export function isCampaignPublishStatus(status: CampaignStatusEnum): status is CampaignPublishStatusEnum {
|
|
79
|
+
return CAMPAIGN_PUBLISH_STATUS_ENUMS.some((publishStatus) => publishStatus === status);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/*
|
|
83
|
+
* Narrow one untrusted JSONB value to a non-array object before reading fields.
|
|
84
|
+
*/
|
|
85
|
+
function isCampaignObjectRecord(value: unknown): value is Record<string, unknown> {
|
|
86
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/*
|
|
90
|
+
* Check whether one value fits GraphQL's signed 32-bit Int scalar.
|
|
91
|
+
*/
|
|
92
|
+
function isCampaignGraphQLInt(value: unknown): value is number {
|
|
93
|
+
return Number.isInteger(value) && (value as number) >= -2147483648 && (value as number) <= 2147483647;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/*
|
|
97
|
+
* Check whether one element collects an answer. "kind" is the structural role,
|
|
98
|
+
* so this is the only correct test - never infer it from "type" being set.
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
export function isCampaignQuestionElement(element?: Pick<CampaignElementData, 'kind'> | null): boolean {
|
|
102
|
+
return element?.kind === 'QUESTION';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
* Return whether one key is reserved for a fixed Web form results Sheet column.
|
|
107
|
+
*/
|
|
108
|
+
export function isCampaignWebFormResultFixedColumnKey(key?: string | null): boolean {
|
|
109
|
+
return CAMPAIGN_WEB_FORM_RESULT_FIXED_COLUMN_KEYS.includes(
|
|
110
|
+
String(key || '') as (typeof CAMPAIGN_WEB_FORM_RESULT_FIXED_COLUMN_KEYS)[number],
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/*
|
|
115
|
+
* Check whether one element's answer must be one of its declared options.
|
|
116
|
+
*/
|
|
117
|
+
|
|
118
|
+
export function isCampaignChoiceElement(element?: Pick<CampaignElementData, 'type'> | null): boolean {
|
|
119
|
+
return !!element?.type && CAMPAIGN_CHOICE_ELEMENT_TYPES.includes(element.type);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
* Check whether one element accepts more than one answer value.
|
|
124
|
+
*/
|
|
125
|
+
|
|
126
|
+
export function isCampaignMultiValueElement(element?: Pick<CampaignElementData, 'type'> | null): boolean {
|
|
127
|
+
return element?.type === 'MULTI_CHOICE' || element?.type === 'FILE';
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/*
|
|
131
|
+
* Check whether one value is a supported element answer type.
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
export function isCampaignElementType(value?: string | null): value is CampaignElementTypeEnum {
|
|
135
|
+
return CAMPAIGN_ELEMENT_TYPES.includes(value as CampaignElementTypeEnum);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/*
|
|
139
|
+
* Remove unusable and duplicated choice rows while preserving the first
|
|
140
|
+
* canonical value-label pair for publication.
|
|
141
|
+
*/
|
|
142
|
+
export function getCampaignPublishableOptions(options?: unknown): CampaignElementOptionObj[] {
|
|
143
|
+
if (!Array.isArray(options)) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const labels = new Set<string>();
|
|
148
|
+
const values = new Set<string>();
|
|
149
|
+
const publishableOptions: CampaignElementOptionObj[] = [];
|
|
150
|
+
|
|
151
|
+
for (const option of options) {
|
|
152
|
+
if (!isCampaignObjectRecord(option) || typeof option.label !== 'string' || typeof option.value !== 'string') {
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const label = option.label.trim();
|
|
157
|
+
const value = option.value.trim();
|
|
158
|
+
if (!label || !value || labels.has(label) || values.has(value)) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
labels.add(label);
|
|
163
|
+
values.add(value);
|
|
164
|
+
publishableOptions.push({ label, value });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return publishableOptions;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/*
|
|
171
|
+
* Clear one publication condition when option cleanup removed a value it
|
|
172
|
+
* depended on. Drafts retain the original condition until publication.
|
|
173
|
+
*/
|
|
174
|
+
function getCampaignPublicationVisibleWhen(
|
|
175
|
+
element: CampaignDefinitionSnapshotElementObj,
|
|
176
|
+
elementsByKey: ReadonlyMap<string, CampaignDefinitionSnapshotElementObj>,
|
|
177
|
+
): CampaignElementVisibleWhenObj | null {
|
|
178
|
+
const condition = getCampaignElementVisibleWhenValues(element.visibleWhen);
|
|
179
|
+
if (!condition) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const referencedElement = elementsByKey.get(condition.elementKey);
|
|
184
|
+
if (
|
|
185
|
+
condition.elementKey === element.key ||
|
|
186
|
+
!referencedElement ||
|
|
187
|
+
referencedElement.kind !== 'QUESTION' ||
|
|
188
|
+
referencedElement.type === 'FILE'
|
|
189
|
+
) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (
|
|
194
|
+
isCampaignChoiceElement(referencedElement) &&
|
|
195
|
+
condition.values.some((value) => !referencedElement.options?.some((option) => option.value === value))
|
|
196
|
+
) {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
elementKey: condition.elementKey,
|
|
202
|
+
operator: condition.operator,
|
|
203
|
+
value: condition.values,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/*
|
|
208
|
+
* Build the respondent-facing definition at the publication boundary. Drafts
|
|
209
|
+
* may save invalid option rows; publishing is where blank and duplicated rows
|
|
210
|
+
* are deleted, and the remaining definition is validated before use.
|
|
211
|
+
*/
|
|
212
|
+
export function getCampaignFormPublicationSnapshot(snapshot: CampaignDefinitionSnapshotObj): CampaignDefinitionSnapshotObj {
|
|
213
|
+
const sections = getCampaignFormWithoutAIQuestions(snapshot).sections.map((section) => ({
|
|
214
|
+
...section,
|
|
215
|
+
elements: (section.elements || []).map((element) =>
|
|
216
|
+
isCampaignChoiceElement(element)
|
|
217
|
+
? {
|
|
218
|
+
...element,
|
|
219
|
+
options: getCampaignPublishableOptions(element.options),
|
|
220
|
+
}
|
|
221
|
+
: element,
|
|
222
|
+
),
|
|
223
|
+
}));
|
|
224
|
+
const elementsByKey = new Map<string, CampaignDefinitionSnapshotElementObj>();
|
|
225
|
+
for (const section of sections) {
|
|
226
|
+
elementsByKey.set(section.key, section);
|
|
227
|
+
for (const element of section.elements) {
|
|
228
|
+
elementsByKey.set(element.key, element);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
...snapshot,
|
|
234
|
+
sections: sections.map((section) => ({
|
|
235
|
+
...section,
|
|
236
|
+
visibleWhen: getCampaignPublicationVisibleWhen(section, elementsByKey),
|
|
237
|
+
elements: section.elements.map((element) => ({
|
|
238
|
+
...element,
|
|
239
|
+
visibleWhen: getCampaignPublicationVisibleWhen(element, elementsByKey),
|
|
240
|
+
})),
|
|
241
|
+
})),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/*
|
|
246
|
+
* Return one choice question's respondent-facing publication errors.
|
|
247
|
+
*/
|
|
248
|
+
function getCampaignChoicePublicationErrors(element: CampaignDefinitionSnapshotElementObj): CampaignFormPublicationError[] {
|
|
249
|
+
if (!isCampaignChoiceElement(element)) {
|
|
250
|
+
return [];
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const options = element.options || [];
|
|
254
|
+
const labels = new Set<string>();
|
|
255
|
+
const values = new Set<string>();
|
|
256
|
+
const invalidOption = options.some((option) => {
|
|
257
|
+
const label = option.label.trim();
|
|
258
|
+
const value = option.value.trim();
|
|
259
|
+
const invalid = !label || !value || labels.has(label) || values.has(value);
|
|
260
|
+
labels.add(label);
|
|
261
|
+
values.add(value);
|
|
262
|
+
return invalid;
|
|
263
|
+
});
|
|
264
|
+
const errors: CampaignFormPublicationError[] = [];
|
|
265
|
+
|
|
266
|
+
if (options.length < 2) {
|
|
267
|
+
errors.push({
|
|
268
|
+
code: 'INSUFFICIENT_CHOICE_OPTIONS',
|
|
269
|
+
elementKey: element.key,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
if (invalidOption) {
|
|
273
|
+
errors.push({ code: 'EMPTY_CHOICE_OPTION', elementKey: element.key });
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return errors;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/*
|
|
280
|
+
* Reject validation ranges that make a published question impossible to
|
|
281
|
+
* answer. Drafts may temporarily contain these values, but respondents must
|
|
282
|
+
* never receive a form whose minimum exceeds its maximum.
|
|
283
|
+
*/
|
|
284
|
+
function isCampaignQuestionValidationInvalid(element: CampaignDefinitionSnapshotElementObj): boolean {
|
|
285
|
+
const validation = getCampaignElementValidation(element.validation);
|
|
286
|
+
if (!validation) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (element.type === 'SHORT_TEXT' || element.type === 'LONG_TEXT') {
|
|
291
|
+
const hardMaxLength = element.type === 'SHORT_TEXT' ? CAMPAIGN_SHORT_TEXT_ANSWER_MAX_LENGTH : CAMPAIGN_LONG_TEXT_ANSWER_MAX_LENGTH;
|
|
292
|
+
return (
|
|
293
|
+
(validation.minLength != null && validation.minLength < 0) ||
|
|
294
|
+
(validation.maxLength != null && validation.maxLength < 1) ||
|
|
295
|
+
(validation.maxLength != null && validation.maxLength > hardMaxLength) ||
|
|
296
|
+
(validation.minLength != null && validation.minLength > hardMaxLength) ||
|
|
297
|
+
(validation.minLength != null && validation.maxLength != null && validation.minLength > validation.maxLength)
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return element.type === 'NUMBER' && validation.min != null && validation.max != null && validation.min > validation.max;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/*
|
|
305
|
+
* Return every semantic issue that prevents one structurally safe form from being published.
|
|
306
|
+
*/
|
|
307
|
+
export function getCampaignFormPublicationErrors(snapshot: CampaignDefinitionSnapshotObj): CampaignFormPublicationError[] {
|
|
308
|
+
const errors: CampaignFormPublicationError[] = [];
|
|
309
|
+
let questionCount = 0;
|
|
310
|
+
|
|
311
|
+
for (const [sectionIndex, section] of (snapshot.sections || []).entries()) {
|
|
312
|
+
const isImplicitFirstSection =
|
|
313
|
+
sectionIndex === 0 &&
|
|
314
|
+
!section.label?.trim() &&
|
|
315
|
+
!section.description?.trim() &&
|
|
316
|
+
!section.storageRels?.length &&
|
|
317
|
+
!section.settings;
|
|
318
|
+
if (!isImplicitFirstSection && !section.label?.trim()) {
|
|
319
|
+
errors.push({ code: 'EMPTY_SECTION', sectionKey: section.key });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
for (const element of section.elements || []) {
|
|
323
|
+
if (element.kind !== 'QUESTION' || isCampaignAIQuestion(element)) {
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
questionCount += 1;
|
|
328
|
+
if (isCampaignWebFormResultFixedColumnKey(element.key)) {
|
|
329
|
+
errors.push({
|
|
330
|
+
code: 'RESERVED_QUESTION_KEY',
|
|
331
|
+
elementKey: element.key,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (!element.label?.trim()) {
|
|
335
|
+
errors.push({
|
|
336
|
+
code: 'EMPTY_QUESTION_LABEL',
|
|
337
|
+
elementKey: element.key,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
if (element.type === 'FILE') {
|
|
341
|
+
errors.push({
|
|
342
|
+
code: 'UNSUPPORTED_FILE_QUESTION',
|
|
343
|
+
elementKey: element.key,
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
if (isCampaignQuestionValidationInvalid(element)) {
|
|
347
|
+
errors.push({
|
|
348
|
+
code: 'INVALID_QUESTION_VALIDATION',
|
|
349
|
+
elementKey: element.key,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
errors.push(...getCampaignChoicePublicationErrors(element));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (!questionCount) {
|
|
357
|
+
errors.push({ code: 'NO_QUESTIONS' });
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return errors;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/*
|
|
364
|
+
* Return whether one structurally safe form is complete enough for respondents.
|
|
365
|
+
*/
|
|
366
|
+
export function isCampaignFormPublishable(snapshot: CampaignDefinitionSnapshotObj): boolean {
|
|
367
|
+
return getCampaignFormPublicationErrors(snapshot).length === 0;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/*
|
|
371
|
+
* Map one template route type to its campaign kind.
|
|
372
|
+
*/
|
|
373
|
+
|
|
374
|
+
export function getCampaignKindFromTemplateType(templateType?: string | null): CampaignKindEnum | null {
|
|
375
|
+
switch (templateType as CampaignTemplateType) {
|
|
376
|
+
case 'forms':
|
|
377
|
+
return 'PUBLIC_FORM';
|
|
378
|
+
case 'emails':
|
|
379
|
+
return 'EMAIL';
|
|
380
|
+
default:
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/*
|
|
386
|
+
* Check whether one value is a reply-to agent domain this app owns.
|
|
387
|
+
*/
|
|
388
|
+
|
|
389
|
+
export function isCampaignReplyToAgentDomain(value?: string | null): boolean {
|
|
390
|
+
return CAMPAIGN_REPLY_TO_AGENT_DOMAINS.includes(
|
|
391
|
+
String(value || '')
|
|
392
|
+
.trim()
|
|
393
|
+
.toLowerCase(),
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/*
|
|
398
|
+
* Return the app inbox domains available to one organization's operation.
|
|
399
|
+
*/
|
|
400
|
+
export function getCampaignReplyToDomains(operation?: string | null): string[] {
|
|
401
|
+
const operationDomains = (OPERATION_INBOX_DOMAINS as Record<string, string[]>)[operation || ''] || OPERATION_INBOX_DOMAINS.DEFAULT;
|
|
402
|
+
|
|
403
|
+
return [...new Set([...operationDomains, ...DO_NOT_REPLY_EMAIL_DOMAINS])];
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/*
|
|
407
|
+
* Check whether one app inbox domain can receive mail today. Domains of disabled inboxes are listed as campaign From
|
|
408
|
+
* options but cannot be chosen until their inbox launches.
|
|
409
|
+
*/
|
|
410
|
+
export function isCampaignReplyToDomainEnabled(domain?: string | null): boolean {
|
|
411
|
+
if (!isCampaignReplyToAgentDomain(domain)) return false;
|
|
412
|
+
const inbox = (DOMAIN_TO_INBOX as Record<string, string | undefined>)[
|
|
413
|
+
String(domain || '')
|
|
414
|
+
.trim()
|
|
415
|
+
.toLowerCase()
|
|
416
|
+
];
|
|
417
|
+
return !inbox || !DISABLED_INBOXES.includes(inbox as (typeof DISABLED_INBOXES)[number]);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/*
|
|
421
|
+
* Return the app inbox domains one organization's operation can choose as a campaign From address today.
|
|
422
|
+
*/
|
|
423
|
+
export function getEnabledCampaignReplyToDomains(operation?: string | null): string[] {
|
|
424
|
+
return getCampaignReplyToDomains(operation).filter(isCampaignReplyToDomainEnabled);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/*
|
|
428
|
+
* Build the Reply-To mailbox list shared by campaign sends and test emails.
|
|
429
|
+
* The manual address is additional when an agent inbox is configured; it must never replace that inbox.
|
|
430
|
+
* Replacing it sends replies only to the human mailbox, bypassing Postmark ingestion and AI order processing.
|
|
431
|
+
* Preserve the full agent address, including any recipient-specific token used to link replies to the sent campaign.
|
|
432
|
+
* Non-agent addresses (such as do-not-reply) use only the manual address when one is set.
|
|
433
|
+
*/
|
|
434
|
+
export function getCampaignEmailReplyToAddress(fromInboxEmail: string, replyToEmail?: string | null): string {
|
|
435
|
+
const manualReplyTo = replyToEmail?.trim();
|
|
436
|
+
if (!manualReplyTo) return fromInboxEmail;
|
|
437
|
+
const inboxDomain = fromInboxEmail.trim().toLowerCase().split('@').at(-1) || '';
|
|
438
|
+
return Object.hasOwn(DOMAIN_TO_INBOX, inboxDomain) ? `${fromInboxEmail}, ${manualReplyTo}` : manualReplyTo;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/*
|
|
442
|
+
* Reserve platform inbox subdomains from manual reply addresses, including unsupported inboxes.
|
|
443
|
+
*/
|
|
444
|
+
export function isCampaignAppReplyToEmail(value?: string | null): boolean {
|
|
445
|
+
const domain =
|
|
446
|
+
String(value || '')
|
|
447
|
+
.trim()
|
|
448
|
+
.toLowerCase()
|
|
449
|
+
.split('@')
|
|
450
|
+
.at(-1) || '';
|
|
451
|
+
return EMAIL_BASE_DOMAINS.some((baseDomain) => domain.endsWith(`.${baseDomain}`));
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/*
|
|
455
|
+
* Check whether one value is usable as a template's secondary reply-to address.
|
|
456
|
+
* The database also rejects uppercase and comma-bearing values.
|
|
457
|
+
*/
|
|
458
|
+
|
|
459
|
+
export function isCampaignSecondaryReplyToEmail(value?: string | null): boolean {
|
|
460
|
+
const normalizedValue = String(value || '').trim();
|
|
461
|
+
|
|
462
|
+
return (
|
|
463
|
+
!!normalizedValue &&
|
|
464
|
+
normalizedValue.length <= CAMPAIGN_EMAIL_TEXT_LIMITS.secondaryReplyToEmail &&
|
|
465
|
+
normalizedValue === normalizedValue.toLowerCase() &&
|
|
466
|
+
!normalizedValue.includes(',') &&
|
|
467
|
+
isEmail(normalizedValue)
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/*
|
|
472
|
+
* Normalize one list of campaign-skipped address ids from JSONB or client input:
|
|
473
|
+
* database ids travel as digit strings so BIGINT identities survive JSON, and
|
|
474
|
+
* the result is deduplicated and sorted so equal sets always serialize equally.
|
|
475
|
+
*/
|
|
476
|
+
|
|
477
|
+
export function getCampaignAudienceExclusionIds(value: unknown): string[] {
|
|
478
|
+
if (!Array.isArray(value)) {
|
|
479
|
+
return [];
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
const ids = new Set<string>();
|
|
483
|
+
for (const item of value) {
|
|
484
|
+
const id = typeof item === 'bigint' ? item.toString() : String(item ?? '');
|
|
485
|
+
if (/^\d+$/.test(id)) {
|
|
486
|
+
ids.add(id);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return [...ids].sort((a, b) => a.length - b.length || (a < b ? -1 : a > b ? 1 : 0));
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/*
|
|
494
|
+
* Read one email settings object from campaign JSONB without trusting unknown
|
|
495
|
+
* stored values to have the expected scalar types.
|
|
496
|
+
*/
|
|
497
|
+
|
|
498
|
+
export function getEmailCampaignSettings(settings?: unknown): EmailCampaignSettingsObj {
|
|
499
|
+
const values = isCampaignObjectRecord(settings) ? settings : {};
|
|
500
|
+
|
|
501
|
+
return {
|
|
502
|
+
subject: typeof values?.subject === 'string' ? values.subject : null,
|
|
503
|
+
preheader: typeof values?.preheader === 'string' ? values.preheader : null,
|
|
504
|
+
body: typeof values?.body === 'string' ? values.body : null,
|
|
505
|
+
aiAgentDomain: typeof values?.aiAgentDomain === 'string' ? values.aiAgentDomain : '',
|
|
506
|
+
secondaryReplyToEmail: typeof values?.secondaryReplyToEmail === 'string' ? values.secondaryReplyToEmail : null,
|
|
507
|
+
sendAt: typeof values?.sendAt === 'string' ? values.sendAt : null,
|
|
508
|
+
design: getCampaignEmailDesignSettings(values?.design),
|
|
509
|
+
trackOpens: values?.trackOpens !== false,
|
|
510
|
+
excludedEmailAddressIds: getCampaignAudienceExclusionIds(values?.excludedEmailAddressIds),
|
|
511
|
+
...(Array.isArray(values.excludedEmails)
|
|
512
|
+
? { excludedEmails: values.excludedEmails.filter((email): email is string => typeof email === 'string') }
|
|
513
|
+
: {}),
|
|
514
|
+
...(Array.isArray(values.excludedOrganizationIds)
|
|
515
|
+
? { excludedOrganizationIds: getCampaignAudienceExclusionIds(values.excludedOrganizationIds) }
|
|
516
|
+
: {}),
|
|
517
|
+
...(values.networkPermissionPolicy === 'RELATIONSHIP' ? { networkPermissionPolicy: 'RELATIONSHIP' as const } : {}),
|
|
518
|
+
...(isCampaignObjectRecord(values.emailReplyReport) &&
|
|
519
|
+
values.emailReplyReport.version === 1 &&
|
|
520
|
+
Array.isArray(values.emailReplyReport.fields)
|
|
521
|
+
? { emailReplyReport: values.emailReplyReport as unknown as NonNullable<EmailCampaignSettingsObj['emailReplyReport']> }
|
|
522
|
+
: {}),
|
|
523
|
+
...(isCampaignObjectRecord(values.emailListRecipientSummary)
|
|
524
|
+
? { emailListRecipientSummary: values.emailListRecipientSummary }
|
|
525
|
+
: {}),
|
|
526
|
+
...(isCampaignObjectRecord(values.networkRecipientSummary) ? { networkRecipientSummary: values.networkRecipientSummary } : {}),
|
|
527
|
+
...(isCampaignObjectRecord(values.emailListSendInfo) &&
|
|
528
|
+
typeof values.emailListSendInfo.senderAddress === 'string' &&
|
|
529
|
+
typeof values.emailListSendInfo.replyAddress === 'string' &&
|
|
530
|
+
typeof values.emailListSendInfo.timeZone === 'string'
|
|
531
|
+
? {
|
|
532
|
+
emailListSendInfo: {
|
|
533
|
+
senderAddress: values.emailListSendInfo.senderAddress,
|
|
534
|
+
replyAddress: values.emailListSendInfo.replyAddress,
|
|
535
|
+
timeZone: values.emailListSendInfo.timeZone,
|
|
536
|
+
},
|
|
537
|
+
}
|
|
538
|
+
: {}),
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/*
|
|
543
|
+
* Read one form settings object from campaign JSONB without trusting unknown
|
|
544
|
+
* stored values to have the expected scalar types.
|
|
545
|
+
*/
|
|
546
|
+
|
|
547
|
+
export function getFormCampaignSettings(settings?: unknown): FormCampaignSettingsObj {
|
|
548
|
+
const values = isCampaignObjectRecord(settings) ? settings : {};
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
color: typeof values?.color === 'string' ? values.color : undefined,
|
|
552
|
+
backgroundFillStyle:
|
|
553
|
+
typeof values?.backgroundFillStyle === 'string' && isCampaignFormBackgroundFillStyle(values.backgroundFillStyle)
|
|
554
|
+
? values.backgroundFillStyle
|
|
555
|
+
: undefined,
|
|
556
|
+
confirmationMessage: typeof values?.confirmationMessage === 'string' ? values.confirmationMessage : undefined,
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/*
|
|
561
|
+
* Return one element's declared options in the canonical value-and-label
|
|
562
|
+
* shape. Invalid persisted JSON is ignored instead of being promoted into a
|
|
563
|
+
* second legacy contract.
|
|
564
|
+
*/
|
|
565
|
+
|
|
566
|
+
export function getCampaignElementOptions(element?: unknown): CampaignElementOptionObj[] {
|
|
567
|
+
const options = isCampaignObjectRecord(element) ? element.options : null;
|
|
568
|
+
|
|
569
|
+
if (!Array.isArray(options)) {
|
|
570
|
+
return [];
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return options.flatMap((option) => {
|
|
574
|
+
if (!isCampaignObjectRecord(option) || typeof option.value !== 'string' || typeof option.label !== 'string') {
|
|
575
|
+
return [];
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const value = option.value.trim();
|
|
579
|
+
const label = option.label.trim();
|
|
580
|
+
|
|
581
|
+
return value ? [{ value, label }] : [];
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/*
|
|
586
|
+
* Read one element validation object while dropping unknown or incorrectly
|
|
587
|
+
* typed JSONB values before they reach GraphQL or another editor save.
|
|
588
|
+
*/
|
|
589
|
+
|
|
590
|
+
export function getCampaignElementValidation(validation?: unknown): CampaignElementValidationObj | null {
|
|
591
|
+
const values = isCampaignObjectRecord(validation) ? validation : {};
|
|
592
|
+
const normalized: CampaignElementValidationObj = {
|
|
593
|
+
minLength: isCampaignGraphQLInt(values?.minLength) ? values.minLength : undefined,
|
|
594
|
+
maxLength: isCampaignGraphQLInt(values?.maxLength) ? values.maxLength : undefined,
|
|
595
|
+
min: typeof values?.min === 'number' && Number.isFinite(values.min) ? values.min : undefined,
|
|
596
|
+
max: typeof values?.max === 'number' && Number.isFinite(values.max) ? values.max : undefined,
|
|
597
|
+
maxFiles: isCampaignGraphQLInt(values?.maxFiles) ? values.maxFiles : undefined,
|
|
598
|
+
acceptedContentTypes: Array.isArray(values?.acceptedContentTypes)
|
|
599
|
+
? values.acceptedContentTypes.filter((value): value is string => typeof value === 'string')
|
|
600
|
+
: undefined,
|
|
601
|
+
};
|
|
602
|
+
|
|
603
|
+
return Object.values(normalized).some((value) => value !== undefined) ? normalized : null;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/*
|
|
607
|
+
* Check whether one value is a supported conditional-visibility operator.
|
|
608
|
+
*/
|
|
609
|
+
|
|
610
|
+
export function isCampaignElementVisibleWhenOperator(value?: string | null): value is CampaignElementVisibleWhenOperatorEnum {
|
|
611
|
+
return CAMPAIGN_ELEMENT_VISIBLE_WHEN_OPERATORS.includes(value as CampaignElementVisibleWhenOperatorEnum);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/*
|
|
615
|
+
* Normalize stored or mutation-input visibility into GraphQL's always-array
|
|
616
|
+
* values shape.
|
|
617
|
+
*/
|
|
618
|
+
|
|
619
|
+
export function getCampaignElementVisibleWhenValues(visibleWhen?: unknown): CampaignElementVisibleWhenValuesObj | null {
|
|
620
|
+
const valuesObject = isCampaignObjectRecord(visibleWhen) ? visibleWhen : null;
|
|
621
|
+
const operator =
|
|
622
|
+
typeof valuesObject?.operator === 'string' && isCampaignElementVisibleWhenOperator(valuesObject.operator)
|
|
623
|
+
? valuesObject.operator
|
|
624
|
+
: null;
|
|
625
|
+
|
|
626
|
+
if (typeof valuesObject?.elementKey !== 'string' || !operator) {
|
|
627
|
+
return null;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
const rawValues = 'values' in valuesObject ? valuesObject.values : valuesObject.value;
|
|
631
|
+
const values =
|
|
632
|
+
rawValues == null
|
|
633
|
+
? []
|
|
634
|
+
: (Array.isArray(rawValues) ? rawValues : [rawValues]).filter((value): value is string => typeof value === 'string');
|
|
635
|
+
|
|
636
|
+
return {
|
|
637
|
+
elementKey: valuesObject.elementKey,
|
|
638
|
+
operator,
|
|
639
|
+
values,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
/*
|
|
644
|
+
* Convert GraphQL's visibility values list into the JSONB storage shape used
|
|
645
|
+
* by campaign definitions and snapshots.
|
|
646
|
+
*/
|
|
647
|
+
|
|
648
|
+
export function getCampaignElementVisibleWhenStorageValue(
|
|
649
|
+
visibleWhen?: CampaignElementVisibleWhenObj | CampaignElementVisibleWhenValuesObj | null,
|
|
650
|
+
): CampaignElementVisibleWhenObj | null {
|
|
651
|
+
const normalized = getCampaignElementVisibleWhenValues(visibleWhen);
|
|
652
|
+
|
|
653
|
+
return normalized
|
|
654
|
+
? {
|
|
655
|
+
elementKey: normalized.elementKey,
|
|
656
|
+
operator: normalized.operator,
|
|
657
|
+
value: normalized.values,
|
|
658
|
+
}
|
|
659
|
+
: null;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/*
|
|
663
|
+
* Resolve sparse element presentation settings to GraphQL's explicit boolean
|
|
664
|
+
* fields.
|
|
665
|
+
*/
|
|
666
|
+
|
|
667
|
+
export function getCampaignElementSettings(settings?: unknown): CampaignElementSettingsGQL {
|
|
668
|
+
const values = isCampaignObjectRecord(settings) ? settings : {};
|
|
669
|
+
const emailRow = getCampaignEmailRowStorageSettings(values.emailRow);
|
|
670
|
+
const emailBlock = getCampaignEmailBlockStorageSettings(values.emailBlock);
|
|
671
|
+
|
|
672
|
+
return {
|
|
673
|
+
numbered: values.numbered === true,
|
|
674
|
+
fullWidth: values.fullWidth === true,
|
|
675
|
+
...(emailRow ? { emailRow } : {}),
|
|
676
|
+
...(emailBlock ? { emailBlock } : {}),
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/*
|
|
681
|
+
* Convert element presentation settings back to sparse JSONB, clearing the
|
|
682
|
+
* object when every flag is false.
|
|
683
|
+
*/
|
|
684
|
+
|
|
685
|
+
export function getCampaignElementSettingsStorageValue(settings?: unknown): CampaignElementSettingsObj | null {
|
|
686
|
+
const values = isCampaignObjectRecord(settings) ? settings : {};
|
|
687
|
+
const emailRow = getCampaignEmailRowStorageSettings(values.emailRow);
|
|
688
|
+
const emailBlock = getCampaignEmailBlockStorageSettings(values.emailBlock);
|
|
689
|
+
const normalized: CampaignElementSettingsObj = {
|
|
690
|
+
...(values.numbered === true ? { numbered: true } : {}),
|
|
691
|
+
...(values.fullWidth === true ? { fullWidth: true } : {}),
|
|
692
|
+
...(emailRow ? { emailRow } : {}),
|
|
693
|
+
...(emailBlock ? { emailBlock } : {}),
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
return Object.keys(normalized).length ? normalized : null;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/*
|
|
700
|
+
* Return one answer's values as an array, so single- and multi-value elements
|
|
701
|
+
* can be validated and rendered through one code path.
|
|
702
|
+
*/
|
|
703
|
+
|
|
704
|
+
export function getCampaignAnswerValues(answer?: CampaignAnswerObj | null): string[] {
|
|
705
|
+
if (answer?.value == null) {
|
|
706
|
+
return [];
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
const values = Array.isArray(answer.value) ? answer.value : [answer.value];
|
|
710
|
+
return values.map((value) => String(value).trim()).filter(Boolean);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
/*
|
|
714
|
+
* Return answer display values using submission-time choice labels first and
|
|
715
|
+
* current element labels only as a fallback for legacy responses.
|
|
716
|
+
*/
|
|
717
|
+
|
|
718
|
+
export function getCampaignAnswerDisplayValues(answer?: CampaignAnswerObj | null, element?: unknown): string[] {
|
|
719
|
+
const labelsByValue = new Map(getCampaignElementOptions(element).map((option) => [option.value, option.label]));
|
|
720
|
+
|
|
721
|
+
for (const option of answer?.selectedOptions || []) {
|
|
722
|
+
if (option && typeof option.value === 'string' && typeof option.label === 'string' && option.value.trim()) {
|
|
723
|
+
labelsByValue.set(option.value, option.label);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
return getCampaignAnswerValues(answer).map((value) => labelsByValue.get(value) ?? value);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/*
|
|
731
|
+
* Check whether one answer holds nothing a respondent actually entered. FILE
|
|
732
|
+
* questions remain empty until an ownership-checked upload transport exists.
|
|
733
|
+
*/
|
|
734
|
+
|
|
735
|
+
export function isCampaignAnswerEmpty(element: Pick<CampaignElementData, 'type'>, answer?: CampaignAnswerObj | null): boolean {
|
|
736
|
+
if (element.type === 'FILE') {
|
|
737
|
+
return true;
|
|
738
|
+
}
|
|
739
|
+
return !getCampaignAnswerValues(answer).length;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
/*
|
|
743
|
+
* Decide whether one element is visible given the answers so far. An element
|
|
744
|
+
* with no condition is always visible; a condition naming an unknown element
|
|
745
|
+
* is treated as visible so a malformed rule can never hide a required
|
|
746
|
+
* question and deadlock the form.
|
|
747
|
+
*/
|
|
748
|
+
|
|
749
|
+
export function isCampaignElementVisible(
|
|
750
|
+
element: Pick<CampaignElementData, 'visibleWhen'>,
|
|
751
|
+
answers: CampaignAnswersObj,
|
|
752
|
+
elementsByKey?: ReadonlyMap<string, unknown>,
|
|
753
|
+
): boolean {
|
|
754
|
+
const condition = element.visibleWhen;
|
|
755
|
+
if (!condition?.elementKey) {
|
|
756
|
+
return true;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
const target = elementsByKey?.get(condition.elementKey);
|
|
760
|
+
if (elementsByKey && !target) {
|
|
761
|
+
return true;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
const values = getCampaignAnswerValues(answers[condition.elementKey]);
|
|
765
|
+
const expected = condition.value == null ? [] : Array.isArray(condition.value) ? condition.value : [condition.value];
|
|
766
|
+
|
|
767
|
+
switch (condition.operator) {
|
|
768
|
+
case 'IS_ANSWERED':
|
|
769
|
+
return values.length > 0;
|
|
770
|
+
case 'NOT_EQUALS':
|
|
771
|
+
return !expected.some((value) => values.includes(value));
|
|
772
|
+
case 'INCLUDES':
|
|
773
|
+
return expected.some((value) => values.includes(value));
|
|
774
|
+
case 'EQUALS':
|
|
775
|
+
return values.length === expected.length && expected.every((value) => values.includes(value));
|
|
776
|
+
default:
|
|
777
|
+
return true;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
export interface CampaignVisibilityElementObj {
|
|
782
|
+
key: string;
|
|
783
|
+
parentKey?: string | null;
|
|
784
|
+
visibleWhen?: CampaignElementVisibleWhenObj | null;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
/*
|
|
788
|
+
* Flatten sections and their children into one visibility graph. A child
|
|
789
|
+
* carries its containing section as an upstream dependency in addition to its
|
|
790
|
+
* own visibleWhen condition.
|
|
791
|
+
*/
|
|
792
|
+
|
|
793
|
+
export function getCampaignDefinitionVisibilityElements(
|
|
794
|
+
sections: Array<{
|
|
795
|
+
key: string;
|
|
796
|
+
visibleWhen?: CampaignElementVisibleWhenObj | null;
|
|
797
|
+
elements?: Array<{
|
|
798
|
+
key: string;
|
|
799
|
+
visibleWhen?: CampaignElementVisibleWhenObj | null;
|
|
800
|
+
}> | null;
|
|
801
|
+
}>,
|
|
802
|
+
): CampaignVisibilityElementObj[] {
|
|
803
|
+
return sections.flatMap((section) => [
|
|
804
|
+
{
|
|
805
|
+
key: section.key,
|
|
806
|
+
visibleWhen: section.visibleWhen || null,
|
|
807
|
+
},
|
|
808
|
+
...(section.elements || []).map((element) => ({
|
|
809
|
+
key: element.key,
|
|
810
|
+
parentKey: section.key,
|
|
811
|
+
visibleWhen: element.visibleWhen || null,
|
|
812
|
+
})),
|
|
813
|
+
]);
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
/*
|
|
817
|
+
* Return the element keys whose own condition and every upstream dependency
|
|
818
|
+
* are visible. Recomputing from the complete definition keeps chained
|
|
819
|
+
* conditions correct even when an earlier answer hides several descendants.
|
|
820
|
+
*/
|
|
821
|
+
|
|
822
|
+
export function getVisibleCampaignElementKeys(elements: CampaignVisibilityElementObj[], answers: CampaignAnswersObj): Set<string> {
|
|
823
|
+
const elementsByKey = new Map(elements.map((element) => [element.key, element]));
|
|
824
|
+
const visibleKeys = new Set(elementsByKey.keys());
|
|
825
|
+
let changed = true;
|
|
826
|
+
|
|
827
|
+
while (changed) {
|
|
828
|
+
changed = false;
|
|
829
|
+
|
|
830
|
+
for (const element of elements) {
|
|
831
|
+
if (!visibleKeys.has(element.key)) {
|
|
832
|
+
continue;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
const dependencyKey = element.visibleWhen?.elementKey;
|
|
836
|
+
const parentIsHidden = !!element.parentKey && elementsByKey.has(element.parentKey) && !visibleKeys.has(element.parentKey);
|
|
837
|
+
const dependencyIsHidden = !!dependencyKey && elementsByKey.has(dependencyKey) && !visibleKeys.has(dependencyKey);
|
|
838
|
+
if (parentIsHidden || dependencyIsHidden || !isCampaignElementVisible(element, answers, elementsByKey)) {
|
|
839
|
+
visibleKeys.delete(element.key);
|
|
840
|
+
changed = true;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
return visibleKeys;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/*
|
|
849
|
+
* Remove answers for questions a respondent can no longer see. The returned
|
|
850
|
+
* map has no object prototype so legacy reserved element keys remain ordinary
|
|
851
|
+
* serializable data until those publications are retired.
|
|
852
|
+
*/
|
|
853
|
+
|
|
854
|
+
export function getVisibleCampaignAnswers(elements: CampaignVisibilityElementObj[], answers: CampaignAnswersObj): CampaignAnswersObj {
|
|
855
|
+
const visibleKeys = getVisibleCampaignElementKeys(elements, answers);
|
|
856
|
+
const visibleAnswers = Object.create(null) as CampaignAnswersObj;
|
|
857
|
+
|
|
858
|
+
for (const [elementKey, answer] of Object.entries(answers)) {
|
|
859
|
+
if (visibleKeys.has(elementKey)) {
|
|
860
|
+
visibleAnswers[elementKey] = answer;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
return visibleAnswers;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/*
|
|
868
|
+
* Validate one respondent's answers against a template definition and return
|
|
869
|
+
* the element keys that failed, so the caller can reject the whole submission
|
|
870
|
+
* with a precise list. Shared by the public form UI and the server so both
|
|
871
|
+
* agree on what "valid" means.
|
|
872
|
+
*
|
|
873
|
+
* Hidden elements are skipped entirely: a required question behind an unmet
|
|
874
|
+
* condition must not block a submission.
|
|
875
|
+
*/
|
|
876
|
+
|
|
877
|
+
export function getInvalidCampaignAnswerKeys(
|
|
878
|
+
elements: CampaignElementData[],
|
|
879
|
+
answers: CampaignAnswersObj,
|
|
880
|
+
options?: {
|
|
881
|
+
requireAnswers?: boolean;
|
|
882
|
+
visibilityElements?: CampaignVisibilityElementObj[];
|
|
883
|
+
},
|
|
884
|
+
): string[] {
|
|
885
|
+
const questions = elements.filter((element) => isCampaignQuestionElement(element) && !isCampaignAIQuestion(element));
|
|
886
|
+
const elementsByKey = new Map(elements.map((element) => [element.key, element]));
|
|
887
|
+
const visibleKeys = getVisibleCampaignElementKeys(options?.visibilityElements || elements, answers);
|
|
888
|
+
const invalidKeys = new Set<string>();
|
|
889
|
+
const textAnswerKeys: string[] = [];
|
|
890
|
+
let totalTextAnswerLength = 0;
|
|
891
|
+
|
|
892
|
+
for (const element of questions) {
|
|
893
|
+
if (!visibleKeys.has(element.key)) {
|
|
894
|
+
continue;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
const answer = answers[element.key];
|
|
898
|
+
const values = getCampaignAnswerValues(answer);
|
|
899
|
+
const isEmpty = isCampaignAnswerEmpty(element, answer);
|
|
900
|
+
if ((element.type === 'SHORT_TEXT' || element.type === 'LONG_TEXT') && values.length) {
|
|
901
|
+
textAnswerKeys.push(element.key);
|
|
902
|
+
totalTextAnswerLength += values.reduce((total, value) => total + value.length, 0);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
if (isEmpty) {
|
|
906
|
+
// Drafts may be incomplete; only a submission enforces "required"
|
|
907
|
+
if (element.required && options?.requireAnswers) {
|
|
908
|
+
invalidKeys.add(element.key);
|
|
909
|
+
}
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (!isCampaignMultiValueElement(element) && values.length > 1) {
|
|
914
|
+
invalidKeys.add(element.key);
|
|
915
|
+
continue;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
if (isCampaignChoiceElement(element)) {
|
|
919
|
+
const allowedValues = new Set(getCampaignElementOptions(element).map((option) => option.value));
|
|
920
|
+
if (values.some((value) => !allowedValues.has(value))) {
|
|
921
|
+
invalidKeys.add(element.key);
|
|
922
|
+
}
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
if (!isCampaignAnswerValueValidForType(element, values)) {
|
|
927
|
+
invalidKeys.add(element.key);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
if (totalTextAnswerLength > CAMPAIGN_RESPONSE_TEXT_ANSWERS_MAX_LENGTH) {
|
|
931
|
+
for (const elementKey of textAnswerKeys) {
|
|
932
|
+
invalidKeys.add(elementKey);
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// An answer naming an element the template does not have is always invalid
|
|
937
|
+
for (const answerKey of Object.keys(answers)) {
|
|
938
|
+
const element = elementsByKey.get(answerKey);
|
|
939
|
+
if (!element || !isCampaignQuestionElement(element)) {
|
|
940
|
+
invalidKeys.add(answerKey);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
return [...invalidKeys];
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
/*
|
|
948
|
+
* Check one ISO-looking date by round-tripping its UTC calendar components.
|
|
949
|
+
*/
|
|
950
|
+
|
|
951
|
+
function isStrictCampaignDate(value: string): boolean {
|
|
952
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
|
|
953
|
+
if (!match) {
|
|
954
|
+
return false;
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
const year = Number(match[1]);
|
|
958
|
+
const month = Number(match[2]);
|
|
959
|
+
const day = Number(match[3]);
|
|
960
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
961
|
+
|
|
962
|
+
return date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/*
|
|
966
|
+
* Check one non-choice answer against its element's type and validation rules.
|
|
967
|
+
*/
|
|
968
|
+
|
|
969
|
+
export function isCampaignAnswerValueValidForType(
|
|
970
|
+
element: Pick<CampaignElementData, 'type' | 'validation'>,
|
|
971
|
+
values: string[],
|
|
972
|
+
): boolean {
|
|
973
|
+
const validation = element.validation || {};
|
|
974
|
+
const textMaxLength = getCampaignTextAnswerMaxLength(element);
|
|
975
|
+
|
|
976
|
+
return values.every((value) => {
|
|
977
|
+
switch (element.type) {
|
|
978
|
+
case 'NUMBER': {
|
|
979
|
+
const numberValue = Number(value);
|
|
980
|
+
if (!Number.isFinite(numberValue)) {
|
|
981
|
+
return false;
|
|
982
|
+
}
|
|
983
|
+
if (validation.min != null && numberValue < validation.min) {
|
|
984
|
+
return false;
|
|
985
|
+
}
|
|
986
|
+
return !(validation.max != null && numberValue > validation.max);
|
|
987
|
+
}
|
|
988
|
+
case 'DATE':
|
|
989
|
+
return isStrictCampaignDate(value);
|
|
990
|
+
case 'EMAIL':
|
|
991
|
+
return isEmail(value);
|
|
992
|
+
case 'PHONE':
|
|
993
|
+
return /^[+()\-.\s\d]{7,20}$/.test(value);
|
|
994
|
+
case 'SHORT_TEXT':
|
|
995
|
+
case 'LONG_TEXT': {
|
|
996
|
+
if (textMaxLength !== null && value.length > textMaxLength) {
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
if (validation.minLength != null && value.length < validation.minLength) {
|
|
1000
|
+
return false;
|
|
1001
|
+
}
|
|
1002
|
+
if (validation.maxLength != null && value.length > validation.maxLength) {
|
|
1003
|
+
return false;
|
|
1004
|
+
}
|
|
1005
|
+
return true;
|
|
1006
|
+
}
|
|
1007
|
+
default:
|
|
1008
|
+
return true;
|
|
1009
|
+
}
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/*
|
|
1014
|
+
* Return the hard public-form text cap, narrowed further by a valid
|
|
1015
|
+
* author-configured maximum when one exists.
|
|
1016
|
+
*/
|
|
1017
|
+
export function getCampaignTextAnswerMaxLength(element: Pick<CampaignElementData, 'type' | 'validation'>): number | null {
|
|
1018
|
+
const hardMaxLength =
|
|
1019
|
+
element.type === 'SHORT_TEXT'
|
|
1020
|
+
? CAMPAIGN_SHORT_TEXT_ANSWER_MAX_LENGTH
|
|
1021
|
+
: element.type === 'LONG_TEXT'
|
|
1022
|
+
? CAMPAIGN_LONG_TEXT_ANSWER_MAX_LENGTH
|
|
1023
|
+
: null;
|
|
1024
|
+
if (hardMaxLength === null) {
|
|
1025
|
+
return null;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const configuredMaxLength = element.validation?.maxLength;
|
|
1029
|
+
return typeof configuredMaxLength === 'number' && Number.isSafeInteger(configuredMaxLength) && configuredMaxLength > 0
|
|
1030
|
+
? Math.min(configuredMaxLength, hardMaxLength)
|
|
1031
|
+
: hardMaxLength;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
/*
|
|
1035
|
+
* Check whether one value is a supported form background fill style.
|
|
1036
|
+
*/
|
|
1037
|
+
|
|
1038
|
+
export function isCampaignFormBackgroundFillStyle(value?: string | null): value is CampaignFormBackgroundFillStyleEnum {
|
|
1039
|
+
return CAMPAIGN_FORM_BACKGROUND_FILL_STYLES.includes(value as CampaignFormBackgroundFillStyleEnum);
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
/*
|
|
1043
|
+
* Resolve a form color into one generated CSS palette name.
|
|
1044
|
+
*/
|
|
1045
|
+
|
|
1046
|
+
export function normalizeCampaignFormColor(color?: string | null): string | null {
|
|
1047
|
+
const normalizedColor = String(color || '')
|
|
1048
|
+
.trim()
|
|
1049
|
+
.toLowerCase();
|
|
1050
|
+
|
|
1051
|
+
if (!normalizedColor) {
|
|
1052
|
+
return null;
|
|
1053
|
+
}
|
|
1054
|
+
|
|
1055
|
+
if (normalizedColor === 'gray' || normalizedColor === 'grey') {
|
|
1056
|
+
return CAMPAIGN_FORM_NEUTRAL_COLOR;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
return CAMPAIGN_FORM_COLORS.includes(normalizedColor) ? normalizedColor : null;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
/*
|
|
1063
|
+
* Resolve the form theme color; GRAYSCALE always renders neutral.
|
|
1064
|
+
*/
|
|
1065
|
+
|
|
1066
|
+
export function getCampaignFormColor(settings?: FormCampaignSettingsObj | null): string | null {
|
|
1067
|
+
if (getCampaignFormBackgroundFillStyle(settings) === 'GRAYSCALE') {
|
|
1068
|
+
return CAMPAIGN_FORM_NEUTRAL_COLOR;
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
return normalizeCampaignFormColor(settings?.color);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/*
|
|
1075
|
+
* Resolve the form background fill style, falling back to the default.
|
|
1076
|
+
*/
|
|
1077
|
+
|
|
1078
|
+
export function getCampaignFormBackgroundFillStyle(settings?: FormCampaignSettingsObj | null): CampaignFormBackgroundFillStyleEnum {
|
|
1079
|
+
const style = String(settings?.backgroundFillStyle || '')
|
|
1080
|
+
.trim()
|
|
1081
|
+
.toUpperCase();
|
|
1082
|
+
|
|
1083
|
+
return isCampaignFormBackgroundFillStyle(style) ? style : CAMPAIGN_FORM_DEFAULT_BACKGROUND_FILL_STYLE;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
/*
|
|
1087
|
+
* Resolve one form's body background colour to a CSS custom property.
|
|
1088
|
+
*
|
|
1089
|
+
* GRAYSCALE ignores the chosen colour entirely; every other fill style maps to
|
|
1090
|
+
* a shade of it. Unknown input falls back to the neutral palette rather than
|
|
1091
|
+
* emitting an undefined variable.
|
|
1092
|
+
*/
|
|
1093
|
+
|
|
1094
|
+
export function getCampaignFormBackgroundColorValue(color?: string | null, backgroundFillStyle?: string | null): string {
|
|
1095
|
+
const style = String(backgroundFillStyle || '')
|
|
1096
|
+
.trim()
|
|
1097
|
+
.toUpperCase();
|
|
1098
|
+
const fillStyle = isCampaignFormBackgroundFillStyle(style) ? style : CAMPAIGN_FORM_DEFAULT_BACKGROUND_FILL_STYLE;
|
|
1099
|
+
const resolvedColor =
|
|
1100
|
+
fillStyle === 'GRAYSCALE' ? CAMPAIGN_FORM_NEUTRAL_COLOR : normalizeCampaignFormColor(color) || CAMPAIGN_FORM_NEUTRAL_COLOR;
|
|
1101
|
+
|
|
1102
|
+
switch (fillStyle) {
|
|
1103
|
+
case 'FADED':
|
|
1104
|
+
return `var(--color-${resolvedColor}-alt-fade)`;
|
|
1105
|
+
case 'LIGHT':
|
|
1106
|
+
return `var(--color-${resolvedColor}-alt-light)`;
|
|
1107
|
+
case 'GRAYSCALE':
|
|
1108
|
+
case 'MEDIUM':
|
|
1109
|
+
return `var(--color-${resolvedColor}-alt-medium)`;
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
return `var(--color-${resolvedColor}-alt-medium)`;
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
/*
|
|
1116
|
+
* Resolve one typed form settings object to its background colour.
|
|
1117
|
+
*/
|
|
1118
|
+
export function getCampaignFormBackgroundColorValueFromSettings(settings?: FormCampaignSettingsObj | null): string {
|
|
1119
|
+
return getCampaignFormBackgroundColorValue(settings?.color, settings?.backgroundFillStyle);
|
|
1120
|
+
}
|
|
1121
|
+
|
|
1122
|
+
/**
|
|
1123
|
+
* Local draft shape for the create-campaign modal.
|
|
1124
|
+
*
|
|
1125
|
+
* Every persisted field is declared so a draft written by an older build is
|
|
1126
|
+
* rejected rather than half-read; the file list is capped at the same limit the
|
|
1127
|
+
* generation request enforces.
|
|
1128
|
+
*/
|
|
1129
|
+
|
|
1130
|
+
export const CAMPAIGN_DRAFT_SCHEMA: DraftSchema = {
|
|
1131
|
+
files: {
|
|
1132
|
+
type: 'array',
|
|
1133
|
+
maxLength: MAX_PUBLIC_FORM_GENERATION_FILES,
|
|
1134
|
+
of: {
|
|
1135
|
+
contentType: 'string',
|
|
1136
|
+
fileKey: 'string',
|
|
1137
|
+
fileUri: 'string',
|
|
1138
|
+
name: 'string',
|
|
1139
|
+
storageId: 'string',
|
|
1140
|
+
uploadedAt: 'number',
|
|
1141
|
+
},
|
|
1142
|
+
},
|
|
1143
|
+
instructions: {
|
|
1144
|
+
EMAIL: { type: 'string', maxLength: CAMPAIGN_INSTRUCTIONS_MAX_LENGTH },
|
|
1145
|
+
PUBLIC_FORM: {
|
|
1146
|
+
type: 'string',
|
|
1147
|
+
maxLength: CAMPAIGN_INSTRUCTIONS_MAX_LENGTH,
|
|
1148
|
+
},
|
|
1149
|
+
},
|
|
1150
|
+
};
|
|
1151
|
+
|
|
1152
|
+
/*
|
|
1153
|
+
* Build the local draft key for one organization's create-campaign modal.
|
|
1154
|
+
* Drafts hold organization content, so the key is scoped per organization.
|
|
1155
|
+
*/
|
|
1156
|
+
|
|
1157
|
+
export function getCampaignDraftKey(organizationId: string | number | bigint) {
|
|
1158
|
+
return `${CAMPAIGN_DRAFT_KEY_PREFIX}:${organizationId}`;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
/*
|
|
1162
|
+
* Build the empty create-campaign draft. Returned fresh each call so callers
|
|
1163
|
+
* can hold it in state without sharing one mutable object.
|
|
1164
|
+
*/
|
|
1165
|
+
|
|
1166
|
+
export function getEmptyCampaignDraft(): CampaignDraft {
|
|
1167
|
+
return {
|
|
1168
|
+
files: [],
|
|
1169
|
+
instructions: {
|
|
1170
|
+
EMAIL: '',
|
|
1171
|
+
PUBLIC_FORM: '',
|
|
1172
|
+
},
|
|
1173
|
+
};
|
|
1174
|
+
}
|