@go-avro/avro-js 0.0.118 → 0.0.120
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.
- package/dist/client/QueryClient.d.ts +7 -27
- package/dist/client/QueryClient.js +67 -729
- package/dist/client/hooks/bills.js +29 -72
- package/dist/client/hooks/catalog_items.js +6 -20
- package/dist/client/hooks/chats.js +7 -8
- package/dist/client/hooks/checkins.js +3 -15
- package/dist/client/hooks/companies.js +8 -24
- package/dist/client/hooks/events.js +25 -85
- package/dist/client/hooks/groups.js +9 -4
- package/dist/client/hooks/jobs.js +15 -35
- package/dist/client/hooks/labels.js +9 -4
- package/dist/client/hooks/messages.js +7 -8
- package/dist/client/hooks/months.js +13 -15
- package/dist/client/hooks/payouts.js +13 -14
- package/dist/client/hooks/prepayments.js +13 -15
- package/dist/client/hooks/proposal.js +3 -9
- package/dist/client/hooks/routes.js +13 -38
- package/dist/client/hooks/skills.js +9 -4
- package/dist/client/hooks/teams.js +9 -4
- package/dist/client/hooks/waivers.js +12 -13
- package/dist/client/realtime.d.ts +64 -0
- package/dist/client/realtime.js +470 -0
- package/dist/types/api/Job.d.ts +1 -1
- package/dist/types/api/Task.d.ts +2 -1
- package/dist/types/api/Task.js +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -2,401 +2,10 @@ import io from 'socket.io-client';
|
|
|
2
2
|
import { useMutation, useQueryClient, } from '@tanstack/react-query';
|
|
3
3
|
import { v4 as uuidv4 } from 'uuid';
|
|
4
4
|
import { fetchCensusFeatures } from '../census';
|
|
5
|
-
import {
|
|
5
|
+
import { LoginResponse, } from '../types/api';
|
|
6
6
|
import { AuthState } from '../types/auth';
|
|
7
7
|
import { StandardError } from '../types/error';
|
|
8
|
-
|
|
9
|
-
return 'invalidateKeys' in c;
|
|
10
|
-
}
|
|
11
|
-
export function matchesEventsListQuery(queryKey, item) {
|
|
12
|
-
if (queryKey.length < 11)
|
|
13
|
-
return 'match';
|
|
14
|
-
if (!item)
|
|
15
|
-
return 'match';
|
|
16
|
-
const knownIds = queryKey[3];
|
|
17
|
-
const unknownIds = queryKey[4];
|
|
18
|
-
const query = queryKey[5];
|
|
19
|
-
const includeUnbilled = queryKey[6];
|
|
20
|
-
const includeBilled = queryKey[7];
|
|
21
|
-
const includePaid = queryKey[8];
|
|
22
|
-
const filterJobId = queryKey[9];
|
|
23
|
-
// Index 13 was appended after the original 13-element key; older
|
|
24
|
-
// keys read undefined here, which means "no type filter".
|
|
25
|
-
const lineItemTypes = queryKey[13];
|
|
26
|
-
if (knownIds && knownIds.length > 0 && !knownIds.includes(item.id))
|
|
27
|
-
return 'nomatch';
|
|
28
|
-
if (unknownIds && unknownIds.length > 0 && unknownIds.includes(item.id))
|
|
29
|
-
return 'nomatch';
|
|
30
|
-
if (filterJobId && item.job_id !== filterJobId)
|
|
31
|
-
return 'nomatch';
|
|
32
|
-
if (lineItemTypes &&
|
|
33
|
-
lineItemTypes.length > 0 &&
|
|
34
|
-
item.line_item_type !== undefined &&
|
|
35
|
-
!lineItemTypes.includes(item.line_item_type))
|
|
36
|
-
return 'nomatch';
|
|
37
|
-
const status = item.status;
|
|
38
|
-
if (status !== undefined) {
|
|
39
|
-
const isPaid = status === 'PAID' || status === 'PREPAID' || status === 'EXTERNALLY_PAID';
|
|
40
|
-
const isBilled = status === 'BILLED' || status === 'EXTERNALLY_BILLED';
|
|
41
|
-
const isUnbilled = !isPaid && !isBilled;
|
|
42
|
-
if (includePaid === false && isPaid)
|
|
43
|
-
return 'nomatch';
|
|
44
|
-
if (includeBilled === false && isBilled)
|
|
45
|
-
return 'nomatch';
|
|
46
|
-
if (includeUnbilled === false && isUnbilled)
|
|
47
|
-
return 'nomatch';
|
|
48
|
-
}
|
|
49
|
-
if (query && query.length > 0)
|
|
50
|
-
return 'unknown';
|
|
51
|
-
return 'match';
|
|
52
|
-
}
|
|
53
|
-
export function matchesBillsListQuery(queryKey, item) {
|
|
54
|
-
if (queryKey.length < 6)
|
|
55
|
-
return 'match';
|
|
56
|
-
if (!item)
|
|
57
|
-
return 'match';
|
|
58
|
-
const query = queryKey[2];
|
|
59
|
-
const knownIds = queryKey[3];
|
|
60
|
-
const unknownIds = queryKey[4];
|
|
61
|
-
const paidFilter = queryKey[5];
|
|
62
|
-
if (knownIds && knownIds.length > 0 && !knownIds.includes(item.id))
|
|
63
|
-
return 'nomatch';
|
|
64
|
-
if (unknownIds && unknownIds.length > 0 && unknownIds.includes(item.id))
|
|
65
|
-
return 'nomatch';
|
|
66
|
-
const status = item.status;
|
|
67
|
-
if (paidFilter !== undefined && status !== undefined) {
|
|
68
|
-
const isPaid = status === 'PAID' || status === 'MANUALLY_PAID';
|
|
69
|
-
if (paidFilter === true && !isPaid)
|
|
70
|
-
return 'nomatch';
|
|
71
|
-
if (paidFilter === false && isPaid)
|
|
72
|
-
return 'nomatch';
|
|
73
|
-
}
|
|
74
|
-
if (query && query.length > 0)
|
|
75
|
-
return 'unknown';
|
|
76
|
-
return 'match';
|
|
77
|
-
}
|
|
78
|
-
export function matchesJobsListQuery(queryKey, item) {
|
|
79
|
-
if (queryKey[0] !== 'infinite')
|
|
80
|
-
return 'match';
|
|
81
|
-
if (queryKey.length < 6)
|
|
82
|
-
return 'match';
|
|
83
|
-
if (!item)
|
|
84
|
-
return 'match';
|
|
85
|
-
const query = queryKey[4];
|
|
86
|
-
const routeId = queryKey[5];
|
|
87
|
-
if (routeId && routeId.length > 0) {
|
|
88
|
-
const routes = item.routes ?? [];
|
|
89
|
-
if (!routes.some((r) => r?.route_id === routeId))
|
|
90
|
-
return 'nomatch';
|
|
91
|
-
}
|
|
92
|
-
if (query && query.length > 0)
|
|
93
|
-
return 'unknown';
|
|
94
|
-
return 'match';
|
|
95
|
-
}
|
|
96
|
-
export function matchesRoutesListQuery(queryKey, _item) {
|
|
97
|
-
if (queryKey.length < 5)
|
|
98
|
-
return 'match';
|
|
99
|
-
const query = queryKey[3];
|
|
100
|
-
if (query && query.length > 0)
|
|
101
|
-
return 'unknown';
|
|
102
|
-
return 'match';
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Maps socket event names to cache-update strategies.
|
|
106
|
-
*
|
|
107
|
-
* *Targeted* events receive `{ id }` from the backend and surgically
|
|
108
|
-
* update / insert / remove only the affected cache entries.
|
|
109
|
-
*
|
|
110
|
-
* *Bulk* events (plural mutations, scheduling, etc.) fall back to
|
|
111
|
-
* full query-key invalidation because the backend doesn't emit per-item ids.
|
|
112
|
-
*/
|
|
113
|
-
const SOCKET_EVENT_CONFIG = {
|
|
114
|
-
// ── Company ──
|
|
115
|
-
create_company: {
|
|
116
|
-
entityKey: 'companies',
|
|
117
|
-
action: 'create',
|
|
118
|
-
fetchPath: (id) => `/company/${id}`,
|
|
119
|
-
alsoInvalidate: [['/company/list']],
|
|
120
|
-
},
|
|
121
|
-
update_company: {
|
|
122
|
-
entityKey: 'companies',
|
|
123
|
-
action: 'update',
|
|
124
|
-
fetchPath: (id) => `/company/${id}`,
|
|
125
|
-
alsoInvalidate: [['companies', 'current'], ['/company/list']],
|
|
126
|
-
},
|
|
127
|
-
delete_company: {
|
|
128
|
-
entityKey: 'companies',
|
|
129
|
-
action: 'delete',
|
|
130
|
-
fetchPath: null,
|
|
131
|
-
alsoInvalidate: [['companies', 'current'], ['/company/list']],
|
|
132
|
-
},
|
|
133
|
-
// ── Users (no single-entity socket events) ──
|
|
134
|
-
user_updated: { invalidateKeys: [['users'], ['user']] },
|
|
135
|
-
// Membership / permission changes.
|
|
136
|
-
update_users: { invalidateKeys: [['users'], ['user']] },
|
|
137
|
-
// Presence / location-status flips: only the member list is affected.
|
|
138
|
-
update_presence: { invalidateKeys: [['users', 'company']] },
|
|
139
|
-
// ── Jobs ──
|
|
140
|
-
create_job: {
|
|
141
|
-
entityKey: 'jobs',
|
|
142
|
-
action: 'create',
|
|
143
|
-
fetchPath: (id) => `/job/${id}`,
|
|
144
|
-
construct: (d) => new Job(d),
|
|
145
|
-
matchesQuery: matchesJobsListQuery,
|
|
146
|
-
},
|
|
147
|
-
update_job: {
|
|
148
|
-
entityKey: 'jobs',
|
|
149
|
-
action: 'update',
|
|
150
|
-
fetchPath: (id) => `/job/${id}`,
|
|
151
|
-
construct: (d) => new Job(d),
|
|
152
|
-
matchesQuery: matchesJobsListQuery,
|
|
153
|
-
},
|
|
154
|
-
delete_job: { entityKey: 'jobs', action: 'delete', fetchPath: null },
|
|
155
|
-
update_jobs: { invalidateKeys: [['jobs'], ['infinite', 'jobs']] },
|
|
156
|
-
delete_jobs: { invalidateKeys: [['jobs'], ['infinite', 'jobs']] },
|
|
157
|
-
// ── Routes ──
|
|
158
|
-
// Route membership lives on jobs too (task.route_ids), so every
|
|
159
|
-
// route mutation must refresh the jobs caches or unrouted/routed
|
|
160
|
-
// state goes stale.
|
|
161
|
-
create_route: {
|
|
162
|
-
entityKey: 'routes',
|
|
163
|
-
action: 'create',
|
|
164
|
-
fetchPath: (id) => `/route/${id}`,
|
|
165
|
-
construct: (d) => new Route(d),
|
|
166
|
-
matchesQuery: matchesRoutesListQuery,
|
|
167
|
-
alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
|
|
168
|
-
},
|
|
169
|
-
update_route: {
|
|
170
|
-
entityKey: 'routes',
|
|
171
|
-
action: 'update',
|
|
172
|
-
fetchPath: (id) => `/route/${id}`,
|
|
173
|
-
construct: (d) => new Route(d),
|
|
174
|
-
matchesQuery: matchesRoutesListQuery,
|
|
175
|
-
alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
|
|
176
|
-
},
|
|
177
|
-
delete_route: {
|
|
178
|
-
entityKey: 'routes',
|
|
179
|
-
action: 'delete',
|
|
180
|
-
fetchPath: null,
|
|
181
|
-
alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
|
|
182
|
-
},
|
|
183
|
-
// ── Events (also refetch parent job — overdueness, last_event, etc.) ──
|
|
184
|
-
create_event: {
|
|
185
|
-
entityKey: 'events',
|
|
186
|
-
action: 'create',
|
|
187
|
-
fetchPath: (id) => `/event/${id}`,
|
|
188
|
-
construct: (d) => constructEvent(d),
|
|
189
|
-
matchesQuery: matchesEventsListQuery,
|
|
190
|
-
relatedRefetch: [
|
|
191
|
-
{
|
|
192
|
-
entityKey: 'jobs',
|
|
193
|
-
idField: 'job_id',
|
|
194
|
-
fetchPath: (id) => `/job/${id}`,
|
|
195
|
-
construct: (d) => new Job(d),
|
|
196
|
-
matchesQuery: matchesJobsListQuery,
|
|
197
|
-
},
|
|
198
|
-
],
|
|
199
|
-
},
|
|
200
|
-
update_event: {
|
|
201
|
-
entityKey: 'events',
|
|
202
|
-
action: 'update',
|
|
203
|
-
fetchPath: (id) => `/event/${id}`,
|
|
204
|
-
construct: (d) => constructEvent(d),
|
|
205
|
-
matchesQuery: matchesEventsListQuery,
|
|
206
|
-
relatedRefetch: [
|
|
207
|
-
{
|
|
208
|
-
entityKey: 'jobs',
|
|
209
|
-
idField: 'job_id',
|
|
210
|
-
fetchPath: (id) => `/job/${id}`,
|
|
211
|
-
construct: (d) => new Job(d),
|
|
212
|
-
matchesQuery: matchesJobsListQuery,
|
|
213
|
-
},
|
|
214
|
-
],
|
|
215
|
-
},
|
|
216
|
-
delete_event: {
|
|
217
|
-
entityKey: 'events',
|
|
218
|
-
action: 'delete',
|
|
219
|
-
fetchPath: null,
|
|
220
|
-
relatedRefetch: [
|
|
221
|
-
{
|
|
222
|
-
entityKey: 'jobs',
|
|
223
|
-
idField: 'job_id',
|
|
224
|
-
fetchPath: (id) => `/job/${id}`,
|
|
225
|
-
construct: (d) => new Job(d),
|
|
226
|
-
matchesQuery: matchesJobsListQuery,
|
|
227
|
-
},
|
|
228
|
-
],
|
|
229
|
-
},
|
|
230
|
-
update_events: { invalidateKeys: [['events']] },
|
|
231
|
-
// ── Teams ──
|
|
232
|
-
create_team: { entityKey: 'teams', action: 'create', fetchPath: null },
|
|
233
|
-
update_team: { entityKey: 'teams', action: 'update', fetchPath: null },
|
|
234
|
-
delete_team: { entityKey: 'teams', action: 'delete', fetchPath: null },
|
|
235
|
-
update_teams: { invalidateKeys: [['teams']] },
|
|
236
|
-
// ── Bills ──
|
|
237
|
-
create_bill: {
|
|
238
|
-
entityKey: 'bills',
|
|
239
|
-
action: 'create',
|
|
240
|
-
fetchPath: (id) => `/bill/${id}`,
|
|
241
|
-
construct: (d) => new Bill(d),
|
|
242
|
-
matchesQuery: matchesBillsListQuery,
|
|
243
|
-
},
|
|
244
|
-
delete_bill: { entityKey: 'bills', action: 'delete', fetchPath: null },
|
|
245
|
-
update_bills: { invalidateKeys: [['bills']] },
|
|
246
|
-
// ── Check-ins (also refresh the job — its payload embeds
|
|
247
|
-
// current_checkins for the on-site indicator) ──
|
|
248
|
-
create_checkin: {
|
|
249
|
-
entityKey: 'checkins',
|
|
250
|
-
action: 'create',
|
|
251
|
-
fetchPath: null,
|
|
252
|
-
alsoInvalidate: [['checkins']],
|
|
253
|
-
relatedRefetch: [
|
|
254
|
-
{
|
|
255
|
-
entityKey: 'jobs',
|
|
256
|
-
idField: 'job_id',
|
|
257
|
-
fetchPath: (id) => `/job/${id}`,
|
|
258
|
-
construct: (d) => new Job(d),
|
|
259
|
-
matchesQuery: matchesJobsListQuery,
|
|
260
|
-
},
|
|
261
|
-
],
|
|
262
|
-
},
|
|
263
|
-
update_checkin: {
|
|
264
|
-
entityKey: 'checkins',
|
|
265
|
-
action: 'update',
|
|
266
|
-
fetchPath: null,
|
|
267
|
-
alsoInvalidate: [['checkins']],
|
|
268
|
-
relatedRefetch: [
|
|
269
|
-
{
|
|
270
|
-
entityKey: 'jobs',
|
|
271
|
-
idField: 'job_id',
|
|
272
|
-
fetchPath: (id) => `/job/${id}`,
|
|
273
|
-
construct: (d) => new Job(d),
|
|
274
|
-
matchesQuery: matchesJobsListQuery,
|
|
275
|
-
},
|
|
276
|
-
],
|
|
277
|
-
},
|
|
278
|
-
// ── Sessions ──
|
|
279
|
-
create_session: {
|
|
280
|
-
entityKey: 'sessions',
|
|
281
|
-
action: 'create',
|
|
282
|
-
fetchPath: null,
|
|
283
|
-
alsoInvalidate: [['analytics', 'sessions']],
|
|
284
|
-
},
|
|
285
|
-
update_session: {
|
|
286
|
-
entityKey: 'sessions',
|
|
287
|
-
action: 'update',
|
|
288
|
-
fetchPath: null,
|
|
289
|
-
alsoInvalidate: [['analytics', 'sessions']],
|
|
290
|
-
},
|
|
291
|
-
// ── Catalog Items ──
|
|
292
|
-
create_catalog_item: {
|
|
293
|
-
entityKey: 'catalog_items',
|
|
294
|
-
action: 'create',
|
|
295
|
-
fetchPath: (id) => `/catalog_item/${id}`,
|
|
296
|
-
},
|
|
297
|
-
update_catalog_item: {
|
|
298
|
-
entityKey: 'catalog_items',
|
|
299
|
-
action: 'update',
|
|
300
|
-
fetchPath: (id) => `/catalog_item/${id}`,
|
|
301
|
-
},
|
|
302
|
-
delete_catalog_item: {
|
|
303
|
-
entityKey: 'catalog_items',
|
|
304
|
-
action: 'delete',
|
|
305
|
-
fetchPath: null,
|
|
306
|
-
},
|
|
307
|
-
// ── Groups ──
|
|
308
|
-
create_group: { entityKey: 'groups', action: 'create', fetchPath: null },
|
|
309
|
-
update_group: { entityKey: 'groups', action: 'update', fetchPath: null },
|
|
310
|
-
delete_group: { entityKey: 'groups', action: 'delete', fetchPath: null },
|
|
311
|
-
// ── Labels ──
|
|
312
|
-
create_label: { entityKey: 'labels', action: 'create', fetchPath: null },
|
|
313
|
-
update_label: { entityKey: 'labels', action: 'update', fetchPath: null },
|
|
314
|
-
delete_label: { entityKey: 'labels', action: 'delete', fetchPath: null },
|
|
315
|
-
// ── Skills ──
|
|
316
|
-
create_skill: { entityKey: 'skills', action: 'create', fetchPath: null },
|
|
317
|
-
update_skill: { entityKey: 'skills', action: 'update', fetchPath: null },
|
|
318
|
-
delete_skill: { entityKey: 'skills', action: 'delete', fetchPath: null },
|
|
319
|
-
// ── Proposals ──
|
|
320
|
-
create_proposal: {
|
|
321
|
-
entityKey: 'proposals',
|
|
322
|
-
action: 'create',
|
|
323
|
-
fetchPath: (id) => `/proposal/${id}`,
|
|
324
|
-
},
|
|
325
|
-
update_proposal: {
|
|
326
|
-
entityKey: 'proposals',
|
|
327
|
-
action: 'update',
|
|
328
|
-
fetchPath: (id) => `/proposal/${id}`,
|
|
329
|
-
},
|
|
330
|
-
delete_proposal: {
|
|
331
|
-
entityKey: 'proposals',
|
|
332
|
-
action: 'delete',
|
|
333
|
-
fetchPath: null,
|
|
334
|
-
},
|
|
335
|
-
// ── Service Months ──
|
|
336
|
-
create_month: { entityKey: 'months', action: 'create', fetchPath: null },
|
|
337
|
-
update_month: { invalidateKeys: [['months']] },
|
|
338
|
-
update_months: { invalidateKeys: [['months']] },
|
|
339
|
-
delete_months: { invalidateKeys: [['months']] },
|
|
340
|
-
// ── Tasks (nested under jobs — target the parent job) ──
|
|
341
|
-
create_task: {
|
|
342
|
-
entityKey: 'jobs',
|
|
343
|
-
action: 'update',
|
|
344
|
-
fetchPath: (id) => `/job/${id}`,
|
|
345
|
-
idField: 'job_id',
|
|
346
|
-
construct: (d) => new Job(d),
|
|
347
|
-
},
|
|
348
|
-
update_task: {
|
|
349
|
-
entityKey: 'jobs',
|
|
350
|
-
action: 'update',
|
|
351
|
-
fetchPath: (id) => `/job/${id}`,
|
|
352
|
-
idField: 'job_id',
|
|
353
|
-
construct: (d) => new Job(d),
|
|
354
|
-
},
|
|
355
|
-
delete_task: {
|
|
356
|
-
entityKey: 'jobs',
|
|
357
|
-
action: 'update',
|
|
358
|
-
fetchPath: (id) => `/job/${id}`,
|
|
359
|
-
idField: 'job_id',
|
|
360
|
-
construct: (d) => new Job(d),
|
|
361
|
-
},
|
|
362
|
-
// ── Scheduling ──
|
|
363
|
-
// Handled surgically in setupSocketInvalidation: when the payload
|
|
364
|
-
// carries `job_ids`, the affected jobs are bulk-fetched and patched
|
|
365
|
-
// in place instead of refetching every jobs query. These keys are
|
|
366
|
-
// the fallback for payloads without ids (legacy backends, runs
|
|
367
|
-
// where the affected set is unknown).
|
|
368
|
-
schedule_complete: {
|
|
369
|
-
invalidateKeys: [
|
|
370
|
-
['routes'],
|
|
371
|
-
['jobs'],
|
|
372
|
-
['infinite', 'jobs'],
|
|
373
|
-
['companies'],
|
|
374
|
-
['/company/list'],
|
|
375
|
-
],
|
|
376
|
-
},
|
|
377
|
-
// Terminal failure of a scheduling run: refetch companies so the
|
|
378
|
-
// "scheduling in progress" banner (company.schedule_locked) clears
|
|
379
|
-
scheduling_error: {
|
|
380
|
-
invalidateKeys: [['companies'], ['/company/list']],
|
|
381
|
-
},
|
|
382
|
-
// ── Location ──
|
|
383
|
-
// Handled surgically in setupSocketInvalidation (patches
|
|
384
|
-
// `current_location` / `last_location` in place); these keys are
|
|
385
|
-
// only the fallback for malformed payloads.
|
|
386
|
-
location_update: { invalidateKeys: [['teams']] },
|
|
387
|
-
// ── Prepayments ──
|
|
388
|
-
update_prepayments: { invalidateKeys: [['prepayments']] },
|
|
389
|
-
// ── Chats ──
|
|
390
|
-
new_message: { invalidateKeys: [['chats'], ['messages']] },
|
|
391
|
-
};
|
|
392
|
-
/**
|
|
393
|
-
* Returns true when `query.queryKey` belongs to the given entity,
|
|
394
|
-
* including the `['infinite', entityKey, ...]` variant used by jobs.
|
|
395
|
-
*/
|
|
396
|
-
function matchesEntityKey(query, entityKey) {
|
|
397
|
-
const k = query.queryKey;
|
|
398
|
-
return k[0] === entityKey || (k[0] === 'infinite' && k[1] === entityKey);
|
|
399
|
-
}
|
|
8
|
+
import { ChangeApplier, fallbackInvalidate, patchMemberLocation, patchTeamLocation, } from '../client/realtime';
|
|
400
9
|
export class AvroQueryClient {
|
|
401
10
|
constructor(config) {
|
|
402
11
|
this._authState = AuthState.UNKNOWN;
|
|
@@ -405,6 +14,8 @@ export class AvroQueryClient {
|
|
|
405
14
|
this.authStateListeners = [];
|
|
406
15
|
this._queryClient = null;
|
|
407
16
|
this._socketInvalidationCleanup = null;
|
|
17
|
+
this._applier = null;
|
|
18
|
+
this._companyVersions = new Map();
|
|
408
19
|
this._emailTracking = new Map();
|
|
409
20
|
this._emailListenersInit = false;
|
|
410
21
|
this.config = {
|
|
@@ -505,263 +116,87 @@ export class AvroQueryClient {
|
|
|
505
116
|
off(eventName, callback) {
|
|
506
117
|
this.socket?.off(eventName, callback);
|
|
507
118
|
}
|
|
508
|
-
/**
|
|
509
|
-
* Wire up automatic query invalidation for socket events.
|
|
510
|
-
* Called once from AvroQueryClientProvider when the tanstack
|
|
511
|
-
* QueryClient is available. Also handles auto-joining the
|
|
512
|
-
* company room on connect so consumer apps don't have to.
|
|
513
|
-
*
|
|
514
|
-
* Targeted events (single-entity CRUD) receive `{ id }` from the
|
|
515
|
-
* backend and surgically create / update / remove only the
|
|
516
|
-
* affected cache entries. Bulk events fall back to full
|
|
517
|
-
* query-key invalidation.
|
|
518
|
-
*
|
|
519
|
-
* Backward-compatible: if a targeted event arrives without an id
|
|
520
|
-
* (old backend), we fall back to full invalidation.
|
|
521
|
-
*/
|
|
119
|
+
/** Wire the socket to the cache (called once from the provider). */
|
|
522
120
|
setupSocketInvalidation(queryClient) {
|
|
523
121
|
if (this._socketInvalidationCleanup)
|
|
524
122
|
return;
|
|
525
123
|
this._queryClient = queryClient;
|
|
124
|
+
const applier = new ChangeApplier(this, queryClient);
|
|
125
|
+
this._applier = applier;
|
|
526
126
|
const handlers = [];
|
|
527
|
-
const
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
queryClient.invalidateQueries({
|
|
531
|
-
predicate: (q) => matchesEntityKey(q, entityKey),
|
|
532
|
-
});
|
|
127
|
+
const on = (event, handler) => {
|
|
128
|
+
this.socket.on(event, handler);
|
|
129
|
+
handlers.push({ event, handler });
|
|
533
130
|
};
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
131
|
+
on('changes', (payload) => {
|
|
132
|
+
if (payload?.company && typeof payload.version === 'number') {
|
|
133
|
+
this._companyVersions.set(payload.company, payload.version);
|
|
134
|
+
}
|
|
135
|
+
applier.apply(payload);
|
|
136
|
+
});
|
|
137
|
+
on('rejoin_company', (data) => {
|
|
138
|
+
if (data?.company_id && data.company_id === this.companyId)
|
|
139
|
+
this.joinCompanyRoom();
|
|
140
|
+
});
|
|
141
|
+
on('update_presence', () => {
|
|
142
|
+
queryClient.invalidateQueries({ queryKey: ['users', 'company'] });
|
|
143
|
+
});
|
|
144
|
+
on('location_update', (data) => {
|
|
145
|
+
if (!data || typeof data.latitude !== 'number') {
|
|
146
|
+
queryClient.invalidateQueries({ queryKey: ['teams'] });
|
|
537
147
|
queryClient.invalidateQueries({ queryKey: ['users', 'company'] });
|
|
538
148
|
return;
|
|
539
149
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
});
|
|
548
|
-
return changed ? next : members;
|
|
549
|
-
};
|
|
550
|
-
queryClient.setQueriesData({ queryKey: ['users', 'company'] }, (old) => Array.isArray(old) ? patchMembers(old) : old);
|
|
551
|
-
};
|
|
552
|
-
const patchTeamLocation = (data) => {
|
|
553
|
-
const teamId = data.team_id;
|
|
554
|
-
if (!teamId || typeof teamId !== 'string')
|
|
555
|
-
return;
|
|
556
|
-
const patchTeams = (teams) => {
|
|
557
|
-
let changed = false;
|
|
558
|
-
const next = teams.map((t) => {
|
|
559
|
-
if (t?.id !== teamId)
|
|
560
|
-
return t;
|
|
561
|
-
changed = true;
|
|
562
|
-
return { ...t, current_location: data };
|
|
563
|
-
});
|
|
564
|
-
return changed ? next : teams;
|
|
565
|
-
};
|
|
566
|
-
queryClient.setQueriesData({ queryKey: ['teams'] }, (old) => {
|
|
567
|
-
if (Array.isArray(old))
|
|
568
|
-
return patchTeams(old);
|
|
569
|
-
if (old?.pages) {
|
|
570
|
-
return {
|
|
571
|
-
...old,
|
|
572
|
-
pages: old.pages.map((page) => Array.isArray(page) ? patchTeams(page) : page),
|
|
573
|
-
};
|
|
574
|
-
}
|
|
575
|
-
if (old?.id === teamId)
|
|
576
|
-
return { ...old, current_location: data };
|
|
577
|
-
return old;
|
|
150
|
+
patchTeamLocation(queryClient, data);
|
|
151
|
+
patchMemberLocation(queryClient, data);
|
|
152
|
+
});
|
|
153
|
+
// schedule_locked lives in a Redis lock, not a row.
|
|
154
|
+
for (const event of ['schedule_complete', 'scheduling_error', 'schedule_adjustments']) {
|
|
155
|
+
on(event, () => {
|
|
156
|
+
queryClient.invalidateQueries({ queryKey: ['companies'] });
|
|
578
157
|
});
|
|
579
|
-
};
|
|
580
|
-
for (const [event, config] of Object.entries(SOCKET_EVENT_CONFIG)) {
|
|
581
|
-
if (event === 'location_update' && isBulkEvent(config)) {
|
|
582
|
-
const handler = (data) => {
|
|
583
|
-
if (!data || typeof data.latitude !== 'number') {
|
|
584
|
-
for (const key of config.invalidateKeys) {
|
|
585
|
-
queryClient.invalidateQueries({ queryKey: key });
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
patchTeamLocation(data);
|
|
590
|
-
}
|
|
591
|
-
patchMemberLocation(data);
|
|
592
|
-
};
|
|
593
|
-
this.socket.on(event, handler);
|
|
594
|
-
handlers.push({ event, handler });
|
|
595
|
-
continue;
|
|
596
|
-
}
|
|
597
|
-
if (event === 'schedule_complete' && isBulkEvent(config)) {
|
|
598
|
-
// Scheduling touches a known set of jobs; refetching every
|
|
599
|
-
// jobs query on each run is untenable. When the payload
|
|
600
|
-
// names the affected ids, bulk-fetch just those jobs and
|
|
601
|
-
// patch them into the cached lists in place.
|
|
602
|
-
const handler = async (data) => {
|
|
603
|
-
// Always cheap and always stale after a run: the new
|
|
604
|
-
// routes and the company's schedule_locked banner.
|
|
605
|
-
queryClient.invalidateQueries({ queryKey: ['routes'] });
|
|
606
|
-
queryClient.invalidateQueries({ queryKey: ['companies'] });
|
|
607
|
-
queryClient.invalidateQueries({ queryKey: ['/company/list'] });
|
|
608
|
-
const invalidateJobs = () => {
|
|
609
|
-
queryClient.invalidateQueries({ queryKey: ['jobs'] });
|
|
610
|
-
queryClient.invalidateQueries({ queryKey: ['infinite', 'jobs'] });
|
|
611
|
-
};
|
|
612
|
-
const rawIds = data?.job_ids;
|
|
613
|
-
if (!Array.isArray(rawIds)) {
|
|
614
|
-
// Legacy backend / unknown affected set
|
|
615
|
-
invalidateJobs();
|
|
616
|
-
return;
|
|
617
|
-
}
|
|
618
|
-
const jobIds = rawIds.filter((x) => typeof x === 'string');
|
|
619
|
-
if (jobIds.length === 0)
|
|
620
|
-
return;
|
|
621
|
-
try {
|
|
622
|
-
// One request for exactly the affected jobs
|
|
623
|
-
// (`unknown_ids` = "only these ids"), serialized
|
|
624
|
-
// server-side with this user's own permissions.
|
|
625
|
-
const raw = await client.fetchJobs({
|
|
626
|
-
unknown_ids: jobIds,
|
|
627
|
-
amt: jobIds.length,
|
|
628
|
-
});
|
|
629
|
-
const fresh = new Map(raw.filter((d) => d?.id).map((d) => [d.id, new Job(d)]));
|
|
630
|
-
const matching = queryClient
|
|
631
|
-
.getQueryCache()
|
|
632
|
-
.findAll({ predicate: (q) => matchesEntityKey(q, 'jobs') });
|
|
633
|
-
for (const query of matching) {
|
|
634
|
-
const queryKey = query.queryKey;
|
|
635
|
-
// Detail cache: ['jobs', jobId]
|
|
636
|
-
if (queryKey.length === 2 && queryKey[0] === 'jobs') {
|
|
637
|
-
const detailId = queryKey[1];
|
|
638
|
-
if (typeof detailId === 'string' && fresh.has(detailId)) {
|
|
639
|
-
queryClient.setQueryData(queryKey, fresh.get(detailId));
|
|
640
|
-
}
|
|
641
|
-
continue;
|
|
642
|
-
}
|
|
643
|
-
// Route-filtered lists: scheduling changes
|
|
644
|
-
// membership, and an in-place patch can't
|
|
645
|
-
// insert newly-assigned jobs — refetch those
|
|
646
|
-
// (only mounted route views hold them).
|
|
647
|
-
if (queryKey[0] === 'infinite' &&
|
|
648
|
-
typeof queryKey[5] === 'string' &&
|
|
649
|
-
queryKey[5].length > 0) {
|
|
650
|
-
queryClient.invalidateQueries({ queryKey, exact: true });
|
|
651
|
-
continue;
|
|
652
|
-
}
|
|
653
|
-
queryClient.setQueryData(queryKey, (old) => {
|
|
654
|
-
if (!old)
|
|
655
|
-
return old;
|
|
656
|
-
const patchList = (list) => {
|
|
657
|
-
let changed = false;
|
|
658
|
-
const next = list.map((x) => {
|
|
659
|
-
const replacement = x?.id ? fresh.get(x.id) : undefined;
|
|
660
|
-
if (!replacement)
|
|
661
|
-
return x;
|
|
662
|
-
changed = true;
|
|
663
|
-
return replacement;
|
|
664
|
-
});
|
|
665
|
-
return changed ? next : list;
|
|
666
|
-
};
|
|
667
|
-
if (old.pages && Array.isArray(old.pages)) {
|
|
668
|
-
return {
|
|
669
|
-
...old,
|
|
670
|
-
pages: old.pages.map((p) => Array.isArray(p) ? patchList(p) : p),
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
|
-
if (Array.isArray(old))
|
|
674
|
-
return patchList(old);
|
|
675
|
-
return old;
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
catch {
|
|
680
|
-
invalidateJobs();
|
|
681
|
-
}
|
|
682
|
-
};
|
|
683
|
-
this.socket.on(event, handler);
|
|
684
|
-
handlers.push({ event, handler });
|
|
685
|
-
continue;
|
|
686
|
-
}
|
|
687
|
-
if (isBulkEvent(config)) {
|
|
688
|
-
// ── Bulk: invalidate all specified keys ──────────────
|
|
689
|
-
const handler = () => {
|
|
690
|
-
for (const key of config.invalidateKeys) {
|
|
691
|
-
queryClient.invalidateQueries({ queryKey: key });
|
|
692
|
-
}
|
|
693
|
-
};
|
|
694
|
-
this.socket.on(event, handler);
|
|
695
|
-
handlers.push({ event, handler });
|
|
696
|
-
}
|
|
697
|
-
else {
|
|
698
|
-
// ── Targeted: surgical cache update ──────────────────
|
|
699
|
-
const { entityKey, action, fetchPath, idField, alsoInvalidate, relatedRefetch } = config;
|
|
700
|
-
const handler = async (data) => {
|
|
701
|
-
const id = data?.[idField ?? 'id'];
|
|
702
|
-
if (!id || typeof id !== 'string') {
|
|
703
|
-
invalidateEntity(entityKey);
|
|
704
|
-
alsoInvalidate?.forEach((k) => queryClient.invalidateQueries({ queryKey: k }));
|
|
705
|
-
return;
|
|
706
|
-
}
|
|
707
|
-
let fetchedItem;
|
|
708
|
-
// ─── CREATE / UPDATE / DELETE via shared _syncEntity ───
|
|
709
|
-
fetchedItem = await client._syncEntity(queryClient, {
|
|
710
|
-
action,
|
|
711
|
-
entityKey,
|
|
712
|
-
id,
|
|
713
|
-
fetchPath: fetchPath ? fetchPath(id) : undefined,
|
|
714
|
-
construct: config.construct,
|
|
715
|
-
matchesQuery: config.matchesQuery,
|
|
716
|
-
});
|
|
717
|
-
// Invalidate any additional keys (e.g. companies → /company/list)
|
|
718
|
-
alsoInvalidate?.forEach((k) => queryClient.invalidateQueries({ queryKey: k }));
|
|
719
|
-
if (relatedRefetch) {
|
|
720
|
-
for (const related of relatedRefetch) {
|
|
721
|
-
const relatedId = data?.[related.idField] ?? fetchedItem?.[related.idField];
|
|
722
|
-
if (!relatedId || typeof relatedId !== 'string')
|
|
723
|
-
continue;
|
|
724
|
-
await client._syncEntity(queryClient, {
|
|
725
|
-
action: 'update',
|
|
726
|
-
entityKey: related.entityKey,
|
|
727
|
-
id: relatedId,
|
|
728
|
-
fetchPath: related.fetchPath(relatedId),
|
|
729
|
-
construct: related.construct,
|
|
730
|
-
matchesQuery: related.matchesQuery,
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
};
|
|
735
|
-
this.socket.on(event, handler);
|
|
736
|
-
handlers.push({ event, handler });
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
// Auto join/leave company room
|
|
740
|
-
const joinCompanyRoom = () => {
|
|
741
|
-
if (this.companyId) {
|
|
742
|
-
this.socket.emit('join_company', { company_id: this.companyId });
|
|
743
|
-
}
|
|
744
|
-
else {
|
|
745
|
-
console.warn(`[socket-debug] joinCompanyRoom skipped — companyId is undefined`);
|
|
746
|
-
}
|
|
747
|
-
};
|
|
748
|
-
this.socket.on('connect', joinCompanyRoom);
|
|
749
|
-
handlers.push({ event: 'connect', handler: joinCompanyRoom });
|
|
750
|
-
// If already connected, join immediately
|
|
751
|
-
if (this.socket.connected && this.companyId) {
|
|
752
|
-
joinCompanyRoom();
|
|
753
158
|
}
|
|
159
|
+
on('new_message', () => {
|
|
160
|
+
queryClient.invalidateQueries({ queryKey: ['chats'] });
|
|
161
|
+
queryClient.invalidateQueries({ queryKey: ['messages'] });
|
|
162
|
+
});
|
|
163
|
+
on('connect', () => this.joinCompanyRoom());
|
|
164
|
+
if (this.socket.connected && this.companyId)
|
|
165
|
+
this.joinCompanyRoom();
|
|
754
166
|
this._socketInvalidationCleanup = () => {
|
|
755
|
-
for (const { event, handler } of handlers)
|
|
167
|
+
for (const { event, handler } of handlers)
|
|
756
168
|
this.socket.off(event, handler);
|
|
757
|
-
|
|
758
|
-
|
|
169
|
+
applier.dispose();
|
|
170
|
+
this._applier = null;
|
|
759
171
|
if (this.socket.connected && this.companyId) {
|
|
760
172
|
this.socket.emit('leave_company', { company_id: this.companyId });
|
|
761
173
|
}
|
|
762
174
|
this._queryClient = null;
|
|
763
175
|
};
|
|
764
176
|
}
|
|
177
|
+
/** Join the company room; resync if the ack version moved while away. */
|
|
178
|
+
joinCompanyRoom() {
|
|
179
|
+
const companyId = this.companyId;
|
|
180
|
+
if (!companyId || !this.socket.connected)
|
|
181
|
+
return;
|
|
182
|
+
this.socket.emit('join_company', { company_id: companyId }, (ack) => {
|
|
183
|
+
if (!ack || typeof ack.version !== 'number')
|
|
184
|
+
return;
|
|
185
|
+
const known = this._companyVersions.get(companyId);
|
|
186
|
+
this._companyVersions.set(companyId, ack.version);
|
|
187
|
+
if (known !== undefined && known !== ack.version) {
|
|
188
|
+
this._queryClient?.invalidateQueries();
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/** Fallback for mutations when the socket is down (else the echo syncs). */
|
|
193
|
+
syncAfterMutation(...types) {
|
|
194
|
+
const qc = this._queryClient;
|
|
195
|
+
if (!qc)
|
|
196
|
+
return;
|
|
197
|
+
if (!this.socket.connected)
|
|
198
|
+
fallbackInvalidate(qc, types);
|
|
199
|
+
}
|
|
765
200
|
/**
|
|
766
201
|
* Remove all invalidation listeners. Called on provider unmount.
|
|
767
202
|
*/
|
|
@@ -955,7 +390,7 @@ export class AvroQueryClient {
|
|
|
955
390
|
if (previousId && previousId !== companyId) {
|
|
956
391
|
this.socket.emit('leave_company', { company_id: previousId });
|
|
957
392
|
}
|
|
958
|
-
this.
|
|
393
|
+
this.joinCompanyRoom();
|
|
959
394
|
}
|
|
960
395
|
return this.config.authManager.setCompanyId(companyId);
|
|
961
396
|
}
|
|
@@ -1005,103 +440,6 @@ export class AvroQueryClient {
|
|
|
1005
440
|
peekQueryClient() {
|
|
1006
441
|
return this._queryClient;
|
|
1007
442
|
}
|
|
1008
|
-
async _syncEntity(queryClient, params) {
|
|
1009
|
-
const { action, entityKey, id, fetchPath, construct, matchesQuery } = params;
|
|
1010
|
-
const predicate = (q) => matchesEntityKey(q, entityKey);
|
|
1011
|
-
const invalidate = () => queryClient.invalidateQueries({ predicate });
|
|
1012
|
-
if (action === 'delete') {
|
|
1013
|
-
queryClient.removeQueries({ queryKey: [entityKey, id], exact: true });
|
|
1014
|
-
queryClient.setQueriesData({ predicate }, (old) => {
|
|
1015
|
-
if (!old)
|
|
1016
|
-
return old;
|
|
1017
|
-
if (old.pages && Array.isArray(old.pages)) {
|
|
1018
|
-
return {
|
|
1019
|
-
...old,
|
|
1020
|
-
pages: old.pages.map((p) => p.filter((x) => x?.id !== id)),
|
|
1021
|
-
};
|
|
1022
|
-
}
|
|
1023
|
-
if (Array.isArray(old)) {
|
|
1024
|
-
return old.filter((x) => x?.id !== id);
|
|
1025
|
-
}
|
|
1026
|
-
return old;
|
|
1027
|
-
});
|
|
1028
|
-
return undefined;
|
|
1029
|
-
}
|
|
1030
|
-
if (!fetchPath) {
|
|
1031
|
-
invalidate();
|
|
1032
|
-
return undefined;
|
|
1033
|
-
}
|
|
1034
|
-
try {
|
|
1035
|
-
// Direct fetch, not fetchQuery: fetchQuery would return an
|
|
1036
|
-
// already-in-flight (older) request for the same entity,
|
|
1037
|
-
// losing the newer state when socket events arrive in a
|
|
1038
|
-
// burst (e.g. auto-checkout followed by check-in)
|
|
1039
|
-
const raw = await this.get({ path: fetchPath });
|
|
1040
|
-
const item = construct ? construct(raw) : raw;
|
|
1041
|
-
queryClient.setQueryData([entityKey, id], item);
|
|
1042
|
-
const matchingQueries = queryClient.getQueryCache().findAll({ predicate });
|
|
1043
|
-
for (const query of matchingQueries) {
|
|
1044
|
-
const queryKey = query.queryKey;
|
|
1045
|
-
if (queryKey.length === 2 && queryKey[0] === entityKey)
|
|
1046
|
-
continue;
|
|
1047
|
-
const verdict = matchesQuery
|
|
1048
|
-
? matchesQuery(queryKey, item)
|
|
1049
|
-
: 'match';
|
|
1050
|
-
if (verdict === 'unknown') {
|
|
1051
|
-
queryClient.invalidateQueries({ queryKey, exact: true });
|
|
1052
|
-
continue;
|
|
1053
|
-
}
|
|
1054
|
-
if (action === 'create') {
|
|
1055
|
-
if (verdict === 'nomatch')
|
|
1056
|
-
continue;
|
|
1057
|
-
queryClient.setQueryData(queryKey, (old) => {
|
|
1058
|
-
if (!old)
|
|
1059
|
-
return old;
|
|
1060
|
-
if (old.pages && Array.isArray(old.pages)) {
|
|
1061
|
-
if (old.pages.some((p) => p.some((x) => x?.id === id)))
|
|
1062
|
-
return old;
|
|
1063
|
-
return {
|
|
1064
|
-
...old,
|
|
1065
|
-
pages: [[item, ...(old.pages[0] || [])], ...old.pages.slice(1)],
|
|
1066
|
-
};
|
|
1067
|
-
}
|
|
1068
|
-
if (Array.isArray(old)) {
|
|
1069
|
-
if (old.some((x) => x?.id === id))
|
|
1070
|
-
return old;
|
|
1071
|
-
return [...old, item];
|
|
1072
|
-
}
|
|
1073
|
-
return old;
|
|
1074
|
-
});
|
|
1075
|
-
}
|
|
1076
|
-
else {
|
|
1077
|
-
queryClient.setQueryData(queryKey, (old) => {
|
|
1078
|
-
if (!old)
|
|
1079
|
-
return old;
|
|
1080
|
-
if (old.pages && Array.isArray(old.pages)) {
|
|
1081
|
-
return {
|
|
1082
|
-
...old,
|
|
1083
|
-
pages: old.pages.map((page) => verdict === 'match'
|
|
1084
|
-
? page.map((x) => (x?.id === id ? item : x))
|
|
1085
|
-
: page.filter((x) => x?.id !== id)),
|
|
1086
|
-
};
|
|
1087
|
-
}
|
|
1088
|
-
if (Array.isArray(old)) {
|
|
1089
|
-
return verdict === 'match'
|
|
1090
|
-
? old.map((x) => (x?.id === id ? item : x))
|
|
1091
|
-
: old.filter((x) => x?.id !== id);
|
|
1092
|
-
}
|
|
1093
|
-
return old;
|
|
1094
|
-
});
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
return item;
|
|
1098
|
-
}
|
|
1099
|
-
catch (err) {
|
|
1100
|
-
console.error(`[socket-debug] _syncEntity fetch FAILED for ${entityKey}/${id}:`, err);
|
|
1101
|
-
invalidate();
|
|
1102
|
-
return undefined;
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
443
|
/**
|
|
1106
444
|
* Drop all local auth state without a server round-trip: stored
|
|
1107
445
|
* tokens, cached company, query cache, socket, and auth state.
|