@betterinternship/core 2.0.5 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/broker/broker.d.ts +11 -11
- package/dist/lib/broker/broker.js +77 -77
- package/dist/lib/broker/broker.js.map +1 -1
- package/dist/lib/broker/index.d.ts +1 -1
- package/dist/lib/broker/index.js +1 -1
- package/dist/lib/broker/index.js.map +1 -1
- package/dist/lib/chat/chat.d.ts +1 -1
- package/dist/lib/chat/chat.js +183 -183
- package/dist/lib/chat/chat.js.map +1 -1
- package/dist/lib/chat/index.d.ts +2 -2
- package/dist/lib/chat/index.js +2 -2
- package/dist/lib/chat/index.js.map +1 -1
- package/dist/lib/chat/pb.d.ts +1 -1
- package/dist/lib/chat/pb.js +15 -15
- package/dist/lib/chat/pb.js.map +1 -1
- package/dist/lib/email/email.js +66 -66
- package/dist/lib/email/email.js.map +1 -1
- package/dist/lib/email/index.d.ts +1 -1
- package/dist/lib/email/index.js +1 -1
- package/dist/lib/email/index.js.map +1 -1
- package/dist/lib/forms/fields.client.d.ts +1 -1
- package/dist/lib/forms/fields.client.js +20 -20
- package/dist/lib/forms/form-metadata.d.ts +127 -127
- package/dist/lib/forms/form-metadata.js +320 -320
- package/dist/lib/forms/index.d.ts +3 -3
- package/dist/lib/forms/index.js +3 -3
- package/dist/lib/forms/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +7 -1
|
@@ -1,321 +1,321 @@
|
|
|
1
|
-
import z from 'zod';
|
|
2
|
-
import { getSchemaClientType, } from './fields.client';
|
|
3
|
-
export const SCHEMA_VERSION = 1;
|
|
4
|
-
export const FIELD_TYPES = ['text', 'signature'];
|
|
5
|
-
export const BLOCK_TYPES = [
|
|
6
|
-
'header',
|
|
7
|
-
'paragraph',
|
|
8
|
-
'form_field',
|
|
9
|
-
'form_phantom_field',
|
|
10
|
-
'divider',
|
|
11
|
-
'image',
|
|
12
|
-
];
|
|
13
|
-
export const ALIGN_H = ['left', 'center', 'right'];
|
|
14
|
-
export const ALIGN_V = ['top', 'middle', 'bottom'];
|
|
15
|
-
export const SOURCES = ['auto', 'prefill', 'derived', 'manual'];
|
|
16
|
-
export class FormMetadata {
|
|
17
|
-
formMetadata;
|
|
18
|
-
blocks;
|
|
19
|
-
constructor(formMetadata) {
|
|
20
|
-
this.formMetadata = formMetadata;
|
|
21
|
-
this.blocks = formMetadata.schema.blocks;
|
|
22
|
-
}
|
|
23
|
-
getBlocksForClientService(signingPartyId) {
|
|
24
|
-
return this.blocks
|
|
25
|
-
.filter((block) => block.signing_party_id === signingPartyId || !signingPartyId)
|
|
26
|
-
.map((block) => {
|
|
27
|
-
const field = block.field_schema;
|
|
28
|
-
const phantomField = block.phantom_field_schema;
|
|
29
|
-
if (!field && !phantomField)
|
|
30
|
-
return block;
|
|
31
|
-
return {
|
|
32
|
-
...block,
|
|
33
|
-
field_schema: this.getFieldForClientService(block._id, 'field'),
|
|
34
|
-
phantom_field_schema: this.getFieldForClientService(block._id, 'phantom'),
|
|
35
|
-
};
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
getSigningPartyBlocks(signingPartyId) {
|
|
39
|
-
const signingParties = this.getSigningParties();
|
|
40
|
-
const singingPartyBlocks = [];
|
|
41
|
-
for (let i = 0; i < signingParties.length; i++) {
|
|
42
|
-
const signingParty = signingParties[i];
|
|
43
|
-
if (signingParty.signatory_source?._id !== signingPartyId)
|
|
44
|
-
continue;
|
|
45
|
-
singingPartyBlocks.push({
|
|
46
|
-
block_type: 'form_phantom_field',
|
|
47
|
-
order: 1000 + signingParty.order,
|
|
48
|
-
signing_party_id: signingPartyId,
|
|
49
|
-
phantom_field_schema: {
|
|
50
|
-
field: `__${signingParty._id}-email:default`,
|
|
51
|
-
type: 'text',
|
|
52
|
-
label: signingParty.signatory_source?.label,
|
|
53
|
-
tooltip_label: signingParty.signatory_source?.tooltip_label,
|
|
54
|
-
signing_party_id: signingPartyId,
|
|
55
|
-
shared: false,
|
|
56
|
-
source: 'manual',
|
|
57
|
-
coerce: (s) => s,
|
|
58
|
-
prefiller: null,
|
|
59
|
-
validator: z.email(),
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
return singingPartyBlocks;
|
|
64
|
-
}
|
|
65
|
-
getSigningPartyFields(signingPartyId) {
|
|
66
|
-
const singingPartyBlocks = this.getSigningPartyBlocks(signingPartyId);
|
|
67
|
-
return singingPartyBlocks
|
|
68
|
-
.map((block) => block.field_schema ?? block.phantom_field_schema)
|
|
69
|
-
.filter((field) => !!field);
|
|
70
|
-
}
|
|
71
|
-
static getSigningPartyValues(signingParties, values) {
|
|
72
|
-
const signingPartyValues = {};
|
|
73
|
-
for (let i = 0; i < signingParties.length; i++) {
|
|
74
|
-
const signingParty = signingParties[i];
|
|
75
|
-
const emailSource = `__${signingParty._id}-email:default`;
|
|
76
|
-
if (!values[emailSource])
|
|
77
|
-
continue;
|
|
78
|
-
signingPartyValues[signingParty._id] = {
|
|
79
|
-
email: values[emailSource],
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
return signingPartyValues;
|
|
83
|
-
}
|
|
84
|
-
getFields() {
|
|
85
|
-
return this.blocks
|
|
86
|
-
.filter((block) => block.block_type === 'form_field')
|
|
87
|
-
.map((block) => ({
|
|
88
|
-
...block.field_schema,
|
|
89
|
-
_id: block._id,
|
|
90
|
-
signing_party_id: block.signing_party_id,
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
|
-
getField(_id) {
|
|
94
|
-
const fields = this.getFields();
|
|
95
|
-
return fields.find((f) => f._id === _id);
|
|
96
|
-
}
|
|
97
|
-
getPhantomFields() {
|
|
98
|
-
return this.blocks
|
|
99
|
-
.filter((block) => block.block_type === 'form_phantom_field')
|
|
100
|
-
.map((block) => ({
|
|
101
|
-
_id: block._id,
|
|
102
|
-
signing_party_id: block.signing_party_id,
|
|
103
|
-
...block.phantom_field_schema,
|
|
104
|
-
}));
|
|
105
|
-
}
|
|
106
|
-
getPhantomField(_id) {
|
|
107
|
-
const phantomFields = this.getPhantomFields();
|
|
108
|
-
return phantomFields.find((f) => f._id === _id);
|
|
109
|
-
}
|
|
110
|
-
encodeAsJSON() {
|
|
111
|
-
return JSON.stringify(this.formMetadata);
|
|
112
|
-
}
|
|
113
|
-
static decodeFromJSON(json) {
|
|
114
|
-
const parsed = JSON.parse(json);
|
|
115
|
-
return new FormMetadata(parsed);
|
|
116
|
-
}
|
|
117
|
-
getLabel() {
|
|
118
|
-
return this.formMetadata.label;
|
|
119
|
-
}
|
|
120
|
-
getFieldForClientService(_id, fieldType, sourceDomains, derivationBase) {
|
|
121
|
-
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
122
|
-
if (!field)
|
|
123
|
-
return undefined;
|
|
124
|
-
const finalParams = {
|
|
125
|
-
...derivationBase,
|
|
126
|
-
...sourceDomains,
|
|
127
|
-
};
|
|
128
|
-
const validator = this.parseValidator(field._id, fieldType, finalParams);
|
|
129
|
-
const prefiller = this.parsePrefiller(field._id, fieldType, finalParams);
|
|
130
|
-
let options = undefined;
|
|
131
|
-
if (validator?.type === 'enum')
|
|
132
|
-
options = validator.options;
|
|
133
|
-
if (validator?.type === 'array') {
|
|
134
|
-
const element = validator.element;
|
|
135
|
-
if (element?.options)
|
|
136
|
-
options = element.options;
|
|
137
|
-
}
|
|
138
|
-
const type = field.type === 'signature'
|
|
139
|
-
? 'signature'
|
|
140
|
-
: validator
|
|
141
|
-
? getSchemaClientType(validator)
|
|
142
|
-
: 'text';
|
|
143
|
-
const coerce = (value) => {
|
|
144
|
-
switch (type) {
|
|
145
|
-
case 'number': {
|
|
146
|
-
const n = parseInt(value);
|
|
147
|
-
return isNaN(n) ? 0 : n;
|
|
148
|
-
}
|
|
149
|
-
case 'date': {
|
|
150
|
-
const n = parseInt(value);
|
|
151
|
-
return isNaN(n) ? new Date() : new Date(n);
|
|
152
|
-
}
|
|
153
|
-
case 'checkbox': {
|
|
154
|
-
return !!value?.trim();
|
|
155
|
-
}
|
|
156
|
-
case 'multiselect': {
|
|
157
|
-
return value?.split('\n');
|
|
158
|
-
}
|
|
159
|
-
default:
|
|
160
|
-
return value;
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
return {
|
|
164
|
-
field: field.field,
|
|
165
|
-
shared: field.shared,
|
|
166
|
-
label: field.label,
|
|
167
|
-
source: field.source,
|
|
168
|
-
tooltip_label: field.tooltip_label,
|
|
169
|
-
signing_party_id: field.signing_party_id,
|
|
170
|
-
coerce,
|
|
171
|
-
type,
|
|
172
|
-
validator,
|
|
173
|
-
prefiller,
|
|
174
|
-
options,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
getFieldsForClientService(signingPartyId, sourceDomains, derivationBase) {
|
|
178
|
-
const fields = this.getFields()
|
|
179
|
-
.filter((field) => field.signing_party_id === signingPartyId || !signingPartyId)
|
|
180
|
-
.map((field) => this.getFieldForClientService(field._id, 'field', sourceDomains, derivationBase))
|
|
181
|
-
.filter((field) => !!field);
|
|
182
|
-
const phantomFields = this.getPhantomFields()
|
|
183
|
-
.filter((phantomField) => phantomField.signing_party_id === signingPartyId || !signingPartyId)
|
|
184
|
-
.map((field) => this.getFieldForClientService(field._id, 'phantom', sourceDomains, derivationBase))
|
|
185
|
-
.filter((field) => !!field);
|
|
186
|
-
return [...fields, ...phantomFields];
|
|
187
|
-
}
|
|
188
|
-
getSignatureFieldsForClientService(signingPartyId) {
|
|
189
|
-
return this.getFieldsForClientService(signingPartyId).filter((field) => field.type === 'signature');
|
|
190
|
-
}
|
|
191
|
-
getSignatureValueForSigningParty(values, signingPartyId) {
|
|
192
|
-
const signatureFields = this.getSignatureFieldsForClientService(signingPartyId);
|
|
193
|
-
const signatureValues = {};
|
|
194
|
-
for (const signatureField of signatureFields)
|
|
195
|
-
signatureValues[signatureField.field] = values[signatureField.field];
|
|
196
|
-
return Object.values(signatureValues)[0];
|
|
197
|
-
}
|
|
198
|
-
setSignatureValueForSigningParty(values, signatureString, signingPartyId) {
|
|
199
|
-
const signatureFields = this.getSignatureFieldsForClientService(signingPartyId);
|
|
200
|
-
const signatureValues = {};
|
|
201
|
-
for (const signatureField of signatureFields)
|
|
202
|
-
signatureValues[signatureField.field] = signatureString;
|
|
203
|
-
return {
|
|
204
|
-
...values,
|
|
205
|
-
...signatureValues,
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
getFieldsForEditorService() {
|
|
209
|
-
return this.getFields();
|
|
210
|
-
}
|
|
211
|
-
getPhantomFieldsForEditorService() {
|
|
212
|
-
return this.getPhantomFields();
|
|
213
|
-
}
|
|
214
|
-
getBlocksForEditorService() {
|
|
215
|
-
return this.blocks;
|
|
216
|
-
}
|
|
217
|
-
getFieldsForSigningService() {
|
|
218
|
-
const fields = this.getFields();
|
|
219
|
-
return fields
|
|
220
|
-
.filter((field) => field.type !== 'image')
|
|
221
|
-
.map((field) => ({
|
|
222
|
-
field: field.field,
|
|
223
|
-
type: field.type,
|
|
224
|
-
x: field.x,
|
|
225
|
-
y: field.y,
|
|
226
|
-
w: field.w,
|
|
227
|
-
h: field.h,
|
|
228
|
-
align_h: (field.align_h ?? 'left'),
|
|
229
|
-
align_v: (field.align_v ?? 'top'),
|
|
230
|
-
page: field.page,
|
|
231
|
-
size: field.size,
|
|
232
|
-
wrap: field.wrap,
|
|
233
|
-
}))
|
|
234
|
-
.filter((field) => field !== null);
|
|
235
|
-
}
|
|
236
|
-
inferParams() {
|
|
237
|
-
const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
|
|
238
|
-
const params = [];
|
|
239
|
-
let match;
|
|
240
|
-
for (const field of this.getFields()) {
|
|
241
|
-
while ((match = inferenceRegex.exec(field.validator ?? '')) !== null)
|
|
242
|
-
if (!params.includes(match[2]))
|
|
243
|
-
params.push(match[2]);
|
|
244
|
-
}
|
|
245
|
-
for (const field of this.getPhantomFields()) {
|
|
246
|
-
while ((match = inferenceRegex.exec(field.validator ?? '')) !== null)
|
|
247
|
-
if (!params.includes(match[2]))
|
|
248
|
-
params.push(match[2]);
|
|
249
|
-
}
|
|
250
|
-
return params;
|
|
251
|
-
}
|
|
252
|
-
parseValidator(_id, fieldType, params = {}) {
|
|
253
|
-
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
254
|
-
if (!field)
|
|
255
|
-
return null;
|
|
256
|
-
const d = new Date();
|
|
257
|
-
const defaults = {
|
|
258
|
-
currentDate: d,
|
|
259
|
-
currentDateTimestamp: d.getTime(),
|
|
260
|
-
};
|
|
261
|
-
const validatorStr = typeof field.validator === 'string' ? field.validator : '';
|
|
262
|
-
const validator = this.populateParams(validatorStr, params);
|
|
263
|
-
const ret = `return ${validator}`;
|
|
264
|
-
const evaluator = new Function('z', 'params', ret);
|
|
265
|
-
return evaluator(z, { ...params, ...defaults });
|
|
266
|
-
}
|
|
267
|
-
parsePrefiller(_id, fieldType, params = {}) {
|
|
268
|
-
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
269
|
-
if (!field || !field.prefiller)
|
|
270
|
-
return null;
|
|
271
|
-
const prefillerStr = typeof field.prefiller === 'string' ? field.prefiller : '';
|
|
272
|
-
const prefiller = this.populateParams(prefillerStr, params);
|
|
273
|
-
try {
|
|
274
|
-
const fn = eval(prefiller);
|
|
275
|
-
return typeof fn === 'function' ? fn : null;
|
|
276
|
-
}
|
|
277
|
-
catch (e) {
|
|
278
|
-
console.warn(`Failed to parse prefiller for field ${_id}`, e);
|
|
279
|
-
console.warn(`The prefiller was: ${field.prefiller}`);
|
|
280
|
-
return null;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
enumerateParams(s) {
|
|
284
|
-
const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
|
|
285
|
-
const params = detectedParams.map((dp) => {
|
|
286
|
-
const [fieldName, propertyTree] = dp
|
|
287
|
-
.replaceAll('#{', '')
|
|
288
|
-
.replaceAll('}', '')
|
|
289
|
-
.replaceAll(')', '')
|
|
290
|
-
.split('(');
|
|
291
|
-
const properties = propertyTree?.split('.') ?? [];
|
|
292
|
-
return [fieldName, ...properties];
|
|
293
|
-
});
|
|
294
|
-
return params?.reduce((acc, cur, i) => ((acc[detectedParams[i]] = cur), acc), {});
|
|
295
|
-
}
|
|
296
|
-
populateParams(s, params) {
|
|
297
|
-
const paramsReflection = this.enumerateParams(s);
|
|
298
|
-
const paramsValues = {};
|
|
299
|
-
for (const match in paramsReflection) {
|
|
300
|
-
const path = paramsReflection[match];
|
|
301
|
-
paramsValues[match] =
|
|
302
|
-
path.reduce((acc, cur) => acc?.[cur], params) ?? '';
|
|
303
|
-
}
|
|
304
|
-
let out = s;
|
|
305
|
-
for (const match in paramsValues) {
|
|
306
|
-
const replacement = JSON.stringify(paramsValues[match]);
|
|
307
|
-
out = out.replaceAll(match, replacement);
|
|
308
|
-
}
|
|
309
|
-
return out;
|
|
310
|
-
}
|
|
311
|
-
getSubscribers() {
|
|
312
|
-
return this.formMetadata.subscribers || [];
|
|
313
|
-
}
|
|
314
|
-
getSigningParties() {
|
|
315
|
-
return this.formMetadata.signing_parties;
|
|
316
|
-
}
|
|
317
|
-
mayInvolveEsign() {
|
|
318
|
-
return this.getFieldsForSigningService().some((field) => field.type === 'signature');
|
|
319
|
-
}
|
|
320
|
-
}
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { getSchemaClientType, } from './fields.client';
|
|
3
|
+
export const SCHEMA_VERSION = 1;
|
|
4
|
+
export const FIELD_TYPES = ['text', 'signature'];
|
|
5
|
+
export const BLOCK_TYPES = [
|
|
6
|
+
'header',
|
|
7
|
+
'paragraph',
|
|
8
|
+
'form_field',
|
|
9
|
+
'form_phantom_field',
|
|
10
|
+
'divider',
|
|
11
|
+
'image',
|
|
12
|
+
];
|
|
13
|
+
export const ALIGN_H = ['left', 'center', 'right'];
|
|
14
|
+
export const ALIGN_V = ['top', 'middle', 'bottom'];
|
|
15
|
+
export const SOURCES = ['auto', 'prefill', 'derived', 'manual'];
|
|
16
|
+
export class FormMetadata {
|
|
17
|
+
formMetadata;
|
|
18
|
+
blocks;
|
|
19
|
+
constructor(formMetadata) {
|
|
20
|
+
this.formMetadata = formMetadata;
|
|
21
|
+
this.blocks = formMetadata.schema.blocks;
|
|
22
|
+
}
|
|
23
|
+
getBlocksForClientService(signingPartyId) {
|
|
24
|
+
return this.blocks
|
|
25
|
+
.filter((block) => block.signing_party_id === signingPartyId || !signingPartyId)
|
|
26
|
+
.map((block) => {
|
|
27
|
+
const field = block.field_schema;
|
|
28
|
+
const phantomField = block.phantom_field_schema;
|
|
29
|
+
if (!field && !phantomField)
|
|
30
|
+
return block;
|
|
31
|
+
return {
|
|
32
|
+
...block,
|
|
33
|
+
field_schema: this.getFieldForClientService(block._id, 'field'),
|
|
34
|
+
phantom_field_schema: this.getFieldForClientService(block._id, 'phantom'),
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
getSigningPartyBlocks(signingPartyId) {
|
|
39
|
+
const signingParties = this.getSigningParties();
|
|
40
|
+
const singingPartyBlocks = [];
|
|
41
|
+
for (let i = 0; i < signingParties.length; i++) {
|
|
42
|
+
const signingParty = signingParties[i];
|
|
43
|
+
if (signingParty.signatory_source?._id !== signingPartyId)
|
|
44
|
+
continue;
|
|
45
|
+
singingPartyBlocks.push({
|
|
46
|
+
block_type: 'form_phantom_field',
|
|
47
|
+
order: 1000 + signingParty.order,
|
|
48
|
+
signing_party_id: signingPartyId,
|
|
49
|
+
phantom_field_schema: {
|
|
50
|
+
field: `__${signingParty._id}-email:default`,
|
|
51
|
+
type: 'text',
|
|
52
|
+
label: signingParty.signatory_source?.label,
|
|
53
|
+
tooltip_label: signingParty.signatory_source?.tooltip_label,
|
|
54
|
+
signing_party_id: signingPartyId,
|
|
55
|
+
shared: false,
|
|
56
|
+
source: 'manual',
|
|
57
|
+
coerce: (s) => s,
|
|
58
|
+
prefiller: null,
|
|
59
|
+
validator: z.email(),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return singingPartyBlocks;
|
|
64
|
+
}
|
|
65
|
+
getSigningPartyFields(signingPartyId) {
|
|
66
|
+
const singingPartyBlocks = this.getSigningPartyBlocks(signingPartyId);
|
|
67
|
+
return singingPartyBlocks
|
|
68
|
+
.map((block) => block.field_schema ?? block.phantom_field_schema)
|
|
69
|
+
.filter((field) => !!field);
|
|
70
|
+
}
|
|
71
|
+
static getSigningPartyValues(signingParties, values) {
|
|
72
|
+
const signingPartyValues = {};
|
|
73
|
+
for (let i = 0; i < signingParties.length; i++) {
|
|
74
|
+
const signingParty = signingParties[i];
|
|
75
|
+
const emailSource = `__${signingParty._id}-email:default`;
|
|
76
|
+
if (!values[emailSource])
|
|
77
|
+
continue;
|
|
78
|
+
signingPartyValues[signingParty._id] = {
|
|
79
|
+
email: values[emailSource],
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return signingPartyValues;
|
|
83
|
+
}
|
|
84
|
+
getFields() {
|
|
85
|
+
return this.blocks
|
|
86
|
+
.filter((block) => block.block_type === 'form_field')
|
|
87
|
+
.map((block) => ({
|
|
88
|
+
...block.field_schema,
|
|
89
|
+
_id: block._id,
|
|
90
|
+
signing_party_id: block.signing_party_id,
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
getField(_id) {
|
|
94
|
+
const fields = this.getFields();
|
|
95
|
+
return fields.find((f) => f._id === _id);
|
|
96
|
+
}
|
|
97
|
+
getPhantomFields() {
|
|
98
|
+
return this.blocks
|
|
99
|
+
.filter((block) => block.block_type === 'form_phantom_field')
|
|
100
|
+
.map((block) => ({
|
|
101
|
+
_id: block._id,
|
|
102
|
+
signing_party_id: block.signing_party_id,
|
|
103
|
+
...block.phantom_field_schema,
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
getPhantomField(_id) {
|
|
107
|
+
const phantomFields = this.getPhantomFields();
|
|
108
|
+
return phantomFields.find((f) => f._id === _id);
|
|
109
|
+
}
|
|
110
|
+
encodeAsJSON() {
|
|
111
|
+
return JSON.stringify(this.formMetadata);
|
|
112
|
+
}
|
|
113
|
+
static decodeFromJSON(json) {
|
|
114
|
+
const parsed = JSON.parse(json);
|
|
115
|
+
return new FormMetadata(parsed);
|
|
116
|
+
}
|
|
117
|
+
getLabel() {
|
|
118
|
+
return this.formMetadata.label;
|
|
119
|
+
}
|
|
120
|
+
getFieldForClientService(_id, fieldType, sourceDomains, derivationBase) {
|
|
121
|
+
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
122
|
+
if (!field)
|
|
123
|
+
return undefined;
|
|
124
|
+
const finalParams = {
|
|
125
|
+
...derivationBase,
|
|
126
|
+
...sourceDomains,
|
|
127
|
+
};
|
|
128
|
+
const validator = this.parseValidator(field._id, fieldType, finalParams);
|
|
129
|
+
const prefiller = this.parsePrefiller(field._id, fieldType, finalParams);
|
|
130
|
+
let options = undefined;
|
|
131
|
+
if (validator?.type === 'enum')
|
|
132
|
+
options = validator.options;
|
|
133
|
+
if (validator?.type === 'array') {
|
|
134
|
+
const element = validator.element;
|
|
135
|
+
if (element?.options)
|
|
136
|
+
options = element.options;
|
|
137
|
+
}
|
|
138
|
+
const type = field.type === 'signature'
|
|
139
|
+
? 'signature'
|
|
140
|
+
: validator
|
|
141
|
+
? getSchemaClientType(validator)
|
|
142
|
+
: 'text';
|
|
143
|
+
const coerce = (value) => {
|
|
144
|
+
switch (type) {
|
|
145
|
+
case 'number': {
|
|
146
|
+
const n = parseInt(value);
|
|
147
|
+
return isNaN(n) ? 0 : n;
|
|
148
|
+
}
|
|
149
|
+
case 'date': {
|
|
150
|
+
const n = parseInt(value);
|
|
151
|
+
return isNaN(n) ? new Date() : new Date(n);
|
|
152
|
+
}
|
|
153
|
+
case 'checkbox': {
|
|
154
|
+
return !!value?.trim();
|
|
155
|
+
}
|
|
156
|
+
case 'multiselect': {
|
|
157
|
+
return value?.split('\n');
|
|
158
|
+
}
|
|
159
|
+
default:
|
|
160
|
+
return value;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
field: field.field,
|
|
165
|
+
shared: field.shared,
|
|
166
|
+
label: field.label,
|
|
167
|
+
source: field.source,
|
|
168
|
+
tooltip_label: field.tooltip_label,
|
|
169
|
+
signing_party_id: field.signing_party_id,
|
|
170
|
+
coerce,
|
|
171
|
+
type,
|
|
172
|
+
validator,
|
|
173
|
+
prefiller,
|
|
174
|
+
options,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
getFieldsForClientService(signingPartyId, sourceDomains, derivationBase) {
|
|
178
|
+
const fields = this.getFields()
|
|
179
|
+
.filter((field) => field.signing_party_id === signingPartyId || !signingPartyId)
|
|
180
|
+
.map((field) => this.getFieldForClientService(field._id, 'field', sourceDomains, derivationBase))
|
|
181
|
+
.filter((field) => !!field);
|
|
182
|
+
const phantomFields = this.getPhantomFields()
|
|
183
|
+
.filter((phantomField) => phantomField.signing_party_id === signingPartyId || !signingPartyId)
|
|
184
|
+
.map((field) => this.getFieldForClientService(field._id, 'phantom', sourceDomains, derivationBase))
|
|
185
|
+
.filter((field) => !!field);
|
|
186
|
+
return [...fields, ...phantomFields];
|
|
187
|
+
}
|
|
188
|
+
getSignatureFieldsForClientService(signingPartyId) {
|
|
189
|
+
return this.getFieldsForClientService(signingPartyId).filter((field) => field.type === 'signature');
|
|
190
|
+
}
|
|
191
|
+
getSignatureValueForSigningParty(values, signingPartyId) {
|
|
192
|
+
const signatureFields = this.getSignatureFieldsForClientService(signingPartyId);
|
|
193
|
+
const signatureValues = {};
|
|
194
|
+
for (const signatureField of signatureFields)
|
|
195
|
+
signatureValues[signatureField.field] = values[signatureField.field];
|
|
196
|
+
return Object.values(signatureValues)[0];
|
|
197
|
+
}
|
|
198
|
+
setSignatureValueForSigningParty(values, signatureString, signingPartyId) {
|
|
199
|
+
const signatureFields = this.getSignatureFieldsForClientService(signingPartyId);
|
|
200
|
+
const signatureValues = {};
|
|
201
|
+
for (const signatureField of signatureFields)
|
|
202
|
+
signatureValues[signatureField.field] = signatureString;
|
|
203
|
+
return {
|
|
204
|
+
...values,
|
|
205
|
+
...signatureValues,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
getFieldsForEditorService() {
|
|
209
|
+
return this.getFields();
|
|
210
|
+
}
|
|
211
|
+
getPhantomFieldsForEditorService() {
|
|
212
|
+
return this.getPhantomFields();
|
|
213
|
+
}
|
|
214
|
+
getBlocksForEditorService() {
|
|
215
|
+
return this.blocks;
|
|
216
|
+
}
|
|
217
|
+
getFieldsForSigningService() {
|
|
218
|
+
const fields = this.getFields();
|
|
219
|
+
return fields
|
|
220
|
+
.filter((field) => field.type !== 'image')
|
|
221
|
+
.map((field) => ({
|
|
222
|
+
field: field.field,
|
|
223
|
+
type: field.type,
|
|
224
|
+
x: field.x,
|
|
225
|
+
y: field.y,
|
|
226
|
+
w: field.w,
|
|
227
|
+
h: field.h,
|
|
228
|
+
align_h: (field.align_h ?? 'left'),
|
|
229
|
+
align_v: (field.align_v ?? 'top'),
|
|
230
|
+
page: field.page,
|
|
231
|
+
size: field.size,
|
|
232
|
+
wrap: field.wrap,
|
|
233
|
+
}))
|
|
234
|
+
.filter((field) => field !== null);
|
|
235
|
+
}
|
|
236
|
+
inferParams() {
|
|
237
|
+
const inferenceRegex = /params\[\s*(['"])(.*?)\1\s*\]/g;
|
|
238
|
+
const params = [];
|
|
239
|
+
let match;
|
|
240
|
+
for (const field of this.getFields()) {
|
|
241
|
+
while ((match = inferenceRegex.exec(field.validator ?? '')) !== null)
|
|
242
|
+
if (!params.includes(match[2]))
|
|
243
|
+
params.push(match[2]);
|
|
244
|
+
}
|
|
245
|
+
for (const field of this.getPhantomFields()) {
|
|
246
|
+
while ((match = inferenceRegex.exec(field.validator ?? '')) !== null)
|
|
247
|
+
if (!params.includes(match[2]))
|
|
248
|
+
params.push(match[2]);
|
|
249
|
+
}
|
|
250
|
+
return params;
|
|
251
|
+
}
|
|
252
|
+
parseValidator(_id, fieldType, params = {}) {
|
|
253
|
+
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
254
|
+
if (!field)
|
|
255
|
+
return null;
|
|
256
|
+
const d = new Date();
|
|
257
|
+
const defaults = {
|
|
258
|
+
currentDate: d,
|
|
259
|
+
currentDateTimestamp: d.getTime(),
|
|
260
|
+
};
|
|
261
|
+
const validatorStr = typeof field.validator === 'string' ? field.validator : '';
|
|
262
|
+
const validator = this.populateParams(validatorStr, params);
|
|
263
|
+
const ret = `return ${validator}`;
|
|
264
|
+
const evaluator = new Function('z', 'params', ret);
|
|
265
|
+
return evaluator(z, { ...params, ...defaults });
|
|
266
|
+
}
|
|
267
|
+
parsePrefiller(_id, fieldType, params = {}) {
|
|
268
|
+
const field = fieldType === 'field' ? this.getField(_id) : this.getPhantomField(_id);
|
|
269
|
+
if (!field || !field.prefiller)
|
|
270
|
+
return null;
|
|
271
|
+
const prefillerStr = typeof field.prefiller === 'string' ? field.prefiller : '';
|
|
272
|
+
const prefiller = this.populateParams(prefillerStr, params);
|
|
273
|
+
try {
|
|
274
|
+
const fn = eval(prefiller);
|
|
275
|
+
return typeof fn === 'function' ? fn : null;
|
|
276
|
+
}
|
|
277
|
+
catch (e) {
|
|
278
|
+
console.warn(`Failed to parse prefiller for field ${_id}`, e);
|
|
279
|
+
console.warn(`The prefiller was: ${field.prefiller}`);
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
enumerateParams(s) {
|
|
284
|
+
const detectedParams = s.match(/#\{[^}]*\}/g) ?? [];
|
|
285
|
+
const params = detectedParams.map((dp) => {
|
|
286
|
+
const [fieldName, propertyTree] = dp
|
|
287
|
+
.replaceAll('#{', '')
|
|
288
|
+
.replaceAll('}', '')
|
|
289
|
+
.replaceAll(')', '')
|
|
290
|
+
.split('(');
|
|
291
|
+
const properties = propertyTree?.split('.') ?? [];
|
|
292
|
+
return [fieldName, ...properties];
|
|
293
|
+
});
|
|
294
|
+
return params?.reduce((acc, cur, i) => ((acc[detectedParams[i]] = cur), acc), {});
|
|
295
|
+
}
|
|
296
|
+
populateParams(s, params) {
|
|
297
|
+
const paramsReflection = this.enumerateParams(s);
|
|
298
|
+
const paramsValues = {};
|
|
299
|
+
for (const match in paramsReflection) {
|
|
300
|
+
const path = paramsReflection[match];
|
|
301
|
+
paramsValues[match] =
|
|
302
|
+
path.reduce((acc, cur) => acc?.[cur], params) ?? '';
|
|
303
|
+
}
|
|
304
|
+
let out = s;
|
|
305
|
+
for (const match in paramsValues) {
|
|
306
|
+
const replacement = JSON.stringify(paramsValues[match]);
|
|
307
|
+
out = out.replaceAll(match, replacement);
|
|
308
|
+
}
|
|
309
|
+
return out;
|
|
310
|
+
}
|
|
311
|
+
getSubscribers() {
|
|
312
|
+
return this.formMetadata.subscribers || [];
|
|
313
|
+
}
|
|
314
|
+
getSigningParties() {
|
|
315
|
+
return this.formMetadata.signing_parties;
|
|
316
|
+
}
|
|
317
|
+
mayInvolveEsign() {
|
|
318
|
+
return this.getFieldsForSigningService().some((field) => field.type === 'signature');
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
321
|
//# sourceMappingURL=form-metadata.js.map
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './form-metadata';
|
|
2
|
-
export * from './fields.client';
|
|
3
|
-
export * from './fields.server';
|
|
1
|
+
export * from './form-metadata.js';
|
|
2
|
+
export * from './fields.client.js';
|
|
3
|
+
export * from './fields.server.js';
|
package/dist/lib/forms/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './form-metadata';
|
|
2
|
-
export * from './fields.client';
|
|
3
|
-
export * from './fields.server';
|
|
1
|
+
export * from './form-metadata.js';
|
|
2
|
+
export * from './fields.client.js';
|
|
3
|
+
export * from './fields.server.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/forms/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../lib/forms/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}
|