@go-avro/avro-js 0.1.15 → 1.6.0
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/auth/AuthManager.d.ts +28 -0
- package/dist/auth/AuthManager.js +119 -43
- package/dist/client/QueryClient.d.ts +56 -1
- package/dist/client/QueryClient.js +260 -79
- package/dist/client/cache.d.ts +24 -0
- package/dist/client/cache.js +51 -0
- package/dist/client/core/fetch.js +58 -49
- package/dist/client/core/policy.d.ts +17 -0
- package/dist/client/core/policy.js +35 -0
- package/dist/client/core/xhr.js +16 -14
- package/dist/client/hooks/bills.js +10 -14
- package/dist/client/hooks/catalog_items.js +18 -8
- package/dist/client/hooks/chats.js +2 -8
- package/dist/client/hooks/companies.js +11 -5
- package/dist/client/hooks/events.js +36 -30
- package/dist/client/hooks/groups.js +11 -26
- package/dist/client/hooks/jobs.js +3 -8
- package/dist/client/hooks/labels.js +11 -26
- package/dist/client/hooks/messages.js +3 -8
- package/dist/client/hooks/months.js +28 -22
- package/dist/client/hooks/payouts.js +3 -8
- package/dist/client/hooks/prepayments.js +28 -22
- package/dist/client/hooks/proposal.js +1 -0
- package/dist/client/hooks/routes.js +8 -6
- package/dist/client/hooks/sessions.js +11 -27
- package/dist/client/hooks/skills.js +6 -4
- package/dist/client/hooks/teams.js +6 -4
- package/dist/client/hooks/timecards.js +7 -7
- package/dist/client/hooks/waivers.js +6 -4
- package/dist/client/persist.js +21 -2
- package/dist/client/realtime.d.ts +12 -0
- package/dist/client/realtime.js +160 -46
- package/dist/types/api/Company.d.ts +2 -0
- package/dist/types/api/LineItem.d.ts +4 -0
- package/dist/types/api/LineItem.js +8 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +7 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query';
|
|
2
2
|
import { AvroQueryClient } from '../QueryClient.js';
|
|
3
3
|
import { keys, isListKey } from '../realtime.js';
|
|
4
|
+
import { removeOptimisticRow, seedDetails } from '../cache.js';
|
|
4
5
|
import { Session } from '../../types/api.js';
|
|
5
6
|
const NS = 'sessions';
|
|
6
7
|
function listFilters(body) {
|
|
@@ -46,16 +47,9 @@ AvroQueryClient.prototype.useGetSessions = function (body = {}) {
|
|
|
46
47
|
return undefined;
|
|
47
48
|
return allPages.flat().length;
|
|
48
49
|
},
|
|
49
|
-
queryFn: ({ pageParam = 0 }) => this.fetchSessions({ ...body, amt, offset: pageParam }).then((sessions) => sessions.map((s) => new Session(s))),
|
|
50
|
+
queryFn: ({ pageParam = 0 }) => this.fetchSessions({ ...body, amt, offset: pageParam }).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
|
|
50
51
|
enabled: Boolean(this.companyId),
|
|
51
52
|
});
|
|
52
|
-
if (result.data) {
|
|
53
|
-
result.data.pages.forEach((page) => {
|
|
54
|
-
page.forEach((session) => {
|
|
55
|
-
queryClient.setQueryData(keys.detail(NS, session.id), session);
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
53
|
return result;
|
|
60
54
|
};
|
|
61
55
|
/** The caller's own open sessions (any company). */
|
|
@@ -63,14 +57,9 @@ AvroQueryClient.prototype.useGetUserSessions = function () {
|
|
|
63
57
|
const queryClient = this.getQueryClient();
|
|
64
58
|
const result = useQuery({
|
|
65
59
|
queryKey: keys.list(NS, 'mine', { active: true }),
|
|
66
|
-
queryFn: () => this.get({ path: '/session' }).then((sessions) => (Array.isArray(sessions) ? sessions : []).map((s) => new Session(s))),
|
|
60
|
+
queryFn: () => this.get({ path: '/session' }).then((sessions) => seedDetails(queryClient, NS, (Array.isArray(sessions) ? sessions : []).map((s) => new Session(s)))),
|
|
67
61
|
enabled: Boolean(this.companyId),
|
|
68
62
|
});
|
|
69
|
-
if (result.data) {
|
|
70
|
-
result.data.forEach((session) => {
|
|
71
|
-
queryClient.setQueryData(keys.detail(NS, session.id), session);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
63
|
return result;
|
|
75
64
|
};
|
|
76
65
|
/** Exactly the given sessions, in one request. */
|
|
@@ -80,14 +69,9 @@ AvroQueryClient.prototype.useGetCompanySessionsByIds = function (sessionIds, par
|
|
|
80
69
|
const sortedIds = [...sessionIds].sort();
|
|
81
70
|
const result = useQuery({
|
|
82
71
|
queryKey: keys.list(NS, companyId, listFilters({ unknown_ids: sortedIds })),
|
|
83
|
-
queryFn: () => this.fetchSessions({ unknown_ids: sortedIds, amt: sortedIds.length }).then((sessions) => sessions.map((s) => new Session(s))),
|
|
72
|
+
queryFn: () => this.fetchSessions({ unknown_ids: sortedIds, amt: sortedIds.length }).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
|
|
84
73
|
enabled: Boolean(companyId) && sortedIds.length > 0 && (params.enabled ?? true),
|
|
85
74
|
});
|
|
86
|
-
if (result.data) {
|
|
87
|
-
result.data.forEach((session) => {
|
|
88
|
-
queryClient.setQueryData(keys.detail(NS, session.id), session);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
75
|
return result;
|
|
92
76
|
};
|
|
93
77
|
/** Currently-open sessions across the company, in one request. */
|
|
@@ -96,14 +80,9 @@ AvroQueryClient.prototype.useGetActiveCompanySessions = function (params = {}) {
|
|
|
96
80
|
const companyId = params.companyId ?? this.companyId;
|
|
97
81
|
const result = useQuery({
|
|
98
82
|
queryKey: keys.list(NS, companyId, listFilters({ active: true })),
|
|
99
|
-
queryFn: () => this.fetchSessions({ active: true, amt: 500 }).then((sessions) => sessions.map((s) => new Session(s))),
|
|
83
|
+
queryFn: () => this.fetchSessions({ active: true, amt: 500 }).then((sessions) => seedDetails(queryClient, NS, sessions.map((s) => new Session(s)))),
|
|
100
84
|
enabled: Boolean(companyId) && (params.enabled ?? true),
|
|
101
85
|
});
|
|
102
|
-
if (result.data) {
|
|
103
|
-
result.data.forEach((session) => {
|
|
104
|
-
queryClient.setQueryData(keys.detail(NS, session.id), session);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
86
|
return result;
|
|
108
87
|
};
|
|
109
88
|
// ─── Mutations ──────────────────────────────────────────────────────
|
|
@@ -129,7 +108,12 @@ AvroQueryClient.prototype.useCreateUserSession = function () {
|
|
|
129
108
|
company_id: this.companyId,
|
|
130
109
|
});
|
|
131
110
|
patchSessionLists(queryClient, (items) => [optimistic, ...items]);
|
|
132
|
-
return { previous };
|
|
111
|
+
return { previous, tempId: optimistic.id };
|
|
112
|
+
},
|
|
113
|
+
onSuccess: (_data, _vars, context) => {
|
|
114
|
+
// The echoed change (or the fallback refetch) brings the real row.
|
|
115
|
+
if (context?.tempId)
|
|
116
|
+
removeOptimisticRow(queryClient, NS, context.tempId);
|
|
133
117
|
},
|
|
134
118
|
onError: (_err, _vars, context) => {
|
|
135
119
|
if (context?.previous)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
2
2
|
import { AvroQueryClient } from '../QueryClient.js';
|
|
3
|
+
import { restoreNs, snapshotNs } from '../cache.js';
|
|
3
4
|
import { keys } from '../realtime.js';
|
|
4
5
|
AvroQueryClient.prototype.useCreateSkill = function () {
|
|
5
6
|
const queryClient = this.getQueryClient();
|
|
@@ -44,6 +45,7 @@ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
|
|
|
44
45
|
const skills = pages.flat();
|
|
45
46
|
return skills;
|
|
46
47
|
},
|
|
48
|
+
enabled: Boolean(this.companyId),
|
|
47
49
|
});
|
|
48
50
|
};
|
|
49
51
|
AvroQueryClient.prototype.useUpdateSkill = function () {
|
|
@@ -59,7 +61,7 @@ AvroQueryClient.prototype.useUpdateSkill = function () {
|
|
|
59
61
|
onMutate: async ({ skillId, updates }) => {
|
|
60
62
|
await queryClient.cancelQueries({ queryKey: ['skills'] });
|
|
61
63
|
await queryClient.cancelQueries({ queryKey: ['skills', skillId] });
|
|
62
|
-
const previousSkills = queryClient
|
|
64
|
+
const previousSkills = snapshotNs(queryClient, 'skills');
|
|
63
65
|
const previousSkill = queryClient.getQueryData(['skills', skillId]);
|
|
64
66
|
queryClient.setQueryData(['skills', skillId], (oldData) => {
|
|
65
67
|
if (!oldData)
|
|
@@ -86,7 +88,7 @@ AvroQueryClient.prototype.useUpdateSkill = function () {
|
|
|
86
88
|
onError: (_err, variables, context) => {
|
|
87
89
|
const { skillId } = variables;
|
|
88
90
|
if (context?.previousSkills) {
|
|
89
|
-
queryClient
|
|
91
|
+
restoreNs(queryClient, context.previousSkills);
|
|
90
92
|
}
|
|
91
93
|
if (context?.previousSkill) {
|
|
92
94
|
queryClient.setQueryData(['skills', skillId], context.previousSkill);
|
|
@@ -102,7 +104,7 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
|
|
|
102
104
|
},
|
|
103
105
|
onMutate: async ({ skillId }) => {
|
|
104
106
|
await queryClient.cancelQueries({ queryKey: ['skills'] });
|
|
105
|
-
const previousSkills = queryClient
|
|
107
|
+
const previousSkills = snapshotNs(queryClient, 'skills');
|
|
106
108
|
queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
|
|
107
109
|
if (!oldData)
|
|
108
110
|
return oldData;
|
|
@@ -122,7 +124,7 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
|
|
|
122
124
|
},
|
|
123
125
|
onError: (_err, variables, context) => {
|
|
124
126
|
if (context?.previousSkills) {
|
|
125
|
-
queryClient
|
|
127
|
+
restoreNs(queryClient, context.previousSkills);
|
|
126
128
|
}
|
|
127
129
|
},
|
|
128
130
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
2
2
|
import { AvroQueryClient } from '../QueryClient.js';
|
|
3
|
+
import { restoreNs, snapshotNs } from '../cache.js';
|
|
3
4
|
import { keys } from '../realtime.js';
|
|
4
5
|
AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
|
|
5
6
|
return useQuery({
|
|
@@ -29,6 +30,7 @@ AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
|
|
|
29
30
|
const teams = pages.flat();
|
|
30
31
|
return teams;
|
|
31
32
|
},
|
|
33
|
+
enabled: Boolean(this.companyId),
|
|
32
34
|
});
|
|
33
35
|
};
|
|
34
36
|
AvroQueryClient.prototype.useCreateTeam = function () {
|
|
@@ -59,7 +61,7 @@ AvroQueryClient.prototype.useUpdateTeam = function () {
|
|
|
59
61
|
onMutate: async ({ teamId, teamData }) => {
|
|
60
62
|
await queryClient.cancelQueries({ queryKey: ['teams'] });
|
|
61
63
|
await queryClient.cancelQueries({ queryKey: ['teams', teamId] });
|
|
62
|
-
const previousTeams = queryClient
|
|
64
|
+
const previousTeams = snapshotNs(queryClient, 'teams');
|
|
63
65
|
const previousTeam = queryClient.getQueryData(['teams', teamId]);
|
|
64
66
|
queryClient.setQueryData(['teams', teamId], (oldData) => {
|
|
65
67
|
if (!oldData)
|
|
@@ -86,7 +88,7 @@ AvroQueryClient.prototype.useUpdateTeam = function () {
|
|
|
86
88
|
onError: (_err, variables, context) => {
|
|
87
89
|
const { teamId } = variables;
|
|
88
90
|
if (context?.previousTeams) {
|
|
89
|
-
queryClient
|
|
91
|
+
restoreNs(queryClient, context.previousTeams);
|
|
90
92
|
}
|
|
91
93
|
if (context?.previousTeam) {
|
|
92
94
|
queryClient.setQueryData(['teams', teamId], context.previousTeam);
|
|
@@ -103,7 +105,7 @@ AvroQueryClient.prototype.useDeleteTeam = function () {
|
|
|
103
105
|
onMutate: async ({ teamId }) => {
|
|
104
106
|
await queryClient.cancelQueries({ queryKey: ['teams'] });
|
|
105
107
|
await queryClient.cancelQueries({ queryKey: ['teams', teamId] });
|
|
106
|
-
const previousRoutes = queryClient
|
|
108
|
+
const previousRoutes = snapshotNs(queryClient, 'teams');
|
|
107
109
|
const previousRoute = queryClient.getQueryData(['teams', teamId]);
|
|
108
110
|
queryClient.setQueryData(['teams', teamId], undefined);
|
|
109
111
|
queryClient.setQueriesData({ queryKey: ['teams'] }, (oldData) => {
|
|
@@ -126,7 +128,7 @@ AvroQueryClient.prototype.useDeleteTeam = function () {
|
|
|
126
128
|
onError: (_err, variables, context) => {
|
|
127
129
|
const { teamId } = variables;
|
|
128
130
|
if (context?.previousRoutes) {
|
|
129
|
-
queryClient
|
|
131
|
+
restoreNs(queryClient, context.previousRoutes);
|
|
130
132
|
}
|
|
131
133
|
if (context?.previousRoute) {
|
|
132
134
|
queryClient.setQueryData(['teams', teamId], context.previousRoute);
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
2
2
|
import { AvroQueryClient } from '../QueryClient.js';
|
|
3
|
+
import { keys } from '../realtime.js';
|
|
3
4
|
AvroQueryClient.prototype.useGetCompanyTimecards = function (params = {}) {
|
|
4
5
|
const companyId = params.companyId ?? this.companyId;
|
|
5
6
|
return useQuery({
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
params.
|
|
10
|
-
params.
|
|
11
|
-
|
|
12
|
-
],
|
|
7
|
+
// keys.list so realtime `timecard` changes patch it (matchTimecards).
|
|
8
|
+
queryKey: keys.list('timecards', companyId, {
|
|
9
|
+
periodStart: params.periodStart ?? null,
|
|
10
|
+
userCompanyId: params.userCompanyId ?? null,
|
|
11
|
+
status: params.status ?? null,
|
|
12
|
+
}),
|
|
13
13
|
queryFn: async () => {
|
|
14
14
|
const search = new URLSearchParams();
|
|
15
15
|
if (typeof params.periodStart === 'number') {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
2
2
|
import { AvroQueryClient } from '../QueryClient.js';
|
|
3
|
+
import { restoreNs, snapshotNs } from '../cache.js';
|
|
3
4
|
import { keys } from '../realtime.js';
|
|
4
5
|
AvroQueryClient.prototype.useCreateWaiver = function () {
|
|
5
6
|
const queryClient = this.getQueryClient();
|
|
@@ -47,6 +48,7 @@ AvroQueryClient.prototype.useGetWaivers = function (body, total, onProgress) {
|
|
|
47
48
|
const waivers = pages.flat();
|
|
48
49
|
return waivers;
|
|
49
50
|
},
|
|
51
|
+
enabled: Boolean(this.companyId),
|
|
50
52
|
});
|
|
51
53
|
};
|
|
52
54
|
AvroQueryClient.prototype.useUpdateWaiver = function () {
|
|
@@ -64,7 +66,7 @@ AvroQueryClient.prototype.useUpdateWaiver = function () {
|
|
|
64
66
|
await queryClient.cancelQueries({
|
|
65
67
|
queryKey: ['waivers', waiverId],
|
|
66
68
|
});
|
|
67
|
-
const previousWaivers = queryClient
|
|
69
|
+
const previousWaivers = snapshotNs(queryClient, 'waivers');
|
|
68
70
|
const previousWaiver = queryClient.getQueryData(['waivers', waiverId]);
|
|
69
71
|
queryClient.setQueryData(['waivers', waiverId], (oldData) => {
|
|
70
72
|
if (!oldData)
|
|
@@ -91,7 +93,7 @@ AvroQueryClient.prototype.useUpdateWaiver = function () {
|
|
|
91
93
|
onError: (_err, variables, context) => {
|
|
92
94
|
const { waiverId } = variables;
|
|
93
95
|
if (context?.previousWaivers) {
|
|
94
|
-
queryClient
|
|
96
|
+
restoreNs(queryClient, context.previousWaivers);
|
|
95
97
|
}
|
|
96
98
|
if (context?.previousWaiver) {
|
|
97
99
|
queryClient.setQueryData(['waivers', waiverId], context.previousWaiver);
|
|
@@ -109,7 +111,7 @@ AvroQueryClient.prototype.useDeleteWaiver = function () {
|
|
|
109
111
|
},
|
|
110
112
|
onMutate: async ({ waiverId }) => {
|
|
111
113
|
await queryClient.cancelQueries({ queryKey: ['waivers'] });
|
|
112
|
-
const previousWaivers = queryClient
|
|
114
|
+
const previousWaivers = snapshotNs(queryClient, 'waivers');
|
|
113
115
|
queryClient.setQueriesData({ queryKey: ['waivers'] }, (oldData) => {
|
|
114
116
|
if (!oldData)
|
|
115
117
|
return oldData;
|
|
@@ -129,7 +131,7 @@ AvroQueryClient.prototype.useDeleteWaiver = function () {
|
|
|
129
131
|
},
|
|
130
132
|
onError: (_err, variables, context) => {
|
|
131
133
|
if (context?.previousWaivers) {
|
|
132
|
-
queryClient
|
|
134
|
+
restoreNs(queryClient, context.previousWaivers);
|
|
133
135
|
}
|
|
134
136
|
},
|
|
135
137
|
});
|
package/dist/client/persist.js
CHANGED
|
@@ -71,9 +71,28 @@ export const PERSISTED_NAMESPACES = [
|
|
|
71
71
|
'health',
|
|
72
72
|
];
|
|
73
73
|
/** JSON loses class instances; restore the ones hooks hand out. */
|
|
74
|
+
/** Revive list (`T[]`), paged (`{pages}`) and detail (`{id,…}`) data alike. */
|
|
75
|
+
function reviver(construct) {
|
|
76
|
+
return (data) => {
|
|
77
|
+
if (Array.isArray(data))
|
|
78
|
+
return data.map((raw) => construct(raw));
|
|
79
|
+
if (data && typeof data === 'object') {
|
|
80
|
+
const pages = data.pages;
|
|
81
|
+
if (Array.isArray(pages)) {
|
|
82
|
+
return {
|
|
83
|
+
...data,
|
|
84
|
+
pages: pages.map((p) => Array.isArray(p) ? p.map((raw) => construct(raw)) : p),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (typeof data.id === 'string')
|
|
88
|
+
return construct(data);
|
|
89
|
+
}
|
|
90
|
+
return data;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
74
93
|
const REVIVERS = {
|
|
75
|
-
jobs: (
|
|
76
|
-
routes: (
|
|
94
|
+
jobs: reviver(constructJob),
|
|
95
|
+
routes: reviver((raw) => new Route(raw)),
|
|
77
96
|
};
|
|
78
97
|
export class QueryPersister {
|
|
79
98
|
constructor(qc, opts) {
|
|
@@ -51,7 +51,19 @@ export declare class ChangeApplier {
|
|
|
51
51
|
private qc;
|
|
52
52
|
private pending;
|
|
53
53
|
private timer;
|
|
54
|
+
/**
|
|
55
|
+
* Latest change sequence per `type:id`. Every change (enqueue, inline
|
|
56
|
+
* data, delete) takes a fresh number; a fetch only applies results for
|
|
57
|
+
* ids whose number is unchanged since it started, so an in-flight
|
|
58
|
+
* fetch can't resurrect a deleted entity or overwrite a newer one.
|
|
59
|
+
*/
|
|
60
|
+
private seq;
|
|
61
|
+
private latest;
|
|
62
|
+
private inflight;
|
|
54
63
|
constructor(client: AvroQueryClient, qc: QueryClient);
|
|
64
|
+
private mark;
|
|
65
|
+
/** Settle a change applied inline: only an in-flight fetch needs the mark. */
|
|
66
|
+
private markApplied;
|
|
55
67
|
apply(payload: ChangesPayload): void;
|
|
56
68
|
private enqueue;
|
|
57
69
|
flush(): Promise<void>;
|
package/dist/client/realtime.js
CHANGED
|
@@ -27,12 +27,17 @@ function searchAware(filters) {
|
|
|
27
27
|
const q = filters.query;
|
|
28
28
|
return typeof q === 'string' && q.length > 0 ? 'unknown' : 'match';
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Backend id filters: non-empty `unknown_ids` means "only these ids";
|
|
32
|
+
* otherwise `known_ids` means "exclude these ids".
|
|
33
|
+
*/
|
|
30
34
|
function withIds(filters, item) {
|
|
31
35
|
const known = filters.known_ids;
|
|
32
36
|
const unknown = filters.unknown_ids;
|
|
33
|
-
if (Array.isArray(
|
|
34
|
-
return 'nomatch';
|
|
35
|
-
|
|
37
|
+
if (Array.isArray(unknown) && unknown.length > 0) {
|
|
38
|
+
return unknown.includes(item.id) ? null : 'nomatch';
|
|
39
|
+
}
|
|
40
|
+
if (Array.isArray(known) && known.length > 0 && known.includes(item.id))
|
|
36
41
|
return 'nomatch';
|
|
37
42
|
return null;
|
|
38
43
|
}
|
|
@@ -67,13 +72,18 @@ function isPaidStatus(status) {
|
|
|
67
72
|
function isBilledStatus(status) {
|
|
68
73
|
return status === 'BILLED' || status === 'EXTERNALLY_BILLED';
|
|
69
74
|
}
|
|
70
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Shared by events / months / prepayments (line-item lists). Mirrors the
|
|
77
|
+
* backend: the `unbilled`/`billed`/`paid` flags are OR-combined status
|
|
78
|
+
* buckets defaulting to `true`/`false`/`false`; "unbilled" is `CREATED`
|
|
79
|
+
* only, and in-flight statuses (`PROCESSING`, `PENDING_AUTOBILL`) are in
|
|
80
|
+
* no bucket, so no list returns them.
|
|
81
|
+
*/
|
|
71
82
|
function matchLineItems(filters, item) {
|
|
72
83
|
const byIds = withIds(filters, item);
|
|
73
84
|
if (byIds)
|
|
74
85
|
return byIds;
|
|
75
|
-
|
|
76
|
-
if (parentKey && filters.jobId && item[parentKey] !== filters.jobId)
|
|
86
|
+
if (typeof filters.jobId === 'string' && filters.jobId && item.job_id !== filters.jobId)
|
|
77
87
|
return 'nomatch';
|
|
78
88
|
if (typeof filters.taskId === 'string' && filters.taskId && item.task_id !== filters.taskId)
|
|
79
89
|
return 'nomatch';
|
|
@@ -85,26 +95,26 @@ function matchLineItems(filters, item) {
|
|
|
85
95
|
return 'nomatch';
|
|
86
96
|
const status = item.status;
|
|
87
97
|
if (status !== undefined) {
|
|
88
|
-
const
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (filters.unbilled === false && unbilled)
|
|
98
|
+
const wantUnbilled = filters.unbilled === undefined ? true : filters.unbilled === true;
|
|
99
|
+
const wantBilled = filters.billed === true;
|
|
100
|
+
const wantPaid = filters.paid === true;
|
|
101
|
+
const inBucket = (isPaidStatus(status) && wantPaid) ||
|
|
102
|
+
(isBilledStatus(status) && wantBilled) ||
|
|
103
|
+
(status === 'CREATED' && wantUnbilled);
|
|
104
|
+
if (!inBucket)
|
|
96
105
|
return 'nomatch';
|
|
97
106
|
}
|
|
98
107
|
return searchAware(filters);
|
|
99
108
|
}
|
|
109
|
+
/** Backend: `paid: true` = fully-paid only; `paid: false` = no paid filter. */
|
|
100
110
|
function matchBills(filters, item) {
|
|
101
111
|
const byIds = withIds(filters, item);
|
|
102
112
|
if (byIds)
|
|
103
113
|
return byIds;
|
|
104
114
|
const status = item.status;
|
|
105
|
-
if (
|
|
115
|
+
if (filters.paid === true && status !== undefined) {
|
|
106
116
|
const paid = status === 'PAID' || status === 'MANUALLY_PAID';
|
|
107
|
-
if (
|
|
117
|
+
if (!paid)
|
|
108
118
|
return 'nomatch';
|
|
109
119
|
}
|
|
110
120
|
const hard = ['created_from', 'created_to', 'paid_from', 'paid_to', 'amount_min', 'amount_max'];
|
|
@@ -164,7 +174,12 @@ function matchPayouts(filters, item) {
|
|
|
164
174
|
return 'unknown';
|
|
165
175
|
return searchAware(filters);
|
|
166
176
|
}
|
|
167
|
-
const bulk = (method) => (client, ids) => client[method]({ unknown_ids: ids, amt: ids.length });
|
|
177
|
+
const bulk = (method, extra = {}) => (client, ids) => client[method]({ ...extra, unknown_ids: ids, amt: ids.length });
|
|
178
|
+
/** Line-item list endpoints default to unbilled-only: ask for every bucket. */
|
|
179
|
+
const ALL_LINE_ITEM_STATUSES = { unbilled: true, billed: true, paid: true };
|
|
180
|
+
/** Single-entity GET (404/403 is treated as gone by the applier). */
|
|
181
|
+
const getOne = (path) => (client, id) => client.get({ path: path(id) });
|
|
182
|
+
const COMPANY_EMBEDS = [['companies'], ['/company/list']];
|
|
168
183
|
export const REGISTRY = {
|
|
169
184
|
job: {
|
|
170
185
|
ns: 'jobs',
|
|
@@ -175,7 +190,7 @@ export const REGISTRY = {
|
|
|
175
190
|
event: {
|
|
176
191
|
ns: 'events',
|
|
177
192
|
construct: constructEvent,
|
|
178
|
-
fetchMany: bulk('fetchEvents'),
|
|
193
|
+
fetchMany: bulk('fetchEvents', ALL_LINE_ITEM_STATUSES),
|
|
179
194
|
listMatch: matchLineItems,
|
|
180
195
|
},
|
|
181
196
|
route: {
|
|
@@ -185,13 +200,31 @@ export const REGISTRY = {
|
|
|
185
200
|
listMatch: searchAware,
|
|
186
201
|
},
|
|
187
202
|
team: { ns: 'teams', fetchMany: bulk('fetchTeams'), listMatch: searchAware },
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
203
|
+
// Labels, groups, skills and catalog items are also embedded in the
|
|
204
|
+
// company payload, so their changes must refresh it too.
|
|
205
|
+
group: {
|
|
206
|
+
ns: 'groups',
|
|
207
|
+
fetchOne: getOne((id) => `/group/${id}`),
|
|
208
|
+
listMatch: searchAware,
|
|
209
|
+
alsoInvalidate: COMPANY_EMBEDS,
|
|
210
|
+
},
|
|
211
|
+
label: {
|
|
212
|
+
ns: 'labels',
|
|
213
|
+
fetchOne: getOne((id) => `/label/${id}`),
|
|
214
|
+
listMatch: searchAware,
|
|
215
|
+
alsoInvalidate: COMPANY_EMBEDS,
|
|
216
|
+
},
|
|
217
|
+
skill: {
|
|
218
|
+
ns: 'skills',
|
|
219
|
+
fetchMany: bulk('fetchSkills'),
|
|
220
|
+
listMatch: searchAware,
|
|
221
|
+
alsoInvalidate: COMPANY_EMBEDS,
|
|
222
|
+
},
|
|
223
|
+
// No single-item GET exists: catalog items only live on the company.
|
|
191
224
|
catalog_item: {
|
|
192
225
|
ns: 'catalog_items',
|
|
193
|
-
|
|
194
|
-
|
|
226
|
+
invalidateOnly: true,
|
|
227
|
+
alsoInvalidate: COMPANY_EMBEDS,
|
|
195
228
|
},
|
|
196
229
|
proposal: {
|
|
197
230
|
ns: 'proposals',
|
|
@@ -203,10 +236,14 @@ export const REGISTRY = {
|
|
|
203
236
|
ns: 'proposal_terms',
|
|
204
237
|
fetchOne: (client, id) => client.get({ path: `/proposal_terms/${id}` }),
|
|
205
238
|
},
|
|
206
|
-
month: {
|
|
239
|
+
month: {
|
|
240
|
+
ns: 'months',
|
|
241
|
+
fetchMany: bulk('fetchMonths', ALL_LINE_ITEM_STATUSES),
|
|
242
|
+
listMatch: matchLineItems,
|
|
243
|
+
},
|
|
207
244
|
prepayment: {
|
|
208
245
|
ns: 'prepayments',
|
|
209
|
-
fetchMany: bulk('fetchPrepayments'),
|
|
246
|
+
fetchMany: bulk('fetchPrepayments', ALL_LINE_ITEM_STATUSES),
|
|
210
247
|
listMatch: matchLineItems,
|
|
211
248
|
},
|
|
212
249
|
bill: {
|
|
@@ -300,12 +337,6 @@ export function upsertEntity(qc, spec, item) {
|
|
|
300
337
|
return mapList(old, (items) => items.map((x) => (x?.id === id ? item : x)));
|
|
301
338
|
return insertInto(old, item);
|
|
302
339
|
});
|
|
303
|
-
// A fresh insert into a server-ordered paged list: settle order.
|
|
304
|
-
if (verdict === 'match' &&
|
|
305
|
-
key[1] === 'infinite' &&
|
|
306
|
-
!listHas(query.state.data, id)) {
|
|
307
|
-
qc.invalidateQueries({ queryKey: key, exact: true });
|
|
308
|
-
}
|
|
309
340
|
}
|
|
310
341
|
}
|
|
311
342
|
export function matchesList(type, key, item) {
|
|
@@ -323,12 +354,43 @@ export function invalidateType(qc, spec) {
|
|
|
323
354
|
// ─── Applying batches ───────────────────────────────────────────────
|
|
324
355
|
const FETCH_COALESCE_MS = 50;
|
|
325
356
|
const FETCH_CHUNK = 100;
|
|
357
|
+
/** A fetch failure that means "this entity is gone or no longer visible". */
|
|
358
|
+
function isGoneError(err) {
|
|
359
|
+
const status = err?.status;
|
|
360
|
+
return status === 404 || status === 403;
|
|
361
|
+
}
|
|
362
|
+
/** Drop an id from every list in `ns`; mark (don't drop) its detail stale. */
|
|
363
|
+
function dropFromLists(qc, ns, id) {
|
|
364
|
+
qc.setQueriesData({ predicate: (q) => q.queryKey[0] === ns && isListKey(q.queryKey) }, (old) => (old ? mapList(old, (items) => items.filter((x) => x?.id !== id)) : old));
|
|
365
|
+
qc.invalidateQueries({ queryKey: keys.detail(ns, id), exact: true });
|
|
366
|
+
}
|
|
326
367
|
export class ChangeApplier {
|
|
327
368
|
constructor(client, qc) {
|
|
328
369
|
this.client = client;
|
|
329
370
|
this.qc = qc;
|
|
330
371
|
this.pending = new Map();
|
|
331
372
|
this.timer = null;
|
|
373
|
+
/**
|
|
374
|
+
* Latest change sequence per `type:id`. Every change (enqueue, inline
|
|
375
|
+
* data, delete) takes a fresh number; a fetch only applies results for
|
|
376
|
+
* ids whose number is unchanged since it started, so an in-flight
|
|
377
|
+
* fetch can't resurrect a deleted entity or overwrite a newer one.
|
|
378
|
+
*/
|
|
379
|
+
this.seq = 0;
|
|
380
|
+
this.latest = new Map();
|
|
381
|
+
this.inflight = new Map();
|
|
382
|
+
}
|
|
383
|
+
mark(type, id) {
|
|
384
|
+
this.latest.set(`${type}:${id}`, ++this.seq);
|
|
385
|
+
}
|
|
386
|
+
/** Settle a change applied inline: only an in-flight fetch needs the mark. */
|
|
387
|
+
markApplied(type, id) {
|
|
388
|
+
this.pending.get(type)?.delete(id);
|
|
389
|
+
const key = `${type}:${id}`;
|
|
390
|
+
if ((this.inflight.get(key) ?? 0) > 0)
|
|
391
|
+
this.mark(type, id);
|
|
392
|
+
else
|
|
393
|
+
this.latest.delete(key);
|
|
332
394
|
}
|
|
333
395
|
apply(payload) {
|
|
334
396
|
const entries = Array.isArray(payload?.changes) ? payload.changes : [];
|
|
@@ -346,11 +408,13 @@ export class ChangeApplier {
|
|
|
346
408
|
if (!entry.id)
|
|
347
409
|
continue;
|
|
348
410
|
if (entry.op === 'delete') {
|
|
411
|
+
this.markApplied(entry.type, entry.id);
|
|
349
412
|
removeEntity(this.qc, spec.ns, entry.id);
|
|
350
413
|
spec.alsoInvalidate?.forEach((k) => this.qc.invalidateQueries({ queryKey: [...k] }));
|
|
351
414
|
continue;
|
|
352
415
|
}
|
|
353
416
|
if (entry.data !== undefined && entry.data !== null) {
|
|
417
|
+
this.markApplied(entry.type, entry.id);
|
|
354
418
|
upsertEntity(this.qc, spec, spec.construct ? spec.construct(entry.data) : entry.data);
|
|
355
419
|
spec.alsoInvalidate?.forEach((k) => this.qc.invalidateQueries({ queryKey: [...k] }));
|
|
356
420
|
continue;
|
|
@@ -359,6 +423,7 @@ export class ChangeApplier {
|
|
|
359
423
|
}
|
|
360
424
|
}
|
|
361
425
|
enqueue(type, id) {
|
|
426
|
+
this.mark(type, id);
|
|
362
427
|
let set = this.pending.get(type);
|
|
363
428
|
if (!set) {
|
|
364
429
|
set = new Set();
|
|
@@ -375,7 +440,9 @@ export class ChangeApplier {
|
|
|
375
440
|
async flush() {
|
|
376
441
|
const batches = Array.from(this.pending.entries());
|
|
377
442
|
this.pending.clear();
|
|
378
|
-
await Promise.all(batches
|
|
443
|
+
await Promise.all(batches
|
|
444
|
+
.filter(([, ids]) => ids.size > 0)
|
|
445
|
+
.map(([type, ids]) => this.fetchAndApply(type, [...ids])));
|
|
379
446
|
}
|
|
380
447
|
/** True when some mounted hook is currently observing this namespace. */
|
|
381
448
|
isObserved(ns) {
|
|
@@ -395,40 +462,85 @@ export class ChangeApplier {
|
|
|
395
462
|
invalidateType(this.qc, spec);
|
|
396
463
|
return;
|
|
397
464
|
}
|
|
465
|
+
const started = new Map(ids.map((id) => [id, this.latest.get(`${type}:${id}`)]));
|
|
466
|
+
// Only ids with no newer change since this fetch began may be applied.
|
|
467
|
+
const current = (id) => this.latest.get(`${type}:${id}`) === started.get(id);
|
|
468
|
+
const settle = (id) => {
|
|
469
|
+
const key = `${type}:${id}`;
|
|
470
|
+
const left = (this.inflight.get(key) ?? 1) - 1;
|
|
471
|
+
if (left > 0) {
|
|
472
|
+
this.inflight.set(key, left);
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
this.inflight.delete(key);
|
|
476
|
+
if (current(id))
|
|
477
|
+
this.latest.delete(key);
|
|
478
|
+
};
|
|
479
|
+
ids.forEach((id) => {
|
|
480
|
+
const key = `${type}:${id}`;
|
|
481
|
+
this.inflight.set(key, (this.inflight.get(key) ?? 0) + 1);
|
|
482
|
+
});
|
|
398
483
|
try {
|
|
399
|
-
|
|
484
|
+
const found = new Map();
|
|
485
|
+
const gone = new Set();
|
|
486
|
+
let failed = false;
|
|
400
487
|
if (spec.fetchMany) {
|
|
401
488
|
for (let i = 0; i < ids.length; i += FETCH_CHUNK) {
|
|
402
489
|
const chunk = ids.slice(i, i + FETCH_CHUNK);
|
|
403
490
|
const got = await spec.fetchMany(this.client, chunk);
|
|
404
|
-
|
|
491
|
+
for (const raw of Array.isArray(got) ? got : []) {
|
|
492
|
+
if (raw?.id)
|
|
493
|
+
found.set(raw.id, raw);
|
|
494
|
+
}
|
|
405
495
|
}
|
|
496
|
+
// The bulk endpoint is authoritative for the ids it was asked for.
|
|
497
|
+
for (const id of ids)
|
|
498
|
+
if (!found.has(id))
|
|
499
|
+
gone.add(id);
|
|
406
500
|
}
|
|
407
501
|
else if (spec.fetchOne) {
|
|
408
502
|
const results = await Promise.allSettled(ids.map((id) => spec.fetchOne(this.client, id)));
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
503
|
+
results.forEach((r, i) => {
|
|
504
|
+
const id = ids[i];
|
|
505
|
+
if (r.status === 'fulfilled') {
|
|
506
|
+
if (r.value?.id)
|
|
507
|
+
found.set(r.value.id, r.value);
|
|
508
|
+
else
|
|
509
|
+
gone.add(id);
|
|
510
|
+
}
|
|
511
|
+
else if (isGoneError(r.reason)) {
|
|
512
|
+
gone.add(id);
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
// Transient failure: not evidence of deletion.
|
|
516
|
+
failed = true;
|
|
517
|
+
console.error(`[realtime] fetch ${type} ${id} failed:`, r.reason);
|
|
518
|
+
}
|
|
519
|
+
});
|
|
413
520
|
}
|
|
414
521
|
else {
|
|
522
|
+
ids.forEach(settle);
|
|
415
523
|
invalidateType(this.qc, spec);
|
|
416
524
|
return;
|
|
417
525
|
}
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
if (!raw?.id)
|
|
526
|
+
for (const [id, raw] of found) {
|
|
527
|
+
if (!current(id))
|
|
421
528
|
continue;
|
|
422
|
-
seen.add(raw.id);
|
|
423
529
|
upsertEntity(this.qc, spec, spec.construct ? spec.construct(raw) : raw);
|
|
424
530
|
}
|
|
425
|
-
for (const id of
|
|
426
|
-
if (
|
|
427
|
-
|
|
428
|
-
|
|
531
|
+
for (const id of gone) {
|
|
532
|
+
if (current(id))
|
|
533
|
+
dropFromLists(this.qc, spec.ns, id);
|
|
534
|
+
}
|
|
535
|
+
ids.forEach(settle);
|
|
536
|
+
if (failed)
|
|
537
|
+
invalidateType(this.qc, spec);
|
|
538
|
+
else
|
|
539
|
+
spec.alsoInvalidate?.forEach((k) => this.qc.invalidateQueries({ queryKey: [...k] }));
|
|
429
540
|
}
|
|
430
541
|
catch (err) {
|
|
431
542
|
console.error(`[realtime] bulk fetch for ${type} failed:`, err);
|
|
543
|
+
ids.forEach(settle);
|
|
432
544
|
invalidateType(this.qc, spec);
|
|
433
545
|
}
|
|
434
546
|
}
|
|
@@ -437,6 +549,8 @@ export class ChangeApplier {
|
|
|
437
549
|
clearTimeout(this.timer);
|
|
438
550
|
this.timer = null;
|
|
439
551
|
this.pending.clear();
|
|
552
|
+
this.latest.clear();
|
|
553
|
+
this.inflight.clear();
|
|
440
554
|
}
|
|
441
555
|
}
|
|
442
556
|
/** Namespaces a mutation should refetch when no socket can deliver the change. */
|