@jsb188/mday 1.1.4 → 1.1.6

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.
@@ -0,0 +1,145 @@
1
+ import { PRODUCT_TYPES } from '@jsb188/mday/constants/product';
2
+ import type { OrganizationOperationEnum } from '../types/organization.d';
3
+ import { z } from 'zod';
4
+ import type { ProductsFilterArgs, ProductTypeEnum } from '../types/product.d';
5
+
6
+ /**
7
+ * Zod schema for query params to filter
8
+ */
9
+
10
+ export const ProductsFilterSchema = z.object({
11
+ preset: z.string()
12
+ .optional()
13
+ .nullable(),
14
+ productType: z.enum(PRODUCT_TYPES as [string]),
15
+ // startDate: z.string()
16
+ // .refine((sd) => isValidCalDate(sd), { message: 'START_DATE_INVALID' })
17
+ // .refine((sd) => !isFutureCalDate(sd, 1), { message: 'START_DATE_FUTURE_NOT_ALLOWED' })
18
+ // .nullable(),
19
+ // endDate: z.string()
20
+ // .refine((ed) => isValidCalDate(ed), { message: 'END_DATE_INVALID' })
21
+ // .nullable(),
22
+ // timeZone: z.string()
23
+ // .refine((tz) => isValidTimeZone(tz), { message: 'INVALID_TIMEZONE' })
24
+ // .nullable(),
25
+ query: z.string()
26
+ .nullable(),
27
+ });
28
+ // }).refine((data) => {
29
+ // if (!data.endDate) {
30
+ // // No need to validate if endDate is not provided
31
+ // return true;
32
+ // } else if (!data.startDate) {
33
+ // // If endDate is provided, startDate is required
34
+ // return false;
35
+ // }
36
+ // // End date must be after or equal to start date
37
+ // return data.endDate >= data.startDate;
38
+ // }, {
39
+ // message: 'END_DATE_MUST_BE_AFTER_START_DATE',
40
+ // path: ['endDate'], // where to attach the error
41
+ // });
42
+
43
+ /**
44
+ * Convert list of digits to product type enums
45
+ * @param digit - Index digit string from URL param
46
+ * @returns Array of product type enums or null
47
+ */
48
+
49
+ export function convertDigitToProductType(digit?: string | null): ProductTypeEnum | null {
50
+ if (!digit) {
51
+ return null;
52
+ }
53
+ return PRODUCT_TYPES[Number(digit)] || null;
54
+ }
55
+
56
+ /**
57
+ * Create a filter object for productsList() query from the URL search query.
58
+ * @param operationType - The organization operation type
59
+ * @param searchQuery - The search query string from the URL
60
+ * @param otherFiltersObj - Additional filter properties to merge into the result
61
+ * @returns ProductsFilterArgs - The filter object to be used in the productsList() query
62
+ */
63
+
64
+ export function getProductsFilterFromURL(
65
+ _operationType: OrganizationOperationEnum | null,
66
+ searchQuery: string,
67
+ otherFiltersObj?: {
68
+ withoutPreset?: Partial<ProductsFilterArgs>,
69
+ withPreset?: Partial<ProductsFilterArgs>
70
+ }
71
+ ) {
72
+ if (!searchQuery && otherFiltersObj?.withPreset) {
73
+ return otherFiltersObj.withPreset;
74
+ } else if (searchQuery === '?preset=none' && otherFiltersObj?.withoutPreset) {
75
+ return otherFiltersObj.withoutPreset;
76
+ }
77
+
78
+ const urlParams = new URLSearchParams(searchQuery);
79
+
80
+ // let startDate = urlParams.get('sd');
81
+ // let endDate = urlParams.get('ed');
82
+ // if (startDate && !endDate) {
83
+ // endDate = startDate;
84
+ // } else if (endDate && !startDate) {
85
+ // startDate = endDate;
86
+ // }
87
+
88
+ const filter: ProductsFilterArgs = {
89
+ productType: convertDigitToProductType(urlParams.get('t'))!,
90
+ // startDate,
91
+ // endDate,
92
+ // timeZone: indexToTimeZone(urlParams.get('z')),
93
+ query: urlParams.get('q') || '',
94
+ ...otherFiltersObj?.withoutPreset,
95
+ };
96
+
97
+ const validation = ProductsFilterSchema.safeParse(filter);
98
+
99
+ // console.log(otherFiltersObj);
100
+ // console.log('filter', filter);
101
+ // console.log('validation', validation);
102
+
103
+ if (!validation.success) {
104
+ // console.log('validation error', validation.error);
105
+ // Return null and force the client to go to a valid page
106
+ return null;
107
+ }
108
+
109
+ return filter;
110
+ }
111
+
112
+ /**
113
+ * Test if URL search query is valid against actual filter object
114
+ */
115
+
116
+ export function productsSearchQueryIsValid(
117
+ searchQuery: string,
118
+ filter: ProductsFilterArgs | null
119
+ ) {
120
+ if (!filter) {
121
+ return !searchQuery; // If no filter, searchQuery must be empty to be valid
122
+ } else if (!searchQuery || searchQuery === '?preset=none') {
123
+ return true; // If no searchQuery, any filter is valid
124
+ }
125
+
126
+ const urlParams = new URLSearchParams(searchQuery);
127
+ // const operation = urlParams.get('o');
128
+ const productType = urlParams.get('t');
129
+ // const accountId = urlParams.get('a');
130
+ // const startDate = urlParams.get('sd');
131
+ // const endDate = urlParams.get('ed');
132
+ // const timeZone = urlParams.get('z');
133
+ const query = urlParams.get('q');
134
+
135
+ return (
136
+ // operation is allowed to pass through
137
+ // !!filter.operation === !!operation &&
138
+ !!filter.productType === !!productType &&
139
+ // !!filter.accountId === !!accountId &&
140
+ // !!filter.startDate === !!startDate &&
141
+ // !!filter.endDate === !!endDate &&
142
+ // !!filter.timeZone === !!timeZone &&
143
+ !!filter.query === !!query
144
+ );
145
+ }
@@ -0,0 +1,107 @@
1
+ import z from 'zod';
2
+ import type { OrganizationOperationEnum } from '../types/organization';
3
+ import type { ReportsFilterArgs } from '../types/report';
4
+ import { convertDigitToProductType } from './productsQuery';
5
+ import { REPORT_TYPES } from '../constants/report';
6
+
7
+ /**
8
+ * Zod schema for query params to filter
9
+ */
10
+
11
+ export const ReportsFilterSchema = z.object({
12
+ preset: z.string()
13
+ .optional()
14
+ .nullable(),
15
+ reportType: z.enum(REPORT_TYPES as [string]),
16
+ query: z.string()
17
+ .nullable(),
18
+ });
19
+
20
+ /**
21
+ * Create a filter object for reports() query from the URL search query.
22
+ * @param operationType - The organization operation type
23
+ * @param searchQuery - The search query string from the URL
24
+ * @param otherFiltersObj - Additional filter properties to merge into the result
25
+ * @returns ReportsFilterArgs - The filter object to be used in the productsList() query
26
+ */
27
+
28
+ export function getReportsFilterFromURL(
29
+ _operationType: OrganizationOperationEnum | null,
30
+ searchQuery: string,
31
+ otherFiltersObj?: {
32
+ withoutPreset?: Partial<ReportsFilterArgs>,
33
+ withPreset?: Partial<ReportsFilterArgs>
34
+ }
35
+ ) {
36
+ if (!searchQuery && otherFiltersObj?.withPreset) {
37
+ return otherFiltersObj.withPreset;
38
+ } else if (searchQuery === '?preset=none' && otherFiltersObj?.withoutPreset) {
39
+ return otherFiltersObj.withoutPreset;
40
+ }
41
+
42
+ const urlParams = new URLSearchParams(searchQuery);
43
+
44
+ // let startDate = urlParams.get('sd');
45
+ // let endDate = urlParams.get('ed');
46
+ // if (startDate && !endDate) {
47
+ // endDate = startDate;
48
+ // } else if (endDate && !startDate) {
49
+ // startDate = endDate;
50
+ // }
51
+
52
+ const filter: ReportsFilterArgs = {
53
+ reportType: convertDigitToProductType(urlParams.get('t'))!,
54
+ period: urlParams.get('p') || '',
55
+ // startDate,
56
+ // endDate,
57
+ query: urlParams.get('q') || '',
58
+ ...otherFiltersObj?.withoutPreset,
59
+ };
60
+
61
+ const validation = ReportsFilterSchema.safeParse(filter);
62
+
63
+ // console.log(otherFiltersObj);
64
+ // console.log('filter', filter);
65
+ // console.log('validation', validation);
66
+
67
+ if (!validation.success) {
68
+ // console.log('validation error', validation.error);
69
+ // Return null and force the client to go to a valid page
70
+ return null;
71
+ }
72
+
73
+ return filter;
74
+ }
75
+
76
+ /**
77
+ * Test if URL search query is valid against actual filter object
78
+ */
79
+
80
+ export function reportsSearchQueryIsValid(
81
+ searchQuery: string,
82
+ filter: ReportsFilterArgs | null
83
+ ) {
84
+ if (!filter) {
85
+ return !searchQuery; // If no filter, searchQuery must be empty to be valid
86
+ } else if (!searchQuery || searchQuery === '?preset=none') {
87
+ return true; // If no searchQuery, any filter is valid
88
+ }
89
+
90
+ const urlParams = new URLSearchParams(searchQuery);
91
+ // const operation = urlParams.get('o');
92
+ const reportType = urlParams.get('t');
93
+ // const accountId = urlParams.get('a');
94
+ // const startDate = urlParams.get('sd');
95
+ // const endDate = urlParams.get('ed');
96
+ const query = urlParams.get('q');
97
+
98
+ return (
99
+ // operation is allowed to pass through
100
+ // !!filter.operation === !!operation &&
101
+ !!filter.reportType === !!reportType &&
102
+ // !!filter.accountId === !!accountId &&
103
+ // !!filter.startDate === !!startDate &&
104
+ // !!filter.endDate === !!endDate &&
105
+ !!filter.query === !!query
106
+ );
107
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Actions data object
3
+ */
4
+
5
+ export interface ActionTaskObj {
6
+ id?: number | bigint; // Optional for updates/inserts
7
+ logId: number | bigint;
8
+ queue?: string | null;
9
+
10
+ title: string;
11
+ instruction: string;
12
+ message?: string | null;
13
+
14
+ scheduledAt: Date | null;
15
+ delay: number | null; // seconds
16
+ completed: boolean;
17
+
18
+ createdAt?: Date;
19
+ updatedAt?: Date;
20
+ }
21
+
22
+ /**
23
+ * Actions GraphQL data
24
+ */
25
+
26
+ export interface ActionTaskGQL {
27
+ __deleted?: boolean;
28
+
29
+ id: string;
30
+ logId: string;
31
+
32
+ title: string;
33
+ instruction: string;
34
+ message: string | null;
35
+
36
+ scheduledAt: string; // ISO date string
37
+ delay: number; // seconds
38
+ completed: boolean;
39
+
40
+ createdAt?: string; // ISO date string
41
+ updatedAt?: string; // ISO date string
42
+ }
@@ -1,45 +1,268 @@
1
- import { LOG_ARABLE_ACTIVITY_ENUMS } from '../constants/log.ts';
2
- import type { OrganizationOperationEnum } from './organization.d.ts';
1
+ import type { AccountObj } from '@jsb188/app/types/account.d';
2
+ import type { ActionTaskGQL, ActionTaskObj } from '@jsb188/mday/types/action.d';
3
+ import type { OrganizationOperationEnum } from '../types/organization.d';
4
+ import {
5
+ LOG_ACTION_STATUS_ENUMS,
6
+ LOG_ARABLE_ACTIVITY_ENUMS,
7
+ LOG_FARMERS_MARKET_ACTIVITY_ENUMS,
8
+ LOG_LIVESTOCK_ACTIVITY_ENUMS,
9
+ LOG_SORT_ENUMS
10
+ } from '../constants/log';
3
11
 
4
- // All logs types
12
+ /**
13
+ * Enums
14
+ */
5
15
 
6
- export type LogArableTypeEnum = 'SEED' | 'FIELD' | 'HARVEST' | 'POST_HARVEST' | 'SALES';
7
- export type LogTypeEnum = LogArableTypeEnum;
16
+ export type LogActionStatusEnum = (typeof LOG_ACTION_STATUS_ENUMS)[number];
17
+ export type LogContentName = 'log' | 'ai_task' | 'invoice' | 'receipt'; // For UI visuals, client-side only
18
+ export type LogSortEnum = typeof LOG_SORT_ENUMS[number];
8
19
 
9
- // All logs activities
20
+ /**
21
+ * Arable
22
+ */
10
23
 
24
+ export type LogArableTypeEnum =
25
+ | 'SEED'
26
+ | 'PLANTING'
27
+ | 'FIELD'
28
+ | 'HARVEST'
29
+ | 'POST_HARVEST'
30
+ | 'SALES'
31
+ | 'WATER'
32
+ | 'HYGIENE'
33
+ | 'SANITATION'
34
+ | 'EQUIPMENTS'
35
+ | 'BIOSECURITY'
36
+ | 'EMPLOYEES';
11
37
  export type LogArableActivityEnum = typeof LOG_ARABLE_ACTIVITY_ENUMS[number];
12
- export type LogActivityEnum = LogArableActivityEnum;
13
38
 
14
- // Log entry object
39
+ /**
40
+ * Farmers Market
41
+ */
42
+
43
+ export type LogFarmersMarketTypeEnum = 'MARKET_RECEIPTS' | 'MARKET_OPERATIONS';
44
+ export type LogFarmersMarketActivityEnum = typeof LOG_FARMERS_MARKET_ACTIVITY_ENUMS[number];
45
+
46
+ /**
47
+ * Livestock
48
+ */
49
+
50
+ export type LogLivestockTypeEnum =
51
+ | 'SUPPLY_PURCHASE'
52
+ | 'LIVESTOCK_LIFE_CYCLE'
53
+ | 'LIVESTOCK_TRACKING'
54
+ | 'PASTURE_LAND_MANAGEMENT'
55
+ | 'LIVESTOCK_HEALTHCARE'
56
+ | 'LIVESTOCK_SALE';
57
+ export type LogLivestockActivityEnum = typeof LOG_LIVESTOCK_ACTIVITY_ENUMS[number];
58
+
59
+ /**
60
+ * All log types/activities Union
61
+ */
62
+
63
+ export type LogTypeEnum = LogArableTypeEnum | LogFarmersMarketTypeEnum | LogLivestockTypeEnum | 'AI_TASK';
64
+ export type LogActivityEnum = LogArableActivityEnum | LogFarmersMarketActivityEnum | LogLivestockActivityEnum;
65
+
66
+ /**
67
+ * Log metadata
68
+ */
69
+
70
+ interface LogDetailsGQLBase {
71
+ __typename: string;
72
+ id: string;
73
+ type: any;
74
+ activity: any;
75
+ notes: string;
76
+ }
77
+
78
+ interface LogMetadataBase {
79
+ __before: any;
80
+ childOrg: never;
81
+ childOrgId: never;
82
+ referenceNumber: never;
83
+
84
+ recurFromLogId?: number;
85
+ recurredCount?: number;
86
+ summary?: string;
87
+ }
88
+
89
+ interface LabelValueObj {
90
+ label: string;
91
+ value: string;
92
+ quantity?: number;
93
+ }
94
+
95
+ /**
96
+ * Log details - Arable
97
+ */
98
+
99
+ export type LogArableMetadata = LogMetadataBase & {
100
+ item: string;
101
+ quantity: number;
102
+ unit: string;
103
+ concentration: number;
104
+ concentrationUnit: string;
105
+ location: string;
106
+ fieldLocation?: string;
107
+ otherParty?: string;
108
+ referenceNumber?: string;
109
+ values?: LabelValueObj[];
110
+ tax: number;
111
+ };
15
112
 
16
113
  export interface LogArableObj {
17
- type: LogTypeEnum;
114
+ type?: LogArableTypeEnum; // Only set in server if manually extended
18
115
  activity: LogArableActivityEnum;
19
- item?: string;
20
- quantity?: number;
21
- unit?: string;
22
- price?: number;
23
- notes: string | null;
116
+ notes: string | null;
117
+ translation?: string | null;
118
+ metadata?: Partial<LogArableMetadata> | null;
119
+ }
120
+
121
+ export interface LogArableDetailsObj extends LogArableObj {
122
+ __table: 'logs_arable';
123
+ id: number | bigint;
124
+ childOrg: never;
125
+ }
126
+
127
+ export type LogArableMetadataGQL = LogArableMetadata & LogDetailsGQLBase;
128
+
129
+ /**
130
+ * Log details - Farmers Market
131
+ */
132
+
133
+ export type LogFarmersMarketMetadata = LogMetadataBase & {
134
+ referenceNumber: string;
135
+ voided: boolean;
136
+ childOrgId?: number;
137
+ values: LabelValueObj[];
138
+ };
139
+
140
+ export interface LogFarmersMarketObj {
141
+ childOrgId: number | bigint;
142
+ type?: LogFarmersMarketTypeEnum; // Only set in server if manually extended
143
+ activity: LogFarmersMarketActivityEnum;
144
+ notes: string | null;
145
+ translation?: string | null;
146
+ metadata?: Partial<LogFarmersMarketMetadata> | null;
147
+ }
148
+
149
+ export interface LogFarmersMarketDetailsObj extends LogFarmersMarketObj {
150
+ __table: 'logs_farmers_market';
151
+ id: number | bigint;
152
+ childOrg: {
153
+ id: number | bigint;
154
+ name: string;
155
+ };
156
+ }
157
+
158
+ export type LogFarmersMarketMetadataGQL = LogFarmersMarketMetadata & LogDetailsGQLBase;
159
+
160
+ /**
161
+ * Log details - Livestock
162
+ */
163
+
164
+ export type LogLivestockMetadata = LogMetadataBase & {
165
+ damIdentifier?: string;
166
+ livestockIdentifiers: string[];
167
+ livestockGroup?: string;
168
+ livestock: string;
169
+ otherParty?: string;
170
+ referenceNumber?: string;
171
+ values?: LabelValueObj[];
172
+ item: string;
173
+ quantity: number;
174
+ unit: string;
175
+ location: string;
176
+ fieldLocation?: string;
177
+ price: number;
178
+ tax: number;
179
+ };
180
+
181
+ export interface LogLivestockObj {
182
+ type?: LogLivestockTypeEnum; // Only set in server if manually extended
183
+ activity: LogLivestockActivityEnum;
184
+ notes: string | null;
185
+ translation?: string | null;
186
+ damIdentifier?: string;
187
+ livestockIdentifiers: string[];
188
+ livestockGroup?: string;
189
+ metadata?: Partial<LogLivestockMetadata> | null;
190
+ }
191
+
192
+ export interface LogLivestockDetailsObj extends LogLivestockObj {
193
+ __table: 'logs_livestock';
194
+ id: number | bigint;
195
+ childOrg: never;
196
+ }
197
+
198
+ export type LogLivestockMetadataGQL = LogLivestockMetadata & LogDetailsGQLBase;
199
+
200
+ /**
201
+ * Union type for log details
202
+ */
203
+
204
+ export type LogDetailsObj = LogArableObj | LogFarmersMarketObj | LogLivestockObj;
205
+ export type LogMetadataGQL = LogArableMetadataGQL | LogFarmersMarketMetadataGQL | LogLivestockMetadataGQL;
206
+
207
+ /**
208
+ * GQL data interfaces
209
+ */
210
+
211
+ export interface LogEntryGQL {
212
+ __deleted?: boolean;
213
+
214
+ id: string;
215
+ accountId: string;
216
+ organizationId: string;
217
+ details: LogMetadataGQL;
218
+ status: LogActionStatusEnum | null;
219
+
220
+ account: any;
221
+ actions?: ActionTaskGQL[];
222
+
223
+ date: string; // ISO date string
224
+ createdAt: string; // ISO date string
225
+ updatedAt: string; // ISO date string
226
+ }
227
+
228
+ export interface LogEntryInsertObj {
229
+ id?: number | bigint; // Only for edits
230
+ accountId: number | bigint;
231
+ organizationId: number | bigint;
232
+ status?: LogActionStatusEnum | null;
233
+ details: any;
234
+ date: Date;
24
235
  }
25
236
 
26
- export interface LogEntryGQLData {
27
- id: string
28
- accountId: string
29
- organizationId: string
30
- details: LogArableObj;
237
+ export interface LogEntryData {
238
+ __table: 'logs';
239
+ id: number | bigint;
240
+ accountId: number | bigint;
241
+ organizationId: number | bigint;
242
+ status: LogActionStatusEnum | null;
243
+ distance?: number; // For vector search
244
+ details: LogArableDetailsObj | LogFarmersMarketDetailsObj | LogLivestockDetailsObj;
245
+ date: Date;
246
+ createdAt: Date;
247
+ updatedAt: Date;
31
248
 
32
- account: any;
33
- createdAt: string // ISO date string
34
- updatedAt: string // ISO date string
249
+ // This is for outputs, but will never be set for inserts
250
+ account?: AccountObj;
251
+ actions?: ActionTaskObj[];
35
252
  }
36
253
 
37
- // Filters for search
254
+ /**
255
+ * Filters for search for client-side
256
+ */
38
257
 
39
258
  export interface FilterLogEntriesArgs {
40
- operation: OrganizationOperationEnum;
41
- type?: LogTypeEnum | null;
42
- startDate?: string | null;
43
- endDate?: string | null;
44
- query?: string;
259
+ operation: OrganizationOperationEnum;
260
+ accountId?: string | null; // Account ID to filter logs by account
261
+ preset?: 'WEEKS_5' | 'AI_TASKS' | 'INCLUDE_AI_TASKS' | 'HIDE_AI_TASKS' | null;
262
+ types?: LogTypeEnum[] | null;
263
+ activities?: LogActivityEnum[] | null;
264
+ startDate?: string | null; // CalDate, with dashes (YYYY-MM-DD)
265
+ endDate?: string | null; // CalDate, with dashes (YYYY-MM-DD)
266
+ timeZone?: string | null; // Timezone string, e.g., 'America/New_York'
267
+ query?: string | null;
45
268
  }