@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,352 @@
1
+ import { isFutureCalDate, isValidCalDate } from '@jsb188/app/utils/datetime';
2
+ import { indexToTimeZone, isValidTimeZone } from '@jsb188/app/utils/timeZone';
3
+ import { z } from 'zod';
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
+
9
+ /**
10
+ * Zod schema for query params to filter
11
+ */
12
+
13
+ export const FilterLogEntriesSchema = z.object({
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()
21
+ .nullable(),
22
+ startDate: z.string()
23
+ .refine((sd) => isValidCalDate(sd), { message: 'START_DATE_INVALID' })
24
+ .refine((sd) => !isFutureCalDate(sd, 1), { message: 'START_DATE_FUTURE_NOT_ALLOWED' })
25
+ .nullable(),
26
+ endDate: z.string()
27
+ .refine((ed) => isValidCalDate(ed), { message: 'END_DATE_INVALID' })
28
+ .nullable(),
29
+ timeZone: z.string()
30
+ .refine((tz) => isValidTimeZone(tz), { message: 'INVALID_TIMEZONE' })
31
+ .nullable(),
32
+ query: z.string()
33
+ .nullable(),
34
+ }).refine((data) => {
35
+ if (!data.endDate) {
36
+ // No need to validate if endDate is not provided
37
+ return true;
38
+ } else if (!data.startDate) {
39
+ // If endDate is provided, startDate is required
40
+ return false;
41
+ }
42
+ // End date must be after or equal to start date
43
+ return data.endDate >= data.startDate;
44
+ }, {
45
+ message: 'END_DATE_MUST_BE_AFTER_START_DATE',
46
+ path: ['endDate'], // where to attach the error
47
+ });
48
+
49
+ /**
50
+ * Convert list of digits to log type enums
51
+ * @param operationType - The type of operation for organization
52
+ * @param types - Comma-separated string of digits representing log types
53
+ * @returns Array of log type enums or null
54
+ */
55
+
56
+ export function convertDigitsToLogTypes(
57
+ operationType: OrganizationOperationEnum | null,
58
+ typesStr?: string | null,
59
+ ) {
60
+ if (!operationType || !typesStr) {
61
+ return null;
62
+ }
63
+
64
+ const logTypes = LOG_TYPES_BY_OPERATION[operationType] as LogTypeEnum[];
65
+ if (!logTypes) {
66
+ console.warn(`No log types found for operation type: ${operationType}`);
67
+ return null;
68
+ }
69
+
70
+ return logTypes.map((type: LogTypeEnum, i: number) => {
71
+ const value = typesStr.charAt(i);
72
+ if (value === '1') {
73
+ return type;
74
+ }
75
+ return null;
76
+ }).filter(Boolean) as FilterLogEntriesArgs['types'];
77
+ }
78
+
79
+ /**
80
+ * Convert list of log type enums to digits
81
+ * @param operationType - The type of operation for organization
82
+ * @param types - Array of log type enums
83
+ * @returns String made of digits (indexes) representing log types
84
+ */
85
+
86
+ export function convertLogTypesToDigits(
87
+ operationType: OrganizationOperationEnum | null,
88
+ types: FilterLogEntriesArgs['types'],
89
+ ): string | null {
90
+ if (!operationType || !types) {
91
+ return null;
92
+ }
93
+
94
+ const logTypes = LOG_TYPES_BY_OPERATION[operationType] as LogTypeEnum[];
95
+ if (!logTypes) {
96
+ console.warn(`No log types found for operation type: ${operationType}`);
97
+ return null;
98
+ }
99
+
100
+ return logTypes.map((type: LogTypeEnum) => {
101
+ return types.includes(type) ? '1' : '0';
102
+ }).join('');
103
+ }
104
+
105
+ /**
106
+ * Create a filter object for logEntries() query from the URL search query.
107
+ * @param operation - The type of operation for organization
108
+ * @param searchQuery - The search query string from the URL
109
+ * @param otherFiltersObj - Additional filter properties to merge into the result
110
+ * @returns FilterLogEntriesArgs - The filter object to be used in the logEntries() query
111
+ */
112
+
113
+ export function getLogFiltersFromURL(
114
+ operation: OrganizationOperationEnum | null,
115
+ searchQuery: string,
116
+ otherFiltersObj?: {
117
+ withoutPreset?: Partial<FilterLogEntriesArgs>,
118
+ withPreset?: Partial<FilterLogEntriesArgs>
119
+ }
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
+
133
+ const urlParams = new URLSearchParams(searchQuery);
134
+
135
+ let startDate = urlParams.get('sd');
136
+ let endDate = urlParams.get('ed');
137
+ if (startDate && !endDate) {
138
+ endDate = startDate;
139
+ } else if (endDate && !startDate) {
140
+ startDate = endDate;
141
+ }
142
+
143
+ const filter: FilterLogEntriesArgs = {
144
+ operation: operation!,
145
+ types: convertDigitsToLogTypes(operation, urlParams.get('t')), // (urlParams.get('t') || null) as FilterLogEntriesArgs['type'] | null,
146
+ startDate,
147
+ endDate,
148
+ timeZone: indexToTimeZone(urlParams.get('z')),
149
+ query: urlParams.get('q') || '',
150
+ ...otherFiltersObj?.withoutPreset,
151
+ };
152
+
153
+ const validation = FilterLogEntriesSchema.safeParse(filter);
154
+ if (!validation.success) {
155
+ // console.log('validation error', validation.error);
156
+ // Return null and force the client to go to a valid page
157
+ return null;
158
+ }
159
+
160
+ return filter;
161
+ }
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
+
218
+ /**
219
+ * Create URL from search query params.
220
+ * This function assumes the query parameters are valid and well-formed.
221
+ * @param operationType - Type of operation for organization
222
+ * @param orgGenericId - Generic ID of the organization
223
+ * @param searchQuery - Search query string from the URL
224
+ * @param fileType - Type of file ('pdf' or 'csv')
225
+ * @returns FilterLogEntriesArgs - The filter object to be used in the logEntries() query
226
+ */
227
+
228
+ export function createLogsFileNameFromURL(
229
+ operationType: OrganizationOperationEnum | null,
230
+ orgGenericId: string,
231
+ searchQuery: string,
232
+ fileType: string
233
+ ) {
234
+ const urlParams = new URLSearchParams(searchQuery);
235
+ const o = urlParams.get('o');
236
+ const t = urlParams.get('t');
237
+ const a = urlParams.get('a');
238
+ const sd = urlParams.get('sd');
239
+ const ed = urlParams.get('ed');
240
+ const z = urlParams.get('z');
241
+ const q = urlParams.get('q');
242
+
243
+ let checkStr = '';
244
+ let paramStr = '';
245
+
246
+ if (o) {
247
+ checkStr += o;
248
+ } else {
249
+ const ix = OPERATION_ENUMS.indexOf(operationType || '');
250
+ checkStr += String(ix + 1);
251
+ }
252
+
253
+ if (t?.split('')?.some(letter => letter !== '0')) {
254
+ checkStr += '1';
255
+ paramStr += `_${t}`;
256
+ } else {
257
+ checkStr += '0';
258
+ }
259
+
260
+ if (a) {
261
+ checkStr += '1';
262
+ paramStr += `_${a}`;
263
+ } else {
264
+ checkStr += '0';
265
+ }
266
+
267
+ if (sd) {
268
+ checkStr += '1';
269
+ paramStr += `_${sd}`;
270
+ } else {
271
+ checkStr += '0';
272
+ }
273
+
274
+ if (ed) {
275
+ checkStr += '1';
276
+ paramStr += `_${ed}`;
277
+ } else {
278
+ checkStr += '0';
279
+ }
280
+
281
+ if (z) {
282
+ checkStr += '1';
283
+ paramStr += `_${z}`;
284
+ } else {
285
+ checkStr += '0';
286
+ }
287
+
288
+ if (q) {
289
+ checkStr += '1';
290
+ paramStr += `_${q}`;
291
+ } else {
292
+ checkStr += '0';
293
+ }
294
+
295
+ if (checkStr.split('').every(digit => digit === '0')) {
296
+ return `logs_${orgGenericId}_x${paramStr}.${fileType}`;
297
+ }
298
+
299
+ return `logs_${orgGenericId}_${checkStr}${paramStr}.${fileType}`;
300
+ }
301
+
302
+ /**
303
+ * Create URL search params from a filter object
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
307
+ * @returns URLSearchParams - The search params object
308
+ */
309
+
310
+ export function createSearchParamsFromFilter(
311
+ filter: FilterLogEntriesArgs,
312
+ sort?: LogSortEnum | null,
313
+ skipOperation?: boolean,
314
+ ): URLSearchParams {
315
+ const params = new URLSearchParams();
316
+ const { operation } = filter;
317
+
318
+ if (operation && !skipOperation) {
319
+ params.set('o', String(OPERATION_ENUMS.indexOf(operation) + 1));
320
+ }
321
+
322
+ if (filter.types && operation) {
323
+ const typeDigits = convertLogTypesToDigits(operation, filter.types);
324
+ if (typeDigits) {
325
+ params.set('t', typeDigits);
326
+ }
327
+ }
328
+
329
+ if (filter.accountId) {
330
+ params.set('a', filter.accountId);
331
+ }
332
+ if (filter.startDate) {
333
+ params.set('sd', filter.startDate);
334
+ }
335
+ if (filter.endDate) {
336
+ params.set('ed', filter.endDate);
337
+ }
338
+ if (filter.timeZone) {
339
+ params.set('z', filter.timeZone);
340
+ }
341
+ if (filter.query) {
342
+ params.set('q', filter.query);
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
+ }
350
+
351
+ return params;
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
+ }