@jsb188/mday 1.1.5 → 1.1.7

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.
@@ -1,42 +1,118 @@
1
1
  import i18n from '@jsb188/app/i18n';
2
2
  import { getCalDate } from '@jsb188/app/utils/datetime';
3
+ import { formatCurrency } from '@jsb188/app/utils/number';
3
4
  import { getObject } from '@jsb188/app/utils/object';
5
+ import { textWithBrackets } from '@jsb188/app/utils/string';
4
6
  import { getTimeZoneCode } from '@jsb188/app/utils/timeZone';
5
- import { ARABLE_ACTIVITIES_GROUPED } from '../constants/log';
6
- import type { LogEntryGQLData } from '../types/log.d';
7
+ import { ARABLE_ACTIVITIES_GROUPED, FARMERS_MARKET_ACTIVITIES_GROUPED, LIVESTOCK_ACTIVITIES_GROUPED } from '../constants/log';
8
+ import type { LogArableMetadataGQL, LogEntryGQL, LogFarmersMarketMetadataGQL, LogLivestockMetadataGQL, LogTypeEnum } from '../types/log.d';
7
9
  import { getLogCategoryColor, getLogTypeFromActivity } from '../utils/log';
8
10
 
11
+ /**
12
+ * Get editable values based on activity & type (which is also used to display UI)
13
+ */
14
+
15
+ export const LOG_TYPENAME_TO_DETAILS_INPUT_NAME = {
16
+ LogArable: 'arableDetails',
17
+ LogFarmersMarket: 'farmersMarketDetails',
18
+ LogLivestock: 'livestockDetails',
19
+ };
20
+
9
21
  /**
10
22
  * Convert GQL data to GQL mutation input format
11
23
  */
12
24
 
