@jsb188/app 1.1.4 → 1.1.5

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,4 +1,4 @@
1
- import { OPERATION_ENUMS, ROLE_ENUMS } from '../constants/organization.ts';
1
+ import { OPERATION_ENUMS, ROLE_ENUMS, ROLE_CATEGORY_ENUMS } from '../constants/organization.ts';
2
2
 
3
3
  /*
4
4
  * ACL
@@ -6,16 +6,33 @@ import { OPERATION_ENUMS, ROLE_ENUMS } from '../constants/organization.ts';
6
6
 
7
7
  export type OrganizationRoleEnum = typeof ROLE_ENUMS[number];
8
8
  export type OrganizationOperationEnum = typeof OPERATION_ENUMS[number];
9
+ export type OrganizationRoleCategoryEnum = typeof ROLE_CATEGORY_ENUMS[number];
10
+
11
+ type ACLPermission = 0 | 1 | 2 | 3; // 0: no access, 1: read-only, 2: allow-write, 3: allow-manage
12
+ type ACLPermissionEnum = 'NONE' | 'READ' | 'WRITE' | 'MANAGE';
9
13
 
10
14
  export interface OrganizationACL {
11
- id: string;
12
- manageBilling: boolean | null;
13
- manageIntegrations: boolean | null;
14
- manageMembers: boolean | null;
15
- manageProducts: boolean | null;
16
- manageSettings: boolean | null;
17
- viewMembers: boolean | null;
18
- role: OrganizationRoleEnum | null;
15
+ id: string;
16
+ billing: ACLPermission;
17
+ logs: ACLPermission;
18
+ members: ACLPermission;
19
+ finances: ACLPermission;
20
+ settings: ACLPermission;
21
+ integrations: ACLPermission;
22
+ digests: ACLPermission;
23
+ reminders: ACLPermission;
24
+ }
25
+
26
+ export interface OrganizationACLGQLData {
27
+ id: string;
28
+ billing: ACLPermissionEnum;
29
+ logs: ACLPermissionEnum;
30
+ members: ACLPermissionEnum;
31
+ finances: ACLPermissionEnum;
32
+ settings: ACLPermissionEnum;
33
+ integrations: ACLPermissionEnum;
34
+ digests: ACLPermissionEnum;
35
+ reminders: ACLPermissionEnum;
19
36
  }
20
37
 
21
38
  /**
@@ -23,18 +40,55 @@ export interface OrganizationACL {
23
40
  */
24
41
 
25
42
  export interface OrganizationShortData {
26
- id: string;
27
- name: string;
28
- primary: boolean;
29
- acl: OrganizationACL;
43
+ id: string;
44
+ name: string;
45
+ primary: boolean;
46
+ acl: OrganizationACL;
30
47
  }
31
48
 
