-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.ts
More file actions
326 lines (314 loc) · 11.7 KB
/
page.ts
File metadata and controls
326 lines (314 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { emptyOverviewFilterOptions } from '../shared/filters';
import type { FacetFilterKey } from '../shared/filters';
import {
fetchFacetedFilterStateWithFallback,
fetchPublishedSnapshotContext,
settledArrayWithFallback,
settledValueWithFallback,
} from '../shared/pageUtils';
import { getCappedTotalPages, normalisePage } from '../shared/pagination';
import { priorityLabelFromRank } from '../shared/priority/priorityRankSql';
import type { AnalyticsQueryOptions } from '../shared/repositories';
import { UserOverviewTaskRow, taskFactsRepository, taskThinRepository } from '../shared/repositories';
import { caseWorkerProfileService, courtVenueService } from '../shared/services';
import { AnalyticsFilters, Task, TaskStatus } from '../shared/types';
import { UserOverviewSort } from '../shared/userOverviewSort';
import { USER_OVERVIEW_PAGE_SIZE } from './pagination';
import { CompletedByDatePoint, userOverviewService } from './service';
import { CompletedByTaskNameAggregate } from './types';
import { buildUserOverviewViewModel } from './viewModel';
type UserOverviewPageViewModel = ReturnType<typeof buildUserOverviewViewModel>;
const userOverviewSections = [
'user-overview-assigned',
'user-overview-completed',
'user-overview-completed-by-date',
'user-overview-completed-by-task-name',
'assigned',
'completed',
] as const;
type UserOverviewAjaxSection = (typeof userOverviewSections)[number];
const deferredSections = new Set<UserOverviewAjaxSection>([
'user-overview-assigned',
'user-overview-completed',
'user-overview-completed-by-date',
'user-overview-completed-by-task-name',
]);
const USER_OVERVIEW_QUERY_OPTIONS: AnalyticsQueryOptions = {
excludeRoleCategories: ['Judicial'],
};
function resolveUserOverviewSection(raw?: string): UserOverviewAjaxSection | undefined {
if (!raw) {
return undefined;
}
return userOverviewSections.includes(raw as UserOverviewAjaxSection) ? (raw as UserOverviewAjaxSection) : undefined;
}
function shouldFetchSection(requested: UserOverviewAjaxSection | undefined, section: UserOverviewAjaxSection): boolean {
if (!requested) {
return !deferredSections.has(section);
}
if (requested === 'assigned') {
return section === 'user-overview-assigned';
}
if (requested === 'completed') {
return section === 'user-overview-completed';
}
return requested === section;
}
function mapUserOverviewRow(row: UserOverviewTaskRow, caseWorkerNames: Record<string, string>): Task {
const totalAssignments = (row.number_of_reassignments ?? 0) + 1;
const withinSla = row.is_within_sla === 'Yes' ? true : row.is_within_sla === 'No' ? false : null;
const assigneeId = row.assignee ?? undefined;
const assigneeName = assigneeId ? (caseWorkerNames[assigneeId] ?? assigneeId) : undefined;
return {
caseId: row.case_id,
taskId: row.task_id,
service: row.jurisdiction_label ?? '',
roleCategory: row.role_category_label ?? '',
region: row.region ?? '',
location: row.location ?? '',
taskName: row.task_name ?? '',
priority: priorityLabelFromRank(row.priority_rank),
createdDate: row.created_date ?? '-',
assignedDate: row.first_assigned_date ?? undefined,
dueDate: row.due_date ?? undefined,
completedDate: row.completed_date ?? undefined,
handlingTimeDays: row.handling_time_days ?? undefined,
withinSla,
assigneeId,
assigneeName,
totalAssignments,
};
}
export async function buildUserOverviewPage(
filters: AnalyticsFilters,
sort: UserOverviewSort,
assignedPage = 1,
completedPage = 1,
ajaxSection?: string,
changedFilter?: FacetFilterKey,
requestedSnapshotId?: number
): Promise<UserOverviewPageViewModel> {
const snapshotContext = await fetchPublishedSnapshotContext(requestedSnapshotId);
const requestedSection = resolveUserOverviewSection(ajaxSection);
const shouldFetchAssigned = shouldFetchSection(requestedSection, 'user-overview-assigned');
const shouldFetchCompleted = shouldFetchSection(requestedSection, 'user-overview-completed');
const shouldFetchCompletedByDate = shouldFetchSection(requestedSection, 'user-overview-completed-by-date');
const shouldFetchCompletedByTaskName = shouldFetchSection(requestedSection, 'user-overview-completed-by-task-name');
const [assignedCountResult, completedCountResult] = await Promise.allSettled([
shouldFetchAssigned
? taskThinRepository.fetchUserOverviewAssignedTaskCount(
snapshotContext.snapshotId,
filters,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve(0),
shouldFetchCompleted
? taskFactsRepository.fetchUserOverviewCompletedTaskCount(
snapshotContext.snapshotId,
filters,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve(0),
]);
const assignedTotalResults = settledValueWithFallback(
assignedCountResult,
'Failed to fetch user overview assigned tasks count from database',
0
);
const completedTotalResults = settledValueWithFallback(
completedCountResult,
'Failed to fetch user overview completed tasks count from database',
0
);
const assignedTotalPages = getCappedTotalPages(assignedTotalResults, USER_OVERVIEW_PAGE_SIZE);
const completedTotalPages = getCappedTotalPages(completedTotalResults, USER_OVERVIEW_PAGE_SIZE);
const resolvedAssignedPage = normalisePage(assignedPage, assignedTotalPages);
const resolvedCompletedPage = normalisePage(completedPage, completedTotalPages);
const [
assignedResult,
completedResult,
assignedAllResult,
completedByDateResult,
completedByTaskNameResult,
completedComplianceResult,
locationDescriptionsResult,
caseWorkerNamesResult,
] = await Promise.allSettled([
shouldFetchAssigned
? taskThinRepository.fetchUserOverviewAssignedTaskRows(
snapshotContext.snapshotId,
filters,
sort.assigned,
{
page: resolvedAssignedPage,
pageSize: USER_OVERVIEW_PAGE_SIZE,
},
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchCompleted
? taskThinRepository.fetchUserOverviewCompletedTaskRows(
snapshotContext.snapshotId,
filters,
sort.completed,
{
page: resolvedCompletedPage,
pageSize: USER_OVERVIEW_PAGE_SIZE,
},
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchAssigned
? taskThinRepository.fetchUserOverviewAssignedTaskRows(
snapshotContext.snapshotId,
filters,
sort.assigned,
null,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchCompletedByDate
? taskThinRepository.fetchUserOverviewCompletedByDateRows(
snapshotContext.snapshotId,
filters,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchCompletedByTaskName
? taskThinRepository.fetchUserOverviewCompletedByTaskNameRows(
snapshotContext.snapshotId,
filters,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchCompleted
? taskFactsRepository.fetchUserOverviewCompletedSummaryRows(
snapshotContext.snapshotId,
filters,
USER_OVERVIEW_QUERY_OPTIONS
)
: Promise.resolve([]),
shouldFetchAssigned || shouldFetchCompleted ? courtVenueService.fetchCourtVenueDescriptions() : Promise.resolve({}),
shouldFetchAssigned || shouldFetchCompleted
? caseWorkerProfileService.fetchCaseWorkerProfileNames()
: Promise.resolve({}),
]);
const assignedRows = settledArrayWithFallback(
assignedResult,
'Failed to fetch user overview assigned tasks from database',
[]
);
const completedRows = settledArrayWithFallback(
completedResult,
'Failed to fetch user overview completed tasks from database',
[]
);
const assignedAllRows = settledArrayWithFallback(
assignedAllResult,
'Failed to fetch user overview assigned tasks for charts from database',
assignedRows
);
const completedByDateRows = settledArrayWithFallback(
completedByDateResult,
'Failed to fetch user overview completed by date rows from database',
[]
);
const completedByTaskNameRows = settledArrayWithFallback(
completedByTaskNameResult,
'Failed to fetch user overview completed by task name rows from database',
[]
);
const completedSummaryRows = settledValueWithFallback(
completedComplianceResult,
'Failed to fetch user overview completed summary from database',
[]
);
const locationDescriptions = settledValueWithFallback(
locationDescriptionsResult,
'Failed to fetch court venue descriptions from database',
{}
);
const caseWorkerNames = settledValueWithFallback(
caseWorkerNamesResult,
'Failed to fetch case worker profiles from database',
{}
);
const assignedTasks = assignedRows.map(row => ({
...mapUserOverviewRow(row, caseWorkerNames),
status: 'assigned' as TaskStatus,
}));
const completedTasks = completedRows.map(row => ({
...mapUserOverviewRow(row, caseWorkerNames),
status: 'completed' as TaskStatus,
}));
const assignedTasksAll = assignedAllRows.map(row => ({
...mapUserOverviewRow(row, caseWorkerNames),
status: 'assigned' as TaskStatus,
}));
const allTasks = shouldFetchAssigned ? [...assignedTasksAll, ...completedTasks] : completedTasks;
const overview = userOverviewService.buildUserOverview(allTasks);
const facetedFilterState = requestedSection
? { filters, filterOptions: emptyOverviewFilterOptions() }
: await fetchFacetedFilterStateWithFallback({
errorMessage: 'Failed to fetch user overview filter options from database',
snapshotId: snapshotContext.snapshotId,
filters,
queryOptions: USER_OVERVIEW_QUERY_OPTIONS,
changedFilter,
includeUserFilter: true,
});
const resolvedFilters = facetedFilterState.filters;
const filterOptions = facetedFilterState.filterOptions;
const completedByDate: CompletedByDatePoint[] = completedByDateRows.map(row => ({
date: row.date_key,
tasks: row.tasks,
withinDue: row.within_due,
beyondDue: row.beyond_due,
handlingTimeSum: row.handling_time_sum ?? 0,
handlingTimeCount: row.handling_time_count,
}));
const completedByTaskName: CompletedByTaskNameAggregate[] = completedByTaskNameRows.map(row => ({
taskName: row.task_name ?? 'Unknown',
tasks: row.tasks,
handlingTimeSum: row.handling_time_sum ?? 0,
handlingTimeCount: row.handling_time_count,
daysBeyondSum: row.days_beyond_sum ?? 0,
daysBeyondCount: row.days_beyond_count,
}));
const completedByDateTotals = completedByDate.reduce(
(acc, row) => ({
tasks: acc.tasks + row.tasks,
withinDue: acc.withinDue + row.withinDue,
}),
{ tasks: 0, withinDue: 0 }
);
const completedSummary = completedSummaryRows[0];
const completedComplianceSummary = {
total: completedSummary?.total ?? completedByDateTotals.tasks,
withinDueYes: completedSummary?.within ?? completedByDateTotals.withinDue,
withinDueNo:
completedSummary?.within !== undefined
? completedSummary.total - completedSummary.within
: completedByDateTotals.tasks - completedByDateTotals.withinDue,
};
return buildUserOverviewViewModel({
filters: resolvedFilters,
snapshotId: snapshotContext.snapshotId,
snapshotToken: snapshotContext.snapshotToken,
overview,
allTasks,
assignedTasks,
completedTasks,
assignedTotalResults,
completedTotalResults,
completedByDate,
completedByTaskName,
completedComplianceSummary,
filterOptions,
locationDescriptions,
sort,
assignedPage: resolvedAssignedPage,
completedPage: resolvedCompletedPage,
freshnessInsetText: snapshotContext.freshnessInsetText,
});
}