@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.
- package/package.json +18 -5
- package/src/constants/log-activityTexts.ts +163 -23
- package/src/constants/log.ts +284 -56
- package/src/constants/organization.ts +54 -0
- package/src/constants/product.ts +66 -0
- package/src/constants/report.ts +25 -0
- package/src/schemas/childOrganizationsQuery.ts +44 -0
- package/src/schemas/editLogEntry.ts +604 -104
- package/src/schemas/{logsQueryParam.ts → logsQuery.ts} +104 -23
- package/src/schemas/organization.ts +228 -0
- package/src/schemas/productsQuery.ts +145 -0
- package/src/schemas/reportsQuery.ts +107 -0
- package/src/types/action.d.ts +42 -0
- package/src/types/log.d.ts +204 -28
- package/src/types/organization.d.ts +246 -0
- package/src/types/product.d.ts +173 -0
- package/src/types/report.d.ts +203 -0
- package/src/utils/log.ts +784 -28
- package/src/utils/organization.ts +233 -0
- package/src/utils/product-calEvent.ts +508 -0
- package/src/utils/route-helpers.ts +583 -0
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import type { OrganizationOperationEnum } from '@jsb188/app/types/organization.d';
|
|
2
|
-
import { OPERATION_ENUMS } from '@jsb188/app/constants/organization';
|
|
3
1
|
import { isFutureCalDate, isValidCalDate } from '@jsb188/app/utils/datetime';
|
|
4
2
|
import { indexToTimeZone, isValidTimeZone } from '@jsb188/app/utils/timeZone';
|
|
5
3
|
import { z } from 'zod';
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
4
|
+
import { LOG_ANY_ACTIVITY_ENUMS, LOG_ANY_TYPE_ENUMS, LOG_SORT_ENUMS, LOG_TYPES_BY_OPERATION } from '../constants/log';
|
|
5
|
+
import { OPERATION_ENUMS } from '../constants/organization';
|
|
6
|
+
import type { FilterLogEntriesArgs, LogSortEnum, LogTypeEnum } from '../types/log';
|
|
7
|
+
import type { OrganizationOperationEnum } from '../types/organization';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Zod schema for query params to filter
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export const FilterLogEntriesSchema = z.object({
|
|
14
|
-
|
|
14
|
+
preset: z.string()
|
|
15
|
+
.optional()
|
|
16
|
+
.nullable(),
|
|
17
|
+
types: z.array(z.enum(LOG_ANY_TYPE_ENUMS as [string]))
|
|
18
|
+
.nullable(),
|
|
19
|
+
activities: z.array(z.enum(LOG_ANY_ACTIVITY_ENUMS as [string]))
|
|
20
|
+
.optional()
|
|
15
21
|
.nullable(),
|
|
16
22
|
startDate: z.string()
|
|
17
23
|
.refine((sd) => isValidCalDate(sd), { message: 'START_DATE_INVALID' })
|
|
@@ -55,7 +61,7 @@ export function convertDigitsToLogTypes(
|
|
|
55
61
|
return null;
|
|
56
62
|
}
|
|
57
63
|
|
|
58
|
-
const logTypes = LOG_TYPES_BY_OPERATION[operationType];
|
|
64
|
+
const logTypes = LOG_TYPES_BY_OPERATION[operationType] as LogTypeEnum[];
|
|
59
65
|
if (!logTypes) {
|
|
60
66
|
console.warn(`No log types found for operation type: ${operationType}`);
|
|
61
67
|
return null;
|
|
@@ -85,7 +91,7 @@ export function convertLogTypesToDigits(
|
|
|
85
91
|
return null;
|
|
86
92
|
}
|
|
87
93
|
|
|
88
|
-
const logTypes = LOG_TYPES_BY_OPERATION[operationType];
|
|
94
|
+
const logTypes = LOG_TYPES_BY_OPERATION[operationType] as LogTypeEnum[];
|
|
89
95
|
if (!logTypes) {
|
|
90
96
|
console.warn(`No log types found for operation type: ${operationType}`);
|
|
91
97
|
return null;
|
|
@@ -98,15 +104,32 @@ export function convertLogTypesToDigits(
|
|
|
98
104
|
|
|
99
105
|
/**
|
|
100
106
|
* Create a filter object for logEntries() query from the URL search query.
|
|
101
|
-
* @param
|
|
107
|
+
* @param operation - The type of operation for organization
|
|
102
108
|
* @param searchQuery - The search query string from the URL
|
|
109
|
+
* @param otherFiltersObj - Additional filter properties to merge into the result
|
|
103
110
|
* @returns FilterLogEntriesArgs - The filter object to be used in the logEntries() query
|
|
104
111
|
*/
|
|
105
112
|
|
|
106
|
-
export function
|
|
107
|
-
|
|
113
|
+
export function getLogFiltersFromURL(
|
|
114
|
+
operation: OrganizationOperationEnum | null,
|
|
108
115
|
searchQuery: string,
|
|
116
|
+
otherFiltersObj?: {
|
|
117
|
+
withoutPreset?: Partial<FilterLogEntriesArgs>,
|
|
118
|
+
withPreset?: Partial<FilterLogEntriesArgs>
|
|
119
|
+
}
|
|
109
120
|
) {
|
|
121
|
+
if (!searchQuery && otherFiltersObj?.withPreset) {
|
|
122
|
+
return {
|
|
123
|
+
operation: operation!,
|
|
124
|
+
...otherFiltersObj.withPreset
|
|
125
|
+
};
|
|
126
|
+
} else if (searchQuery === '?preset=none' && otherFiltersObj?.withoutPreset) {
|
|
127
|
+
return {
|
|
128
|
+
operation: operation!,
|
|
129
|
+
...otherFiltersObj.withoutPreset
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
110
133
|
const urlParams = new URLSearchParams(searchQuery);
|
|
111
134
|
|
|
112
135
|
let startDate = urlParams.get('sd');
|
|
@@ -117,17 +140,6 @@ export function createFilterFromURL(
|
|
|
117
140
|
startDate = endDate;
|
|
118
141
|
}
|
|
119
142
|
|
|
120
|
-
let operation = urlParams.get('o');
|
|
121
|
-
if (operation) {
|
|
122
|
-
operation = OPERATION_ENUMS[Number(operation) - 1] || operationType;
|
|
123
|
-
} else {
|
|
124
|
-
operation = operationType;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (!operation) {
|
|
128
|
-
operation = OPERATION_ENUMS[0]; // Default to first operation if none provided
|
|
129
|
-
}
|
|
130
|
-
|
|
131
143
|
const filter: FilterLogEntriesArgs = {
|
|
132
144
|
operation: operation!,
|
|
133
145
|
types: convertDigitsToLogTypes(operation, urlParams.get('t')), // (urlParams.get('t') || null) as FilterLogEntriesArgs['type'] | null,
|
|
@@ -135,10 +147,12 @@ export function createFilterFromURL(
|
|
|
135
147
|
endDate,
|
|
136
148
|
timeZone: indexToTimeZone(urlParams.get('z')),
|
|
137
149
|
query: urlParams.get('q') || '',
|
|
150
|
+
...otherFiltersObj?.withoutPreset,
|
|
138
151
|
};
|
|
139
152
|
|
|
140
153
|
const validation = FilterLogEntriesSchema.safeParse(filter);
|
|
141
154
|
if (!validation.success) {
|
|
155
|
+
// console.log('validation error', validation.error);
|
|
142
156
|
// Return null and force the client to go to a valid page
|
|
143
157
|
return null;
|
|
144
158
|
}
|
|
@@ -146,6 +160,61 @@ export function createFilterFromURL(
|
|
|
146
160
|
return filter;
|
|
147
161
|
}
|
|
148
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Get Sort enum value from URL
|
|
165
|
+
*/
|
|
166
|
+
|
|
167
|
+
export function getLogSorValueFromURL(
|
|
168
|
+
searchQuery: string,
|
|
169
|
+
) {
|
|
170
|
+
const urlParams = new URLSearchParams(searchQuery);
|
|
171
|
+
const sortIndex = urlParams.get('s');
|
|
172
|
+
return LOG_SORT_ENUMS[Number(sortIndex) - 1] || null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Test if URL search query is valid against actual filter object
|
|
177
|
+
*/
|
|
178
|
+
|
|
179
|
+
export function logsSearchQueryIsValid(
|
|
180
|
+
searchQuery: string,
|
|
181
|
+
filter: FilterLogEntriesArgs | null
|
|
182
|
+
) {
|
|
183
|
+
if (!filter) {
|
|
184
|
+
return !searchQuery; // If no filter, searchQuery must be empty to be valid
|
|
185
|
+
} else if (!searchQuery || searchQuery === '?preset=none') {
|
|
186
|
+
return true; // If no searchQuery, any filter is valid
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const urlParams = new URLSearchParams(searchQuery);
|
|
190
|
+
// const operation = urlParams.get('o');
|
|
191
|
+
// const types = urlParams.get('t');
|
|
192
|
+
const accountId = urlParams.get('a');
|
|
193
|
+
const startDate = urlParams.get('sd');
|
|
194
|
+
const endDate = urlParams.get('ed');
|
|
195
|
+
const timeZone = urlParams.get('z');
|
|
196
|
+
const query = urlParams.get('q');
|
|
197
|
+
|
|
198
|
+
// console.log(
|
|
199
|
+
// 'a', !!filter.accountId === !!accountId,
|
|
200
|
+
// 'sd', !!filter.startDate === !!startDate,
|
|
201
|
+
// 'ed', !!filter.endDate === !!endDate,
|
|
202
|
+
// 'z', !!filter.timeZone === !!timeZone,
|
|
203
|
+
// 'q', !!filter.query === !!query
|
|
204
|
+
// );
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
// operation is allowed to pass through
|
|
208
|
+
// !!filter.operation === !!operation &&
|
|
209
|
+
// !!filter.types?.length === !!types && // Allow this to pass because of forced filters from page
|
|
210
|
+
!!filter.accountId === !!accountId &&
|
|
211
|
+
!!filter.startDate === !!startDate &&
|
|
212
|
+
!!filter.endDate === !!endDate &&
|
|
213
|
+
!!filter.timeZone === !!timeZone &&
|
|
214
|
+
!!filter.query === !!query
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
149
218
|
/**
|
|
150
219
|
* Create URL from search query params.
|
|
151
220
|
* This function assumes the query parameters are valid and well-formed.
|
|
@@ -233,14 +302,20 @@ export function createLogsFileNameFromURL(
|
|
|
233
302
|
/**
|
|
234
303
|
* Create URL search params from a filter object
|
|
235
304
|
* @param filter - The filter object to convert to URL search params
|
|
305
|
+
* @param sort - Optional sort enum value
|
|
306
|
+
* @param skipOperation - Whether to skip including the operation in the params
|
|
236
307
|
* @returns URLSearchParams - The search params object
|
|
237
308
|
*/
|
|
238
309
|
|
|
239
|
-
export function createSearchParamsFromFilter(
|
|
310
|
+
export function createSearchParamsFromFilter(
|
|
311
|
+
filter: FilterLogEntriesArgs,
|
|
312
|
+
sort?: LogSortEnum | null,
|
|
313
|
+
skipOperation?: boolean,
|
|
314
|
+
): URLSearchParams {
|
|
240
315
|
const params = new URLSearchParams();
|
|
241
316
|
const { operation } = filter;
|
|
242
317
|
|
|
243
|
-
if (operation) {
|
|
318
|
+
if (operation && !skipOperation) {
|
|
244
319
|
params.set('o', String(OPERATION_ENUMS.indexOf(operation) + 1));
|
|
245
320
|
}
|
|
246
321
|
|
|
@@ -266,6 +341,12 @@ export function createSearchParamsFromFilter(filter: FilterLogEntriesArgs): URLS
|
|
|
266
341
|
if (filter.query) {
|
|
267
342
|
params.set('q', filter.query);
|
|
268
343
|
}
|
|
344
|
+
if (sort) {
|
|
345
|
+
params.set('s', String(LOG_SORT_ENUMS.indexOf(sort) + 1));
|
|
346
|
+
}
|
|
347
|
+
if (!params.size && filter.preset === null) {
|
|
348
|
+
params.set('preset', 'none');
|
|
349
|
+
}
|
|
269
350
|
|
|
270
351
|
return params;
|
|
271
352
|
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { SUPPORTED_COUNTRIES } from '@jsb188/app/constants/app';
|
|
2
|
+
import i18n from '@jsb188/app/i18n';
|
|
3
|
+
import type { OrganizationGQL } from '../types/organization.d';
|
|
4
|
+
import type { OpenModalPopUpFn } from '@jsb188/react/states';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Length values for validation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const MIN_LEN_VALUES = {
|
|
11
|
+
name: 40,
|
|
12
|
+
address_city: 100,
|
|
13
|
+
address_state: 32,
|
|
14
|
+
address_postalCode: 20,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Rules for each field
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
const RULES = function() {
|
|
23
|
+
|
|
24
|
+
const addressRule = (_value: string, fv: any, k: string) => {
|
|
25
|
+
const addr = fv.address;
|
|
26
|
+
const hasAddressFV = Object.values(addr).some((v: any) => v?.trim()?.length > 0);
|
|
27
|
+
|
|
28
|
+
if (hasAddressFV) {
|
|
29
|
+
return !!addr?.[k]?.trim().length;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
'address.line1': {
|
|
37
|
+
for: 'address.line1',
|
|
38
|
+
rule: (v: any, fv: any) => addressRule(v, fv, 'line1'),
|
|
39
|
+
error: () => ({
|
|
40
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t('form.line1') }),
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
'address.postalCode': {
|
|
44
|
+
for: 'address.postalCode',
|
|
45
|
+
rule: (v: any, fv: any) => addressRule(v, fv, 'postalCode'),
|
|
46
|
+
error: () => ({
|
|
47
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t('form.postalCode') }),
|
|
48
|
+
}),
|
|
49
|
+
},
|
|
50
|
+
'address.city': {
|
|
51
|
+
for: 'address.city',
|
|
52
|
+
rule: (v: any, fv: any) => addressRule(v, fv, 'city'),
|
|
53
|
+
error: () => ({
|
|
54
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t('form.city') }),
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
'address.state': {
|
|
58
|
+
for: 'address.state',
|
|
59
|
+
rule: (v: any, fv: any) => addressRule(v, fv, 'state'),
|
|
60
|
+
error: () => ({
|
|
61
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t('form.state') }),
|
|
62
|
+
}),
|
|
63
|
+
},
|
|
64
|
+
'address.country': {
|
|
65
|
+
for: 'address.country',
|
|
66
|
+
rule: (v: any, fv: any) => addressRule(v, fv, 'country'),
|
|
67
|
+
error: () => ({
|
|
68
|
+
message: i18n.t('error.msg_value_required', { value: i18n.t('form.country') }),
|
|
69
|
+
}),
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}();
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Schema; Edit account
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
export function makeEditOrganizationSchema(
|
|
79
|
+
formId: string,
|
|
80
|
+
focusedName: string,
|
|
81
|
+
formValues: OrganizationGQL, // always append with "|| nodeData"
|
|
82
|
+
openModalPopUp: OpenModalPopUpFn
|
|
83
|
+
) {
|
|
84
|
+
|
|
85
|
+
const poProps = {
|
|
86
|
+
// scrollAreaDOMId,
|
|
87
|
+
zClassName: 'z9',
|
|
88
|
+
position: 'bottom_left',
|
|
89
|
+
offsetX: 0,
|
|
90
|
+
offsetY: 7,
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
listData: [{
|
|
95
|
+
// __type: 'subtitle',
|
|
96
|
+
// item: {
|
|
97
|
+
// text: i18n.t('account.profile'),
|
|
98
|
+
// },
|
|
99
|
+
// }, {
|
|
100
|
+
__type: 'input',
|
|
101
|
+
item: {
|
|
102
|
+
name: 'name',
|
|
103
|
+
maxLength: MIN_LEN_VALUES.name,
|
|
104
|
+
label: i18n.t('org.name_of'),
|
|
105
|
+
placeholder: formValues.name,
|
|
106
|
+
autoComplete: 'off',
|
|
107
|
+
}
|
|
108
|
+
}, {
|
|
109
|
+
__type: 'input',
|
|
110
|
+
locked: true,
|
|
111
|
+
item: {
|
|
112
|
+
name: 'operation',
|
|
113
|
+
label: i18n.t('org.type_of'),
|
|
114
|
+
placeholder: i18n.t(`org.type.${formValues.operation}`),
|
|
115
|
+
getter: (value: string) => i18n.t(`org.type.${value}`),
|
|
116
|
+
autoComplete: 'off',
|
|
117
|
+
info: i18n.t('org.contact_to_change'),
|
|
118
|
+
}
|
|
119
|
+
}, {
|
|
120
|
+
__type: 'section_title',
|
|
121
|
+
item: {
|
|
122
|
+
title: i18n.t('form.address'),
|
|
123
|
+
description: i18n.t('org.address_notice_msg')
|
|
124
|
+
}
|
|
125
|
+
}, {
|
|
126
|
+
__type: 'input',
|
|
127
|
+
item: {
|
|
128
|
+
name: 'address.line1',
|
|
129
|
+
label: i18n.t('form.line1'),
|
|
130
|
+
placeholder: i18n.t('form.line1_ph'),
|
|
131
|
+
// autoComplete: 'off',
|
|
132
|
+
}
|
|
133
|
+
}, {
|
|
134
|
+
__type: 'input',
|
|
135
|
+
item: {
|
|
136
|
+
name: 'address.line2',
|
|
137
|
+
label: i18n.t('form.line2_optional'),
|
|
138
|
+
placeholder: i18n.t('form.line2_ph'),
|
|
139
|
+
// autoComplete: 'off',
|
|
140
|
+
}
|
|
141
|
+
}, {
|
|
142
|
+
__type: 'input',
|
|
143
|
+
item: {
|
|
144
|
+
name: 'address.postalCode',
|
|
145
|
+
label: i18n.t('form.postalCode'),
|
|
146
|
+
placeholder: formValues?.address?.postalCode || i18n.t('form.postalCode_ph'),
|
|
147
|
+
// autoComplete: 'off',
|
|
148
|
+
}
|
|
149
|
+
}, {
|
|
150
|
+
__type: 'group',
|
|
151
|
+
item: {
|
|
152
|
+
items: [{
|
|
153
|
+
__type: 'input',
|
|
154
|
+
item: {
|
|
155
|
+
name: 'address.city',
|
|
156
|
+
maxLength: MIN_LEN_VALUES.address_city,
|
|
157
|
+
label: i18n.t('form.city'),
|
|
158
|
+
placeholder: formValues?.address?.city || i18n.t('form.city_ph'),
|
|
159
|
+
},
|
|
160
|
+
}, {
|
|
161
|
+
__type: 'input',
|
|
162
|
+
item: {
|
|
163
|
+
name: 'address.state',
|
|
164
|
+
maxLength: MIN_LEN_VALUES.address_state,
|
|
165
|
+
label: i18n.t('form.state'),
|
|
166
|
+
placeholder: formValues?.address?.state,
|
|
167
|
+
},
|
|
168
|
+
}],
|
|
169
|
+
}
|
|
170
|
+
}, {
|
|
171
|
+
__type: 'input_click',
|
|
172
|
+
forceClickId: 'input_click_address.country',
|
|
173
|
+
label: i18n.t('form.country'),
|
|
174
|
+
item: {
|
|
175
|
+
label: i18n.t('form.country'),
|
|
176
|
+
locked: true,
|
|
177
|
+
focused: focusedName === (formId + '_address.country'),
|
|
178
|
+
name: 'address.country',
|
|
179
|
+
getter: (value: string) => value ? i18n.t(`country.from_code.${value}`) : '',
|
|
180
|
+
placeholder: formValues?.address?.country || i18n.t('form.country_ph'),
|
|
181
|
+
|
|
182
|
+
popOverProps: {
|
|
183
|
+
...poProps,
|
|
184
|
+
|
|
185
|
+
id: formId + '_address.country',
|
|
186
|
+
doNotFixToBottom: true,
|
|
187
|
+
iface: {
|
|
188
|
+
name: 'PO_LIST',
|
|
189
|
+
|
|
190
|
+
variables: {
|
|
191
|
+
designClassName: 'w_400',
|
|
192
|
+
className: 'max_h_50vh',
|
|
193
|
+
initialState: {
|
|
194
|
+
'address.country': formValues?.address?.country,
|
|
195
|
+
},
|
|
196
|
+
options: ['US','MX','CA'].map(cCode => ({
|
|
197
|
+
__type: 'LIST_ITEM' as const,
|
|
198
|
+
text: i18n.t(`country.from_code.${cCode}`),
|
|
199
|
+
// textClassName: 'ft_xs',
|
|
200
|
+
// rightIconClassName: 'ft_xs mb_2',
|
|
201
|
+
name: 'address.country',
|
|
202
|
+
value: cCode,
|
|
203
|
+
selected: formValues?.address?.country === cCode,
|
|
204
|
+
})).concat({
|
|
205
|
+
__type: 'BREAK' as const
|
|
206
|
+
}).concat(SUPPORTED_COUNTRIES.map(countryCode => ({
|
|
207
|
+
__type: 'LIST_ITEM' as const,
|
|
208
|
+
text: i18n.t(`country.from_code.${countryCode}`),
|
|
209
|
+
// textClassName: 'ft_xs',
|
|
210
|
+
// rightIconClassName: 'ft_xs mb_2',
|
|
211
|
+
name: 'address.country',
|
|
212
|
+
value: countryCode,
|
|
213
|
+
selected: formValues?.address?.country === countryCode,
|
|
214
|
+
})))
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
}],
|
|
220
|
+
rules: [
|
|
221
|
+
RULES['address.line1'],
|
|
222
|
+
RULES['address.postalCode'],
|
|
223
|
+
RULES['address.city'],
|
|
224
|
+
RULES['address.state'],
|
|
225
|
+
RULES['address.country'],
|
|
226
|
+
],
|
|
227
|
+
};
|
|
228
|
+
}
|
|
@@ -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
|
+
}
|