|
58 | 58 | return { type: "general" as const, message: usersWithAccessError }; |
59 | 59 | }); |
60 | 60 |
|
61 | | - // Group users by view_id and access_source for display in Views Available |
62 | | - let usersByView = $derived.by(() => { |
63 | | - if (!usersWithAccess?.users) return new Map<string, { direct: string[]; abac: string[] }>(); |
64 | | - const map = new Map<string, { direct: string[]; abac: string[] }>(); |
65 | | - for (const user of usersWithAccess.users) { |
66 | | - if (!user.views) continue; |
67 | | - for (const view of user.views) { |
68 | | - if (!map.has(view.view_id)) { |
69 | | - map.set(view.view_id, { direct: [], abac: [] }); |
70 | | - } |
71 | | - const entry = map.get(view.view_id)!; |
72 | | - const name = user.username || user.user_id || "Unknown"; |
73 | | - if (view.access_source === "ABAC") { |
74 | | - entry.abac.push(name); |
75 | | - } else { |
76 | | - entry.direct.push(name); |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - return map; |
81 | | - }); |
| 61 | + // Users grouped by view_id for display in Views Available |
| 62 | + let usersByView = $state(new Map<string, { direct: string[]; abac: string[] }>()); |
| 63 | + let viewErrors = $state(new Map<string, string>()); |
82 | 64 |
|
83 | 65 | let bankId = $derived(page.params.bank_id || ""); |
84 | 66 | let accountId = $derived(page.params.account_id || ""); |
|
134 | 116 | } |
135 | 117 | } |
136 | 118 |
|
137 | | - async function fetchUsersWithAccess(bankId: string, accountId: string) { |
| 119 | + async function fetchUsersWithAccess(bankId: string, accountId: string, views: any[]) { |
138 | 120 | usersWithAccessLoading = true; |
139 | 121 | usersWithAccessError = null; |
140 | | - try { |
141 | | - const res = await trackedFetch( |
142 | | - `/api/obp/banks/${encodeURIComponent(bankId)}/accounts/${encodeURIComponent(accountId)}/users-with-access` |
143 | | - ); |
144 | | - if (!res.ok) { |
145 | | - const data = await res.json().catch(() => ({})); |
146 | | - throw new Error(data.error || "Failed to fetch users with account access"); |
| 122 | + usersByView = new Map(); |
| 123 | + viewErrors = new Map(); |
| 124 | +
|
| 125 | + const settled = await Promise.allSettled( |
| 126 | + views.map(async (view) => { |
| 127 | + const res = await trackedFetch( |
| 128 | + `/api/obp/banks/${encodeURIComponent(bankId)}/accounts/${encodeURIComponent(accountId)}/views/${encodeURIComponent(view.id)}/users-with-access` |
| 129 | + ); |
| 130 | + if (!res.ok) { |
| 131 | + const data = await res.json().catch(() => ({})); |
| 132 | + throw new Error(data.error || `Failed to fetch users with access for view ${view.id}`); |
| 133 | + } |
| 134 | + const data = await res.json(); |
| 135 | + return { viewId: view.id, users: data.users || [] }; |
| 136 | + }) |
| 137 | + ); |
| 138 | +
|
| 139 | + const map = new Map<string, { direct: string[]; abac: string[] }>(); |
| 140 | + const errors = new Map<string, string>(); |
| 141 | + let anySuccess = false; |
| 142 | +
|
| 143 | + for (let i = 0; i < settled.length; i++) { |
| 144 | + const result = settled[i]; |
| 145 | + const viewId = views[i].id; |
| 146 | + if (result.status === "fulfilled") { |
| 147 | + anySuccess = true; |
| 148 | + const entry = { direct: [] as string[], abac: [] as string[] }; |
| 149 | + for (const user of result.value.users) { |
| 150 | + const name = user.username || user.user_id || "Unknown"; |
| 151 | + if (user.access_source === "ABAC") { |
| 152 | + entry.abac.push(name); |
| 153 | + } else { |
| 154 | + entry.direct.push(name); |
| 155 | + } |
| 156 | + } |
| 157 | + map.set(viewId, entry); |
| 158 | + } else { |
| 159 | + const msg = result.reason instanceof Error ? result.reason.message : String(result.reason); |
| 160 | + errors.set(viewId, msg); |
147 | 161 | } |
148 | | - usersWithAccess = await res.json(); |
149 | | - } catch (err) { |
150 | | - usersWithAccessError = err instanceof Error ? err.message : "Failed to fetch users with account access"; |
151 | | - usersWithAccess = null; |
152 | | - } finally { |
153 | | - usersWithAccessLoading = false; |
154 | 162 | } |
| 163 | +
|
| 164 | + usersByView = map; |
| 165 | + viewErrors = errors; |
| 166 | + usersWithAccess = anySuccess ? { users: [] } : null; |
| 167 | + if (errors.size > 0 && !anySuccess) { |
| 168 | + usersWithAccessError = "Failed to fetch users with access for all views"; |
| 169 | + } |
| 170 | + usersWithAccessLoading = false; |
155 | 171 | } |
156 | 172 |
|
157 | 173 | async function loadPage(bankId: string, accountId: string, viewId: string) { |
158 | 174 | account = null; |
159 | 175 | error = null; |
160 | 176 | usersWithAccess = null; |
| 177 | + usersByView = new Map(); |
| 178 | + viewErrors = new Map(); |
161 | 179 | await checkAccountAccess(bankId, accountId, viewId); |
162 | 180 | if (hasAccountAccess) { |
163 | | - await Promise.all([ |
164 | | - fetchAccount(bankId, accountId, viewId), |
165 | | - fetchUsersWithAccess(bankId, accountId) |
166 | | - ]); |
| 181 | + await fetchAccount(bankId, accountId, viewId); |
| 182 | + if (account?.views_available?.length) { |
| 183 | + await fetchUsersWithAccess(bankId, accountId, account.views_available); |
| 184 | + } |
167 | 185 | } |
168 | 186 | } |
169 | 187 |
|
|
431 | 449 | </div> |
432 | 450 | {#each account.views_available as view} |
433 | 451 | {@const viewUsers = usersByView.get(view.id)} |
| 452 | + {@const viewError = viewErrors.get(view.id)} |
434 | 453 | <div class="views-table-row"> |
435 | 454 | <div class="views-col-name"> |
436 | 455 | <a href="/account-access/accounts/{encodeURIComponent(bankId)}/{encodeURIComponent(accountId)}/{encodeURIComponent(view.id)}/transactions" class="view-name-link">{toTitleCase(view.id)}</a> |
437 | 456 | {#if view.is_public} |
438 | 457 | <span class="view-badge public">PUBLIC</span> |
439 | 458 | {/if} |
440 | 459 | </div> |
441 | | - <div class="views-col-users"> |
442 | | - {#if usersWithAccessLoading} |
443 | | - <Loader2 size={14} class="spinner-icon" /> |
444 | | - {:else if viewUsers?.direct?.length} |
445 | | - {#each viewUsers.direct as username} |
446 | | - <span class="user-chip direct">{username}</span> |
447 | | - {/each} |
448 | | - {:else if !usersWithAccessError} |
449 | | - <span class="no-users">—</span> |
450 | | - {/if} |
451 | | - </div> |
452 | | - <div class="views-col-users"> |
453 | | - {#if usersWithAccessLoading} |
454 | | - <Loader2 size={14} class="spinner-icon" /> |
455 | | - {:else if viewUsers?.abac?.length} |
456 | | - {#each viewUsers.abac as username} |
457 | | - <span class="user-chip abac">{username}</span> |
458 | | - {/each} |
459 | | - {:else if !usersWithAccessError} |
460 | | - <span class="no-users">—</span> |
461 | | - {/if} |
462 | | - </div> |
| 460 | + {#if viewError} |
| 461 | + <div class="views-col-users view-error" style="grid-column: span 2;"> |
| 462 | + {viewError} |
| 463 | + </div> |
| 464 | + {:else} |
| 465 | + <div class="views-col-users"> |
| 466 | + {#if usersWithAccessLoading} |
| 467 | + <Loader2 size={14} class="spinner-icon" /> |
| 468 | + {:else if viewUsers?.direct?.length} |
| 469 | + {#each viewUsers.direct as username} |
| 470 | + <span class="user-chip direct">{username}</span> |
| 471 | + {/each} |
| 472 | + {:else} |
| 473 | + <span class="no-users">—</span> |
| 474 | + {/if} |
| 475 | + </div> |
| 476 | + <div class="views-col-users"> |
| 477 | + {#if usersWithAccessLoading} |
| 478 | + <Loader2 size={14} class="spinner-icon" /> |
| 479 | + {:else if viewUsers?.abac?.length} |
| 480 | + {#each viewUsers.abac as username} |
| 481 | + <span class="user-chip abac">{username}</span> |
| 482 | + {/each} |
| 483 | + {:else} |
| 484 | + <span class="no-users">—</span> |
| 485 | + {/if} |
| 486 | + </div> |
| 487 | + {/if} |
463 | 488 | </div> |
464 | 489 | {/each} |
465 | 490 | </div> |
|
1183 | 1208 | color: rgb(var(--color-warning-300)); |
1184 | 1209 | } |
1185 | 1210 |
|
| 1211 | + .view-error { |
| 1212 | + font-size: 0.75rem; |
| 1213 | + color: #dc2626; |
| 1214 | + padding: 0.5rem 0.75rem; |
| 1215 | + } |
| 1216 | +
|
| 1217 | + :global([data-mode="dark"]) .view-error { |
| 1218 | + color: rgb(var(--color-error-400)); |
| 1219 | + } |
| 1220 | +
|
1186 | 1221 | .no-users { |
1187 | 1222 | color: #d1d5db; |
1188 | 1223 | font-size: 0.875rem; |
|
0 commit comments