@jsb188/mday 1.1.5 → 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,233 @@
1
+ import { COLORS } from '@jsb188/app/constants/app';
2
+ import i18n from '@jsb188/app/i18n';
3
+ import { intersection } from '@jsb188/app/utils/object';
4
+ import { DEFAULT_TIMEZONE } from '@jsb188/app/utils/timeZone';
5
+ import { COMMON_ICON_NAMES } from '@jsb188/react-web/svgs/Icon';
6
+ import { FEATURES_BY_OPERATION } from '../constants/product';
7
+ import type {
8
+ OrganizationContact,
9
+ OrganizationFeatureEnum,
10
+ OrganizationGQL,
11
+ OrganizationOperationEnum,
12
+ OrganizationRelData,
13
+ OrganizationRelGQL,
14
+ OrganizationRoleEnum,
15
+ OrganizationSettingsObj
16
+ } from '../types/organization.d';
17
+
18
+ // Placeholder to match Server import
19
+ type ViewerOrganization = any;
20
+
21
+ export const PERMISSION_TO_INT = {
22
+ NONE: 0,
23
+ READ: 1,
24
+ WRITE: 2,
25
+ MANAGE: 3,
26
+ };
27
+
28
+ const INT_TO_PERMISSION = Object.keys(PERMISSION_TO_INT);
29
+
30
+ /**
31
+ * Get numeric value of role's permission level (used for comparisons)
32
+ * @param role - Role enum
33
+ */
34
+
35
+ export function getRoleValue(role: OrganizationRoleEnum): number {
36
+ return [
37
+ 'GUEST',
38
+ 'MEMBER',
39
+ 'MANAGER',
40
+ 'ADMIN',
41
+ 'OWNER',
42
+ ].indexOf(role);
43
+ }
44
+
45
+ /**
46
+ * Get default permissions by role
47
+ */
48
+
49
+ export function getDefaultPermissionsByRole(orgRel: OrganizationRelGQL | OrganizationRelData | ViewerOrganization) {
50
+ const role = orgRel?.role || 'MEMBER';
51
+ const acl: Record<string, any> = { ...orgRel?.acl };
52
+
53
+ for (const key in acl) {
54
+ // if (typeof acl[key] === 'number') {
55
+ if (!isNaN(Number(acl[key]))) {
56
+ acl[key] = INT_TO_PERMISSION[acl[key]] || 'NONE';
57
+ }
58
+ }
59
+
60
+ // Also keep this synced with [resolvers/organizationTypeResolvers.ts]
61
+
62
+ // 0 = "NONE"
63
+ // 1 = "READ"
64
+ // 2 = "WRITE"
65
+ // 3 = "MANAGE"
66
+
67
+ switch (role) {
68
+ case 'ADMIN':
69
+ case 'OWNER':
70
+ return {
71
+ billing: acl.billing || 'MANAGE',
72
+ compliance: acl.compliance || 'MANAGE',
73
+ digests: acl.digests || 'MANAGE', // 2/3 = receive digests, 1 = only see from web app
74
+ finances: acl.finances || 'MANAGE',
75
+ integrations: acl.integrations || 'MANAGE',
76
+ logs: acl.logs || 'MANAGE', // "READ" for logs does nothing
77
+ members: acl.members || 'MANAGE',
78
+ orgManagement: acl.orgManagement || 'MANAGE',
79
+ products: acl.products || 'MANAGE',
80
+ settings: acl.settings || 'MANAGE',
81
+ viewData: acl.viewData || 'MANAGE', // This blocks access from users being able to read other people's logs
82
+ };
83
+ case 'MANAGER':
84
+ return {
85
+ billing: acl.billing || 'READ',
86
+ compliance: acl.compliance || 'WRITE',
87
+ digests: acl.digests || 'READ', // 2/3 = receive digests, 1 = only see from web app
88
+ finances: acl.finances || 'READ',
89
+ integrations: acl.integrations || 'NONE',
90
+ logs: acl.logs || 'MANAGE', // "READ" for logs does nothing
91
+ members: acl.members || 'WRITE',
92
+ orgManagement: acl.orgManagement || 'WRITE',
93
+ products: acl.products || 'READ',
94
+ settings: acl.settings || 'READ',
95
+ viewData: acl.viewData || 'MANAGE', // This blocks access from users being able to read other people's logs
96
+ };
97
+ case 'GUEST':
98
+ return {
99
+ billing: acl.billing || 'NONE',
100
+ compliance: acl.compliance || 'NONE',
101
+ digests: acl.digests || 'NONE',
102
+ finances: acl.finances || 'NONE',
103
+ integrations: acl.integrations || 'NONE',
104
+ logs: acl.logs || 'NONE',
105
+ members: acl.members || 'NONE',
106
+ orgManagement: acl.orgManagement || 'NONE',
107
+ products: acl.products || 'READ',
108
+ settings: acl.settings || 'NONE',
109
+ viewData: acl.viewData || 'NONE',
110
+ };
111
+ case 'MEMBER':
112
+ default:
113
+ }
114
+
115
+ return {
116
+ billing: acl.billing || 'NONE',
117
+ compliance: acl.compliance || 'READ',
118
+ digests: acl.digests || 'NONE', // 2/3 = receive digests, 1 = only see from web app
119
+ finances: acl.finances || 'NONE',
120
+ integrations: acl.integrations || 'NONE',
121
+ logs: acl.logs || 'WRITE', // "READ" for logs does nothing
122
+ members: acl.members || 'READ',
123
+ orgManagement: acl.orgManagement || 'READ',
124
+ products: acl.products || 'READ',
125
+ settings: acl.settings || 'READ',
126
+ viewData: acl.viewData || 'NONE', // This blocks access from users being able to read other people's logs
127
+ };
128
+ }
129
+
130
+ /**
131
+ * Check if this account's ACL has required permissions for an action
132
+ */
133
+
134
+ export type PermissionCheckFor = keyof ReturnType<typeof getDefaultPermissionsByRole>;
135
+ export type ACLPermissionCheck = 'READ' | 'WRITE' | 'MANAGE';
136
+
137
+ export function checkACLPermission(
138
+ orgRel: OrganizationRelGQL | OrganizationRelData | ViewerOrganization | null | undefined,
139
+ check: PermissionCheckFor,
140
+ requiredPermission: ACLPermissionCheck,
141
+ ): boolean | null {
142
+ if (!orgRel) {
143
+ return null;
144
+ }
145
+
146
+ const acl = getDefaultPermissionsByRole(orgRel);
147
+
148
+ const requiredInt = PERMISSION_TO_INT[requiredPermission] || 4; // If invalid, it will always return false
149
+ const permission = acl[check] as ACLPermissionCheck;
150
+ const acccountPermissionInt = PERMISSION_TO_INT[permission] || 0; // If invalid, it will always return false
151
+
152
+ return acccountPermissionInt >= requiredInt;
153
+ }
154
+
155
+ /**
156
+ * Get all title icons for organization/vendor
157
+ * @param org - Organization GQL data
158
+ * @param showIconsRule - Rules for which icons to show/not-show
159
+ * @returns Array of icon label objects with icon name and tooltip text
160
+ */
161
+
162
+ export function getTitleIconsForOrganization(
163
+ org: OrganizationGQL,
164
+ showIconsRule: Record<string, boolean> = {},
165
+ ) {
166
+ const { operation, compliance } = org;
167
+ const titleIcons = [];
168
+
169
+ if (operation && showIconsRule.operation !== false) {
170
+ titleIcons.push({
171
+ iconName: COMMON_ICON_NAMES[operation] || 'info-circle',
172
+ tooltipText: i18n.t(`org.type.${operation}`),
173
+ });
174
+ }
175
+
176
+ if (compliance?.length && showIconsRule.compliance !== false) {
177
+ const today = new Date();
178
+ const todayCalDate = today.toISOString().split('T')[0];
179
+ const notExpired = compliance.filter((item: any) => item.expirationDate && item.expirationDate > todayCalDate);
180
+
181
+ if (notExpired.length > 0) {
182
+ titleIcons.push({
183
+ iconName: 'document-license',
184
+ tooltipText: notExpired.map((item: any) => item.name).join(', '),
185
+ });
186
+ }
187
+ }
188
+
189
+ return titleIcons;
190
+ }
191
+
192
+ /**
193
+ * Get the default object for Oganization settings
194
+ * @param timeZone - Time zone string
195
+ * @return Default organization settings object
196
+ */
197
+
198
+ export function getDefaultOrganizationSettings(
199
+ timeZone: string | null,
200
+ contacts?: OrganizationContact[],
201
+ // do params here for priority service + manage roles
202
+ ): Partial<OrganizationSettingsObj> {
203
+ const orgSettings: Record<string, any> = {
204
+ timeZone: timeZone || DEFAULT_TIMEZONE,
205
+ color: COLORS[Math.floor(Math.random() * COLORS.length)],
206
+ };
207
+
208
+ if (contacts?.length) {
209
+ orgSettings.directory = contacts.map((contact) => {
210
+ return {
211
+ ...contact,
212
+ department: contact.department || 'OTHER',
213
+ };
214
+ }).filter((contact) => contact.emailAddress || contact.phoneNumber);
215
+ }
216
+
217
+ return orgSettings;
218
+ }
219
+
220
+ /**
221
+ * Get the enabled features for the organization;
222
+ * If none or empty, return the default features based on operation
223
+ */
224
+
225
+ export function getOrganizationFeatures(
226
+ operation: OrganizationOperationEnum,
227
+ enabledFeatures?: OrganizationFeatureEnum[] | null,
228
+ ): OrganizationFeatureEnum[] {
229
+ // @ts-expect-error - Allow operation as string
230
+ const allowedFeatures = FEATURES_BY_OPERATION[operation] || [];
231
+ const filteredFeatures = intersection(enabledFeatures || [], allowedFeatures);
232
+ return filteredFeatures.length ? filteredFeatures : allowedFeatures[0] ? [allowedFeatures[0]] : [];
233
+ }