32
49
  export interface OrganizationData {
33
- id: string;
34
- name: string;
35
- primary: boolean;
36
- acl: OrganizationACL;
37
- stripeCustomerId: string | null;
38
- domains: string[] | null;
39
- // members: OrganizationMemberData[] | null;
50
+ id: number;
51
+ stripeCustomerId: string | null;
52
+ name: string;
53
+ operation: OrganizationOperationEnum;
54
+ dailyDigestTime: string | null;
55
+ reminders: string | null;
56
+ domains: string[] | null;
57
+ settings?: {
58
+ emoji: string | null;
59
+ timeZone: string | null;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Full organization object with relationship
65
+ */
66
+
67
+ export interface OrganizationRelData {
68
+ id: string;
69
+ primary: boolean;
70
+ role: OrganizationRoleEnum;
71
+ acl: OrganizationACL;
72
+ organization: OrganizationData;
73
+ }
74
+
75
+ /**
76
+ * GraphQL data for organization relationship
77
+ */
78
+
79
+ export interface OrganizationGQLData {
80
+ id: string;
81
+ name: string;
82
+ stripeCustomerId: string | null;
83
+ reminders: string | null;
84
+ domains: string[] | null;
85
+ membersCount: number;
86
+ }
87
+
88
+ export interface OrganizationRelGQLData {
89
+ id: string;
90
+ primary: boolean;
91
+ role: OrganizationRoleEnum;
92
+ acl: OrganizationACLGQLData;
93
+ organization: OrganizationGQLData;
40
94
  }
package/utils/datetime.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { DateTime } from 'luxon';
2
2
  import i18n from '../i18n';
3
+ import { getObject } from './object';
3
4
  import { isValidTimeZone, parseDateInTimezone } from './timeZone';
4
5
 
5
6
  /**
@@ -32,17 +33,6 @@ interface TimeAgoParams {
32
33
  skipTodayCheck: boolean;
33
34
  }
34
35
 
35
- interface FullTimeParams {
36
- timeZone: string | null;
37
- locales: string;
38
- hideYear: boolean;
39
- hideDate: boolean;
40
- hideTime: boolean;
41
- textDateStyle: 'long' | 'short' | null;
42
- alwaysShowYear: boolean;
43
- includeWeekday: boolean;
44
- }
45
-
46
36
  /**
47
37
  * Get start of day
48
38
  */
@@ -106,7 +96,16 @@ export function getDatePeriod(d1: Date, d2: Date): DatePeriodEnum {
106
96
 
107
97
  export function getFullDateTime(
108
98
  _d: Date | string | number | null,
109
- params?: Partial<FullTimeParams>,
99
+ params?: Partial<{
100
+ timeZone: string | null;
101
+ locales: string;
102
+ hideYear: boolean;
103
+ hideDate: boolean;
104
+ hideTime: boolean;
105
+ textDateStyle: 'long' | 'short' | 'numeric' | null;
106
+ alwaysShowYear: boolean;
107
+ includeWeekday: boolean | 'long' | 'short' | 'narrow';
108
+ }>,
110
109
  ) {
111
110
  const {
112
111
  locales = 'en-US',
@@ -150,10 +149,17 @@ export function getFullDateTime(
150
149
  hideYear_ = true;
151
150
  }
152
151
 
152
+ let weekday;
153
+ if (['long', 'short', 'narrow'].includes(includeWeekday as string)) {
154
+ weekday = includeWeekday;
155
+ } else if (includeWeekday) {
156
+ weekday = textDateStyle === 'long' ? 'long' : 'short';
157
+ }
158
+
153
159
  return d.toLocaleDateString(locales, {
154
160
  timeZone,
155
161
  day: 'numeric',
156
- weekday: includeWeekday && textDateStyle === 'long' ? 'long' : includeWeekday ? 'short' : undefined,
162
+ weekday: weekday as 'long' | 'short' | 'narrow',
157
163
  month: textDateStyle || 'numeric',
158
164
  year: hideYear_ ? undefined : 'numeric',
159
165
  hour: hideTime ? undefined : 'numeric',
@@ -195,6 +201,9 @@ export function getTodayStartOfDay(timeZone?: string | null) {
195
201
 
196
202
  /**
197
203
  * Get timezone enforced date
204
+ * @param d - Date to convert
205
+ * @param timeZone - Timezone to enforce
206
+ * @returns Date - Enforced date in the specified timezone
198
207
  */
199
208
 
200
209
  export function getCalDate(d: Date, timeZone_?: string | null) {
@@ -218,17 +227,80 @@ export function getCalDate(d: Date, timeZone_?: string | null) {
218
227
  };
219
228
  }
220
229
 
230
+ /**
231
+ * Update JS date with year/month/day and hour/minutes
232
+ */
233
+
234
+ export function updateDate(
235
+ d_: Date | string,
236
+ update: Partial<{
237
+ year: number;
238
+ month: number;
239
+ day: number;
240
+ hour: number;
241
+ minute: number;
242
+ }>,
243
+ timeZone?: string | null,
244
+ ): Date | null {
245
+ if (timeZone) {
246
+ // Use luxon to handle timezone-aware date manipulation
247
+ let dt = DateTime.fromJSDate(d_ instanceof Date ? d_ : new Date(d_), { zone: timeZone });
248
+
249
+ if (update.year !== undefined) {
250
+ dt = dt.set({ year: update.year });
251
+ }
252
+ if (update.month !== undefined) {
253
+ dt = dt.set({ month: update.month });
254
+ }
255
+ if (update.day !== undefined) {
256
+ dt = dt.set({ day: update.day });
257
+ }
258
+ if (update.hour !== undefined) {
259
+ dt = dt.set({ hour: update.hour });
260
+ }
261
+ if (update.minute !== undefined) {
262
+ dt = dt.set({ minute: update.minute });
263
+ }
264
+
265
+ return dt.isValid ? dt.toJSDate() : null;
266
+ }
267
+
268
+ const d = d_ instanceof Date ? d_ : new Date(d_);
269
+
270
+ if (update.year) {
271
+ d.setFullYear(update.year);
272
+ }
273
+
274
+ if (update.month !== undefined) {
275
+ d.setMonth(update.month - 1); // JS months are 0-indexed
276
+ }
277
+
278
+ if (update.day) {
279
+ d.setDate(update.day);
280
+ }
281
+
282
+ if (update.hour !== undefined) {
283
+ d.setHours(update.hour);
284
+ }
285
+
286
+ if (update.minute !== undefined) {
287
+ d.setMinutes(update.minute);
288
+ }
289
+
290
+ return d;
291
+ }
292
+
221
293
  /**
222
294
  * Convert Int cal date to String YYYY-MM-DD format
223
295
  */
224
296
 
225
- export function convertIntToCalDate(calDateInt: number | string): string {
226
- const value = String(calDateInt);
227
- if (value.length === 8) {
228
- return `${value.slice(0, 4)}-${value.slice(4, 6)}-${value.slice(6, 8)}`;
229
- }
230
- // If not 8 digits, return as is
231
- return value;
297
+ export function convertIntToCalDate(calDateInt: number | string, delimiter: string = '-'): string {
298
+ const value = String(calDateInt);
299
+ if (value.length === 8) {
300
+ return `${value.slice(0, 4)}${delimiter}${value.slice(4, 6)}${delimiter}${value.slice(6, 8)}`;
301
+ }
302
+ // If not 8 digits, return as is
303
+ return value;
232
304
  }
233
305
 
234
306
  /**
@@ -360,6 +432,7 @@ export function getTimeAgo(
360
432
  export function getFullDate(
361
433
  _d: Date | string | number | null,
362
434
  outputStyle: 'DATE_ONLY' | 'DATE_TEXT' | 'MINIMAL' | 'DETAILED' = 'DATE_ONLY',
435
+ timeZone: string | null,
363
436
  locales: string = 'en-US',
364
437
  ) {
365
438
  let d;
@@ -373,19 +446,21 @@ export function getFullDate(
373
446
  switch (outputStyle) {
374
447
  case 'DATE_ONLY':
375
448
  // Expected output: "9/1/2024"
376
- return new Intl.DateTimeFormat(locales).format(d);
449
+ return new Intl.DateTimeFormat(locales, {
450
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
451
+ }).format(d);
377
452
  case 'MINIMAL':
378
453
  // Expected output: "Sep 1, 2024, 6:54 PM"
379
454
  return new Intl.DateTimeFormat(locales, {
380
455
  dateStyle: 'medium', // 'full', 'long', 'medium', 'short'
381
456
  timeStyle: 'short', // 'full', 'long', 'medium', 'short'
382
- // timeZone: 'Australia/Sydney',
457
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
383
458
  }).format(d);
384
459
  case 'DATE_TEXT':
385
460
  // Expected output: "September 1, 2024"
386
461
  return new Intl.DateTimeFormat(locales, {
387
462
  dateStyle: 'long',
388
- // timeZone: 'Australia/Sydney',
463
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
389
464
  }).format(d);
390
465
  case 'DETAILED':
391
466
  default:
@@ -395,7 +470,7 @@ export function getFullDate(
395
470
  const date = new Intl.DateTimeFormat(locales, {
396
471
  dateStyle: 'long', // 'full', 'long', 'medium', 'short'
397
472
  timeStyle: 'short', // 'full', 'long', 'medium', 'short'
398
- // timeZone: 'Australia/Sydney',
473
+ timeZone: timeZone || undefined, // null is not allowed, it will throw error
399
474
  }).format(d);
400
475
 
401
476
  const parts = new Intl.DateTimeFormat(locales, {
@@ -450,14 +525,14 @@ export function getDateFromCalDate(calDate: string) {
450
525
 
451
526
  export function groupByDatePeriod<T>(
452
527
  items: T[],
453
- dateKey: string,
528
+ dateKey: string, // "." delimited getter is allowed here
454
529
  groupByDate?: boolean,
455
530
  timeZone?: string | null,
456
531
  mapFn?: (item: any) => any,
457
532
  ): DatePeriodObj[][] {
458
533
  const ordered = items.sort((a: any, b: any) => {
459
- const valueA = a[dateKey];
460
- const valueB = b[dateKey];
534
+ const valueA = getObject(a, dateKey);
535
+ const valueB = getObject(b, dateKey);
461
536
 
462
537
  if (valueA < valueB) {
463
538
  return 1;
@@ -480,7 +555,7 @@ export function groupByDatePeriod<T>(
480
555
 
481
556
  const lastItem = group[group.length - 1];
482
557
  const lastDatePeriod: any = lastItem?.datePeriod;
483
- const date = new Date(item[dateKey]);
558
+ const date = new Date(getObject(item, dateKey));
484
559
  const datePeriod = groupByDate ? getCalDate(date, timeZone).calDate : getDatePeriod(date, d);
485
560
 
486
561
  const dateObj = {
@@ -563,7 +638,7 @@ export function labelSortByDatePeriod<T>(
563
638
  output.push(dateObj);
564
639
  }
565
640
 
566
- return output;
641
+ return output as DatePeriodObj[];
567
642
  }
568
643
 
569
644
  /**
@@ -580,3 +655,27 @@ export function isValidDate(d?: Date | string | number | null) {
580
655
  }
581
656
  return false;
582
657
  }
658
+
659
+ /**
660
+ * Get HHMM in increments
661
+ */
662
+
663
+ export function getHHMMIncrements(
664
+ d: Date,
665
+ minutesIncrement: number,
666
+ utc: boolean = false,
667
+ ) {
668
+ let hh, minutes;
669
+ if (utc) {
670
+ hh = d.getUTCHours().toString().padStart(2, '0');
671
+ minutes = d.getUTCMinutes();
672
+ } else {
673
+ hh = d.getHours().toString().padStart(2, '0');
674
+ minutes = d.getMinutes();
675
+ }
676
+
677
+ const interval = Math.floor(minutes / minutesIncrement) * minutesIncrement;
678
+ const hhmm = `${hh}${interval.toString().padStart(2, '0')}`;
679
+
680
+ return hhmm;
681
+ }
package/utils/logic.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { sortObjectByKeys } from './object.ts';
2
+
1
3
  /**
2
4
  * Types
3
5
  */
@@ -34,16 +36,28 @@ export function getDatabaseAction(
34
36
  if (Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined) {
35
37
  // trim() on all string databse fields should be OK;
36
38
  // But if not, add an extra param.
37
- const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
39
+ let value = typeof data[key] === 'string' ? data[key].trim() : data[key];
40
+ if (value instanceof Date) {
41
+ value = value.toISOString();
42
+ } else if (value && typeof value === 'object') {
43
+ value = JSON.stringify(sortObjectByKeys(value));
44
+ }
38
45
 
39
46
  const isNullValue = value !== null || allowNull;
40
47
  if (isNullValue) {
41
48
  documentData[key] = value;
42
49
  }
43
50
 
51
+ let currentValue = currentData[key];
52
+ if (currentValue instanceof Date) {
53
+ currentValue = currentValue.toISOString();
54
+ } else if (currentValue && typeof currentValue === 'object') {
55
+ currentValue = JSON.stringify(sortObjectByKeys(currentValue));
56
+ }
57
+
44
58
  if (
45
59
  // Doing loose != check instead of !== type check
46
- currentData[key] != value &&
60
+ currentValue != value &&
47
61
  isNullValue
48
62
  ) {
49
63
  hasChanges = true;
@@ -100,19 +114,19 @@ export function timeoutPromise<T>(
100
114
  ): Promise<T> {
101
115
  return new Promise<T>((resolve, reject) => {
102
116
  const timeout = setTimeout(() => {
103
- resolve(onTimeout());
117
+ resolve(onTimeout());
104
118
  }, timeoutMs);
105
119
 
106
120
  const p = mainPromise();
107
- if (typeof p?.then === 'function') {
121
+ if (typeof p?.then === 'function') {
108
122
  p.then((result: any) => {
109
123
  clearTimeout(timeout);
110
124
  resolve(result);
111
125
  })
112
- .catch((err: any) => {
113
- clearTimeout(timeout);
114
- reject(err);
115
- });
116
- }
126
+ .catch((err: any) => {
127
+ clearTimeout(timeout);
128
+ reject(err);
129
+ });
130
+ }
117
131
  });
118
132
  }
package/utils/number.ts CHANGED
@@ -125,15 +125,94 @@ export function calculateDiscount(normalPrice: number, discountedPrice: number)
125
125
  *
126
126
  * @param value - The number to format
127
127
  * @param trimDecimals - If true, trims the decimal places to the first non-zero digit
128
+ * @param doNotAllowNaN - If true, returns empty string "" if the value is NaN
128
129
  * @returns Formatted string representation of the number
129
130
  */
130
131
 
131
- export function formatDecimal(value_: string | number, trimDecimals: boolean = false): string {
132
- const value = String(value_);
133
- if (Number(value) % 1 === 0) {
134
- return Number(value).toString(); // Whole number
135
- } else if (trimDecimals) {
136
- return parseFloat(value.toString()).toString(); // Trims to first non-zero decimal
132
+ export function formatDecimal(
133
+ value_: string | number,
134
+ trimDecimals: boolean = false,
135
+ doNotAllowNaN: boolean = false,
136
+ ): string {
137
+ const value = String(value_);
138
+
139
+ let formattedStr;
140
+ if (Number(value) % 1 === 0) {
141
+ formattedStr = Number(value).toString(); // Whole number
142
+ } else if (trimDecimals) {
143
+ formattedStr = parseFloat(value.toString()).toString(); // Trims to first non-zero decimal
144
+ } else {
145
+ formattedStr = String(value); // Keep existing decimal places
146
+ }
147
+ return doNotAllowNaN && isNaN(Number(formattedStr)) ? '' : formattedStr;
148
+ }
149
+
150
+ /**
151
+ * Get currency symbol from settings
152
+ * Example 1: getCurrencySymbol('en-US', 'USD'); // "$"
153
+ * Example 2: getCurrencySymbol('fr-FR', 'EUR'); // "€"
154
+ * Example 3: getCurrencySymbol('ja-JP', 'JPY'); // "¥"
155
+ * Example 4: getCurrencySymbol('en-GB', 'GBP'); // "£"
156
+ *
157
+ * @param locale - The locale to use for formatting
158
+ * @param currency - The currency code (e.g., 'USD', 'EUR')
159
+ * @returns currency symbol as a string
160
+ */
161
+
162
+ export function getCurrencySymbol(locale: string, currency: string): string {
163
+ const parts = new Intl.NumberFormat(locale, {
164
+ style: 'currency',
165
+ currency,
166
+ minimumFractionDigits: 0,
167
+ maximumFractionDigits: 0,
168
+ }).formatToParts(1);
169
+
170
+ const symbolPart = parts.find((part) => part.type === 'currency');
171
+ return symbolPart?.value || '';
172
+ }
173
+
174
+ /**
175
+ * Format number to currency format
176
+ */
177
+
178
+ export function formatCurrency(
179
+ amount: string | number,
180
+ locale: string = 'en-US',
181
+ currency: string = 'USD',
182
+ ): string {
183
+ const num = typeof amount === 'string' ? parseFloat(amount) : amount;
184
+ if (!num && num !== 0) {
185
+ return '';
186
+ }
187
+
188
+ if (isNaN(num)) {
189
+ return '$0';
190
+ }
191
+
192
+ const hasDecimals = num % 1 !== 0;
193
+ const symbol = getCurrencySymbol(locale, currency);
194
+
195
+ return symbol + num.toLocaleString(locale, {
196
+ minimumFractionDigits: hasDecimals ? 2 : 0,
197
+ maximumFractionDigits: hasDecimals ? 2 : 0,
198
+ });
199
+ }
200
+
201
+ /**
202
+ * Format number to have "," between every thousands
203
+ */
204
+
205
+ export function kFormat(
206
+ num: string | number,
207
+ locale: string = 'en-US',
208
+ ): string {
209
+ const number = typeof num === 'string' ? parseFloat(num) : num;
210
+ if (isNaN(number)) {
211
+ return String(num);
137
212
  }
138
- return String(value); // Keep existing decimal places
213
+
214
+ return number.toLocaleString(locale, {
215
+ minimumFractionDigits: 0,
216
+ maximumFractionDigits: 0,
217
+ });
139
218
  }