@vllnt/convex-invitations 0.1.0-canary.01d468e
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/LICENSE +21 -0
- package/README.md +139 -0
- package/dist/client/index.d.ts +196 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +181 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +93 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +40 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +148 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/crons.d.ts +19 -0
- package/dist/component/crons.d.ts.map +1 -0
- package/dist/component/crons.js +22 -0
- package/dist/component/crons.js.map +1 -0
- package/dist/component/mutations.d.ts +118 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +272 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +98 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +93 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +56 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +44 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +49 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +37 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/shared.d.ts +33 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +40 -0
- package/dist/shared.js.map +1 -0
- package/package.json +101 -0
- package/src/client/index.ts +357 -0
- package/src/client/types.ts +98 -0
- package/src/component/_generated/api.ts +56 -0
- package/src/component/_generated/component.ts +179 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/crons.ts +26 -0
- package/src/component/mutations.ts +307 -0
- package/src/component/queries.ts +113 -0
- package/src/component/schema.ts +44 -0
- package/src/component/validators.ts +44 -0
- package/src/shared.ts +48 -0
- package/src/test.ts +12 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Issue an invitation to a `resourceRef` and return the single-use `token` the
|
|
3
|
+
* host hands to the invitee (out of band — an email, a link). The invite is
|
|
4
|
+
* inserted `pending` with `createdAt` stamped from the server clock and
|
|
5
|
+
* `expiresAt` set from the caller-resolved TTL (the client computes it from
|
|
6
|
+
* `ttlMs` or passes an absolute `expiresAt`).
|
|
7
|
+
*
|
|
8
|
+
* `token` is host-supplied (the client mints a random one) and must be unique —
|
|
9
|
+
* re-using an existing token throws
|
|
10
|
+
* `ConvexError({ code: "DUPLICATE_TOKEN" })` so a collision can never clobber an
|
|
11
|
+
* open invite. `resourceRef`, `role`, `inviterRef`, `inviteeRef`, and `payload`
|
|
12
|
+
* are opaque host data carried through to acceptance; the component never
|
|
13
|
+
* inspects them.
|
|
14
|
+
*/
|
|
15
|
+
export declare const issue: import("convex/server").RegisteredMutation<"public", {
|
|
16
|
+
expiresAt: number;
|
|
17
|
+
inviteeRef?: string | undefined;
|
|
18
|
+
inviterRef?: string | undefined;
|
|
19
|
+
payload?: any;
|
|
20
|
+
resourceRef: string;
|
|
21
|
+
role?: any;
|
|
22
|
+
token: string;
|
|
23
|
+
}, Promise<{
|
|
24
|
+
token: string;
|
|
25
|
+
expiresAt: number;
|
|
26
|
+
}>>;
|
|
27
|
+
/**
|
|
28
|
+
* Redeem an invitation by its `token` — the single-use accept transaction.
|
|
29
|
+
* Atomically: the invite must exist, be `pending`, and not be past its
|
|
30
|
+
* `expiresAt` as of the server clock. On success it transitions to `accepted`
|
|
31
|
+
* (recording `acceptedAt` and the opaque `acceptedBy` subject) and returns the
|
|
32
|
+
* grant the host applies — `resourceRef` + opaque `role`/`payload`. Because the
|
|
33
|
+
* single check-and-patch runs in one mutation, two racing accepts yield exactly
|
|
34
|
+
* one winner: the second sees a non-`pending` invite and is rejected.
|
|
35
|
+
*
|
|
36
|
+
* A `pending` invite found past its TTL is rejected with `EXPIRED` but NOT
|
|
37
|
+
* flipped here — a thrown mutation rolls back its own writes, so the flip would
|
|
38
|
+
* not persist. The non-throwing `peek` mutation and the prune cron sweep
|
|
39
|
+
* `pending → expired` instead; the TTL is still enforced on read (the stale
|
|
40
|
+
* invite can never be accepted).
|
|
41
|
+
*
|
|
42
|
+
* @throws `ConvexError({ code: "NOT_FOUND" })` when no invitation has `token`.
|
|
43
|
+
* @throws `ConvexError({ code: "EXPIRED" })` when the invite's TTL has elapsed.
|
|
44
|
+
* @throws `ConvexError({ code: "ALREADY_<STATE>" })` when the invite was already
|
|
45
|
+
* accepted, revoked, or expired (single-use — a terminal invite cannot be
|
|
46
|
+
* redeemed again).
|
|
47
|
+
*/
|
|
48
|
+
export declare const accept: import("convex/server").RegisteredMutation<"public", {
|
|
49
|
+
acceptedBy: string;
|
|
50
|
+
token: string;
|
|
51
|
+
}, Promise<{
|
|
52
|
+
resourceRef: string;
|
|
53
|
+
role: any;
|
|
54
|
+
payload: any;
|
|
55
|
+
}>>;
|
|
56
|
+
/**
|
|
57
|
+
* Revoke a still-`pending` invitation by its `token`, transitioning it to
|
|
58
|
+
* `revoked` (recording `revokedAt`). A revoked invite can never be accepted.
|
|
59
|
+
* Already-terminal invites are rejected — revoking is itself a single
|
|
60
|
+
* `pending → revoked` transition, so a revoke racing an accept yields one
|
|
61
|
+
* winner.
|
|
62
|
+
*
|
|
63
|
+
* @throws `ConvexError({ code: "NOT_FOUND" })` when no invitation has `token`.
|
|
64
|
+
* @throws `ConvexError({ code: "ALREADY_<STATE>" })` when the invite is already
|
|
65
|
+
* accepted, revoked, or expired.
|
|
66
|
+
*/
|
|
67
|
+
export declare const revoke: import("convex/server").RegisteredMutation<"public", {
|
|
68
|
+
token: string;
|
|
69
|
+
}, Promise<null>>;
|
|
70
|
+
/**
|
|
71
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
72
|
+
* invite is held. Lives in `mutations.ts` (not `queries.ts`) because it flips a
|
|
73
|
+
* `pending` invite found past its TTL to `expired` before returning — the same
|
|
74
|
+
* read-time TTL enforcement `accept` does, so a status check never reports a
|
|
75
|
+
* stale invite as still open.
|
|
76
|
+
*/
|
|
77
|
+
export declare const peek: import("convex/server").RegisteredMutation<"public", {
|
|
78
|
+
token: string;
|
|
79
|
+
}, Promise<{
|
|
80
|
+
token: string;
|
|
81
|
+
resourceRef: string;
|
|
82
|
+
role: unknown;
|
|
83
|
+
inviterRef: string | undefined;
|
|
84
|
+
inviteeRef: string | undefined;
|
|
85
|
+
payload: unknown;
|
|
86
|
+
state: "accepted" | "expired" | "pending" | "revoked";
|
|
87
|
+
createdAt: number;
|
|
88
|
+
expiresAt: number;
|
|
89
|
+
acceptedAt: number | undefined;
|
|
90
|
+
acceptedBy: string | undefined;
|
|
91
|
+
revokedAt: number | undefined;
|
|
92
|
+
} | null>>;
|
|
93
|
+
/**
|
|
94
|
+
* Sweep up to `batch` invitations in two passes, oldest first. First — the
|
|
95
|
+
* retention sweep — terminal invites whose `createdAt < retentionBefore` are
|
|
96
|
+
* deleted (via `by_state_created` across `accepted`/`revoked`/`expired`). Then,
|
|
97
|
+
* with whatever batch budget remains — the TTL sweep — `pending` invites whose
|
|
98
|
+
* `expiresAt < before` are flipped to `expired` (via `by_state_expires`).
|
|
99
|
+
*
|
|
100
|
+
* Retention runs before TTL deliberately: a `pending` invite flipped to
|
|
101
|
+
* `expired` this pass would otherwise re-qualify for the retention delete in the
|
|
102
|
+
* same pass and be double-counted. Flipping last leaves the freshly-expired rows
|
|
103
|
+
* for the NEXT pass (where `createdAt < retentionBefore` still holds), so each
|
|
104
|
+
* invite is touched once per pass.
|
|
105
|
+
*
|
|
106
|
+
* `before` and `retentionBefore` default to the server clock. If a full batch
|
|
107
|
+
* was touched there may be more, so the sweep self-reschedules through
|
|
108
|
+
* `ctx.scheduler` until a short batch signals the tail is clean. Idempotent —
|
|
109
|
+
* only ever deletes already-terminal, past-retention rows and expires
|
|
110
|
+
* already-stale pending invites. Returns the total touched (deleted + expired)
|
|
111
|
+
* in this pass. The built-in daily cron drives this automatically.
|
|
112
|
+
*/
|
|
113
|
+
export declare const prune: import("convex/server").RegisteredMutation<"public", {
|
|
114
|
+
batch: number;
|
|
115
|
+
before?: number | undefined;
|
|
116
|
+
retentionBefore?: number | undefined;
|
|
117
|
+
}, Promise<number>>;
|
|
118
|
+
//# sourceMappingURL=mutations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;GAoChB,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,MAAM;;;;;;;GA4CjB,CAAC;AAEH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM;;iBAuBjB,CAAC;AAEH;;;;;;GAMG;AACH,eAAO,MAAM,IAAI;;;;;;;;;;;;;;;UAiBf,CAAC;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,KAAK;;;;mBAqEhB,CAAC"}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
|
+
import { api } from "./_generated/api";
|
|
3
|
+
import { mutation } from "./_generated/server";
|
|
4
|
+
import { invitationView, jsonValue } from "./validators";
|
|
5
|
+
/**
|
|
6
|
+
* Issue an invitation to a `resourceRef` and return the single-use `token` the
|
|
7
|
+
* host hands to the invitee (out of band — an email, a link). The invite is
|
|
8
|
+
* inserted `pending` with `createdAt` stamped from the server clock and
|
|
9
|
+
* `expiresAt` set from the caller-resolved TTL (the client computes it from
|
|
10
|
+
* `ttlMs` or passes an absolute `expiresAt`).
|
|
11
|
+
*
|
|
12
|
+
* `token` is host-supplied (the client mints a random one) and must be unique —
|
|
13
|
+
* re-using an existing token throws
|
|
14
|
+
* `ConvexError({ code: "DUPLICATE_TOKEN" })` so a collision can never clobber an
|
|
15
|
+
* open invite. `resourceRef`, `role`, `inviterRef`, `inviteeRef`, and `payload`
|
|
16
|
+
* are opaque host data carried through to acceptance; the component never
|
|
17
|
+
* inspects them.
|
|
18
|
+
*/
|
|
19
|
+
export const issue = mutation({
|
|
20
|
+
args: {
|
|
21
|
+
token: v.string(),
|
|
22
|
+
resourceRef: v.string(),
|
|
23
|
+
role: v.optional(jsonValue),
|
|
24
|
+
inviterRef: v.optional(v.string()),
|
|
25
|
+
inviteeRef: v.optional(v.string()),
|
|
26
|
+
payload: v.optional(jsonValue),
|
|
27
|
+
expiresAt: v.number(),
|
|
28
|
+
},
|
|
29
|
+
returns: v.object({ token: v.string(), expiresAt: v.number() }),
|
|
30
|
+
handler: async (ctx, args) => {
|
|
31
|
+
const existing = await ctx.db
|
|
32
|
+
.query("invitations")
|
|
33
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
34
|
+
.unique();
|
|
35
|
+
if (existing !== null) {
|
|
36
|
+
throw new ConvexError({
|
|
37
|
+
code: "DUPLICATE_TOKEN",
|
|
38
|
+
message: `invitation token already exists`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
await ctx.db.insert("invitations", {
|
|
42
|
+
token: args.token,
|
|
43
|
+
resourceRef: args.resourceRef,
|
|
44
|
+
role: args.role,
|
|
45
|
+
inviterRef: args.inviterRef,
|
|
46
|
+
inviteeRef: args.inviteeRef,
|
|
47
|
+
payload: args.payload,
|
|
48
|
+
state: "pending",
|
|
49
|
+
createdAt: Date.now(),
|
|
50
|
+
expiresAt: args.expiresAt,
|
|
51
|
+
});
|
|
52
|
+
return { token: args.token, expiresAt: args.expiresAt };
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Redeem an invitation by its `token` — the single-use accept transaction.
|
|
57
|
+
* Atomically: the invite must exist, be `pending`, and not be past its
|
|
58
|
+
* `expiresAt` as of the server clock. On success it transitions to `accepted`
|
|
59
|
+
* (recording `acceptedAt` and the opaque `acceptedBy` subject) and returns the
|
|
60
|
+
* grant the host applies — `resourceRef` + opaque `role`/`payload`. Because the
|
|
61
|
+
* single check-and-patch runs in one mutation, two racing accepts yield exactly
|
|
62
|
+
* one winner: the second sees a non-`pending` invite and is rejected.
|
|
63
|
+
*
|
|
64
|
+
* A `pending` invite found past its TTL is rejected with `EXPIRED` but NOT
|
|
65
|
+
* flipped here — a thrown mutation rolls back its own writes, so the flip would
|
|
66
|
+
* not persist. The non-throwing `peek` mutation and the prune cron sweep
|
|
67
|
+
* `pending → expired` instead; the TTL is still enforced on read (the stale
|
|
68
|
+
* invite can never be accepted).
|
|
69
|
+
*
|
|
70
|
+
* @throws `ConvexError({ code: "NOT_FOUND" })` when no invitation has `token`.
|
|
71
|
+
* @throws `ConvexError({ code: "EXPIRED" })` when the invite's TTL has elapsed.
|
|
72
|
+
* @throws `ConvexError({ code: "ALREADY_<STATE>" })` when the invite was already
|
|
73
|
+
* accepted, revoked, or expired (single-use — a terminal invite cannot be
|
|
74
|
+
* redeemed again).
|
|
75
|
+
*/
|
|
76
|
+
export const accept = mutation({
|
|
77
|
+
args: { token: v.string(), acceptedBy: v.string() },
|
|
78
|
+
returns: v.object({
|
|
79
|
+
resourceRef: v.string(),
|
|
80
|
+
role: v.optional(jsonValue),
|
|
81
|
+
payload: v.optional(jsonValue),
|
|
82
|
+
}),
|
|
83
|
+
handler: async (ctx, args) => {
|
|
84
|
+
const invite = await ctx.db
|
|
85
|
+
.query("invitations")
|
|
86
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
87
|
+
.unique();
|
|
88
|
+
if (invite === null) {
|
|
89
|
+
throw new ConvexError({
|
|
90
|
+
code: "NOT_FOUND",
|
|
91
|
+
message: `invitation not found`,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (invite.state !== "pending") {
|
|
95
|
+
throw new ConvexError({
|
|
96
|
+
code: `ALREADY_${invite.state.toUpperCase()}`,
|
|
97
|
+
message: `invitation is already ${invite.state} and cannot be accepted`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
if (invite.expiresAt <= now) {
|
|
102
|
+
throw new ConvexError({
|
|
103
|
+
code: "EXPIRED",
|
|
104
|
+
message: `invitation expired`,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
await ctx.db.patch("invitations", invite._id, {
|
|
108
|
+
state: "accepted",
|
|
109
|
+
acceptedAt: now,
|
|
110
|
+
acceptedBy: args.acceptedBy,
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
resourceRef: invite.resourceRef,
|
|
114
|
+
role: invite.role,
|
|
115
|
+
payload: invite.payload,
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
/**
|
|
120
|
+
* Revoke a still-`pending` invitation by its `token`, transitioning it to
|
|
121
|
+
* `revoked` (recording `revokedAt`). A revoked invite can never be accepted.
|
|
122
|
+
* Already-terminal invites are rejected — revoking is itself a single
|
|
123
|
+
* `pending → revoked` transition, so a revoke racing an accept yields one
|
|
124
|
+
* winner.
|
|
125
|
+
*
|
|
126
|
+
* @throws `ConvexError({ code: "NOT_FOUND" })` when no invitation has `token`.
|
|
127
|
+
* @throws `ConvexError({ code: "ALREADY_<STATE>" })` when the invite is already
|
|
128
|
+
* accepted, revoked, or expired.
|
|
129
|
+
*/
|
|
130
|
+
export const revoke = mutation({
|
|
131
|
+
args: { token: v.string() },
|
|
132
|
+
returns: v.null(),
|
|
133
|
+
handler: async (ctx, args) => {
|
|
134
|
+
const invite = await ctx.db
|
|
135
|
+
.query("invitations")
|
|
136
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
137
|
+
.unique();
|
|
138
|
+
if (invite === null) {
|
|
139
|
+
throw new ConvexError({
|
|
140
|
+
code: "NOT_FOUND",
|
|
141
|
+
message: `invitation not found`,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (invite.state !== "pending") {
|
|
145
|
+
throw new ConvexError({
|
|
146
|
+
code: `ALREADY_${invite.state.toUpperCase()}`,
|
|
147
|
+
message: `invitation is already ${invite.state} and cannot be revoked`,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
await ctx.db.patch("invitations", invite._id, { state: "revoked", revokedAt: Date.now() });
|
|
151
|
+
return null;
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
/**
|
|
155
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
156
|
+
* invite is held. Lives in `mutations.ts` (not `queries.ts`) because it flips a
|
|
157
|
+
* `pending` invite found past its TTL to `expired` before returning — the same
|
|
158
|
+
* read-time TTL enforcement `accept` does, so a status check never reports a
|
|
159
|
+
* stale invite as still open.
|
|
160
|
+
*/
|
|
161
|
+
export const peek = mutation({
|
|
162
|
+
args: { token: v.string() },
|
|
163
|
+
returns: v.union(v.null(), invitationView),
|
|
164
|
+
handler: async (ctx, args) => {
|
|
165
|
+
const invite = await ctx.db
|
|
166
|
+
.query("invitations")
|
|
167
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
168
|
+
.unique();
|
|
169
|
+
if (invite === null) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
if (invite.state === "pending" && invite.expiresAt <= Date.now()) {
|
|
173
|
+
await ctx.db.patch("invitations", invite._id, { state: "expired" });
|
|
174
|
+
return { ...view(invite), state: "expired" };
|
|
175
|
+
}
|
|
176
|
+
return view(invite);
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
/**
|
|
180
|
+
* Sweep up to `batch` invitations in two passes, oldest first. First — the
|
|
181
|
+
* retention sweep — terminal invites whose `createdAt < retentionBefore` are
|
|
182
|
+
* deleted (via `by_state_created` across `accepted`/`revoked`/`expired`). Then,
|
|
183
|
+
* with whatever batch budget remains — the TTL sweep — `pending` invites whose
|
|
184
|
+
* `expiresAt < before` are flipped to `expired` (via `by_state_expires`).
|
|
185
|
+
*
|
|
186
|
+
* Retention runs before TTL deliberately: a `pending` invite flipped to
|
|
187
|
+
* `expired` this pass would otherwise re-qualify for the retention delete in the
|
|
188
|
+
* same pass and be double-counted. Flipping last leaves the freshly-expired rows
|
|
189
|
+
* for the NEXT pass (where `createdAt < retentionBefore` still holds), so each
|
|
190
|
+
* invite is touched once per pass.
|
|
191
|
+
*
|
|
192
|
+
* `before` and `retentionBefore` default to the server clock. If a full batch
|
|
193
|
+
* was touched there may be more, so the sweep self-reschedules through
|
|
194
|
+
* `ctx.scheduler` until a short batch signals the tail is clean. Idempotent —
|
|
195
|
+
* only ever deletes already-terminal, past-retention rows and expires
|
|
196
|
+
* already-stale pending invites. Returns the total touched (deleted + expired)
|
|
197
|
+
* in this pass. The built-in daily cron drives this automatically.
|
|
198
|
+
*/
|
|
199
|
+
export const prune = mutation({
|
|
200
|
+
args: {
|
|
201
|
+
before: v.optional(v.number()),
|
|
202
|
+
retentionBefore: v.optional(v.number()),
|
|
203
|
+
batch: v.number(),
|
|
204
|
+
},
|
|
205
|
+
returns: v.number(),
|
|
206
|
+
handler: async (ctx, args) => {
|
|
207
|
+
const now = Date.now();
|
|
208
|
+
const before = args.before ?? now;
|
|
209
|
+
const retentionBefore = args.retentionBefore ?? now;
|
|
210
|
+
// Retention sweep: the three terminal states, queried explicitly (no
|
|
211
|
+
// query-in-loop), each consuming whatever batch budget the earlier passes
|
|
212
|
+
// left.
|
|
213
|
+
const accepted = await ctx.db
|
|
214
|
+
.query("invitations")
|
|
215
|
+
.withIndex("by_state_created", (q) => q.eq("state", "accepted").lt("createdAt", retentionBefore))
|
|
216
|
+
.take(args.batch);
|
|
217
|
+
const revoked = accepted.length < args.batch
|
|
218
|
+
? await ctx.db
|
|
219
|
+
.query("invitations")
|
|
220
|
+
.withIndex("by_state_created", (q) => q.eq("state", "revoked").lt("createdAt", retentionBefore))
|
|
221
|
+
.take(args.batch - accepted.length)
|
|
222
|
+
: [];
|
|
223
|
+
const expired = accepted.length + revoked.length < args.batch
|
|
224
|
+
? await ctx.db
|
|
225
|
+
.query("invitations")
|
|
226
|
+
.withIndex("by_state_created", (q) => q.eq("state", "expired").lt("createdAt", retentionBefore))
|
|
227
|
+
.take(args.batch - accepted.length - revoked.length)
|
|
228
|
+
: [];
|
|
229
|
+
for (const row of [...accepted, ...revoked, ...expired]) {
|
|
230
|
+
await ctx.db.delete("invitations", row._id);
|
|
231
|
+
}
|
|
232
|
+
const removed = accepted.length + revoked.length + expired.length;
|
|
233
|
+
// TTL sweep: flip stale pending invites with the remaining batch budget.
|
|
234
|
+
const remaining = args.batch - removed;
|
|
235
|
+
const stale = remaining > 0
|
|
236
|
+
? await ctx.db
|
|
237
|
+
.query("invitations")
|
|
238
|
+
.withIndex("by_state_expires", (q) => q.eq("state", "pending").lt("expiresAt", before))
|
|
239
|
+
.take(remaining)
|
|
240
|
+
: [];
|
|
241
|
+
for (const row of stale) {
|
|
242
|
+
await ctx.db.patch("invitations", row._id, { state: "expired" });
|
|
243
|
+
}
|
|
244
|
+
const touched = removed + stale.length;
|
|
245
|
+
if (touched === args.batch) {
|
|
246
|
+
await ctx.scheduler.runAfter(0, api.mutations.prune, {
|
|
247
|
+
before,
|
|
248
|
+
retentionBefore,
|
|
249
|
+
batch: args.batch,
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
return touched;
|
|
253
|
+
},
|
|
254
|
+
});
|
|
255
|
+
/** Project a stored invitation row to its public view (drops internal fields). */
|
|
256
|
+
function view(invite) {
|
|
257
|
+
return {
|
|
258
|
+
token: invite.token,
|
|
259
|
+
resourceRef: invite.resourceRef,
|
|
260
|
+
role: invite.role,
|
|
261
|
+
inviterRef: invite.inviterRef,
|
|
262
|
+
inviteeRef: invite.inviteeRef,
|
|
263
|
+
payload: invite.payload,
|
|
264
|
+
state: invite.state,
|
|
265
|
+
createdAt: invite.createdAt,
|
|
266
|
+
expiresAt: invite.expiresAt,
|
|
267
|
+
acceptedAt: invite.acceptedAt,
|
|
268
|
+
acceptedBy: invite.acceptedBy,
|
|
269
|
+
revokedAt: invite.revokedAt,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=mutations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,QAAQ,CAAC;IAC5B,IAAI,EAAE;QACJ,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;QACjB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE;QACvB,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,CAAC;QAC3B,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
|
1
|
+
/**
|
|
2
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
3
|
+
* invite is held. This is a pure read — it reports the stored `state` as-is and
|
|
4
|
+
* does NOT flip a stale `pending` invite to `expired` (a query cannot write).
|
|
5
|
+
* Read the `expiresAt`/`state` pair, or call the `peek` mutation when you need
|
|
6
|
+
* read-time TTL enforcement.
|
|
7
|
+
*/
|
|
8
|
+
export declare const getByToken: import("convex/server").RegisteredQuery<"public", {
|
|
9
|
+
token: string;
|
|
10
|
+
}, Promise<{
|
|
11
|
+
token: string;
|
|
12
|
+
resourceRef: string;
|
|
13
|
+
role: any;
|
|
14
|
+
inviterRef: string | undefined;
|
|
15
|
+
inviteeRef: string | undefined;
|
|
16
|
+
payload: any;
|
|
17
|
+
state: "accepted" | "expired" | "pending" | "revoked";
|
|
18
|
+
createdAt: number;
|
|
19
|
+
expiresAt: number;
|
|
20
|
+
acceptedAt: number | undefined;
|
|
21
|
+
acceptedBy: string | undefined;
|
|
22
|
+
revokedAt: number | undefined;
|
|
23
|
+
} | null>>;
|
|
24
|
+
/**
|
|
25
|
+
* Page the still-`pending` invitations for one `resourceRef`, oldest first via
|
|
26
|
+
* the `by_resource_state` index. Takes the standard Convex `paginationOpts` and
|
|
27
|
+
* returns the standard paginated envelope (`page`, `isDone`, `continueCursor`)
|
|
28
|
+
* so the host can render an org's open-invite list reactively. Past-TTL invites
|
|
29
|
+
* may still appear `pending` here until a `peek`/`accept`/cron sweep flips them;
|
|
30
|
+
* compare `expiresAt` against the clock to hide stale ones in the host UI.
|
|
31
|
+
*/
|
|
32
|
+
export declare const listPending: import("convex/server").RegisteredQuery<"public", {
|
|
33
|
+
paginationOpts: {
|
|
34
|
+
id?: number;
|
|
35
|
+
endCursor?: string | null;
|
|
36
|
+
maximumRowsRead?: number;
|
|
37
|
+
maximumBytesRead?: number;
|
|
38
|
+
numItems: number;
|
|
39
|
+
cursor: string | null;
|
|
40
|
+
};
|
|
41
|
+
resourceRef: string;
|
|
42
|
+
}, Promise<{
|
|
43
|
+
isDone: boolean;
|
|
44
|
+
continueCursor: import("convex/server").Cursor;
|
|
45
|
+
splitCursor?: import("convex/server").Cursor | null;
|
|
46
|
+
pageStatus?: "SplitRecommended" | "SplitRequired" | null;
|
|
47
|
+
page: {
|
|
48
|
+
token: string;
|
|
49
|
+
resourceRef: string;
|
|
50
|
+
role: any;
|
|
51
|
+
inviterRef: string | undefined;
|
|
52
|
+
inviteeRef: string | undefined;
|
|
53
|
+
payload: any;
|
|
54
|
+
state: "accepted" | "expired" | "pending" | "revoked";
|
|
55
|
+
createdAt: number;
|
|
56
|
+
expiresAt: number;
|
|
57
|
+
acceptedAt: number | undefined;
|
|
58
|
+
acceptedBy: string | undefined;
|
|
59
|
+
revokedAt: number | undefined;
|
|
60
|
+
}[];
|
|
61
|
+
}>>;
|
|
62
|
+
/**
|
|
63
|
+
* Page invitations for one `resourceRef` in a given `state`, oldest first — the
|
|
64
|
+
* general listing `listPending` specializes. Lets the host render accepted /
|
|
65
|
+
* revoked / expired history for an audit surface.
|
|
66
|
+
*/
|
|
67
|
+
export declare const listByResourceState: import("convex/server").RegisteredQuery<"public", {
|
|
68
|
+
paginationOpts: {
|
|
69
|
+
id?: number;
|
|
70
|
+
endCursor?: string | null;
|
|
71
|
+
maximumRowsRead?: number;
|
|
72
|
+
maximumBytesRead?: number;
|
|
73
|
+
numItems: number;
|
|
74
|
+
cursor: string | null;
|
|
75
|
+
};
|
|
76
|
+
resourceRef: string;
|
|
77
|
+
state: "accepted" | "expired" | "pending" | "revoked";
|
|
78
|
+
}, Promise<{
|
|
79
|
+
isDone: boolean;
|
|
80
|
+
continueCursor: import("convex/server").Cursor;
|
|
81
|
+
splitCursor?: import("convex/server").Cursor | null;
|
|
82
|
+
pageStatus?: "SplitRecommended" | "SplitRequired" | null;
|
|
83
|
+
page: {
|
|
84
|
+
token: string;
|
|
85
|
+
resourceRef: string;
|
|
86
|
+
role: any;
|
|
87
|
+
inviterRef: string | undefined;
|
|
88
|
+
inviteeRef: string | undefined;
|
|
89
|
+
payload: any;
|
|
90
|
+
state: "accepted" | "expired" | "pending" | "revoked";
|
|
91
|
+
createdAt: number;
|
|
92
|
+
expiresAt: number;
|
|
93
|
+
acceptedAt: number | undefined;
|
|
94
|
+
acceptedBy: string | undefined;
|
|
95
|
+
revokedAt: number | undefined;
|
|
96
|
+
}[];
|
|
97
|
+
}>>;
|
|
98
|
+
//# sourceMappingURL=queries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAwBA;;;;;;GAMG;AACH,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;UAUrB,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyBtB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,mBAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6B9B,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { paginationOptsValidator } from "convex/server";
|
|
3
|
+
import { query } from "./_generated/server";
|
|
4
|
+
import { invitationState, invitationView } from "./validators";
|
|
5
|
+
/** Project a stored invitation row to its public view (drops internal fields). */
|
|
6
|
+
function view(invite) {
|
|
7
|
+
return {
|
|
8
|
+
token: invite.token,
|
|
9
|
+
resourceRef: invite.resourceRef,
|
|
10
|
+
role: invite.role,
|
|
11
|
+
inviterRef: invite.inviterRef,
|
|
12
|
+
inviteeRef: invite.inviteeRef,
|
|
13
|
+
payload: invite.payload,
|
|
14
|
+
state: invite.state,
|
|
15
|
+
createdAt: invite.createdAt,
|
|
16
|
+
expiresAt: invite.expiresAt,
|
|
17
|
+
acceptedAt: invite.acceptedAt,
|
|
18
|
+
acceptedBy: invite.acceptedBy,
|
|
19
|
+
revokedAt: invite.revokedAt,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The current view of one invitation by its `token`, or `null` if no such
|
|
24
|
+
* invite is held. This is a pure read — it reports the stored `state` as-is and
|
|
25
|
+
* does NOT flip a stale `pending` invite to `expired` (a query cannot write).
|
|
26
|
+
* Read the `expiresAt`/`state` pair, or call the `peek` mutation when you need
|
|
27
|
+
* read-time TTL enforcement.
|
|
28
|
+
*/
|
|
29
|
+
export const getByToken = query({
|
|
30
|
+
args: { token: v.string() },
|
|
31
|
+
returns: v.union(v.null(), invitationView),
|
|
32
|
+
handler: async (ctx, args) => {
|
|
33
|
+
const invite = await ctx.db
|
|
34
|
+
.query("invitations")
|
|
35
|
+
.withIndex("by_token", (q) => q.eq("token", args.token))
|
|
36
|
+
.unique();
|
|
37
|
+
return invite === null ? null : view(invite);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
/**
|
|
41
|
+
* Page the still-`pending` invitations for one `resourceRef`, oldest first via
|
|
42
|
+
* the `by_resource_state` index. Takes the standard Convex `paginationOpts` and
|
|
43
|
+
* returns the standard paginated envelope (`page`, `isDone`, `continueCursor`)
|
|
44
|
+
* so the host can render an org's open-invite list reactively. Past-TTL invites
|
|
45
|
+
* may still appear `pending` here until a `peek`/`accept`/cron sweep flips them;
|
|
46
|
+
* compare `expiresAt` against the clock to hide stale ones in the host UI.
|
|
47
|
+
*/
|
|
48
|
+
export const listPending = query({
|
|
49
|
+
args: { resourceRef: v.string(), paginationOpts: paginationOptsValidator },
|
|
50
|
+
returns: v.object({
|
|
51
|
+
page: v.array(invitationView),
|
|
52
|
+
isDone: v.boolean(),
|
|
53
|
+
continueCursor: v.string(),
|
|
54
|
+
splitCursor: v.optional(v.union(v.string(), v.null())),
|
|
55
|
+
pageStatus: v.optional(v.union(v.literal("SplitRecommended"), v.literal("SplitRequired"), v.null())),
|
|
56
|
+
}),
|
|
57
|
+
handler: async (ctx, args) => {
|
|
58
|
+
const result = await ctx.db
|
|
59
|
+
.query("invitations")
|
|
60
|
+
.withIndex("by_resource_state", (q) => q.eq("resourceRef", args.resourceRef).eq("state", "pending"))
|
|
61
|
+
.order("asc")
|
|
62
|
+
.paginate(args.paginationOpts);
|
|
63
|
+
return { ...result, page: result.page.map(view) };
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
/**
|
|
67
|
+
* Page invitations for one `resourceRef` in a given `state`, oldest first — the
|
|
68
|
+
* general listing `listPending` specializes. Lets the host render accepted /
|
|
69
|
+
* revoked / expired history for an audit surface.
|
|
70
|
+
*/
|
|
71
|
+
export const listByResourceState = query({
|
|
72
|
+
args: {
|
|
73
|
+
resourceRef: v.string(),
|
|
74
|
+
state: invitationState,
|
|
75
|
+
paginationOpts: paginationOptsValidator,
|
|
76
|
+
},
|
|
77
|
+
returns: v.object({
|
|
78
|
+
page: v.array(invitationView),
|
|
79
|
+
isDone: v.boolean(),
|
|
80
|
+
continueCursor: v.string(),
|
|
81
|
+
splitCursor: v.optional(v.union(v.string(), v.null())),
|
|
82
|
+
pageStatus: v.optional(v.union(v.literal("SplitRecommended"), v.literal("SplitRequired"), v.null())),
|
|
83
|
+
}),
|
|
84
|
+
handler: async (ctx, args) => {
|
|
85
|
+
const result = await ctx.db
|
|
86
|
+
.query("invitations")
|
|
87
|
+
.withIndex("by_resource_state", (q) => q.eq("resourceRef", args.resourceRef).eq("state", args.state))
|
|
88
|
+
.order("asc")
|
|
89
|
+
.paginate(args.paginationOpts);
|
|
90
|
+
return { ...result, page: result.page.map(view) };
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
//# sourceMappingURL=queries.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queries.js","sourceRoot":"","sources":["../../src/component/queries.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAq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