13
- export function makeFormValuesFromData(logEntry: LogEntryGQLData) {
25
+ export function makeFormValuesFromData(logEntry: LogEntryGQL) {
14
26
  const formValues = {
15
27
  logEntryId: logEntry.id,
28
+ organizationId: logEntry.organizationId,
16
29
  accountId: logEntry.accountId,
17
30
  date: logEntry.date,
18
31
  } as Record<string, any>;
19
32
 
20
- const { details } = logEntry;
21
- switch (details?.__typename) {
22
- case 'LogEntryArable':
33
+ switch (logEntry.details?.__typename) {
34
+ case 'LogArable': {
35
+ const details = logEntry.details as LogArableMetadataGQL;
23
36
  formValues.arableDetails = {
24
37
  activity: details.activity,
25
38
  quantity: details.quantity,
26
- crop: details.crop,
39
+ item: details.item,
27
40
  unit: details.unit,
28
41
  concentration: details.concentration,
29
42
  concentrationUnit: details.concentrationUnit,
43
+ location: details.location,
44
+ fieldLocation: details.fieldLocation,
45
+ referenceNumber: details.referenceNumber,
46
+ otherParty: details.otherParty,
47
+ values: details.values,
48
+ notes: details.notes,
49
+ };
50
+ } break;
51
+ case 'LogFarmersMarket': {
52
+ const details = logEntry.details as LogFarmersMarketMetadataGQL;
53
+ formValues.farmersMarketDetails = {
54
+ activity: details.activity,
55
+ referenceNumber: details.referenceNumber,
56
+ voided: details.voided,
57
+ values: details.values,
58
+ notes: details.notes,
59
+ };
60
+ } break;
61
+ case 'LogLivestock': {
62
+ const details = logEntry.details as LogLivestockMetadataGQL;
63
+ formValues.livestockDetails = {
64
+ activity: details.activity,
65
+ livestock: details.livestock,
66
+ livestockIdentifiers: details.livestockIdentifiers,
67
+ livestockGroup: details.livestockGroup,
68
+ referenceNumber: details.referenceNumber,
69
+ otherParty: details.otherParty,
70
+ values: details.values,
71
+ item: details.item,
72
+ quantity: details.quantity,
73
+ unit: details.unit,
74
+ location: details.location,
75
+ fieldLocation: details.fieldLocation,
30
76
  price: details.price,
77
+ tax: details.tax,
31
78
  notes: details.notes,
32
79
  };
33
- break;
34
- case 'LogEntryLivestock':
35
- break;
80
+ } break;
36
81
  default:
37
- console.log(`${details?.__typename} is not done yet.`);
82
+ console.log(`${logEntry.details?.__typename} is not done yet.`);
38
83
  }
39
84
 
85
+ // input LogArableInput {
86
+ // activity: LogArableActivity
87
+ // item: String
88
+ // quantity: Float
89
+ // unit: String
90
+ // concentration: Float
91
+ // concentrationUnit: String
92
+ // price: Float
93
+ // notes: String
94
+ // }
95
+
96
+ // input LogFarmersMarketInput {
97
+ // activity: LogFarmersMarketActivity
98
+ // childOrgId: GenericID
99
+ // voided: Boolean
100
+ // values: [LabelAndValueInput]
101
+ // notes: String
102
+ // }
103
+
104
+ // input LogLivestockInput {
105
+ // activity: LogLivestockType
106
+ // livestock: String
107
+ // livestockIdentifiers: [String]
108
+ // item: String
109
+ // quantity: Float
110
+ // unit: String
111
+ // price: Float
112
+ // notes: String
113
+ // }
114
+
115
+
40
116
  return formValues;
41
117
  }
42
118
 
@@ -49,46 +125,518 @@ export function formatFormValuesForMutation(formValues: Record<string, any>) {
49
125
  if (formValues[key] && typeof formValues[key] === 'object') {
50
126
 
51
127
  for (const subKey in formValues[key]) {
52
- if (['quantity','price','concentration'].includes(subKey)) {
128
+ if (['quantity','price','tax','concentration'].includes(subKey)) {
53
129
  formValues[key][subKey] = parseFloat(formValues[key][subKey]);
54
130
  }
55
131
  }
56
132
  }
57
133
  }
134
+
58
135
  return formValues;
59
136
  }
60
137
 
61
138
  /**
62
- * Get editable values based on activity & type (which is also used to display UI)
139
+ * Arable log metadata schema
63
140
  */
64
141
 
65
- export function getLogDetailsInputName(__typename: string) {
142
+ export type ValidMetadataFieldName =
143
+ | 'activity'
144
+
145
+ // ARABLE
146
+ | 'water_testing'
147
+ | 'concentration_unit'
148
+ | 'water_quantity'
149
+ | 'quantity'
150
+ | 'water_unit'
151
+ | 'unit'
152
+ | 'price'
153
+ | 'crop'
154
+ | 'purchased_item'
155
+ | 'fieldLocation'
156
+ | 'location_water'
157
+ | 'location_arable'
158
+
159
+ // FARMERS_MARKET
160
+ | 'receiptNumber'
161
+ // | 'childOrgId' // Not editable
162
+ | 'void'
163
+ | 'marketCredits'
164
+
165
+ // LIVESTOCK
166
+ | 'livestock'
167
+ | 'livestockIdentifiers'
168
+ | 'livestockGroup'
169
+ | 'otherParty'
170
+ | 'invoiceNumber'
171
+ | 'invoiceItems'
172
+ | 'tax'
173
+ | 'item_used'
174
+ | 'location_livestock'
175
+ | null; // Need this for eslint satisfcation, but null will never happen (because of a filter())
176
+
177
+ function makeMetadataSchema(
178
+ namespace: string,
179
+ formValues: Record<string, any>,
180
+ metadataParams: MakeMetadataSchemaParams,
181
+ basePopOverProps: Record<string, any>,
182
+ schemaFields: ValidMetadataFieldName[]
183
+ ) {
184
+ const { scrollAreaDOMId, formId, timeZone, focusedName, isCreateNew } = metadataParams;
185
+ const schemaItems = schemaFields.map((field: ValidMetadataFieldName) => {
186
+ switch (field) {
187
+ case 'activity':
188
+ return {
189
+ __type: 'input_click',
190
+ forceClickId: `input_click_${namespace}.activity`,
191
+ label: i18n.t('form.activity'),
192
+ item: {
193
+ // editable: false,
194
+ locked: () => true, // Conditional lock is possible
195
+ focused: focusedName === (formId + '_activity'),
196
+ name: `${namespace}.activity`,
197
+ placeholder: isCreateNew ? i18n.t(`form.activity_ph`) : '',
198
+ getter: (value: string) => value ? i18n.t(`log.activity.${value}`) : '',
199
+ },
200
+ };
201
+ case 'water_testing':
202
+ return {
203
+ __type: 'input',
204
+ label: i18n.t('form.concentration'),
205
+ item: {
206
+ name: `${namespace}.concentration`,
207
+ type: 'number',
208
+ step: 0.1,
209
+ min: 0.1,
210
+ max: 99999999.99, // database max is numeric(10, 2)
211
+ placeholder: isCreateNew ? i18n.t('form.concentration_ph') : '',
212
+ }
213
+ };
214
+ case 'concentration_unit':
215
+ return {
216
+ __type: 'input',
217
+ label: i18n.t('form.concentration_unit'),
218
+ item: {
219
+ name: `${namespace}.concentrationUnit`,
220
+ maxLength: 40,
221
+ placeholder: isCreateNew ? i18n.t('form.concentration_unit_ph') : '',
222
+ },
223
+ };
224
+ case 'water_quantity':
225
+ return {
226
+ __type: 'input',
227
+ label: i18n.t('form.water_quantity'),
228
+ item: {
229
+ name: `${namespace}.quantity`,
230
+ type: 'number',
231
+ step: 0.1,
232
+ min: 0.1,
233
+ max: 99999999.99, // database max is numeric(10, 2)
234
+ placeholder: isCreateNew ? i18n.t('form.quantity_ph') : '',
235
+ }
236
+ };
237
+ case 'quantity':
238
+ return {
239
+ __type: 'input',
240
+ label: i18n.t('form.quantity'),
241
+ item: {
242
+ name: `${namespace}.quantity`,
243
+ type: 'number',
244
+ step: 0.1,
245
+ min: 0.1,
246
+ max: 99999999.99, // database max is numeric(10, 2)
247
+ placeholder: isCreateNew ? i18n.t('form.quantity_ph') : '',
248
+ }
249
+ };
250
+ case 'water_unit':
251
+ return {
252
+ __type: 'input',
253
+ label: i18n.t('form.water_unit'),
254
+ item: {
255
+ name: `${namespace}.unit`,
256
+ maxLength: 40,
257
+ placeholder: isCreateNew ? i18n.t('form.water_unit_ph') : '',
258
+ }
259
+ };
260
+ case 'unit':
261
+ return {
262
+ __type: 'input',
263
+ label: i18n.t('form.unit'),
264
+ item: {
265
+ name: `${namespace}.unit`,
266
+ maxLength: 40,
267
+ placeholder: isCreateNew ? i18n.t('log.unit_arable_ph') : '',
268
+ }
269
+ };
270
+ case 'location_arable':
271
+ return {
272
+ __type: 'input',
273
+ label: i18n.t('form.location'),
274
+ item: {
275
+ name: `${namespace}.location`,
276
+ maxLength: 40,
277
+ placeholder: isCreateNew ? i18n.t('log.location_arable_ph') : '',
278
+ }
279
+ };
280
+ case 'location_water':
281
+ return {
282
+ __type: 'input',
283
+ label: i18n.t('form.location'),
284
+ item: {
285
+ name: `${namespace}.location`,
286
+ maxLength: 40,
287
+ placeholder: isCreateNew ? i18n.t('log.location_water_ph') : '',
288
+ }
289
+ };
290
+ case 'location_livestock':
291
+ return {
292
+ __type: 'input',
293
+ label: i18n.t('form.location'),
294
+ item: {
295
+ name: `${namespace}.location`,
296
+ maxLength: 100,
297
+ placeholder: isCreateNew ? i18n.t('log.location_livestock_ph') : '',
298
+ }
299
+ };
300
+ case 'fieldLocation':
301
+ return {
302
+ __type: 'input',
303
+ label: i18n.t('log.fieldLocation'),
304
+ item: {
305
+ name: `${namespace}.fieldLocation`,
306
+ maxLength: 100,
307
+ placeholder: isCreateNew ? i18n.t('log.fieldLocation_ph') : '',
308
+ }
309
+ };
310
+ case 'price':
311
+ return {
312
+ __type: 'input',
313
+ label: i18n.t('form.price'),
314
+ item: {
315
+ name: `${namespace}.price`,
316
+ type: 'number',
317
+ step: 0.1,
318
+ min: 0.1,
319
+ max: 99999999.99, // database max is numeric(10, 2)
320
+ placeholder: isCreateNew ? i18n.t('form.price_ph') : '',
321
+ },
322
+ };
323
+ case 'tax':
324
+ return {
325
+ __type: 'input',
326
+ label: i18n.t('form.tax'),
327
+ item: {
328
+ name: `${namespace}.tax`,
329
+ type: 'number',
330
+ step: 0.1,
331
+ min: 0.1,
332
+ max: 99999999.99, // database max is numeric(10, 2)
333
+ placeholder: isCreateNew ? i18n.t('form.tax_ph') : '',
334
+ },
335
+ };
336
+ case 'crop':
337
+ return {
338
+ __type: 'input',
339
+ label: i18n.t('form.crop'),
340
+ item: {
341
+ name: `${namespace}.item`,
342
+ placeholder: isCreateNew ? i18n.t('log.crop_ph') : '',
343
+ }
344
+ };
345
+ case 'purchased_item':
346
+ return {
347
+ __type: 'input',
348
+ label: i18n.t('form.purchase_summary'),
349
+ item: {
350
+ name: `${namespace}.item`,
351
+ placeholder: isCreateNew ? i18n.t('log.crop_ph') : '',
352
+ }
353
+ };
354
+ case 'livestock':
355
+ return {
356
+ __type: 'input',
357
+ label: i18n.t('log.livestock'),
358
+ item: {
359
+ name: `${namespace}.livestock`,
360
+ placeholder: isCreateNew ? i18n.t('log.livestock_ph') : '',
361
+ }
362
+ };
363
+ case 'livestockIdentifiers':
364
+ return {
365
+ __type: 'input',
366
+ label: i18n.t('log.id_tags'),
367
+ item: {
368
+ name: `${namespace}.livestockIdentifiers`,
369
+ placeholder: isCreateNew ? i18n.t('log.id_tags_ph') : '',
370
+ getter: (value: string[]) => value ? value.join(',') : '',
371
+ setter: (value: string) => value.split(',')
372
+ }
373
+ };
374
+ case 'livestockGroup':
375
+ return {
376
+ __type: 'input',
377
+ label: i18n.t('log.group'),
378
+ item: {
379
+ name: `${namespace}.livestockGroup`,
380
+ placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
381
+ }
382
+ };
383
+ case 'otherParty':
384
+ return {
385
+ __type: 'input',
386
+ label: i18n.t('form.vendor'),
387
+ item: {
388
+ name: `${namespace}.otherParty`,
389
+ }
390
+ };
391
+ case 'invoiceNumber':
392
+ return {
393
+ __type: 'input',
394
+ label: i18n.t('log.invoice_number'),
395
+ item: {
396
+ // editable: false,
397
+ locked: true,
398
+ name: `${namespace}.referenceNumber`,
399
+ }
400
+ };
401
+ case 'receiptNumber':
402
+ return {
403
+ __type: 'input',
404
+ label: i18n.t('log.receipt_number'),
405
+ item: {
406
+ // editable: false,
407
+ locked: true,
408
+ name: `${namespace}.referenceNumber`,
409
+ // placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
410
+ // getter: (value: string[]) => value ? value.join(',') : '',
411
+ // setter: (value: string) => value.split(',')
412
+ }
413
+ };
414
+
415
+ // Not allowed to edit
416
+ // case 'childOrgId':
417
+ // return {
418
+ // __type: 'input',
419
+ // label: i18n.t('log.receipt_for'),
420
+ // item: {
421
+ // name: `${namespace}.childOrgId`,
422
+ // // placeholder: isCreateNew ? i18n.t('log.group_ph') : '',
423
+ // // getter: (value: string[]) => value ? value.join(',') : '',
424
+ // // setter: (value: string) => value.split(',')
425
+ // }
426
+ // };
427
+ case 'void':
428
+ return {
429
+ __type: 'input_click',
430
+ forceClickId: `input_click_${namespace}.voided`,
431
+ label: i18n.t('form.void'),
432
+ inputClassName: formValues[namespace]?.voided ? 'cl_red' : '',
433
+ item: {
434
+ // locked: () => true,
435
+ focused: focusedName === (formId + '_void'),
436
+ name: `${namespace}.voided`,
437
+ placeholder: isCreateNew ? i18n.t(`form.void_ph`) : '',
438
+ getter: (value: string) => i18n.t(value ? 'log.receipt_is_void' : 'log.receipt_is_not_void'),
439
+ },
440
+ popOverProps: {
441
+ id: formId + '_void',
442
+ ...basePopOverProps,
443
+ iface: {
444
+ name: 'PO_LIST',
445
+ variables: {
446
+ designClassName: 'w_250',
447
+ className: 'max_h_40vh',
448
+ options: [{
449
+ __type: 'LIST_ITEM' as const,
450
+ text: i18n.t('log.receipt_is_not_void'),
451
+ iconName: 'receipt-dollar',
452
+ selected: !formValues[namespace]?.voided,
453
+ name: `${namespace}.voided`,
454
+ value: false,
455
+ }, {
456
+ __type: 'LIST_ITEM' as const,
457
+ text: i18n.t('log.receipt_is_void'),
458
+ iconName: 'receipt-off',
459
+ selected: formValues[namespace]?.voided === true,
460
+ name: `${namespace}.voided`,
461
+ value: true,
462
+ }]
463
+ }
464
+ }
465
+ }
466
+ };
467
+ case 'invoiceItems':
468
+ return {
469
+ __type: 'input_click',
470
+ label: i18n.t('log.invoice_items'),
471
+ forceClickId: `input_click_${namespace}.values`,
472
+ item: {
473
+ // locked: () => true,
474
+ focused: focusedName === (formId + '_values'),
475
+ name: `${namespace}.values`,
476
+ placeholder: isCreateNew ? i18n.t(`log.invoice_items_ph`) : '',
477
+ getter: (labelsAndValues: string[]) => {
478
+ return labelsAndValues?.map((lv: any) => {
479
+ return textWithBrackets(
480
+ `${lv.quantity ? `${lv.quantity} ` : ''}${lv.label}`,
481
+ formatCurrency(lv.value, false),
482
+ );
483
+ }).join(', ');
484
+ },
485
+ },
486
+ popOverProps: {
487
+ id: formId + '_invoiceItems',
488
+ ...basePopOverProps,
489
+ iface: {
490
+ name: 'PO_LABELS_AND_VALUES',
491
+ variables: {
492
+ gridLayoutStyle: '70px 1fr 85px',
493
+ designClassName: 'w_400',
494
+ className: 'max_h_40vh',
495
+ // flipInputOrder: true,
496
+ forceNumericValues: true,
497
+ includeQuantity: true,
498
+ name: `${namespace}.values`,
499
+ labels: [i18n.t('form.quantity'), i18n.t('log.invoice_item'), `$ ${i18n.t('form.amount')}`],
500
+ inputs: formValues[namespace]?.values || [],
501
+ }
502
+ }
503
+ }
504
+ };
505
+ case 'marketCredits':
506
+ return {
507
+ __type: 'input_click',
508
+ label: i18n.t('log.marketCredits'),
509
+ forceClickId: `input_click_${namespace}.values`,
510
+ item: {
511
+ // locked: () => true,
512
+ focused: focusedName === (formId + '_values'),
513
+ name: `${namespace}.values`,
514
+ placeholder: isCreateNew ? i18n.t(`log.marketCredits_ph`) : '',
515
+ getter: (labelsAndValues: string[]) => {
516
+ return labelsAndValues?.map((lv: any) => {
517
+ return `${formatCurrency(lv.value, false)} ${lv.label}`;
518
+ }).join(', ');
519
+ },
520
+ },
521
+ popOverProps: {
522
+ id: formId + '_marketCredits',
523
+ ...basePopOverProps,
524
+ iface: {
525
+ name: 'PO_LABELS_AND_VALUES',
526
+ variables: {
527
+ gridLayoutStyle: '85px 1fr',
528
+ designClassName: 'w_250',
529
+ className: 'max_h_40vh',
530
+ flipInputOrder: true,
531
+ forceNumericValues: true,
532
+ name: `${namespace}.values`,
533
+ labels: [`$ ${i18n.t('form.amount')}`, i18n.t('log.name_of_credit')],
534
+ inputs: formValues[namespace]?.values || [],
535
+ }
536
+ }
537
+ }
538
+ };
539
+ default:
540
+ console.warn(`Field ${field} is not defined in metadata schema.`);
541
+ return null;
542
+ }
543
+ }).filter(Boolean);
544
+
545
+ return schemaItems;
546
+ }
547
+
548
+ /**
549
+ * Get schema fields from log type
550
+ */
551
+
552
+ export function getSchemaFieldsFromLog(__typename: string, logType: LogTypeEnum): ValidMetadataFieldName[] {
553
+
554
+ let schemaFields: ValidMetadataFieldName[];
66
555
  switch (__typename) {
67
- case 'LogEntryArable':
68
- // Some of the types omit some fields, but for now, we're including all
69
- return 'arableDetails';
556
+ case 'LogArable': {
557
+ const isWaterTesting = logType === 'WATER';
558
+ const isSale = ['SALES'].includes(logType!);
559
+ const isPurchase = ['SEED'].includes(logType!);
560
+ const isSaleOrPurchase = isSale || isPurchase;
561
+
562
+ schemaFields = [
563
+ 'activity',
564
+ isWaterTesting ? 'water_testing' : null,
565
+ isWaterTesting ? 'concentration_unit' : null,
566
+ isWaterTesting ? 'water_quantity' : isSaleOrPurchase ? null : 'quantity',
567
+ isWaterTesting ? 'water_unit' : isSaleOrPurchase ? null : 'unit',
568
+ isWaterTesting ? null : isSaleOrPurchase ? 'purchased_item' : 'crop',
569
+ isSaleOrPurchase ? 'otherParty' : null,
570
+ isSaleOrPurchase ? 'invoiceNumber' : null,
571
+ isSaleOrPurchase ? 'invoiceItems' : null,
572
+ isSaleOrPurchase ? 'tax' : null,
573
+ isSaleOrPurchase ? null : isWaterTesting ? 'location_water' : 'location_arable',
574
+ isSaleOrPurchase ? null : 'fieldLocation',
575
+ ];
576
+ } break;
577
+ case 'LogFarmersMarket': {
578
+ const isReceipt = ['MARKET_RECEIPTS'].includes(logType);
579
+
580
+ schemaFields = [
581
+ 'activity',
582
+ isReceipt ? 'receiptNumber' : null,
583
+ // 'childOrgId', // not editable
584
+ isReceipt ? 'void' : null,
585
+ isReceipt ? 'marketCredits' : null,
586
+ ];
587
+ } break;
588
+ case 'LogLivestock': {
589
+ const isLivestock = ['LIVESTOCK_LIFE_CYCLE', 'LIVESTOCK_TRACKING', 'LIVESTOCK_HEALTHCARE'].includes(logType);
590
+ const isSupplyPurchase = logType === 'SUPPLY_PURCHASE';
591
+ const isLandManagement = logType === 'PASTURE_LAND_MANAGEMENT';
592
+
593
+ schemaFields = [
594
+ 'activity',
595
+ isLivestock ? 'livestock' : null,
596
+ isLivestock ? 'livestockIdentifiers' : null,
597
+ isLivestock ? 'livestockGroup' : null,
598
+ isSupplyPurchase ? 'otherParty' : null,
599
+ isSupplyPurchase ? 'invoiceNumber' : null,
600
+ isSupplyPurchase ? 'invoiceItems' : null,
601
+ isSupplyPurchase ? 'tax' : null,
602
+ isLandManagement ? 'item_used' : null,
603
+ isLivestock || isSupplyPurchase ? null : 'quantity',
604
+ isLivestock || isSupplyPurchase ? null : 'unit',
605
+ !isSupplyPurchase ? 'location_livestock' : null,
606
+ isLivestock ? 'price' : null,
607
+ isLivestock ? null : 'fieldLocation',
608
+ ];
609
+ } break;
70
610
  default:
611
+ schemaFields = [];
71
612
  }
72
- return '';
613
+
614
+ return schemaFields.filter(Boolean);
73
615
  }
74
616
 
75
617
  /**
76
618
  * Get editable values based on activity & type (which is also used to display UI)
77
619
  */
78
620
 
79
- export function makeLogEntryDetailsSchema(
80
- scrollAreaDOMId: string,
81
- formId: string,
82
- timeZone: string | null | undefined,
83
- __typename: string,
84
- focusedName: string,
85
- formValues: Record<string, any> = {}
621
+ interface MakeMetadataSchemaParams {
622
+ __typename: string;
623
+ scrollAreaDOMId: string;
624
+ formId: string;
625
+ timeZone: string | null | undefined;
626
+ focusedName: string;
627
+ isCreateNew: boolean;
628
+ }
629
+
630
+ export function makeLogMetadataSchema(
631
+ formValues: Record<string, any> = {},
632
+ metadataParams: MakeMetadataSchemaParams,
86
633
  ) {
634
+ const { __typename, scrollAreaDOMId, formId, timeZone, focusedName, isCreateNew } = metadataParams;
87
635
 
88
636
  let activitiesList: any[] = [];
89
637
  let schemaItems: any[] = [];
90
638
 
91
- const poProps = {
639
+ const basePopOverProps = {
92
640
  scrollAreaDOMId,
93
641
  zClassName: 'z9',
94
642
  position: 'bottom_left',
@@ -102,86 +650,37 @@ export function makeLogEntryDetailsSchema(
102
650
  const maxDate = new Date();
103
651
  maxDate.setMonth(maxDate.getMonth() + 1);
104
652
 
105
- switch (__typename) {
106
- case 'LogEntryArable':
107
- const logType = getLogTypeFromActivity('ARABLE', formValues.arableDetails?.activity);
108
- const isWaterTesting = logType === 'WATER';
109
- const isPriceRelated = ['SALES', 'SEED'].includes(logType!);
653
+ const namespace = {
654
+ LogArable: 'arableDetails',
655
+ LogFarmersMarket: 'farmersMarketDetails',
656
+ LogLivestock: 'livestockDetails',
657
+ }[__typename] || '';
658
+
659
+ const logActivity = formValues[namespace]?.activity;
660
+ const logType = getLogTypeFromActivity(__typename, logActivity)!;
661
+ const schemaFields = getSchemaFieldsFromLog(__typename, logType);
662
+ const isAITask = logType === 'AI_TASK';
110
663
 
664
+ switch (__typename) {
665
+ case 'LogArable': {
111
666
  activitiesList = ARABLE_ACTIVITIES_GROUPED;
112
- schemaItems = [{
113
- __type: 'input_click',
114
- forceClickId: 'input_click_arableDetails.activity',
115
- label: i18n.t('form.activity'),
116
- item: {
117
- locked: () => true,
118
- focused: focusedName === (formId + '_activity'),
119
- name: 'arableDetails.activity',
120
- placeholder: i18n.t(`form.activity_ph`),
121
- getter: (value: string) => value ? i18n.t(`log.activity.${value}`) : '',
122
- },
123
- }, {
124
- // Water testing inputs
125
- __type: isWaterTesting ? 'input' : 'none',
126
- label: i18n.t('form.concentration'),
127
- item: {
128
- name: 'arableDetails.concentration',
129
- type: 'number',
130
- step: 0.1,
131
- min: 0.1,
132
- max: 99999999.99, // database max is numeric(10, 2)
133
- placeholder: i18n.t('form.concentration_ph'),
134
- },
135
- }, {
136
- __type: isWaterTesting ? 'input' : 'none',
137
- label: i18n.t('form.concentration_unit'),
138
- item: {
139
- name: 'arableDetails.concentrationUnit',
140
- maxLength: 40,
141
- placeholder: i18n.t('form.concentration_unit_ph'),
142
- },
143
- }, {
144
- // Regular inputs
145
- __type: 'input',
146
- label: i18n.t(isWaterTesting ? 'form.water_quantity' : 'form.quantity'),
147
- item: {
148
- name: 'arableDetails.quantity',
149
- type: 'number',
150
- step: 0.1,
151
- min: 0.1,
152
- max: 99999999.99, // database max is numeric(10, 2)
153
- placeholder: i18n.t('form.quantity_ph'),
154
- },
155
- }, {
156
- __type: 'input',
157
- label: i18n.t(isWaterTesting ? 'form.water_unit' : 'form.unit'),
158
- item: {
159
- name: 'arableDetails.unit',
160
- maxLength: 40,
161
- placeholder: i18n.t(isWaterTesting ? 'form.water_unit_ph' : 'log.unit_arable_ph'),
162
- },
163
- }, {
164
- __type: isPriceRelated ? 'input' : 'none',
165
- label: i18n.t('form.price'),
166
- item: {
167
- name: 'arableDetails.price',
168
- type: 'number',
169
- step: 0.1,
170
- min: 0.1,
171
- max: 99999999.99, // database max is numeric(10, 2)
172
- placeholder: i18n.t('log.price_arable_ph'),
173
- },
174
- }, {
175
- __type: isWaterTesting ? 'none' : 'input',
176
- label: i18n.t('form.crop'),
177
- item: {
178
- name: 'arableDetails.crop',
179
- placeholder: 'Tomato, broccoli, etc.',
180
- }
181
- }];
667
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
668
+ } break;
669
+ case 'LogFarmersMarket': {
670
+ activitiesList = FARMERS_MARKET_ACTIVITIES_GROUPED;
671
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
672
+ } break;
673
+ case 'LogLivestock': {
674
+ activitiesList = LIVESTOCK_ACTIVITIES_GROUPED;
675
+ schemaItems = makeMetadataSchema(namespace, formValues, metadataParams, basePopOverProps, schemaFields);
676
+ } break;
182
677
  default:
183
678
  }
184
679
 
680
+ if (!isAITask) {
681
+ activitiesList = activitiesList.filter(([type]) => type !== 'AI_TASK');
682
+ }
683
+
185
684
  // Date, time, createdBy is common across all log types
186
685
 
187
686
  if (schemaItems.length) {
@@ -198,11 +697,11 @@ export function makeLogEntryDetailsSchema(
198
697
  },
199
698
  popOverProps: {
200
699
  id: formId + '_date',
201
- ...poProps,
700
+ ...basePopOverProps,
202
701
  iface: {
203
702
  name: 'PO_LIST',
204
703
  variables: {
205
- designClassName: 'w_300',
704
+ designClassName: 'w_350',
206
705
  className: 'max_h_40vh',
207
706
  initialState: {
208
707
  date: formValues.date ? new Date(formValues.date) : new Date(),
@@ -233,7 +732,8 @@ export function makeLogEntryDetailsSchema(
233
732
  const activityValue = getObject(formValues, schemaItems[0].item.name);
234
733
  schemaItems[0].item.popOverProps = {
235
734
  id: formId + '_activity',
236
- ...poProps,
735
+ ...basePopOverProps,
736
+ disabled: isAITask, // AI tasks' activity cannot be changed
237
737
  iface: {
238
738
  name: 'PO_LIST',
239
739
  variables: {