@robelest/convex-auth 0.0.4-preview.13 → 0.0.4-preview.16
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/README.md +140 -9
- package/dist/bin.cjs +5957 -5478
- package/dist/client/index.d.ts +3 -7
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +27 -26
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +14 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +1672 -24
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/convex.config.d.ts +2 -2
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/index.d.ts +1 -1
- package/dist/component/index.js +2 -2
- package/dist/component/model.d.ts +153 -0
- package/dist/component/model.d.ts.map +1 -0
- package/dist/component/model.js +343 -0
- package/dist/component/model.js.map +1 -0
- package/dist/component/providers/sso.d.ts +1 -1
- package/dist/component/public/enterprise.d.ts +54 -0
- package/dist/component/public/enterprise.d.ts.map +1 -0
- package/dist/component/public/enterprise.js +515 -0
- package/dist/component/public/enterprise.js.map +1 -0
- package/dist/component/public/factors.d.ts +52 -0
- package/dist/component/public/factors.d.ts.map +1 -0
- package/dist/component/public/factors.js +285 -0
- package/dist/component/public/factors.js.map +1 -0
- package/dist/component/public/groups.d.ts +116 -0
- package/dist/component/public/groups.d.ts.map +1 -0
- package/dist/component/public/groups.js +596 -0
- package/dist/component/public/groups.js.map +1 -0
- package/dist/component/public/identity.d.ts +93 -0
- package/dist/component/public/identity.d.ts.map +1 -0
- package/dist/component/public/identity.js +426 -0
- package/dist/component/public/identity.js.map +1 -0
- package/dist/component/public/keys.d.ts +41 -0
- package/dist/component/public/keys.d.ts.map +1 -0
- package/dist/component/public/keys.js +157 -0
- package/dist/component/public/keys.js.map +1 -0
- package/dist/component/public/shared.d.ts +26 -0
- package/dist/component/public/shared.d.ts.map +1 -0
- package/dist/component/public/shared.js +32 -0
- package/dist/component/public/shared.js.map +1 -0
- package/dist/component/public.d.ts +9 -321
- package/dist/component/public.d.ts.map +1 -1
- package/dist/component/public.js +6 -2145
- package/dist/component/schema.d.ts +406 -260
- package/dist/component/schema.js +37 -32
- package/dist/component/schema.js.map +1 -1
- package/dist/component/server/auth.d.ts +161 -15
- package/dist/component/server/auth.d.ts.map +1 -1
- package/dist/component/server/auth.js +100 -7
- package/dist/component/server/auth.js.map +1 -1
- package/dist/component/server/cookies.js +3 -0
- package/dist/component/server/cookies.js.map +1 -1
- package/dist/component/server/db.js +1 -0
- package/dist/component/server/db.js.map +1 -1
- package/dist/component/server/device.js +3 -1
- package/dist/component/server/device.js.map +1 -1
- package/dist/component/server/domains/core.js +629 -0
- package/dist/component/server/domains/core.js.map +1 -0
- package/dist/component/server/domains/sso.js +884 -0
- package/dist/component/server/domains/sso.js.map +1 -0
- package/dist/component/server/factory.d.ts +136 -0
- package/dist/component/server/factory.d.ts.map +1 -0
- package/dist/component/server/factory.js +1134 -0
- package/dist/component/server/factory.js.map +1 -0
- package/dist/component/server/fx.js +2 -1
- package/dist/component/server/fx.js.map +1 -1
- package/dist/component/server/http.js +287 -0
- package/dist/component/server/http.js.map +1 -0
- package/dist/component/server/identity.js +13 -0
- package/dist/component/server/identity.js.map +1 -0
- package/dist/component/server/keys.js +4 -0
- package/dist/component/server/keys.js.map +1 -1
- package/dist/component/server/mutations/account.js +1 -1
- package/dist/component/server/mutations/index.js +2 -2
- package/dist/component/server/mutations/index.js.map +1 -1
- package/dist/component/server/mutations/invalidate.js +1 -1
- package/dist/component/server/mutations/oauth.js +10 -7
- package/dist/component/server/mutations/oauth.js.map +1 -1
- package/dist/component/server/mutations/refresh.js +1 -1
- package/dist/component/server/mutations/register.js +1 -1
- package/dist/component/server/mutations/retrieve.js +1 -1
- package/dist/component/server/mutations/signature.js +1 -1
- package/dist/component/server/mutations/store.js +6 -3
- package/dist/component/server/mutations/store.js.map +1 -1
- package/dist/component/server/mutations/verify.js +1 -1
- package/dist/component/server/oauth.js +3 -0
- package/dist/component/server/oauth.js.map +1 -1
- package/dist/component/server/passkey.js +3 -2
- package/dist/component/server/passkey.js.map +1 -1
- package/dist/component/server/provider.js +2 -0
- package/dist/component/server/provider.js.map +1 -1
- package/dist/component/server/providers.js +10 -0
- package/dist/component/server/providers.js.map +1 -1
- package/dist/component/server/ratelimit.js +3 -0
- package/dist/component/server/ratelimit.js.map +1 -1
- package/dist/component/server/redirects.js +2 -0
- package/dist/component/server/redirects.js.map +1 -1
- package/dist/component/server/refresh.js +5 -0
- package/dist/component/server/refresh.js.map +1 -1
- package/dist/component/server/sessions.js +5 -0
- package/dist/component/server/sessions.js.map +1 -1
- package/dist/component/server/signin.js +2 -1
- package/dist/component/server/signin.js.map +1 -1
- package/dist/component/server/sso.js +166 -19
- package/dist/component/server/sso.js.map +1 -1
- package/dist/component/server/tokens.js +1 -0
- package/dist/component/server/tokens.js.map +1 -1
- package/dist/component/server/totp.js +4 -2
- package/dist/component/server/totp.js.map +1 -1
- package/dist/component/server/types.d.ts +106 -38
- package/dist/component/server/types.d.ts.map +1 -1
- package/dist/component/server/types.js.map +1 -1
- package/dist/component/server/users.js +1 -0
- package/dist/component/server/users.js.map +1 -1
- package/dist/component/server/utils.js +44 -2
- package/dist/component/server/utils.js.map +1 -1
- package/dist/providers/anonymous.d.ts +1 -1
- package/dist/providers/credentials.d.ts +1 -1
- package/dist/providers/password.d.ts +1 -1
- package/dist/providers/sso.d.ts +1 -1
- package/dist/providers/sso.js.map +1 -1
- package/dist/server/auth.d.ts +163 -17
- package/dist/server/auth.d.ts.map +1 -1
- package/dist/server/auth.js +100 -7
- package/dist/server/auth.js.map +1 -1
- package/dist/server/cookies.d.ts +1 -38
- package/dist/server/cookies.js +3 -0
- package/dist/server/cookies.js.map +1 -1
- package/dist/server/db.d.ts +1 -125
- package/dist/server/db.js +1 -0
- package/dist/server/db.js.map +1 -1
- package/dist/server/device.d.ts +1 -24
- package/dist/server/device.js +3 -1
- package/dist/server/device.js.map +1 -1
- package/dist/server/domains/core.d.ts +434 -0
- package/dist/server/domains/core.d.ts.map +1 -0
- package/dist/server/domains/core.js +629 -0
- package/dist/server/domains/core.js.map +1 -0
- package/dist/server/domains/sso.d.ts +409 -0
- package/dist/server/domains/sso.d.ts.map +1 -0
- package/dist/server/domains/sso.js +884 -0
- package/dist/server/domains/sso.js.map +1 -0
- package/dist/server/enterpriseValidators.d.ts +1 -0
- package/dist/server/enterpriseValidators.js +60 -0
- package/dist/server/enterpriseValidators.js.map +1 -0
- package/dist/server/factory.d.ts +136 -0
- package/dist/server/factory.d.ts.map +1 -0
- package/dist/server/factory.js +1134 -0
- package/dist/server/factory.js.map +1 -0
- package/dist/server/fx.d.ts +1 -16
- package/dist/server/fx.d.ts.map +1 -1
- package/dist/server/fx.js +1 -0
- package/dist/server/fx.js.map +1 -1
- package/dist/server/http.d.ts +59 -0
- package/dist/server/http.d.ts.map +1 -0
- package/dist/server/http.js +287 -0
- package/dist/server/http.js.map +1 -0
- package/dist/server/identity.d.ts +1 -0
- package/dist/server/identity.js +13 -0
- package/dist/server/identity.js.map +1 -0
- package/dist/server/index.d.ts +468 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +530 -36
- package/dist/server/index.js.map +1 -1
- package/dist/server/keys.d.ts +1 -57
- package/dist/server/keys.js +4 -0
- package/dist/server/keys.js.map +1 -1
- package/dist/server/mutations/account.d.ts +7 -7
- package/dist/server/mutations/account.d.ts.map +1 -1
- package/dist/server/mutations/code.d.ts +13 -13
- package/dist/server/mutations/code.d.ts.map +1 -1
- package/dist/server/mutations/index.d.ts +107 -107
- package/dist/server/mutations/index.d.ts.map +1 -1
- package/dist/server/mutations/index.js +1 -1
- package/dist/server/mutations/index.js.map +1 -1
- package/dist/server/mutations/invalidate.d.ts +5 -5
- package/dist/server/mutations/invalidate.d.ts.map +1 -1
- package/dist/server/mutations/oauth.d.ts +10 -10
- package/dist/server/mutations/oauth.d.ts.map +1 -1
- package/dist/server/mutations/oauth.js +9 -6
- package/dist/server/mutations/oauth.js.map +1 -1
- package/dist/server/mutations/refresh.d.ts +4 -4
- package/dist/server/mutations/register.d.ts +12 -12
- package/dist/server/mutations/register.d.ts.map +1 -1
- package/dist/server/mutations/retrieve.d.ts +7 -7
- package/dist/server/mutations/signature.d.ts +5 -5
- package/dist/server/mutations/signin.d.ts +6 -6
- package/dist/server/mutations/signin.d.ts.map +1 -1
- package/dist/server/mutations/signout.d.ts +1 -1
- package/dist/server/mutations/store.d.ts +3 -2
- package/dist/server/mutations/store.d.ts.map +1 -1
- package/dist/server/mutations/store.js +6 -3
- package/dist/server/mutations/store.js.map +1 -1
- package/dist/server/mutations/verifier.d.ts +1 -1
- package/dist/server/mutations/verify.d.ts +11 -11
- package/dist/server/mutations/verify.d.ts.map +1 -1
- package/dist/server/oauth.d.ts +1 -59
- package/dist/server/oauth.js +3 -0
- package/dist/server/oauth.js.map +1 -1
- package/dist/server/passkey.d.ts.map +1 -1
- package/dist/server/passkey.js +3 -2
- package/dist/server/passkey.js.map +1 -1
- package/dist/server/provider.d.ts +1 -14
- package/dist/server/provider.d.ts.map +1 -1
- package/dist/server/provider.js +2 -0
- package/dist/server/provider.js.map +1 -1
- package/dist/server/providers.js +10 -0
- package/dist/server/providers.js.map +1 -1
- package/dist/server/ratelimit.d.ts +1 -22
- package/dist/server/ratelimit.js +3 -0
- package/dist/server/ratelimit.js.map +1 -1
- package/dist/server/redirects.d.ts +1 -10
- package/dist/server/redirects.js +2 -0
- package/dist/server/redirects.js.map +1 -1
- package/dist/server/refresh.d.ts +1 -37
- package/dist/server/refresh.js +5 -0
- package/dist/server/refresh.js.map +1 -1
- package/dist/server/sessions.d.ts +1 -28
- package/dist/server/sessions.js +5 -0
- package/dist/server/sessions.js.map +1 -1
- package/dist/server/signin.d.ts +1 -55
- package/dist/server/signin.js +2 -1
- package/dist/server/signin.js.map +1 -1
- package/dist/server/sso.d.ts +1 -348
- package/dist/server/sso.js +165 -18
- package/dist/server/sso.js.map +1 -1
- package/dist/server/templates.d.ts +1 -21
- package/dist/server/templates.js +1 -0
- package/dist/server/templates.js.map +1 -1
- package/dist/server/tokens.d.ts +1 -11
- package/dist/server/tokens.js +1 -0
- package/dist/server/tokens.js.map +1 -1
- package/dist/server/totp.d.ts +1 -23
- package/dist/server/totp.js +4 -2
- package/dist/server/totp.js.map +1 -1
- package/dist/server/types.d.ts +114 -77
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js.map +1 -1
- package/dist/server/users.d.ts +1 -31
- package/dist/server/users.js +1 -0
- package/dist/server/users.js.map +1 -1
- package/dist/server/utils.d.ts +1 -27
- package/dist/server/utils.js +44 -2
- package/dist/server/utils.js.map +1 -1
- package/dist/server/version.d.ts +1 -1
- package/dist/server/version.js +1 -1
- package/dist/server/version.js.map +1 -1
- package/package.json +4 -5
- package/src/cli/bin.ts +5 -0
- package/src/cli/index.ts +22 -9
- package/src/cli/keys.ts +3 -0
- package/src/client/index.ts +36 -37
- package/src/component/_generated/api.ts +14 -0
- package/src/component/_generated/component.ts +2106 -9
- package/src/component/index.ts +3 -1
- package/src/component/model.ts +441 -0
- package/src/component/public/enterprise.ts +753 -0
- package/src/component/public/factors.ts +332 -0
- package/src/component/public/groups.ts +932 -0
- package/src/component/public/identity.ts +566 -0
- package/src/component/public/keys.ts +209 -0
- package/src/component/public/shared.ts +119 -0
- package/src/component/public.ts +5 -2965
- package/src/component/schema.ts +68 -63
- package/src/providers/sso.ts +1 -1
- package/src/server/auth.ts +413 -18
- package/src/server/cookies.ts +3 -0
- package/src/server/db.ts +3 -0
- package/src/server/device.ts +3 -1
- package/src/server/domains/core.ts +1071 -0
- package/src/server/domains/sso.ts +1749 -0
- package/src/server/enterpriseValidators.ts +93 -0
- package/src/server/factory.ts +2181 -0
- package/src/server/fx.ts +1 -0
- package/src/server/http.ts +529 -0
- package/src/server/identity.ts +18 -0
- package/src/server/index.ts +806 -40
- package/src/server/keys.ts +4 -0
- package/src/server/mutations/index.ts +1 -1
- package/src/server/mutations/oauth.ts +36 -8
- package/src/server/mutations/store.ts +6 -3
- package/src/server/oauth.ts +6 -0
- package/src/server/passkey.ts +3 -2
- package/src/server/provider.ts +2 -0
- package/src/server/providers.ts +20 -0
- package/src/server/ratelimit.ts +3 -0
- package/src/server/redirects.ts +2 -0
- package/src/server/refresh.ts +5 -0
- package/src/server/sessions.ts +5 -0
- package/src/server/signin.ts +1 -0
- package/src/server/sso.ts +259 -17
- package/src/server/templates.ts +1 -0
- package/src/server/tokens.ts +1 -0
- package/src/server/totp.ts +4 -2
- package/src/server/types.ts +178 -83
- package/src/server/users.ts +1 -0
- package/src/server/utils.ts +71 -1
- package/src/server/version.ts +1 -1
- package/dist/component/public.js.map +0 -1
- package/dist/component/server/implementation.d.ts +0 -1264
- package/dist/component/server/implementation.d.ts.map +0 -1
- package/dist/component/server/implementation.js +0 -2365
- package/dist/component/server/implementation.js.map +0 -1
- package/dist/server/cookies.d.ts.map +0 -1
- package/dist/server/db.d.ts.map +0 -1
- package/dist/server/device.d.ts.map +0 -1
- package/dist/server/implementation.d.ts +0 -1264
- package/dist/server/implementation.d.ts.map +0 -1
- package/dist/server/implementation.js +0 -2365
- package/dist/server/implementation.js.map +0 -1
- package/dist/server/keys.d.ts.map +0 -1
- package/dist/server/oauth.d.ts.map +0 -1
- package/dist/server/ratelimit.d.ts.map +0 -1
- package/dist/server/redirects.d.ts.map +0 -1
- package/dist/server/refresh.d.ts.map +0 -1
- package/dist/server/sessions.d.ts.map +0 -1
- package/dist/server/signin.d.ts.map +0 -1
- package/dist/server/sso.d.ts.map +0 -1
- package/dist/server/templates.d.ts.map +0 -1
- package/dist/server/tokens.d.ts.map +0 -1
- package/dist/server/totp.d.ts.map +0 -1
- package/dist/server/users.d.ts.map +0 -1
- package/dist/server/utils.d.ts.map +0 -1
- package/src/server/implementation.ts +0 -5336
package/dist/component/public.js
CHANGED
|
@@ -1,2146 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { accountDelete, accountGet, accountGetById, accountInsert, accountListByUser, accountPatch, refreshTokenCreate, refreshTokenDeleteAll, refreshTokenGetActive, refreshTokenGetById, refreshTokenGetChildren, refreshTokenListBySession, refreshTokenPatch, sessionCreate, sessionDelete, sessionGetById, sessionList, sessionListByUser, userDelete, userFindByVerifiedEmail, userFindByVerifiedPhone, userGetById, userInsert, userList, userPatch, userUpsert, verificationCodeCreate, verificationCodeDelete, verificationCodeGetByAccountId, verificationCodeGetByCode, verifierCreate, verifierDelete, verifierGetById, verifierGetBySignature, verifierPatch } from "./public/identity.js";
|
|
2
|
+
import { deviceAuthorize, deviceDelete, deviceGetByCodeHash, deviceGetByUserCode, deviceInsert, deviceUpdateLastPolled, passkeyDelete, passkeyGetByCredentialId, passkeyInsert, passkeyListByUserId, passkeyUpdateCounter, passkeyUpdateMeta, rateLimitCreate, rateLimitDelete, rateLimitGet, rateLimitPatch, totpDelete, totpGetById, totpGetVerifiedByUserId, totpInsert, totpListByUserId, totpMarkVerified, totpUpdateLastUsed } from "./public/factors.js";
|
|
3
|
+
import { groupCreate, groupDelete, groupGet, groupList, groupUpdate, inviteAccept, inviteAcceptByToken, inviteCreate, inviteGet, inviteGetByTokenHash, inviteList, inviteRevoke, memberAdd, memberGet, memberGetByGroupAndUser, memberList, memberListByUser, memberRemove, memberUpdate } from "./public/groups.js";
|
|
4
|
+
import { enterpriseAuditEventCreate, enterpriseAuditEventList, enterpriseCreate, enterpriseDelete, enterpriseDomainAdd, enterpriseDomainDelete, enterpriseDomainList, enterpriseDomainVerificationDelete, enterpriseDomainVerificationGet, enterpriseDomainVerificationUpsert, enterpriseDomainVerify, enterpriseGet, enterpriseGetByDomain, enterpriseGetByGroup, enterpriseList, enterpriseScimConfigGetByEnterprise, enterpriseScimConfigGetByTokenHash, enterpriseScimConfigUpsert, enterpriseScimIdentityDelete, enterpriseScimIdentityGet, enterpriseScimIdentityGetByEnterpriseAndUser, enterpriseScimIdentityGetByMappedGroup, enterpriseScimIdentityGetByUser, enterpriseScimIdentityListByEnterprise, enterpriseScimIdentityUpsert, enterpriseSecretDelete, enterpriseSecretGet, enterpriseSecretUpsert, enterpriseUpdate, enterpriseWebhookDeliveryEnqueue, enterpriseWebhookDeliveryList, enterpriseWebhookDeliveryListReady, enterpriseWebhookDeliveryPatch, enterpriseWebhookEndpointCreate, enterpriseWebhookEndpointGet, enterpriseWebhookEndpointList, enterpriseWebhookEndpointUpdate } from "./public/enterprise.js";
|
|
5
|
+
import { keyDelete, keyGetByHashedKey, keyGetById, keyInsert, keyList, keyListByUserId, keyPatch } from "./public/keys.js";
|
|
3
6
|
|
|
4
|
-
|
|
5
|
-
/** Validator for a single `{ key, value }` tag pair. */
|
|
6
|
-
const vTag = v.object({
|
|
7
|
-
key: v.string(),
|
|
8
|
-
value: v.string()
|
|
9
|
-
});
|
|
10
|
-
const TABLES = {
|
|
11
|
-
User: "User",
|
|
12
|
-
Session: "Session",
|
|
13
|
-
Account: "Account",
|
|
14
|
-
AuthVerifier: "AuthVerifier",
|
|
15
|
-
VerificationCode: "VerificationCode",
|
|
16
|
-
RefreshToken: "RefreshToken",
|
|
17
|
-
Passkey: "Passkey",
|
|
18
|
-
TotpFactor: "TotpFactor",
|
|
19
|
-
RateLimit: "RateLimit",
|
|
20
|
-
Group: "Group",
|
|
21
|
-
GroupTag: "GroupTag",
|
|
22
|
-
GroupMember: "GroupMember",
|
|
23
|
-
GroupInvite: "GroupInvite",
|
|
24
|
-
Enterprise: "Enterprise",
|
|
25
|
-
EnterpriseDomain: "EnterpriseDomain",
|
|
26
|
-
EnterpriseScimConfig: "EnterpriseScimConfig",
|
|
27
|
-
EnterpriseScimIdentity: "EnterpriseScimIdentity",
|
|
28
|
-
EnterpriseAuditEvent: "EnterpriseAuditEvent",
|
|
29
|
-
EnterpriseWebhookEndpoint: "EnterpriseWebhookEndpoint",
|
|
30
|
-
EnterpriseWebhookDelivery: "EnterpriseWebhookDelivery",
|
|
31
|
-
ApiKey: "ApiKey",
|
|
32
|
-
DeviceCode: "DeviceCode"
|
|
33
|
-
};
|
|
34
|
-
const vInviteStatus = v.union(v.literal("pending"), v.literal("accepted"), v.literal("revoked"), v.literal("expired"));
|
|
35
|
-
const vDeviceStatus = v.union(v.literal("pending"), v.literal("authorized"), v.literal("denied"));
|
|
36
|
-
const vEnterpriseStatus = v.union(v.literal("draft"), v.literal("active"), v.literal("disabled"));
|
|
37
|
-
const vScimStatus = v.union(v.literal("draft"), v.literal("active"), v.literal("disabled"));
|
|
38
|
-
const vScimResourceType = v.union(v.literal("user"), v.literal("group"));
|
|
39
|
-
const vAuditActorType = v.union(v.literal("user"), v.literal("system"), v.literal("scim"), v.literal("api_key"), v.literal("webhook"));
|
|
40
|
-
const vAuditStatus = v.union(v.literal("success"), v.literal("failure"));
|
|
41
|
-
const vWebhookEndpointStatus = v.union(v.literal("active"), v.literal("disabled"));
|
|
42
|
-
const vWebhookDeliveryStatus = v.union(v.literal("pending"), v.literal("processing"), v.literal("delivered"), v.literal("failed"));
|
|
43
|
-
const vInviteTokenAcceptStatus = v.union(v.literal("accepted"), v.literal("already_accepted"));
|
|
44
|
-
const vMembershipStatus = v.union(v.literal("joined"), v.literal("already_joined"), v.literal("not_applicable"));
|
|
45
|
-
const vApiKeyScope = v.object({
|
|
46
|
-
resource: v.string(),
|
|
47
|
-
actions: v.array(v.string())
|
|
48
|
-
});
|
|
49
|
-
const vApiKeyRateLimit = v.object({
|
|
50
|
-
maxRequests: v.number(),
|
|
51
|
-
windowMs: v.number()
|
|
52
|
-
});
|
|
53
|
-
const vApiKeyRateLimitState = v.object({
|
|
54
|
-
attemptsLeft: v.number(),
|
|
55
|
-
lastAttemptTime: v.number()
|
|
56
|
-
});
|
|
57
|
-
function vDocMeta(tableName) {
|
|
58
|
-
return {
|
|
59
|
-
_id: v.id(tableName),
|
|
60
|
-
_creationTime: v.number()
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
const vUserDoc = v.object({
|
|
64
|
-
...vDocMeta(TABLES.User),
|
|
65
|
-
name: v.optional(v.string()),
|
|
66
|
-
image: v.optional(v.string()),
|
|
67
|
-
email: v.optional(v.string()),
|
|
68
|
-
emailVerificationTime: v.optional(v.number()),
|
|
69
|
-
phone: v.optional(v.string()),
|
|
70
|
-
phoneVerificationTime: v.optional(v.number()),
|
|
71
|
-
isAnonymous: v.optional(v.boolean()),
|
|
72
|
-
extend: v.optional(v.any())
|
|
73
|
-
});
|
|
74
|
-
const vSessionDoc = v.object({
|
|
75
|
-
...vDocMeta(TABLES.Session),
|
|
76
|
-
userId: v.id(TABLES.User),
|
|
77
|
-
expirationTime: v.number()
|
|
78
|
-
});
|
|
79
|
-
const vAccountDoc = v.object({
|
|
80
|
-
...vDocMeta(TABLES.Account),
|
|
81
|
-
userId: v.id(TABLES.User),
|
|
82
|
-
provider: v.string(),
|
|
83
|
-
providerAccountId: v.string(),
|
|
84
|
-
secret: v.optional(v.string()),
|
|
85
|
-
emailVerified: v.optional(v.string()),
|
|
86
|
-
phoneVerified: v.optional(v.string()),
|
|
87
|
-
extend: v.optional(v.any())
|
|
88
|
-
});
|
|
89
|
-
const vAuthVerifierDoc = v.object({
|
|
90
|
-
...vDocMeta(TABLES.AuthVerifier),
|
|
91
|
-
sessionId: v.optional(v.id(TABLES.Session)),
|
|
92
|
-
signature: v.optional(v.string())
|
|
93
|
-
});
|
|
94
|
-
const vVerificationCodeDoc = v.object({
|
|
95
|
-
...vDocMeta(TABLES.VerificationCode),
|
|
96
|
-
accountId: v.id(TABLES.Account),
|
|
97
|
-
provider: v.string(),
|
|
98
|
-
code: v.string(),
|
|
99
|
-
expirationTime: v.number(),
|
|
100
|
-
verifier: v.optional(v.string()),
|
|
101
|
-
emailVerified: v.optional(v.string()),
|
|
102
|
-
phoneVerified: v.optional(v.string())
|
|
103
|
-
});
|
|
104
|
-
const vRefreshTokenDoc = v.object({
|
|
105
|
-
...vDocMeta(TABLES.RefreshToken),
|
|
106
|
-
sessionId: v.id(TABLES.Session),
|
|
107
|
-
expirationTime: v.number(),
|
|
108
|
-
firstUsedTime: v.optional(v.number()),
|
|
109
|
-
parentRefreshTokenId: v.optional(v.id(TABLES.RefreshToken))
|
|
110
|
-
});
|
|
111
|
-
const vPasskeyDoc = v.object({
|
|
112
|
-
...vDocMeta(TABLES.Passkey),
|
|
113
|
-
userId: v.id(TABLES.User),
|
|
114
|
-
credentialId: v.string(),
|
|
115
|
-
publicKey: v.bytes(),
|
|
116
|
-
algorithm: v.number(),
|
|
117
|
-
counter: v.number(),
|
|
118
|
-
transports: v.optional(v.array(v.string())),
|
|
119
|
-
deviceType: v.string(),
|
|
120
|
-
backedUp: v.boolean(),
|
|
121
|
-
name: v.optional(v.string()),
|
|
122
|
-
createdAt: v.number(),
|
|
123
|
-
lastUsedAt: v.optional(v.number())
|
|
124
|
-
});
|
|
125
|
-
const vTotpFactorDoc = v.object({
|
|
126
|
-
...vDocMeta(TABLES.TotpFactor),
|
|
127
|
-
userId: v.id(TABLES.User),
|
|
128
|
-
secret: v.bytes(),
|
|
129
|
-
digits: v.number(),
|
|
130
|
-
period: v.number(),
|
|
131
|
-
verified: v.boolean(),
|
|
132
|
-
name: v.optional(v.string()),
|
|
133
|
-
createdAt: v.number(),
|
|
134
|
-
lastUsedAt: v.optional(v.number())
|
|
135
|
-
});
|
|
136
|
-
v.object({
|
|
137
|
-
...vDocMeta(TABLES.RateLimit),
|
|
138
|
-
identifier: v.string(),
|
|
139
|
-
last_attempt_time: v.number(),
|
|
140
|
-
attempts_left: v.number()
|
|
141
|
-
});
|
|
142
|
-
const vGroupDoc = v.object({
|
|
143
|
-
...vDocMeta(TABLES.Group),
|
|
144
|
-
name: v.string(),
|
|
145
|
-
slug: v.optional(v.string()),
|
|
146
|
-
type: v.optional(v.string()),
|
|
147
|
-
parentGroupId: v.optional(v.id(TABLES.Group)),
|
|
148
|
-
tags: v.optional(v.array(vTag)),
|
|
149
|
-
extend: v.optional(v.any())
|
|
150
|
-
});
|
|
151
|
-
const vGroupMemberDoc = v.object({
|
|
152
|
-
...vDocMeta(TABLES.GroupMember),
|
|
153
|
-
groupId: v.id(TABLES.Group),
|
|
154
|
-
userId: v.id(TABLES.User),
|
|
155
|
-
role: v.optional(v.string()),
|
|
156
|
-
status: v.optional(v.string()),
|
|
157
|
-
extend: v.optional(v.any())
|
|
158
|
-
});
|
|
159
|
-
const vGroupInviteDoc = v.object({
|
|
160
|
-
...vDocMeta(TABLES.GroupInvite),
|
|
161
|
-
groupId: v.optional(v.id(TABLES.Group)),
|
|
162
|
-
invitedByUserId: v.optional(v.id(TABLES.User)),
|
|
163
|
-
email: v.optional(v.string()),
|
|
164
|
-
tokenHash: v.string(),
|
|
165
|
-
role: v.optional(v.string()),
|
|
166
|
-
status: vInviteStatus,
|
|
167
|
-
expiresTime: v.optional(v.number()),
|
|
168
|
-
acceptedByUserId: v.optional(v.id(TABLES.User)),
|
|
169
|
-
acceptedTime: v.optional(v.number()),
|
|
170
|
-
extend: v.optional(v.any())
|
|
171
|
-
});
|
|
172
|
-
const vApiKeyDoc = v.object({
|
|
173
|
-
...vDocMeta(TABLES.ApiKey),
|
|
174
|
-
userId: v.id(TABLES.User),
|
|
175
|
-
prefix: v.string(),
|
|
176
|
-
hashedKey: v.string(),
|
|
177
|
-
name: v.string(),
|
|
178
|
-
scopes: v.array(vApiKeyScope),
|
|
179
|
-
rateLimit: v.optional(vApiKeyRateLimit),
|
|
180
|
-
rateLimitState: v.optional(vApiKeyRateLimitState),
|
|
181
|
-
expiresAt: v.optional(v.number()),
|
|
182
|
-
lastUsedAt: v.optional(v.number()),
|
|
183
|
-
createdAt: v.number(),
|
|
184
|
-
revoked: v.boolean()
|
|
185
|
-
});
|
|
186
|
-
const vDeviceCodeDoc = v.object({
|
|
187
|
-
...vDocMeta(TABLES.DeviceCode),
|
|
188
|
-
deviceCodeHash: v.string(),
|
|
189
|
-
userCode: v.string(),
|
|
190
|
-
expiresAt: v.number(),
|
|
191
|
-
interval: v.number(),
|
|
192
|
-
status: vDeviceStatus,
|
|
193
|
-
userId: v.optional(v.id(TABLES.User)),
|
|
194
|
-
sessionId: v.optional(v.id(TABLES.Session)),
|
|
195
|
-
lastPolledAt: v.optional(v.number())
|
|
196
|
-
});
|
|
197
|
-
const vEnterpriseDoc = v.object({
|
|
198
|
-
...vDocMeta(TABLES.Enterprise),
|
|
199
|
-
groupId: v.id(TABLES.Group),
|
|
200
|
-
slug: v.optional(v.string()),
|
|
201
|
-
name: v.optional(v.string()),
|
|
202
|
-
status: vEnterpriseStatus,
|
|
203
|
-
config: v.optional(v.any()),
|
|
204
|
-
extend: v.optional(v.any())
|
|
205
|
-
});
|
|
206
|
-
const vEnterpriseDomainDoc = v.object({
|
|
207
|
-
...vDocMeta(TABLES.EnterpriseDomain),
|
|
208
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
209
|
-
groupId: v.id(TABLES.Group),
|
|
210
|
-
domain: v.string(),
|
|
211
|
-
isPrimary: v.boolean(),
|
|
212
|
-
verifiedAt: v.optional(v.number())
|
|
213
|
-
});
|
|
214
|
-
const vEnterpriseScimConfigDoc = v.object({
|
|
215
|
-
...vDocMeta(TABLES.EnterpriseScimConfig),
|
|
216
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
217
|
-
groupId: v.id(TABLES.Group),
|
|
218
|
-
status: vScimStatus,
|
|
219
|
-
basePath: v.string(),
|
|
220
|
-
tokenHash: v.string(),
|
|
221
|
-
lastRotatedAt: v.optional(v.number()),
|
|
222
|
-
deprovisionMode: v.optional(v.union(v.literal("soft"), v.literal("hard"))),
|
|
223
|
-
extend: v.optional(v.any())
|
|
224
|
-
});
|
|
225
|
-
const vEnterpriseScimIdentityDoc = v.object({
|
|
226
|
-
...vDocMeta(TABLES.EnterpriseScimIdentity),
|
|
227
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
228
|
-
groupId: v.id(TABLES.Group),
|
|
229
|
-
resourceType: vScimResourceType,
|
|
230
|
-
externalId: v.string(),
|
|
231
|
-
userId: v.optional(v.id(TABLES.User)),
|
|
232
|
-
mappedGroupId: v.optional(v.id(TABLES.Group)),
|
|
233
|
-
lastProvisionedAt: v.optional(v.number()),
|
|
234
|
-
active: v.optional(v.boolean()),
|
|
235
|
-
raw: v.optional(v.any())
|
|
236
|
-
});
|
|
237
|
-
const vEnterpriseAuditEventDoc = v.object({
|
|
238
|
-
...vDocMeta(TABLES.EnterpriseAuditEvent),
|
|
239
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
240
|
-
groupId: v.id(TABLES.Group),
|
|
241
|
-
eventType: v.string(),
|
|
242
|
-
actorType: vAuditActorType,
|
|
243
|
-
actorId: v.optional(v.string()),
|
|
244
|
-
subjectType: v.string(),
|
|
245
|
-
subjectId: v.optional(v.string()),
|
|
246
|
-
status: vAuditStatus,
|
|
247
|
-
occurredAt: v.number(),
|
|
248
|
-
requestId: v.optional(v.string()),
|
|
249
|
-
ip: v.optional(v.string()),
|
|
250
|
-
metadata: v.optional(v.any())
|
|
251
|
-
});
|
|
252
|
-
const vEnterpriseWebhookEndpointDoc = v.object({
|
|
253
|
-
...vDocMeta(TABLES.EnterpriseWebhookEndpoint),
|
|
254
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
255
|
-
groupId: v.id(TABLES.Group),
|
|
256
|
-
url: v.string(),
|
|
257
|
-
status: vWebhookEndpointStatus,
|
|
258
|
-
secretHash: v.string(),
|
|
259
|
-
subscriptions: v.array(v.string()),
|
|
260
|
-
createdByUserId: v.optional(v.id(TABLES.User)),
|
|
261
|
-
lastSuccessAt: v.optional(v.number()),
|
|
262
|
-
lastFailureAt: v.optional(v.number()),
|
|
263
|
-
failureCount: v.number(),
|
|
264
|
-
extend: v.optional(v.any())
|
|
265
|
-
});
|
|
266
|
-
const vEnterpriseWebhookDeliveryDoc = v.object({
|
|
267
|
-
...vDocMeta(TABLES.EnterpriseWebhookDelivery),
|
|
268
|
-
enterpriseId: v.id(TABLES.Enterprise),
|
|
269
|
-
endpointId: v.id(TABLES.EnterpriseWebhookEndpoint),
|
|
270
|
-
auditEventId: v.optional(v.id(TABLES.EnterpriseAuditEvent)),
|
|
271
|
-
eventType: v.string(),
|
|
272
|
-
status: vWebhookDeliveryStatus,
|
|
273
|
-
attemptCount: v.number(),
|
|
274
|
-
nextAttemptAt: v.number(),
|
|
275
|
-
lastAttemptAt: v.optional(v.number()),
|
|
276
|
-
lastResponseStatus: v.optional(v.number()),
|
|
277
|
-
lastError: v.optional(v.string()),
|
|
278
|
-
payload: v.any()
|
|
279
|
-
});
|
|
280
|
-
const vRateLimitResult = v.object({
|
|
281
|
-
...vDocMeta(TABLES.RateLimit),
|
|
282
|
-
identifier: v.string(),
|
|
283
|
-
last_attempt_time: v.number(),
|
|
284
|
-
attempts_left: v.number(),
|
|
285
|
-
attemptsLeft: v.number(),
|
|
286
|
-
lastAttemptTime: v.number()
|
|
287
|
-
});
|
|
288
|
-
const vInviteAcceptByTokenResult = v.object({
|
|
289
|
-
inviteId: v.id(TABLES.GroupInvite),
|
|
290
|
-
groupId: v.union(v.id(TABLES.Group), v.null()),
|
|
291
|
-
memberId: v.optional(v.id(TABLES.GroupMember)),
|
|
292
|
-
inviteStatus: vInviteTokenAcceptStatus,
|
|
293
|
-
membershipStatus: vMembershipStatus
|
|
294
|
-
});
|
|
295
|
-
const vPaginated = (item) => v.object({
|
|
296
|
-
items: v.array(item),
|
|
297
|
-
nextCursor: v.union(v.string(), v.null())
|
|
298
|
-
});
|
|
299
|
-
/** Normalize a single tag: trim + lowercase key and value. */
|
|
300
|
-
function normalizeTag(tag) {
|
|
301
|
-
return {
|
|
302
|
-
key: tag.key.trim().toLowerCase(),
|
|
303
|
-
value: tag.value.trim().toLowerCase()
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Normalize and deduplicate an array of tags.
|
|
308
|
-
* Deduplication is based on the normalized `key\0value` composite.
|
|
309
|
-
*/
|
|
310
|
-
function normalizeTags(tags) {
|
|
311
|
-
const seen = /* @__PURE__ */ new Set();
|
|
312
|
-
const result = [];
|
|
313
|
-
for (const raw of tags) {
|
|
314
|
-
const t = normalizeTag(raw);
|
|
315
|
-
const composite = `${t.key}\0${t.value}`;
|
|
316
|
-
if (!seen.has(composite)) {
|
|
317
|
-
seen.add(composite);
|
|
318
|
-
result.push(t);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
return result;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* List users with optional filtering, sorting, and pagination.
|
|
325
|
-
*
|
|
326
|
-
* Returns `{ items, nextCursor }` — pass `nextCursor` back as `cursor`
|
|
327
|
-
* for the next page, or `null` when exhausted.
|
|
328
|
-
*/
|
|
329
|
-
const userList = query({
|
|
330
|
-
args: {
|
|
331
|
-
where: v.optional(v.object({
|
|
332
|
-
email: v.optional(v.string()),
|
|
333
|
-
phone: v.optional(v.string()),
|
|
334
|
-
isAnonymous: v.optional(v.boolean()),
|
|
335
|
-
name: v.optional(v.string())
|
|
336
|
-
})),
|
|
337
|
-
limit: v.optional(v.number()),
|
|
338
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
339
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("name"), v.literal("email"), v.literal("phone"))),
|
|
340
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
341
|
-
},
|
|
342
|
-
returns: vPaginated(vUserDoc),
|
|
343
|
-
handler: async (ctx, args) => {
|
|
344
|
-
const where = args.where ?? {};
|
|
345
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
346
|
-
const order = args.order ?? "desc";
|
|
347
|
-
let q;
|
|
348
|
-
if (where.email !== void 0) q = ctx.db.query("User").withIndex("email", (idx) => idx.eq("email", where.email));
|
|
349
|
-
else if (where.phone !== void 0) q = ctx.db.query("User").withIndex("phone", (idx) => idx.eq("phone", where.phone));
|
|
350
|
-
else q = ctx.db.query("User");
|
|
351
|
-
if (where.isAnonymous !== void 0) q = q.filter((f) => f.eq(f.field("isAnonymous"), where.isAnonymous));
|
|
352
|
-
if (where.name !== void 0) q = q.filter((f) => f.eq(f.field("name"), where.name));
|
|
353
|
-
if (where.email !== void 0 && where.phone !== void 0) q = q.filter((f) => f.eq(f.field("phone"), where.phone));
|
|
354
|
-
q = q.order(order);
|
|
355
|
-
const all = await q.collect();
|
|
356
|
-
let startIdx = 0;
|
|
357
|
-
if (args.cursor) {
|
|
358
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
359
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
360
|
-
}
|
|
361
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
362
|
-
const hasMore = page.length > limit;
|
|
363
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
364
|
-
return {
|
|
365
|
-
items,
|
|
366
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
/** Retrieve a user by their document ID. */
|
|
371
|
-
const userGetById = query({
|
|
372
|
-
args: { userId: v.id("User") },
|
|
373
|
-
returns: v.union(vUserDoc, v.null()),
|
|
374
|
-
handler: async (ctx, { userId }) => {
|
|
375
|
-
return await ctx.db.get("User", userId);
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
/**
|
|
379
|
-
* Find a user by their verified email address. Returns `null` if no user
|
|
380
|
-
* has this email verified, or if multiple users share the same verified email
|
|
381
|
-
* (ambiguous — should not happen in normal operation).
|
|
382
|
-
*/
|
|
383
|
-
const userFindByVerifiedEmail = query({
|
|
384
|
-
args: { email: v.string() },
|
|
385
|
-
returns: v.union(vUserDoc, v.null()),
|
|
386
|
-
handler: async (ctx, { email }) => {
|
|
387
|
-
const users = await ctx.db.query("User").withIndex("email", (q) => q.eq("email", email)).filter((q) => q.neq(q.field("emailVerificationTime"), void 0)).take(2);
|
|
388
|
-
return users.length === 1 ? users[0] : null;
|
|
389
|
-
}
|
|
390
|
-
});
|
|
391
|
-
/**
|
|
392
|
-
* Find a user by their verified phone number. Returns `null` if no user
|
|
393
|
-
* has this phone verified, or if multiple users share the same verified phone
|
|
394
|
-
* (ambiguous — should not happen in normal operation).
|
|
395
|
-
*/
|
|
396
|
-
const userFindByVerifiedPhone = query({
|
|
397
|
-
args: { phone: v.string() },
|
|
398
|
-
returns: v.union(vUserDoc, v.null()),
|
|
399
|
-
handler: async (ctx, { phone }) => {
|
|
400
|
-
const users = await ctx.db.query("User").withIndex("phone", (q) => q.eq("phone", phone)).filter((q) => q.neq(q.field("phoneVerificationTime"), void 0)).take(2);
|
|
401
|
-
return users.length === 1 ? users[0] : null;
|
|
402
|
-
}
|
|
403
|
-
});
|
|
404
|
-
/** Insert a new user document. */
|
|
405
|
-
const userInsert = mutation({
|
|
406
|
-
args: { data: v.any() },
|
|
407
|
-
returns: v.id("User"),
|
|
408
|
-
handler: async (ctx, { data }) => {
|
|
409
|
-
return await ctx.db.insert("User", data);
|
|
410
|
-
}
|
|
411
|
-
});
|
|
412
|
-
/** Insert a new user or update an existing one. */
|
|
413
|
-
const userUpsert = mutation({
|
|
414
|
-
args: {
|
|
415
|
-
userId: v.optional(v.id("User")),
|
|
416
|
-
data: v.any()
|
|
417
|
-
},
|
|
418
|
-
returns: v.id("User"),
|
|
419
|
-
handler: async (ctx, { userId, data }) => {
|
|
420
|
-
if (userId !== void 0) {
|
|
421
|
-
await ctx.db.patch("User", userId, data);
|
|
422
|
-
return userId;
|
|
423
|
-
}
|
|
424
|
-
return await ctx.db.insert("User", data);
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
/** Patch an existing user document with partial data. */
|
|
428
|
-
const userPatch = mutation({
|
|
429
|
-
args: {
|
|
430
|
-
userId: v.id("User"),
|
|
431
|
-
data: v.any()
|
|
432
|
-
},
|
|
433
|
-
returns: v.null(),
|
|
434
|
-
handler: async (ctx, { userId, data }) => {
|
|
435
|
-
await ctx.db.patch("User", userId, data);
|
|
436
|
-
return null;
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
/** Delete a user document by ID. No-op if the user does not exist. */
|
|
440
|
-
const userDelete = mutation({
|
|
441
|
-
args: { userId: v.id("User") },
|
|
442
|
-
returns: v.null(),
|
|
443
|
-
handler: async (ctx, { userId }) => {
|
|
444
|
-
if (await ctx.db.get("User", userId) !== null) await ctx.db.delete("User", userId);
|
|
445
|
-
return null;
|
|
446
|
-
}
|
|
447
|
-
});
|
|
448
|
-
/** List all accounts for a user. */
|
|
449
|
-
const accountListByUser = query({
|
|
450
|
-
args: { userId: v.id("User") },
|
|
451
|
-
returns: v.array(vAccountDoc),
|
|
452
|
-
handler: async (ctx, { userId }) => {
|
|
453
|
-
return await ctx.db.query("Account").withIndex("user_id_provider", (q) => q.eq("userId", userId)).collect();
|
|
454
|
-
}
|
|
455
|
-
});
|
|
456
|
-
/** Look up an account by provider and provider-specific account ID. */
|
|
457
|
-
const accountGet = query({
|
|
458
|
-
args: {
|
|
459
|
-
provider: v.string(),
|
|
460
|
-
providerAccountId: v.string()
|
|
461
|
-
},
|
|
462
|
-
returns: v.union(vAccountDoc, v.null()),
|
|
463
|
-
handler: async (ctx, { provider, providerAccountId }) => {
|
|
464
|
-
return await ctx.db.query("Account").withIndex("provider_account_id", (q) => q.eq("provider", provider).eq("providerAccountId", providerAccountId)).unique();
|
|
465
|
-
}
|
|
466
|
-
});
|
|
467
|
-
/** Retrieve an account by its document ID. */
|
|
468
|
-
const accountGetById = query({
|
|
469
|
-
args: { accountId: v.id("Account") },
|
|
470
|
-
returns: v.union(vAccountDoc, v.null()),
|
|
471
|
-
handler: async (ctx, { accountId }) => {
|
|
472
|
-
return await ctx.db.get("Account", accountId);
|
|
473
|
-
}
|
|
474
|
-
});
|
|
475
|
-
/** Create a new account linking a user to an auth provider. */
|
|
476
|
-
const accountInsert = mutation({
|
|
477
|
-
args: {
|
|
478
|
-
userId: v.id("User"),
|
|
479
|
-
provider: v.string(),
|
|
480
|
-
providerAccountId: v.string(),
|
|
481
|
-
secret: v.optional(v.string()),
|
|
482
|
-
extend: v.optional(v.any())
|
|
483
|
-
},
|
|
484
|
-
returns: v.id("Account"),
|
|
485
|
-
handler: async (ctx, args) => {
|
|
486
|
-
return await ctx.db.insert("Account", args);
|
|
487
|
-
}
|
|
488
|
-
});
|
|
489
|
-
/** Patch an existing account document with partial data. */
|
|
490
|
-
const accountPatch = mutation({
|
|
491
|
-
args: {
|
|
492
|
-
accountId: v.id("Account"),
|
|
493
|
-
data: v.any()
|
|
494
|
-
},
|
|
495
|
-
returns: v.null(),
|
|
496
|
-
handler: async (ctx, { accountId, data }) => {
|
|
497
|
-
await ctx.db.patch("Account", accountId, data);
|
|
498
|
-
return null;
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
/** Delete an account document. */
|
|
502
|
-
const accountDelete = mutation({
|
|
503
|
-
args: { accountId: v.id("Account") },
|
|
504
|
-
returns: v.null(),
|
|
505
|
-
handler: async (ctx, { accountId }) => {
|
|
506
|
-
await ctx.db.delete("Account", accountId);
|
|
507
|
-
return null;
|
|
508
|
-
}
|
|
509
|
-
});
|
|
510
|
-
/**
|
|
511
|
-
* List sessions with optional filtering and pagination.
|
|
512
|
-
*
|
|
513
|
-
* Returns `{ items, nextCursor }`.
|
|
514
|
-
*/
|
|
515
|
-
const sessionList = query({
|
|
516
|
-
args: {
|
|
517
|
-
where: v.optional(v.object({ userId: v.optional(v.id("User")) })),
|
|
518
|
-
limit: v.optional(v.number()),
|
|
519
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
520
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
521
|
-
},
|
|
522
|
-
returns: vPaginated(vSessionDoc),
|
|
523
|
-
handler: async (ctx, args) => {
|
|
524
|
-
const where = args.where ?? {};
|
|
525
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
526
|
-
const order = args.order ?? "desc";
|
|
527
|
-
let q;
|
|
528
|
-
if (where.userId !== void 0) q = ctx.db.query("Session").withIndex("user_id", (idx) => idx.eq("userId", where.userId));
|
|
529
|
-
else q = ctx.db.query("Session");
|
|
530
|
-
q = q.order(order);
|
|
531
|
-
const all = await q.collect();
|
|
532
|
-
let startIdx = 0;
|
|
533
|
-
if (args.cursor) {
|
|
534
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
535
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
536
|
-
}
|
|
537
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
538
|
-
const hasMore = page.length > limit;
|
|
539
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
540
|
-
return {
|
|
541
|
-
items,
|
|
542
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
543
|
-
};
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
|
-
/** Create a new session for a user with an expiration time. */
|
|
547
|
-
const sessionCreate = mutation({
|
|
548
|
-
args: {
|
|
549
|
-
userId: v.id("User"),
|
|
550
|
-
expirationTime: v.number()
|
|
551
|
-
},
|
|
552
|
-
returns: v.id("Session"),
|
|
553
|
-
handler: async (ctx, { userId, expirationTime }) => {
|
|
554
|
-
return await ctx.db.insert("Session", {
|
|
555
|
-
userId,
|
|
556
|
-
expirationTime
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
});
|
|
560
|
-
/** Retrieve a session by its document ID. */
|
|
561
|
-
const sessionGetById = query({
|
|
562
|
-
args: { sessionId: v.id("Session") },
|
|
563
|
-
returns: v.union(vSessionDoc, v.null()),
|
|
564
|
-
handler: async (ctx, { sessionId }) => {
|
|
565
|
-
return await ctx.db.get("Session", sessionId);
|
|
566
|
-
}
|
|
567
|
-
});
|
|
568
|
-
/** Delete a session. No-op if the session does not exist. */
|
|
569
|
-
const sessionDelete = mutation({
|
|
570
|
-
args: { sessionId: v.id("Session") },
|
|
571
|
-
returns: v.null(),
|
|
572
|
-
handler: async (ctx, { sessionId }) => {
|
|
573
|
-
if (await ctx.db.get("Session", sessionId) !== null) await ctx.db.delete("Session", sessionId);
|
|
574
|
-
return null;
|
|
575
|
-
}
|
|
576
|
-
});
|
|
577
|
-
/** List all sessions for a user. */
|
|
578
|
-
const sessionListByUser = query({
|
|
579
|
-
args: { userId: v.id("User") },
|
|
580
|
-
returns: v.array(vSessionDoc),
|
|
581
|
-
handler: async (ctx, { userId }) => {
|
|
582
|
-
return await ctx.db.query("Session").withIndex("user_id", (q) => q.eq("userId", userId)).collect();
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
/** Create a new PKCE verifier, optionally linked to a session. */
|
|
586
|
-
const verifierCreate = mutation({
|
|
587
|
-
args: { sessionId: v.optional(v.id("Session")) },
|
|
588
|
-
returns: v.id("AuthVerifier"),
|
|
589
|
-
handler: async (ctx, { sessionId }) => {
|
|
590
|
-
return await ctx.db.insert("AuthVerifier", { sessionId });
|
|
591
|
-
}
|
|
592
|
-
});
|
|
593
|
-
/** Retrieve a verifier by its document ID. */
|
|
594
|
-
const verifierGetById = query({
|
|
595
|
-
args: { verifierId: v.id("AuthVerifier") },
|
|
596
|
-
returns: v.union(vAuthVerifierDoc, v.null()),
|
|
597
|
-
handler: async (ctx, { verifierId }) => {
|
|
598
|
-
return await ctx.db.get("AuthVerifier", verifierId);
|
|
599
|
-
}
|
|
600
|
-
});
|
|
601
|
-
/** Look up a verifier by its cryptographic signature. */
|
|
602
|
-
const verifierGetBySignature = query({
|
|
603
|
-
args: { signature: v.string() },
|
|
604
|
-
returns: v.union(vAuthVerifierDoc, v.null()),
|
|
605
|
-
handler: async (ctx, { signature }) => {
|
|
606
|
-
return await ctx.db.query("AuthVerifier").withIndex("signature", (q) => q.eq("signature", signature)).unique();
|
|
607
|
-
}
|
|
608
|
-
});
|
|
609
|
-
/** Patch a verifier document with partial data. */
|
|
610
|
-
const verifierPatch = mutation({
|
|
611
|
-
args: {
|
|
612
|
-
verifierId: v.id("AuthVerifier"),
|
|
613
|
-
data: v.any()
|
|
614
|
-
},
|
|
615
|
-
returns: v.null(),
|
|
616
|
-
handler: async (ctx, { verifierId, data }) => {
|
|
617
|
-
await ctx.db.patch("AuthVerifier", verifierId, data);
|
|
618
|
-
return null;
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
/** Delete a verifier document. */
|
|
622
|
-
const verifierDelete = mutation({
|
|
623
|
-
args: { verifierId: v.id("AuthVerifier") },
|
|
624
|
-
returns: v.null(),
|
|
625
|
-
handler: async (ctx, { verifierId }) => {
|
|
626
|
-
await ctx.db.delete("AuthVerifier", verifierId);
|
|
627
|
-
return null;
|
|
628
|
-
}
|
|
629
|
-
});
|
|
630
|
-
/** Find a verification code by its associated account ID. */
|
|
631
|
-
const verificationCodeGetByAccountId = query({
|
|
632
|
-
args: { accountId: v.id("Account") },
|
|
633
|
-
returns: v.union(vVerificationCodeDoc, v.null()),
|
|
634
|
-
handler: async (ctx, { accountId }) => {
|
|
635
|
-
return await ctx.db.query("VerificationCode").withIndex("account_id", (q) => q.eq("accountId", accountId)).unique();
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
/** Find a verification code by its code string. */
|
|
639
|
-
const verificationCodeGetByCode = query({
|
|
640
|
-
args: { code: v.string() },
|
|
641
|
-
returns: v.union(vVerificationCodeDoc, v.null()),
|
|
642
|
-
handler: async (ctx, { code }) => {
|
|
643
|
-
return await ctx.db.query("VerificationCode").withIndex("code", (q) => q.eq("code", code)).unique();
|
|
644
|
-
}
|
|
645
|
-
});
|
|
646
|
-
/** Create a new verification code for OTP, magic link, or OAuth flows. */
|
|
647
|
-
const verificationCodeCreate = mutation({
|
|
648
|
-
args: {
|
|
649
|
-
accountId: v.id("Account"),
|
|
650
|
-
provider: v.string(),
|
|
651
|
-
code: v.string(),
|
|
652
|
-
expirationTime: v.number(),
|
|
653
|
-
verifier: v.optional(v.string()),
|
|
654
|
-
emailVerified: v.optional(v.string()),
|
|
655
|
-
phoneVerified: v.optional(v.string())
|
|
656
|
-
},
|
|
657
|
-
returns: v.id("VerificationCode"),
|
|
658
|
-
handler: async (ctx, args) => {
|
|
659
|
-
return await ctx.db.insert("VerificationCode", args);
|
|
660
|
-
}
|
|
661
|
-
});
|
|
662
|
-
/** Delete a verification code document. */
|
|
663
|
-
const verificationCodeDelete = mutation({
|
|
664
|
-
args: { verificationCodeId: v.id("VerificationCode") },
|
|
665
|
-
returns: v.null(),
|
|
666
|
-
handler: async (ctx, { verificationCodeId }) => {
|
|
667
|
-
await ctx.db.delete("VerificationCode", verificationCodeId);
|
|
668
|
-
return null;
|
|
669
|
-
}
|
|
670
|
-
});
|
|
671
|
-
/** Create a new refresh token for a session. */
|
|
672
|
-
const refreshTokenCreate = mutation({
|
|
673
|
-
args: {
|
|
674
|
-
sessionId: v.id("Session"),
|
|
675
|
-
expirationTime: v.number(),
|
|
676
|
-
parentRefreshTokenId: v.optional(v.id("RefreshToken"))
|
|
677
|
-
},
|
|
678
|
-
returns: v.id("RefreshToken"),
|
|
679
|
-
handler: async (ctx, args) => {
|
|
680
|
-
return await ctx.db.insert("RefreshToken", args);
|
|
681
|
-
}
|
|
682
|
-
});
|
|
683
|
-
/** Retrieve a refresh token by its document ID. */
|
|
684
|
-
const refreshTokenGetById = query({
|
|
685
|
-
args: { refreshTokenId: v.id("RefreshToken") },
|
|
686
|
-
returns: v.union(vRefreshTokenDoc, v.null()),
|
|
687
|
-
handler: async (ctx, { refreshTokenId }) => {
|
|
688
|
-
return await ctx.db.get("RefreshToken", refreshTokenId);
|
|
689
|
-
}
|
|
690
|
-
});
|
|
691
|
-
/** Patch a refresh token document with partial data. */
|
|
692
|
-
const refreshTokenPatch = mutation({
|
|
693
|
-
args: {
|
|
694
|
-
refreshTokenId: v.id("RefreshToken"),
|
|
695
|
-
data: v.any()
|
|
696
|
-
},
|
|
697
|
-
returns: v.null(),
|
|
698
|
-
handler: async (ctx, { refreshTokenId, data }) => {
|
|
699
|
-
await ctx.db.patch("RefreshToken", refreshTokenId, data);
|
|
700
|
-
return null;
|
|
701
|
-
}
|
|
702
|
-
});
|
|
703
|
-
/** Get child tokens that were created by exchanging a specific parent token. */
|
|
704
|
-
const refreshTokenGetChildren = query({
|
|
705
|
-
args: {
|
|
706
|
-
sessionId: v.id("Session"),
|
|
707
|
-
parentRefreshTokenId: v.id("RefreshToken")
|
|
708
|
-
},
|
|
709
|
-
returns: v.array(vRefreshTokenDoc),
|
|
710
|
-
handler: async (ctx, { sessionId, parentRefreshTokenId }) => {
|
|
711
|
-
return await ctx.db.query("RefreshToken").withIndex("session_id_parent_refresh_token_id", (q) => q.eq("sessionId", sessionId).eq("parentRefreshTokenId", parentRefreshTokenId)).collect();
|
|
712
|
-
}
|
|
713
|
-
});
|
|
714
|
-
/** List all refresh tokens for a session. */
|
|
715
|
-
const refreshTokenListBySession = query({
|
|
716
|
-
args: { sessionId: v.id("Session") },
|
|
717
|
-
returns: v.array(vRefreshTokenDoc),
|
|
718
|
-
handler: async (ctx, { sessionId }) => {
|
|
719
|
-
return await ctx.db.query("RefreshToken").withIndex("session_id_parent_refresh_token_id", (q) => q.eq("sessionId", sessionId)).collect();
|
|
720
|
-
}
|
|
721
|
-
});
|
|
722
|
-
/** Delete all refresh tokens for a session. */
|
|
723
|
-
const refreshTokenDeleteAll = mutation({
|
|
724
|
-
args: { sessionId: v.id("Session") },
|
|
725
|
-
returns: v.null(),
|
|
726
|
-
handler: async (ctx, { sessionId }) => {
|
|
727
|
-
const tokens = await ctx.db.query("RefreshToken").withIndex("session_id_parent_refresh_token_id", (q) => q.eq("sessionId", sessionId)).collect();
|
|
728
|
-
await Promise.all(tokens.map((token) => ctx.db.delete("RefreshToken", token._id)));
|
|
729
|
-
return null;
|
|
730
|
-
}
|
|
731
|
-
});
|
|
732
|
-
/** Get the active (unused) refresh token for a session. */
|
|
733
|
-
const refreshTokenGetActive = query({
|
|
734
|
-
args: { sessionId: v.id("Session") },
|
|
735
|
-
returns: v.union(vRefreshTokenDoc, v.null()),
|
|
736
|
-
handler: async (ctx, { sessionId }) => {
|
|
737
|
-
return await ctx.db.query("RefreshToken").withIndex("session_id", (q) => q.eq("sessionId", sessionId)).filter((q) => q.eq(q.field("firstUsedTime"), void 0)).order("desc").first();
|
|
738
|
-
}
|
|
739
|
-
});
|
|
740
|
-
/** Store a new passkey credential for a user. */
|
|
741
|
-
const passkeyInsert = mutation({
|
|
742
|
-
args: {
|
|
743
|
-
userId: v.id("User"),
|
|
744
|
-
credentialId: v.string(),
|
|
745
|
-
publicKey: v.bytes(),
|
|
746
|
-
algorithm: v.number(),
|
|
747
|
-
counter: v.number(),
|
|
748
|
-
transports: v.optional(v.array(v.string())),
|
|
749
|
-
deviceType: v.string(),
|
|
750
|
-
backedUp: v.boolean(),
|
|
751
|
-
name: v.optional(v.string()),
|
|
752
|
-
createdAt: v.number()
|
|
753
|
-
},
|
|
754
|
-
returns: v.id("Passkey"),
|
|
755
|
-
handler: async (ctx, args) => {
|
|
756
|
-
return await ctx.db.insert("Passkey", args);
|
|
757
|
-
}
|
|
758
|
-
});
|
|
759
|
-
/** Look up a passkey by its credential ID. */
|
|
760
|
-
const passkeyGetByCredentialId = query({
|
|
761
|
-
args: { credentialId: v.string() },
|
|
762
|
-
returns: v.union(vPasskeyDoc, v.null()),
|
|
763
|
-
handler: async (ctx, { credentialId }) => {
|
|
764
|
-
return await ctx.db.query("Passkey").withIndex("credential_id", (q) => q.eq("credentialId", credentialId)).unique();
|
|
765
|
-
}
|
|
766
|
-
});
|
|
767
|
-
/** List all passkeys for a user. */
|
|
768
|
-
const passkeyListByUserId = query({
|
|
769
|
-
args: { userId: v.id("User") },
|
|
770
|
-
returns: v.array(vPasskeyDoc),
|
|
771
|
-
handler: async (ctx, { userId }) => {
|
|
772
|
-
return await ctx.db.query("Passkey").withIndex("user_id", (q) => q.eq("userId", userId)).collect();
|
|
773
|
-
}
|
|
774
|
-
});
|
|
775
|
-
/** Update a passkey's counter and last used timestamp after authentication. */
|
|
776
|
-
const passkeyUpdateCounter = mutation({
|
|
777
|
-
args: {
|
|
778
|
-
passkeyId: v.id("Passkey"),
|
|
779
|
-
counter: v.number(),
|
|
780
|
-
lastUsedAt: v.number()
|
|
781
|
-
},
|
|
782
|
-
returns: v.null(),
|
|
783
|
-
handler: async (ctx, { passkeyId, counter, lastUsedAt }) => {
|
|
784
|
-
await ctx.db.patch("Passkey", passkeyId, {
|
|
785
|
-
counter,
|
|
786
|
-
lastUsedAt
|
|
787
|
-
});
|
|
788
|
-
return null;
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
/** Update a passkey's metadata (name). */
|
|
792
|
-
const passkeyUpdateMeta = mutation({
|
|
793
|
-
args: {
|
|
794
|
-
passkeyId: v.id("Passkey"),
|
|
795
|
-
data: v.any()
|
|
796
|
-
},
|
|
797
|
-
returns: v.null(),
|
|
798
|
-
handler: async (ctx, { passkeyId, data }) => {
|
|
799
|
-
await ctx.db.patch("Passkey", passkeyId, data);
|
|
800
|
-
return null;
|
|
801
|
-
}
|
|
802
|
-
});
|
|
803
|
-
/** Delete a passkey credential. */
|
|
804
|
-
const passkeyDelete = mutation({
|
|
805
|
-
args: { passkeyId: v.id("Passkey") },
|
|
806
|
-
returns: v.null(),
|
|
807
|
-
handler: async (ctx, { passkeyId }) => {
|
|
808
|
-
await ctx.db.delete("Passkey", passkeyId);
|
|
809
|
-
return null;
|
|
810
|
-
}
|
|
811
|
-
});
|
|
812
|
-
/** Store a new TOTP enrollment for a user. */
|
|
813
|
-
const totpInsert = mutation({
|
|
814
|
-
args: {
|
|
815
|
-
userId: v.id("User"),
|
|
816
|
-
secret: v.bytes(),
|
|
817
|
-
digits: v.number(),
|
|
818
|
-
period: v.number(),
|
|
819
|
-
verified: v.boolean(),
|
|
820
|
-
name: v.optional(v.string()),
|
|
821
|
-
createdAt: v.number()
|
|
822
|
-
},
|
|
823
|
-
returns: v.id("TotpFactor"),
|
|
824
|
-
handler: async (ctx, args) => {
|
|
825
|
-
return await ctx.db.insert("TotpFactor", args);
|
|
826
|
-
}
|
|
827
|
-
});
|
|
828
|
-
/** Get a verified TOTP enrollment for a user (returns first match). */
|
|
829
|
-
const totpGetVerifiedByUserId = query({
|
|
830
|
-
args: { userId: v.id("User") },
|
|
831
|
-
returns: v.union(vTotpFactorDoc, v.null()),
|
|
832
|
-
handler: async (ctx, { userId }) => {
|
|
833
|
-
return await ctx.db.query("TotpFactor").withIndex("user_id", (q) => q.eq("userId", userId)).filter((q) => q.eq(q.field("verified"), true)).first();
|
|
834
|
-
}
|
|
835
|
-
});
|
|
836
|
-
/** List all TOTP enrollments for a user. */
|
|
837
|
-
const totpListByUserId = query({
|
|
838
|
-
args: { userId: v.id("User") },
|
|
839
|
-
returns: v.array(vTotpFactorDoc),
|
|
840
|
-
handler: async (ctx, { userId }) => {
|
|
841
|
-
return await ctx.db.query("TotpFactor").withIndex("user_id", (q) => q.eq("userId", userId)).collect();
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
/** Get a TOTP enrollment by its ID. */
|
|
845
|
-
const totpGetById = query({
|
|
846
|
-
args: { totpId: v.id("TotpFactor") },
|
|
847
|
-
returns: v.union(vTotpFactorDoc, v.null()),
|
|
848
|
-
handler: async (ctx, { totpId }) => {
|
|
849
|
-
return await ctx.db.get("TotpFactor", totpId);
|
|
850
|
-
}
|
|
851
|
-
});
|
|
852
|
-
/** Mark a TOTP enrollment as verified (setup complete). */
|
|
853
|
-
const totpMarkVerified = mutation({
|
|
854
|
-
args: {
|
|
855
|
-
totpId: v.id("TotpFactor"),
|
|
856
|
-
lastUsedAt: v.number()
|
|
857
|
-
},
|
|
858
|
-
returns: v.null(),
|
|
859
|
-
handler: async (ctx, { totpId, lastUsedAt }) => {
|
|
860
|
-
await ctx.db.patch("TotpFactor", totpId, {
|
|
861
|
-
verified: true,
|
|
862
|
-
lastUsedAt
|
|
863
|
-
});
|
|
864
|
-
return null;
|
|
865
|
-
}
|
|
866
|
-
});
|
|
867
|
-
/** Update a TOTP enrollment's last used timestamp. */
|
|
868
|
-
const totpUpdateLastUsed = mutation({
|
|
869
|
-
args: {
|
|
870
|
-
totpId: v.id("TotpFactor"),
|
|
871
|
-
lastUsedAt: v.number()
|
|
872
|
-
},
|
|
873
|
-
returns: v.null(),
|
|
874
|
-
handler: async (ctx, { totpId, lastUsedAt }) => {
|
|
875
|
-
await ctx.db.patch("TotpFactor", totpId, { lastUsedAt });
|
|
876
|
-
return null;
|
|
877
|
-
}
|
|
878
|
-
});
|
|
879
|
-
/** Delete a TOTP enrollment. */
|
|
880
|
-
const totpDelete = mutation({
|
|
881
|
-
args: { totpId: v.id("TotpFactor") },
|
|
882
|
-
returns: v.null(),
|
|
883
|
-
handler: async (ctx, { totpId }) => {
|
|
884
|
-
await ctx.db.delete("TotpFactor", totpId);
|
|
885
|
-
return null;
|
|
886
|
-
}
|
|
887
|
-
});
|
|
888
|
-
/** Look up a rate limit entry by its identifier. */
|
|
889
|
-
const rateLimitGet = query({
|
|
890
|
-
args: { identifier: v.string() },
|
|
891
|
-
returns: v.union(vRateLimitResult, v.null()),
|
|
892
|
-
handler: async (ctx, { identifier }) => {
|
|
893
|
-
const row = await ctx.db.query("RateLimit").withIndex("by_identifier", (q) => q.eq("identifier", identifier)).unique();
|
|
894
|
-
if (row === null) return null;
|
|
895
|
-
return {
|
|
896
|
-
...row,
|
|
897
|
-
attemptsLeft: row.attempts_left,
|
|
898
|
-
lastAttemptTime: row.last_attempt_time
|
|
899
|
-
};
|
|
900
|
-
}
|
|
901
|
-
});
|
|
902
|
-
/** Create a new rate limit entry. */
|
|
903
|
-
const rateLimitCreate = mutation({
|
|
904
|
-
args: {
|
|
905
|
-
identifier: v.string(),
|
|
906
|
-
attemptsLeft: v.number(),
|
|
907
|
-
lastAttemptTime: v.number()
|
|
908
|
-
},
|
|
909
|
-
returns: v.id("RateLimit"),
|
|
910
|
-
handler: async (ctx, { identifier, attemptsLeft, lastAttemptTime }) => {
|
|
911
|
-
return await ctx.db.insert("RateLimit", {
|
|
912
|
-
identifier,
|
|
913
|
-
attempts_left: attemptsLeft,
|
|
914
|
-
last_attempt_time: lastAttemptTime
|
|
915
|
-
});
|
|
916
|
-
}
|
|
917
|
-
});
|
|
918
|
-
/** Patch a rate limit entry with partial data. */
|
|
919
|
-
const rateLimitPatch = mutation({
|
|
920
|
-
args: {
|
|
921
|
-
rateLimitId: v.id("RateLimit"),
|
|
922
|
-
data: v.any()
|
|
923
|
-
},
|
|
924
|
-
returns: v.null(),
|
|
925
|
-
handler: async (ctx, { rateLimitId, data }) => {
|
|
926
|
-
const nextData = { ...data };
|
|
927
|
-
if (nextData.attemptsLeft !== void 0) {
|
|
928
|
-
nextData.attempts_left = nextData.attemptsLeft;
|
|
929
|
-
delete nextData.attemptsLeft;
|
|
930
|
-
}
|
|
931
|
-
if (nextData.lastAttemptTime !== void 0) {
|
|
932
|
-
nextData.last_attempt_time = nextData.lastAttemptTime;
|
|
933
|
-
delete nextData.lastAttemptTime;
|
|
934
|
-
}
|
|
935
|
-
await ctx.db.patch("RateLimit", rateLimitId, nextData);
|
|
936
|
-
return null;
|
|
937
|
-
}
|
|
938
|
-
});
|
|
939
|
-
/** Delete a rate limit entry. */
|
|
940
|
-
const rateLimitDelete = mutation({
|
|
941
|
-
args: { rateLimitId: v.id("RateLimit") },
|
|
942
|
-
returns: v.null(),
|
|
943
|
-
handler: async (ctx, { rateLimitId }) => {
|
|
944
|
-
await ctx.db.delete("RateLimit", rateLimitId);
|
|
945
|
-
return null;
|
|
946
|
-
}
|
|
947
|
-
});
|
|
948
|
-
/**
|
|
949
|
-
* Create a new group. Groups are hierarchical — set `parentGroupId` to nest
|
|
950
|
-
* under an existing group, or omit it to create a root-level group.
|
|
951
|
-
*
|
|
952
|
-
* @returns The ID of the newly created group.
|
|
953
|
-
*/
|
|
954
|
-
const groupCreate = mutation({
|
|
955
|
-
args: {
|
|
956
|
-
name: v.string(),
|
|
957
|
-
slug: v.optional(v.string()),
|
|
958
|
-
type: v.optional(v.string()),
|
|
959
|
-
parentGroupId: v.optional(v.id("Group")),
|
|
960
|
-
tags: v.optional(v.array(vTag)),
|
|
961
|
-
extend: v.optional(v.any())
|
|
962
|
-
},
|
|
963
|
-
returns: v.id("Group"),
|
|
964
|
-
handler: async (ctx, args) => {
|
|
965
|
-
const { tags: rawTags, ...rest } = args;
|
|
966
|
-
const normalizedTags = rawTags ? normalizeTags(rawTags) : void 0;
|
|
967
|
-
const groupId = await ctx.db.insert("Group", {
|
|
968
|
-
...rest,
|
|
969
|
-
tags: normalizedTags
|
|
970
|
-
});
|
|
971
|
-
if (normalizedTags) for (const tag of normalizedTags) await ctx.db.insert("GroupTag", {
|
|
972
|
-
group_id: groupId,
|
|
973
|
-
key: tag.key,
|
|
974
|
-
value: tag.value
|
|
975
|
-
});
|
|
976
|
-
return groupId;
|
|
977
|
-
}
|
|
978
|
-
});
|
|
979
|
-
/** Retrieve a group by its document ID. Returns `null` if not found. */
|
|
980
|
-
const groupGet = query({
|
|
981
|
-
args: { groupId: v.id("Group") },
|
|
982
|
-
returns: v.union(vGroupDoc, v.null()),
|
|
983
|
-
handler: async (ctx, { groupId }) => {
|
|
984
|
-
return await ctx.db.get("Group", groupId);
|
|
985
|
-
}
|
|
986
|
-
});
|
|
987
|
-
/**
|
|
988
|
-
* List groups with optional filtering, sorting, and pagination.
|
|
989
|
-
*
|
|
990
|
-
* Returns `{ items, nextCursor }`. Empty `where` returns **all** groups.
|
|
991
|
-
*/
|
|
992
|
-
const groupList = query({
|
|
993
|
-
args: {
|
|
994
|
-
where: v.optional(v.object({
|
|
995
|
-
slug: v.optional(v.string()),
|
|
996
|
-
type: v.optional(v.string()),
|
|
997
|
-
parentGroupId: v.optional(v.id("Group")),
|
|
998
|
-
name: v.optional(v.string()),
|
|
999
|
-
isRoot: v.optional(v.boolean()),
|
|
1000
|
-
tagsAll: v.optional(v.array(vTag)),
|
|
1001
|
-
tagsAny: v.optional(v.array(vTag))
|
|
1002
|
-
})),
|
|
1003
|
-
limit: v.optional(v.number()),
|
|
1004
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
1005
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("name"), v.literal("slug"), v.literal("type"))),
|
|
1006
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
1007
|
-
},
|
|
1008
|
-
returns: vPaginated(vGroupDoc),
|
|
1009
|
-
handler: async (ctx, args) => {
|
|
1010
|
-
const where = args.where ?? {};
|
|
1011
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
1012
|
-
const order = args.order ?? "desc";
|
|
1013
|
-
let tagFilteredIds = null;
|
|
1014
|
-
if (where.tagsAll && where.tagsAll.length > 0) {
|
|
1015
|
-
let allSet = null;
|
|
1016
|
-
for (const rawTag of where.tagsAll) {
|
|
1017
|
-
const t = normalizeTag(rawTag);
|
|
1018
|
-
const rows = await ctx.db.query("GroupTag").withIndex("by_key_value", (idx) => idx.eq("key", t.key).eq("value", t.value)).collect();
|
|
1019
|
-
const ids = new Set(rows.map((r) => r.group_id));
|
|
1020
|
-
if (allSet === null) allSet = ids;
|
|
1021
|
-
else for (const id of allSet) if (!ids.has(id)) allSet.delete(id);
|
|
1022
|
-
if (allSet.size === 0) break;
|
|
1023
|
-
}
|
|
1024
|
-
tagFilteredIds = allSet ?? /* @__PURE__ */ new Set();
|
|
1025
|
-
}
|
|
1026
|
-
if (where.tagsAny && where.tagsAny.length > 0) {
|
|
1027
|
-
const anySet = /* @__PURE__ */ new Set();
|
|
1028
|
-
for (const rawTag of where.tagsAny) {
|
|
1029
|
-
const t = normalizeTag(rawTag);
|
|
1030
|
-
const rows = await ctx.db.query("GroupTag").withIndex("by_key_value", (idx) => idx.eq("key", t.key).eq("value", t.value)).collect();
|
|
1031
|
-
for (const r of rows) anySet.add(r.group_id);
|
|
1032
|
-
}
|
|
1033
|
-
if (tagFilteredIds !== null) {
|
|
1034
|
-
for (const id of tagFilteredIds) if (!anySet.has(id)) tagFilteredIds.delete(id);
|
|
1035
|
-
} else tagFilteredIds = anySet;
|
|
1036
|
-
}
|
|
1037
|
-
let q;
|
|
1038
|
-
if (where.type !== void 0 && where.parentGroupId !== void 0) q = ctx.db.query("Group").withIndex("type_parent_group_id", (idx) => idx.eq("type", where.type).eq("parentGroupId", where.parentGroupId));
|
|
1039
|
-
else if (where.slug !== void 0) q = ctx.db.query("Group").withIndex("slug", (idx) => idx.eq("slug", where.slug));
|
|
1040
|
-
else if (where.type !== void 0) q = ctx.db.query("Group").withIndex("type", (idx) => idx.eq("type", where.type));
|
|
1041
|
-
else if (where.parentGroupId !== void 0) q = ctx.db.query("Group").withIndex("parent_group_id", (idx) => idx.eq("parentGroupId", where.parentGroupId));
|
|
1042
|
-
else q = ctx.db.query("Group");
|
|
1043
|
-
if (where.name !== void 0) q = q.filter((f) => f.eq(f.field("name"), where.name));
|
|
1044
|
-
if (where.isRoot === true) q = q.filter((f) => f.eq(f.field("parentGroupId"), void 0));
|
|
1045
|
-
else if (where.isRoot === false) q = q.filter((f) => f.neq(f.field("parentGroupId"), void 0));
|
|
1046
|
-
if (where.slug !== void 0 && where.type !== void 0) q = q.filter((f) => f.eq(f.field("slug"), where.slug));
|
|
1047
|
-
q = q.order(order);
|
|
1048
|
-
let all = await q.collect();
|
|
1049
|
-
if (tagFilteredIds !== null) all = all.filter((doc) => tagFilteredIds.has(doc._id));
|
|
1050
|
-
let startIdx = 0;
|
|
1051
|
-
if (args.cursor) {
|
|
1052
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
1053
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
1054
|
-
}
|
|
1055
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
1056
|
-
const hasMore = page.length > limit;
|
|
1057
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
1058
|
-
return {
|
|
1059
|
-
items,
|
|
1060
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
1061
|
-
};
|
|
1062
|
-
}
|
|
1063
|
-
});
|
|
1064
|
-
/** Update a group's fields (name, slug, tags, extend, parentGroupId). */
|
|
1065
|
-
const groupUpdate = mutation({
|
|
1066
|
-
args: {
|
|
1067
|
-
groupId: v.id("Group"),
|
|
1068
|
-
data: v.any()
|
|
1069
|
-
},
|
|
1070
|
-
returns: v.null(),
|
|
1071
|
-
handler: async (ctx, { groupId, data }) => {
|
|
1072
|
-
if (data.tags !== void 0) {
|
|
1073
|
-
const normalizedTags = Array.isArray(data.tags) ? normalizeTags(data.tags) : [];
|
|
1074
|
-
const existingTags = await ctx.db.query("GroupTag").withIndex("by_group", (idx) => idx.eq("group_id", groupId)).collect();
|
|
1075
|
-
for (const existing of existingTags) await ctx.db.delete("GroupTag", existing._id);
|
|
1076
|
-
for (const tag of normalizedTags) await ctx.db.insert("GroupTag", {
|
|
1077
|
-
group_id: groupId,
|
|
1078
|
-
key: tag.key,
|
|
1079
|
-
value: tag.value
|
|
1080
|
-
});
|
|
1081
|
-
await ctx.db.patch("Group", groupId, {
|
|
1082
|
-
...data,
|
|
1083
|
-
tags: normalizedTags.length > 0 ? normalizedTags : void 0
|
|
1084
|
-
});
|
|
1085
|
-
} else await ctx.db.patch("Group", groupId, data);
|
|
1086
|
-
return null;
|
|
1087
|
-
}
|
|
1088
|
-
});
|
|
1089
|
-
/**
|
|
1090
|
-
* Delete a group and all of its descendants. This cascades to:
|
|
1091
|
-
* - All child groups (recursively)
|
|
1092
|
-
* - All members of this group and its descendants
|
|
1093
|
-
* - All invites for this group and its descendants
|
|
1094
|
-
*/
|
|
1095
|
-
const groupDelete = mutation({
|
|
1096
|
-
args: { groupId: v.id("Group") },
|
|
1097
|
-
returns: v.null(),
|
|
1098
|
-
handler: async (ctx, { groupId }) => {
|
|
1099
|
-
const deleteGroup = async (id) => {
|
|
1100
|
-
const children = await ctx.db.query("Group").withIndex("parent_group_id", (q) => q.eq("parentGroupId", id)).collect();
|
|
1101
|
-
for (const child of children) await deleteGroup(child._id);
|
|
1102
|
-
const members = await ctx.db.query("GroupMember").withIndex("group_id", (q) => q.eq("groupId", id)).collect();
|
|
1103
|
-
for (const member of members) await ctx.db.delete("GroupMember", member._id);
|
|
1104
|
-
const invites = await ctx.db.query("GroupInvite").withIndex("group_id", (q) => q.eq("groupId", id)).collect();
|
|
1105
|
-
for (const invite of invites) await ctx.db.delete("GroupInvite", invite._id);
|
|
1106
|
-
const tags = await ctx.db.query("GroupTag").withIndex("by_group", (q) => q.eq("group_id", id)).collect();
|
|
1107
|
-
for (const tag of tags) await ctx.db.delete("GroupTag", tag._id);
|
|
1108
|
-
await ctx.db.delete("Group", id);
|
|
1109
|
-
};
|
|
1110
|
-
await deleteGroup(groupId);
|
|
1111
|
-
return null;
|
|
1112
|
-
}
|
|
1113
|
-
});
|
|
1114
|
-
/**
|
|
1115
|
-
* Add a user as a member of a group.
|
|
1116
|
-
*
|
|
1117
|
-
* The `role` field is an application-defined string (e.g. "owner", "admin",
|
|
1118
|
-
* "member", "viewer"). The auth component stores it but does not enforce
|
|
1119
|
-
* access control — your application defines what each role means.
|
|
1120
|
-
*
|
|
1121
|
-
* Throws `ConvexError` with code `DUPLICATE_MEMBERSHIP` when the user is
|
|
1122
|
-
* already a member of the target group.
|
|
1123
|
-
*
|
|
1124
|
-
* @returns The ID of the new member record.
|
|
1125
|
-
*/
|
|
1126
|
-
const memberAdd = mutation({
|
|
1127
|
-
args: {
|
|
1128
|
-
groupId: v.id("Group"),
|
|
1129
|
-
userId: v.id("User"),
|
|
1130
|
-
role: v.optional(v.string()),
|
|
1131
|
-
status: v.optional(v.string()),
|
|
1132
|
-
extend: v.optional(v.any())
|
|
1133
|
-
},
|
|
1134
|
-
returns: v.id("GroupMember"),
|
|
1135
|
-
handler: async (ctx, args) => {
|
|
1136
|
-
const existingMembership = await ctx.db.query("GroupMember").withIndex("group_id_user_id", (q) => q.eq("groupId", args.groupId).eq("userId", args.userId)).unique();
|
|
1137
|
-
if (existingMembership !== null) throw new ConvexError({
|
|
1138
|
-
code: "DUPLICATE_MEMBERSHIP",
|
|
1139
|
-
message: "User is already a member of this group",
|
|
1140
|
-
groupId: args.groupId,
|
|
1141
|
-
userId: args.userId,
|
|
1142
|
-
existingMemberId: existingMembership._id
|
|
1143
|
-
});
|
|
1144
|
-
return await ctx.db.insert("GroupMember", args);
|
|
1145
|
-
}
|
|
1146
|
-
});
|
|
1147
|
-
/** Retrieve a member record by its document ID. Returns `null` if not found. */
|
|
1148
|
-
const memberGet = query({
|
|
1149
|
-
args: { memberId: v.id("GroupMember") },
|
|
1150
|
-
returns: v.union(vGroupMemberDoc, v.null()),
|
|
1151
|
-
handler: async (ctx, { memberId }) => {
|
|
1152
|
-
return await ctx.db.get("GroupMember", memberId);
|
|
1153
|
-
}
|
|
1154
|
-
});
|
|
1155
|
-
/**
|
|
1156
|
-
* List members with optional filtering, sorting, and pagination.
|
|
1157
|
-
*
|
|
1158
|
-
* Returns `{ items, nextCursor }`. Supports filtering by `groupId`,
|
|
1159
|
-
* `userId`, `role`, and `status`.
|
|
1160
|
-
*/
|
|
1161
|
-
const memberList = query({
|
|
1162
|
-
args: {
|
|
1163
|
-
where: v.optional(v.object({
|
|
1164
|
-
groupId: v.optional(v.id("Group")),
|
|
1165
|
-
userId: v.optional(v.id("User")),
|
|
1166
|
-
role: v.optional(v.string()),
|
|
1167
|
-
status: v.optional(v.string())
|
|
1168
|
-
})),
|
|
1169
|
-
limit: v.optional(v.number()),
|
|
1170
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
1171
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("role"), v.literal("status"))),
|
|
1172
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
1173
|
-
},
|
|
1174
|
-
returns: vPaginated(vGroupMemberDoc),
|
|
1175
|
-
handler: async (ctx, args) => {
|
|
1176
|
-
const where = args.where ?? {};
|
|
1177
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
1178
|
-
const order = args.order ?? "desc";
|
|
1179
|
-
let q;
|
|
1180
|
-
if (where.groupId !== void 0 && where.userId !== void 0) q = ctx.db.query("GroupMember").withIndex("group_id_user_id", (idx) => idx.eq("groupId", where.groupId).eq("userId", where.userId));
|
|
1181
|
-
else if (where.groupId !== void 0) q = ctx.db.query("GroupMember").withIndex("group_id", (idx) => idx.eq("groupId", where.groupId));
|
|
1182
|
-
else if (where.userId !== void 0) q = ctx.db.query("GroupMember").withIndex("user_id", (idx) => idx.eq("userId", where.userId));
|
|
1183
|
-
else q = ctx.db.query("GroupMember");
|
|
1184
|
-
if (where.role !== void 0) q = q.filter((f) => f.eq(f.field("role"), where.role));
|
|
1185
|
-
if (where.status !== void 0) q = q.filter((f) => f.eq(f.field("status"), where.status));
|
|
1186
|
-
q = q.order(order);
|
|
1187
|
-
const all = await q.collect();
|
|
1188
|
-
let startIdx = 0;
|
|
1189
|
-
if (args.cursor) {
|
|
1190
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
1191
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
1192
|
-
}
|
|
1193
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
1194
|
-
const hasMore = page.length > limit;
|
|
1195
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
1196
|
-
return {
|
|
1197
|
-
items,
|
|
1198
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
1199
|
-
};
|
|
1200
|
-
}
|
|
1201
|
-
});
|
|
1202
|
-
/**
|
|
1203
|
-
* @deprecated Use `memberList` with `where: { userId }` instead.
|
|
1204
|
-
* Kept for backward compatibility with generated component types.
|
|
1205
|
-
*/
|
|
1206
|
-
const memberListByUser = query({
|
|
1207
|
-
args: { userId: v.id("User") },
|
|
1208
|
-
returns: v.array(vGroupMemberDoc),
|
|
1209
|
-
handler: async (ctx, { userId }) => {
|
|
1210
|
-
return await ctx.db.query("GroupMember").withIndex("user_id", (q) => q.eq("userId", userId)).collect();
|
|
1211
|
-
}
|
|
1212
|
-
});
|
|
1213
|
-
/**
|
|
1214
|
-
* Look up a specific user's membership in a specific group.
|
|
1215
|
-
* Returns `null` if the user is not a member of the group.
|
|
1216
|
-
*/
|
|
1217
|
-
const memberGetByGroupAndUser = query({
|
|
1218
|
-
args: {
|
|
1219
|
-
groupId: v.id("Group"),
|
|
1220
|
-
userId: v.id("User")
|
|
1221
|
-
},
|
|
1222
|
-
returns: v.union(vGroupMemberDoc, v.null()),
|
|
1223
|
-
handler: async (ctx, { groupId, userId }) => {
|
|
1224
|
-
return await ctx.db.query("GroupMember").withIndex("group_id_user_id", (q) => q.eq("groupId", groupId).eq("userId", userId)).unique();
|
|
1225
|
-
}
|
|
1226
|
-
});
|
|
1227
|
-
/** Remove a member from a group by deleting the member record. */
|
|
1228
|
-
const memberRemove = mutation({
|
|
1229
|
-
args: { memberId: v.id("GroupMember") },
|
|
1230
|
-
returns: v.null(),
|
|
1231
|
-
handler: async (ctx, { memberId }) => {
|
|
1232
|
-
await ctx.db.delete("GroupMember", memberId);
|
|
1233
|
-
return null;
|
|
1234
|
-
}
|
|
1235
|
-
});
|
|
1236
|
-
/**
|
|
1237
|
-
* Update a member record's fields (role, status, extend).
|
|
1238
|
-
*
|
|
1239
|
-
* Common usage: `memberUpdate({ memberId, data: { role: "admin" } })`
|
|
1240
|
-
*/
|
|
1241
|
-
const memberUpdate = mutation({
|
|
1242
|
-
args: {
|
|
1243
|
-
memberId: v.id("GroupMember"),
|
|
1244
|
-
data: v.any()
|
|
1245
|
-
},
|
|
1246
|
-
returns: v.null(),
|
|
1247
|
-
handler: async (ctx, { memberId, data }) => {
|
|
1248
|
-
await ctx.db.patch("GroupMember", memberId, data);
|
|
1249
|
-
return null;
|
|
1250
|
-
}
|
|
1251
|
-
});
|
|
1252
|
-
/**
|
|
1253
|
-
* Create a new platform-level invitation. Optionally set `groupId` to tie
|
|
1254
|
-
* the invite to a specific group. The invitation is sent to an email address
|
|
1255
|
-
* and includes a hashed token for secure acceptance.
|
|
1256
|
-
*
|
|
1257
|
-
* Throws `ConvexError` with code `DUPLICATE_INVITE` when a pending invite
|
|
1258
|
-
* already exists for the same email and scope:
|
|
1259
|
-
* - group invite: same `email` + same `groupId`
|
|
1260
|
-
* - platform invite: same `email` with no `groupId`
|
|
1261
|
-
*
|
|
1262
|
-
* @returns The ID of the new invite record.
|
|
1263
|
-
*/
|
|
1264
|
-
const inviteCreate = mutation({
|
|
1265
|
-
args: {
|
|
1266
|
-
groupId: v.optional(v.id("Group")),
|
|
1267
|
-
invitedByUserId: v.optional(v.id("User")),
|
|
1268
|
-
email: v.optional(v.string()),
|
|
1269
|
-
tokenHash: v.string(),
|
|
1270
|
-
role: v.optional(v.string()),
|
|
1271
|
-
status: vInviteStatus,
|
|
1272
|
-
expiresTime: v.optional(v.number()),
|
|
1273
|
-
extend: v.optional(v.any())
|
|
1274
|
-
},
|
|
1275
|
-
returns: v.id("GroupInvite"),
|
|
1276
|
-
handler: async (ctx, args) => {
|
|
1277
|
-
const now = Date.now();
|
|
1278
|
-
if (args.email !== void 0) if (args.groupId !== void 0) {
|
|
1279
|
-
const existingGroupInvites = await ctx.db.query("GroupInvite").withIndex("group_id_status", (q) => q.eq("groupId", args.groupId).eq("status", "pending")).filter((q) => q.eq(q.field("email"), args.email)).collect();
|
|
1280
|
-
for (const existingGroupInvite of existingGroupInvites) {
|
|
1281
|
-
if (existingGroupInvite.expiresTime !== void 0 && existingGroupInvite.expiresTime <= now) {
|
|
1282
|
-
await ctx.db.patch("GroupInvite", existingGroupInvite._id, { status: "expired" });
|
|
1283
|
-
continue;
|
|
1284
|
-
}
|
|
1285
|
-
throw new ConvexError({
|
|
1286
|
-
code: "DUPLICATE_INVITE",
|
|
1287
|
-
message: "A pending invite already exists for this email in this group",
|
|
1288
|
-
email: args.email,
|
|
1289
|
-
groupId: args.groupId,
|
|
1290
|
-
existingInviteId: existingGroupInvite._id
|
|
1291
|
-
});
|
|
1292
|
-
}
|
|
1293
|
-
} else {
|
|
1294
|
-
const existingPlatformInvites = await ctx.db.query("GroupInvite").withIndex("email_status", (q) => q.eq("email", args.email).eq("status", "pending")).filter((q) => q.eq(q.field("groupId"), void 0)).collect();
|
|
1295
|
-
for (const existingPlatformInvite of existingPlatformInvites) {
|
|
1296
|
-
if (existingPlatformInvite.expiresTime !== void 0 && existingPlatformInvite.expiresTime <= now) {
|
|
1297
|
-
await ctx.db.patch("GroupInvite", existingPlatformInvite._id, { status: "expired" });
|
|
1298
|
-
continue;
|
|
1299
|
-
}
|
|
1300
|
-
throw new ConvexError({
|
|
1301
|
-
code: "DUPLICATE_INVITE",
|
|
1302
|
-
message: "A pending platform invite already exists for this email",
|
|
1303
|
-
email: args.email,
|
|
1304
|
-
existingInviteId: existingPlatformInvite._id
|
|
1305
|
-
});
|
|
1306
|
-
}
|
|
1307
|
-
}
|
|
1308
|
-
return await ctx.db.insert("GroupInvite", args);
|
|
1309
|
-
}
|
|
1310
|
-
});
|
|
1311
|
-
/** Retrieve an invite by its document ID. Returns `null` if not found. */
|
|
1312
|
-
const inviteGet = query({
|
|
1313
|
-
args: { inviteId: v.id("GroupInvite") },
|
|
1314
|
-
returns: v.union(vGroupInviteDoc, v.null()),
|
|
1315
|
-
handler: async (ctx, { inviteId }) => {
|
|
1316
|
-
return await ctx.db.get("GroupInvite", inviteId);
|
|
1317
|
-
}
|
|
1318
|
-
});
|
|
1319
|
-
/** Retrieve an invite by hashed token. Returns `null` if not found. */
|
|
1320
|
-
const inviteGetByTokenHash = query({
|
|
1321
|
-
args: { tokenHash: v.string() },
|
|
1322
|
-
returns: v.union(vGroupInviteDoc, v.null()),
|
|
1323
|
-
handler: async (ctx, { tokenHash }) => {
|
|
1324
|
-
return await ctx.db.query("GroupInvite").withIndex("token_hash", (q) => q.eq("tokenHash", tokenHash)).first();
|
|
1325
|
-
}
|
|
1326
|
-
});
|
|
1327
|
-
/**
|
|
1328
|
-
* List invites with optional filtering, sorting, and pagination.
|
|
1329
|
-
*
|
|
1330
|
-
* Returns `{ items, nextCursor }`. Supports filtering by `groupId`,
|
|
1331
|
-
* `status`, `email`, `invitedByUserId`, `role`, `acceptedByUserId`, and `tokenHash`.
|
|
1332
|
-
*/
|
|
1333
|
-
const inviteList = query({
|
|
1334
|
-
args: {
|
|
1335
|
-
where: v.optional(v.object({
|
|
1336
|
-
tokenHash: v.optional(v.string()),
|
|
1337
|
-
groupId: v.optional(v.id("Group")),
|
|
1338
|
-
status: v.optional(vInviteStatus),
|
|
1339
|
-
email: v.optional(v.string()),
|
|
1340
|
-
invitedByUserId: v.optional(v.id("User")),
|
|
1341
|
-
role: v.optional(v.string()),
|
|
1342
|
-
acceptedByUserId: v.optional(v.id("User"))
|
|
1343
|
-
})),
|
|
1344
|
-
limit: v.optional(v.number()),
|
|
1345
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
1346
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("status"), v.literal("email"), v.literal("expiresTime"), v.literal("acceptedTime"))),
|
|
1347
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
1348
|
-
},
|
|
1349
|
-
returns: vPaginated(vGroupInviteDoc),
|
|
1350
|
-
handler: async (ctx, args) => {
|
|
1351
|
-
const where = args.where ?? {};
|
|
1352
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
1353
|
-
const order = args.order ?? "desc";
|
|
1354
|
-
let q;
|
|
1355
|
-
if (where.tokenHash !== void 0) q = ctx.db.query("GroupInvite").withIndex("token_hash", (idx) => idx.eq("tokenHash", where.tokenHash));
|
|
1356
|
-
else if (where.role !== void 0 && where.status !== void 0 && where.acceptedByUserId !== void 0) q = ctx.db.query("GroupInvite").withIndex("role_status_accepted_by_user_id", (idx) => idx.eq("role", where.role).eq("status", where.status).eq("acceptedByUserId", where.acceptedByUserId));
|
|
1357
|
-
else if (where.groupId !== void 0 && where.status !== void 0) q = ctx.db.query("GroupInvite").withIndex("group_id_status", (idx) => idx.eq("groupId", where.groupId).eq("status", where.status));
|
|
1358
|
-
else if (where.email !== void 0 && where.status !== void 0) q = ctx.db.query("GroupInvite").withIndex("email_status", (idx) => idx.eq("email", where.email).eq("status", where.status));
|
|
1359
|
-
else if (where.invitedByUserId !== void 0 && where.status !== void 0) q = ctx.db.query("GroupInvite").withIndex("invited_by_user_id_status", (idx) => idx.eq("invitedByUserId", where.invitedByUserId).eq("status", where.status));
|
|
1360
|
-
else if (where.groupId !== void 0) q = ctx.db.query("GroupInvite").withIndex("group_id", (idx) => idx.eq("groupId", where.groupId));
|
|
1361
|
-
else if (where.status !== void 0) q = ctx.db.query("GroupInvite").withIndex("status", (idx) => idx.eq("status", where.status));
|
|
1362
|
-
else q = ctx.db.query("GroupInvite");
|
|
1363
|
-
if (where.groupId !== void 0) q = q.filter((f) => f.eq(f.field("groupId"), where.groupId));
|
|
1364
|
-
if (where.status !== void 0) q = q.filter((f) => f.eq(f.field("status"), where.status));
|
|
1365
|
-
if (where.email !== void 0) q = q.filter((f) => f.eq(f.field("email"), where.email));
|
|
1366
|
-
if (where.invitedByUserId !== void 0) q = q.filter((f) => f.eq(f.field("invitedByUserId"), where.invitedByUserId));
|
|
1367
|
-
if (where.role !== void 0) q = q.filter((f) => f.eq(f.field("role"), where.role));
|
|
1368
|
-
if (where.acceptedByUserId !== void 0) q = q.filter((f) => f.eq(f.field("acceptedByUserId"), where.acceptedByUserId));
|
|
1369
|
-
if (where.tokenHash !== void 0) q = q.filter((f) => f.eq(f.field("tokenHash"), where.tokenHash));
|
|
1370
|
-
q = q.order(order);
|
|
1371
|
-
const all = await q.collect();
|
|
1372
|
-
let startIdx = 0;
|
|
1373
|
-
if (args.cursor) {
|
|
1374
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
1375
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
1376
|
-
}
|
|
1377
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
1378
|
-
const hasMore = page.length > limit;
|
|
1379
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
1380
|
-
return {
|
|
1381
|
-
items,
|
|
1382
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
1383
|
-
};
|
|
1384
|
-
}
|
|
1385
|
-
});
|
|
1386
|
-
/**
|
|
1387
|
-
* Accept a pending invitation.
|
|
1388
|
-
*
|
|
1389
|
-
* Marks the invite as "accepted" and records the acceptance timestamp.
|
|
1390
|
-
* Throws a structured `ConvexError` when the invite doesn't exist or is not
|
|
1391
|
-
* currently pending.
|
|
1392
|
-
*
|
|
1393
|
-
* The caller is responsible for creating the corresponding member record.
|
|
1394
|
-
*/
|
|
1395
|
-
const inviteAccept = mutation({
|
|
1396
|
-
args: {
|
|
1397
|
-
inviteId: v.id("GroupInvite"),
|
|
1398
|
-
acceptedByUserId: v.optional(v.id("User"))
|
|
1399
|
-
},
|
|
1400
|
-
returns: v.null(),
|
|
1401
|
-
handler: async (ctx, { inviteId, acceptedByUserId }) => {
|
|
1402
|
-
const invite = await ctx.db.get("GroupInvite", inviteId);
|
|
1403
|
-
if (invite === null) throw new ConvexError({
|
|
1404
|
-
code: "INVITE_NOT_FOUND",
|
|
1405
|
-
message: "Invite not found",
|
|
1406
|
-
inviteId
|
|
1407
|
-
});
|
|
1408
|
-
if (invite.status !== "pending") throw new ConvexError({
|
|
1409
|
-
code: "INVITE_NOT_PENDING",
|
|
1410
|
-
message: `Cannot accept invite with status "${invite.status}"`,
|
|
1411
|
-
inviteId,
|
|
1412
|
-
currentStatus: invite.status
|
|
1413
|
-
});
|
|
1414
|
-
if (invite.expiresTime !== void 0 && invite.expiresTime <= Date.now()) {
|
|
1415
|
-
await ctx.db.patch("GroupInvite", inviteId, { status: "expired" });
|
|
1416
|
-
throw new ConvexError({
|
|
1417
|
-
code: "INVITE_EXPIRED",
|
|
1418
|
-
message: "Invite has expired",
|
|
1419
|
-
inviteId
|
|
1420
|
-
});
|
|
1421
|
-
}
|
|
1422
|
-
await ctx.db.patch("GroupInvite", inviteId, {
|
|
1423
|
-
status: "accepted",
|
|
1424
|
-
acceptedTime: Date.now(),
|
|
1425
|
-
...acceptedByUserId ? { acceptedByUserId } : {}
|
|
1426
|
-
});
|
|
1427
|
-
return null;
|
|
1428
|
-
}
|
|
1429
|
-
});
|
|
1430
|
-
/**
|
|
1431
|
-
* Accept an invitation by raw token hash and atomically join group membership.
|
|
1432
|
-
*
|
|
1433
|
-
* Returns idempotent success when the invite was already accepted by the same
|
|
1434
|
-
* user. If the invite targets a group, this mutation also ensures membership.
|
|
1435
|
-
*/
|
|
1436
|
-
const inviteAcceptByToken = mutation({
|
|
1437
|
-
args: {
|
|
1438
|
-
tokenHash: v.string(),
|
|
1439
|
-
acceptedByUserId: v.id("User")
|
|
1440
|
-
},
|
|
1441
|
-
returns: vInviteAcceptByTokenResult,
|
|
1442
|
-
handler: async (ctx, { tokenHash, acceptedByUserId }) => {
|
|
1443
|
-
const invite = await ctx.db.query("GroupInvite").withIndex("token_hash", (q) => q.eq("tokenHash", tokenHash)).first();
|
|
1444
|
-
if (invite === null) throw new ConvexError({
|
|
1445
|
-
code: "INVITE_NOT_FOUND",
|
|
1446
|
-
message: "Invite not found"
|
|
1447
|
-
});
|
|
1448
|
-
const now = Date.now();
|
|
1449
|
-
if (invite.status === "pending") {
|
|
1450
|
-
if (invite.expiresTime !== void 0 && invite.expiresTime <= now) {
|
|
1451
|
-
await ctx.db.patch("GroupInvite", invite._id, { status: "expired" });
|
|
1452
|
-
throw new ConvexError({
|
|
1453
|
-
code: "INVITE_EXPIRED",
|
|
1454
|
-
message: "Invite has expired",
|
|
1455
|
-
inviteId: invite._id
|
|
1456
|
-
});
|
|
1457
|
-
}
|
|
1458
|
-
} else if (invite.status === "accepted") {
|
|
1459
|
-
if (invite.acceptedByUserId !== acceptedByUserId) throw new ConvexError({
|
|
1460
|
-
code: "INVITE_ALREADY_ACCEPTED",
|
|
1461
|
-
message: "Invite already accepted by another user",
|
|
1462
|
-
inviteId: invite._id
|
|
1463
|
-
});
|
|
1464
|
-
} else throw new ConvexError({
|
|
1465
|
-
code: "INVITE_NOT_PENDING",
|
|
1466
|
-
message: `Cannot accept invite with status "${invite.status}"`,
|
|
1467
|
-
inviteId: invite._id,
|
|
1468
|
-
currentStatus: invite.status
|
|
1469
|
-
});
|
|
1470
|
-
if (invite.email !== void 0) {
|
|
1471
|
-
const user = await ctx.db.get("User", acceptedByUserId);
|
|
1472
|
-
const normalizedInviteEmail = invite.email.trim().toLowerCase();
|
|
1473
|
-
const normalizedUserEmail = user?.email?.trim().toLowerCase();
|
|
1474
|
-
if (normalizedUserEmail === void 0 || normalizedUserEmail !== normalizedInviteEmail) throw new ConvexError({
|
|
1475
|
-
code: "INVITE_EMAIL_MISMATCH",
|
|
1476
|
-
message: "Invite email does not match accepting user's email",
|
|
1477
|
-
inviteId: invite._id
|
|
1478
|
-
});
|
|
1479
|
-
}
|
|
1480
|
-
let membershipStatus = "not_applicable";
|
|
1481
|
-
let memberId;
|
|
1482
|
-
if (invite.groupId !== void 0) {
|
|
1483
|
-
const existingMembership = await ctx.db.query("GroupMember").withIndex("group_id_user_id", (q) => q.eq("groupId", invite.groupId).eq("userId", acceptedByUserId)).unique();
|
|
1484
|
-
if (existingMembership !== null) {
|
|
1485
|
-
membershipStatus = "already_joined";
|
|
1486
|
-
memberId = existingMembership._id;
|
|
1487
|
-
} else {
|
|
1488
|
-
memberId = await ctx.db.insert("GroupMember", {
|
|
1489
|
-
groupId: invite.groupId,
|
|
1490
|
-
userId: acceptedByUserId,
|
|
1491
|
-
role: invite.role,
|
|
1492
|
-
status: "active"
|
|
1493
|
-
});
|
|
1494
|
-
membershipStatus = "joined";
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
if (invite.status === "pending") await ctx.db.patch("GroupInvite", invite._id, {
|
|
1498
|
-
status: "accepted",
|
|
1499
|
-
acceptedByUserId,
|
|
1500
|
-
acceptedTime: now
|
|
1501
|
-
});
|
|
1502
|
-
const inviteStatus = invite.status === "accepted" ? "already_accepted" : "accepted";
|
|
1503
|
-
return {
|
|
1504
|
-
inviteId: invite._id,
|
|
1505
|
-
groupId: invite.groupId ?? null,
|
|
1506
|
-
memberId,
|
|
1507
|
-
inviteStatus,
|
|
1508
|
-
membershipStatus
|
|
1509
|
-
};
|
|
1510
|
-
}
|
|
1511
|
-
});
|
|
1512
|
-
/**
|
|
1513
|
-
* Revoke a pending invitation.
|
|
1514
|
-
*
|
|
1515
|
-
* Marks the invite as "revoked". Throws a structured `ConvexError` when the
|
|
1516
|
-
* invite doesn't exist or is not currently pending.
|
|
1517
|
-
*/
|
|
1518
|
-
const inviteRevoke = mutation({
|
|
1519
|
-
args: { inviteId: v.id("GroupInvite") },
|
|
1520
|
-
returns: v.null(),
|
|
1521
|
-
handler: async (ctx, { inviteId }) => {
|
|
1522
|
-
const invite = await ctx.db.get("GroupInvite", inviteId);
|
|
1523
|
-
if (invite === null) throw new ConvexError({
|
|
1524
|
-
code: "INVITE_NOT_FOUND",
|
|
1525
|
-
message: "Invite not found",
|
|
1526
|
-
inviteId
|
|
1527
|
-
});
|
|
1528
|
-
if (invite.status !== "pending") throw new ConvexError({
|
|
1529
|
-
code: "INVITE_NOT_PENDING",
|
|
1530
|
-
message: `Cannot revoke invite with status "${invite.status}"`,
|
|
1531
|
-
inviteId,
|
|
1532
|
-
currentStatus: invite.status
|
|
1533
|
-
});
|
|
1534
|
-
await ctx.db.patch("GroupInvite", inviteId, { status: "revoked" });
|
|
1535
|
-
return null;
|
|
1536
|
-
}
|
|
1537
|
-
});
|
|
1538
|
-
/** Create an enterprise record attached to a root group. */
|
|
1539
|
-
const enterpriseCreate = mutation({
|
|
1540
|
-
args: {
|
|
1541
|
-
groupId: v.id("Group"),
|
|
1542
|
-
slug: v.optional(v.string()),
|
|
1543
|
-
name: v.optional(v.string()),
|
|
1544
|
-
status: v.optional(vEnterpriseStatus),
|
|
1545
|
-
config: v.optional(v.any()),
|
|
1546
|
-
extend: v.optional(v.any())
|
|
1547
|
-
},
|
|
1548
|
-
returns: v.id("Enterprise"),
|
|
1549
|
-
handler: async (ctx, args) => {
|
|
1550
|
-
if (await ctx.db.query("Enterprise").withIndex("group_id", (idx) => idx.eq("groupId", args.groupId)).first()) throw new ConvexError({
|
|
1551
|
-
code: "ENTERPRISE_ALREADY_EXISTS",
|
|
1552
|
-
message: "An enterprise record already exists for this group."
|
|
1553
|
-
});
|
|
1554
|
-
return await ctx.db.insert("Enterprise", {
|
|
1555
|
-
...args,
|
|
1556
|
-
status: args.status ?? "draft"
|
|
1557
|
-
});
|
|
1558
|
-
}
|
|
1559
|
-
});
|
|
1560
|
-
/** Retrieve an enterprise record by ID. */
|
|
1561
|
-
const enterpriseGet = query({
|
|
1562
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1563
|
-
returns: v.union(vEnterpriseDoc, v.null()),
|
|
1564
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1565
|
-
return await ctx.db.get("Enterprise", enterpriseId);
|
|
1566
|
-
}
|
|
1567
|
-
});
|
|
1568
|
-
/** Retrieve an enterprise record by group ID. */
|
|
1569
|
-
const enterpriseGetByGroup = query({
|
|
1570
|
-
args: { groupId: v.id("Group") },
|
|
1571
|
-
returns: v.union(vEnterpriseDoc, v.null()),
|
|
1572
|
-
handler: async (ctx, { groupId }) => {
|
|
1573
|
-
return await ctx.db.query("Enterprise").withIndex("group_id", (idx) => idx.eq("groupId", groupId)).first();
|
|
1574
|
-
}
|
|
1575
|
-
});
|
|
1576
|
-
/** Retrieve an enterprise record by a linked domain. */
|
|
1577
|
-
const enterpriseGetByDomain = query({
|
|
1578
|
-
args: { domain: v.string() },
|
|
1579
|
-
returns: v.union(v.object({
|
|
1580
|
-
enterprise: vEnterpriseDoc,
|
|
1581
|
-
domain: vEnterpriseDomainDoc
|
|
1582
|
-
}), v.null()),
|
|
1583
|
-
handler: async (ctx, { domain }) => {
|
|
1584
|
-
const domainRow = await ctx.db.query("EnterpriseDomain").withIndex("domain", (idx) => idx.eq("domain", domain)).first();
|
|
1585
|
-
if (!domainRow) return null;
|
|
1586
|
-
const enterprise = await ctx.db.get("Enterprise", domainRow.enterpriseId);
|
|
1587
|
-
if (!enterprise) return null;
|
|
1588
|
-
return {
|
|
1589
|
-
enterprise,
|
|
1590
|
-
domain: domainRow
|
|
1591
|
-
};
|
|
1592
|
-
}
|
|
1593
|
-
});
|
|
1594
|
-
/** List enterprises with lightweight filtering and cursor pagination. */
|
|
1595
|
-
const enterpriseList = query({
|
|
1596
|
-
args: {
|
|
1597
|
-
where: v.optional(v.object({
|
|
1598
|
-
groupId: v.optional(v.id("Group")),
|
|
1599
|
-
slug: v.optional(v.string()),
|
|
1600
|
-
status: v.optional(vEnterpriseStatus)
|
|
1601
|
-
})),
|
|
1602
|
-
limit: v.optional(v.number()),
|
|
1603
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
1604
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("name"), v.literal("slug"), v.literal("status"))),
|
|
1605
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
1606
|
-
},
|
|
1607
|
-
returns: vPaginated(vEnterpriseDoc),
|
|
1608
|
-
handler: async (ctx, args) => {
|
|
1609
|
-
const where = args.where ?? {};
|
|
1610
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
1611
|
-
const order = args.order ?? "desc";
|
|
1612
|
-
let q;
|
|
1613
|
-
if (where.groupId !== void 0) q = ctx.db.query("Enterprise").withIndex("group_id", (idx) => idx.eq("groupId", where.groupId));
|
|
1614
|
-
else if (where.slug !== void 0) q = ctx.db.query("Enterprise").withIndex("slug", (idx) => idx.eq("slug", where.slug));
|
|
1615
|
-
else if (where.status !== void 0) q = ctx.db.query("Enterprise").withIndex("status", (idx) => idx.eq("status", where.status));
|
|
1616
|
-
else q = ctx.db.query("Enterprise");
|
|
1617
|
-
if (where.groupId !== void 0 && where.slug !== void 0) q = q.filter((f) => f.eq(f.field("slug"), where.slug));
|
|
1618
|
-
if (where.status !== void 0 && where.groupId === void 0) {} else if (where.status !== void 0) q = q.filter((f) => f.eq(f.field("status"), where.status));
|
|
1619
|
-
q = q.order(order);
|
|
1620
|
-
const all = await q.collect();
|
|
1621
|
-
let startIdx = 0;
|
|
1622
|
-
if (args.cursor) {
|
|
1623
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
1624
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
1625
|
-
}
|
|
1626
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
1627
|
-
const hasMore = page.length > limit;
|
|
1628
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
1629
|
-
return {
|
|
1630
|
-
items,
|
|
1631
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
1632
|
-
};
|
|
1633
|
-
}
|
|
1634
|
-
});
|
|
1635
|
-
/** Patch an enterprise record. */
|
|
1636
|
-
const enterpriseUpdate = mutation({
|
|
1637
|
-
args: {
|
|
1638
|
-
enterpriseId: v.id("Enterprise"),
|
|
1639
|
-
data: v.any()
|
|
1640
|
-
},
|
|
1641
|
-
returns: v.null(),
|
|
1642
|
-
handler: async (ctx, { enterpriseId, data }) => {
|
|
1643
|
-
await ctx.db.patch(enterpriseId, data);
|
|
1644
|
-
return null;
|
|
1645
|
-
}
|
|
1646
|
-
});
|
|
1647
|
-
/** Delete an enterprise record. */
|
|
1648
|
-
const enterpriseDelete = mutation({
|
|
1649
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1650
|
-
returns: v.null(),
|
|
1651
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1652
|
-
const domains = await ctx.db.query("EnterpriseDomain").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).collect();
|
|
1653
|
-
for (const domain of domains) await ctx.db.delete(domain._id);
|
|
1654
|
-
await ctx.db.delete(enterpriseId);
|
|
1655
|
-
return null;
|
|
1656
|
-
}
|
|
1657
|
-
});
|
|
1658
|
-
/** Link a domain to an enterprise record. */
|
|
1659
|
-
const enterpriseDomainAdd = mutation({
|
|
1660
|
-
args: {
|
|
1661
|
-
enterpriseId: v.id("Enterprise"),
|
|
1662
|
-
groupId: v.id("Group"),
|
|
1663
|
-
domain: v.string(),
|
|
1664
|
-
isPrimary: v.optional(v.boolean()),
|
|
1665
|
-
verifiedAt: v.optional(v.number())
|
|
1666
|
-
},
|
|
1667
|
-
returns: v.id("EnterpriseDomain"),
|
|
1668
|
-
handler: async (ctx, args) => {
|
|
1669
|
-
const existingByDomain = await ctx.db.query("EnterpriseDomain").withIndex("domain", (idx) => idx.eq("domain", args.domain)).first();
|
|
1670
|
-
if (existingByDomain && existingByDomain.enterpriseId !== args.enterpriseId) throw new ConvexError({
|
|
1671
|
-
code: "ENTERPRISE_DOMAIN_TAKEN",
|
|
1672
|
-
message: "That domain is already attached to another enterprise."
|
|
1673
|
-
});
|
|
1674
|
-
const existingForEnterprise = await ctx.db.query("EnterpriseDomain").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", args.enterpriseId)).collect();
|
|
1675
|
-
for (const row of existingForEnterprise) if (row.domain === args.domain) {
|
|
1676
|
-
await ctx.db.patch(row._id, {
|
|
1677
|
-
isPrimary: args.isPrimary ?? row.isPrimary,
|
|
1678
|
-
verifiedAt: args.verifiedAt ?? row.verifiedAt
|
|
1679
|
-
});
|
|
1680
|
-
return row._id;
|
|
1681
|
-
}
|
|
1682
|
-
if (args.isPrimary === true) {
|
|
1683
|
-
for (const row of existingForEnterprise) if (row.isPrimary) await ctx.db.patch(row._id, { isPrimary: false });
|
|
1684
|
-
}
|
|
1685
|
-
return await ctx.db.insert("EnterpriseDomain", {
|
|
1686
|
-
...args,
|
|
1687
|
-
isPrimary: args.isPrimary ?? existingForEnterprise.length === 0
|
|
1688
|
-
});
|
|
1689
|
-
}
|
|
1690
|
-
});
|
|
1691
|
-
/** List domains linked to an enterprise. */
|
|
1692
|
-
const enterpriseDomainList = query({
|
|
1693
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1694
|
-
returns: v.array(vEnterpriseDomainDoc),
|
|
1695
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1696
|
-
return await ctx.db.query("EnterpriseDomain").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).collect();
|
|
1697
|
-
}
|
|
1698
|
-
});
|
|
1699
|
-
/** Remove a linked enterprise domain. */
|
|
1700
|
-
const enterpriseDomainDelete = mutation({
|
|
1701
|
-
args: { domainId: v.id("EnterpriseDomain") },
|
|
1702
|
-
returns: v.null(),
|
|
1703
|
-
handler: async (ctx, { domainId }) => {
|
|
1704
|
-
await ctx.db.delete(domainId);
|
|
1705
|
-
return null;
|
|
1706
|
-
}
|
|
1707
|
-
});
|
|
1708
|
-
/** Create or rotate SCIM configuration for an enterprise. */
|
|
1709
|
-
const enterpriseScimConfigUpsert = mutation({
|
|
1710
|
-
args: {
|
|
1711
|
-
enterpriseId: v.id("Enterprise"),
|
|
1712
|
-
groupId: v.id("Group"),
|
|
1713
|
-
status: vScimStatus,
|
|
1714
|
-
basePath: v.string(),
|
|
1715
|
-
tokenHash: v.string(),
|
|
1716
|
-
lastRotatedAt: v.optional(v.number()),
|
|
1717
|
-
deprovisionMode: v.optional(v.union(v.literal("soft"), v.literal("hard"))),
|
|
1718
|
-
extend: v.optional(v.any())
|
|
1719
|
-
},
|
|
1720
|
-
returns: v.id("EnterpriseScimConfig"),
|
|
1721
|
-
handler: async (ctx, args) => {
|
|
1722
|
-
const existing = await ctx.db.query("EnterpriseScimConfig").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", args.enterpriseId)).first();
|
|
1723
|
-
if (existing) {
|
|
1724
|
-
await ctx.db.patch(existing._id, args);
|
|
1725
|
-
return existing._id;
|
|
1726
|
-
}
|
|
1727
|
-
return await ctx.db.insert("EnterpriseScimConfig", args);
|
|
1728
|
-
}
|
|
1729
|
-
});
|
|
1730
|
-
const enterpriseScimConfigGetByEnterprise = query({
|
|
1731
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1732
|
-
returns: v.union(vEnterpriseScimConfigDoc, v.null()),
|
|
1733
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1734
|
-
return await ctx.db.query("EnterpriseScimConfig").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).first();
|
|
1735
|
-
}
|
|
1736
|
-
});
|
|
1737
|
-
const enterpriseScimConfigGetByTokenHash = query({
|
|
1738
|
-
args: { tokenHash: v.string() },
|
|
1739
|
-
returns: v.union(vEnterpriseScimConfigDoc, v.null()),
|
|
1740
|
-
handler: async (ctx, { tokenHash }) => {
|
|
1741
|
-
return await ctx.db.query("EnterpriseScimConfig").withIndex("token_hash", (idx) => idx.eq("tokenHash", tokenHash)).first();
|
|
1742
|
-
}
|
|
1743
|
-
});
|
|
1744
|
-
const enterpriseScimIdentityGet = query({
|
|
1745
|
-
args: {
|
|
1746
|
-
enterpriseId: v.id("Enterprise"),
|
|
1747
|
-
resourceType: vScimResourceType,
|
|
1748
|
-
externalId: v.string()
|
|
1749
|
-
},
|
|
1750
|
-
returns: v.union(vEnterpriseScimIdentityDoc, v.null()),
|
|
1751
|
-
handler: async (ctx, args) => {
|
|
1752
|
-
return await ctx.db.query("EnterpriseScimIdentity").withIndex("enterprise_id_resource_type_external_id", (idx) => idx.eq("enterpriseId", args.enterpriseId).eq("resourceType", args.resourceType).eq("externalId", args.externalId)).first();
|
|
1753
|
-
}
|
|
1754
|
-
});
|
|
1755
|
-
const enterpriseScimIdentityGetByUser = query({
|
|
1756
|
-
args: { userId: v.id("User") },
|
|
1757
|
-
returns: v.union(vEnterpriseScimIdentityDoc, v.null()),
|
|
1758
|
-
handler: async (ctx, { userId }) => {
|
|
1759
|
-
return await ctx.db.query("EnterpriseScimIdentity").withIndex("user_id", (idx) => idx.eq("userId", userId)).first();
|
|
1760
|
-
}
|
|
1761
|
-
});
|
|
1762
|
-
const enterpriseScimIdentityGetByMappedGroup = query({
|
|
1763
|
-
args: { mappedGroupId: v.id("Group") },
|
|
1764
|
-
returns: v.union(vEnterpriseScimIdentityDoc, v.null()),
|
|
1765
|
-
handler: async (ctx, { mappedGroupId }) => {
|
|
1766
|
-
return await ctx.db.query("EnterpriseScimIdentity").withIndex("mapped_group_id", (idx) => idx.eq("mappedGroupId", mappedGroupId)).first();
|
|
1767
|
-
}
|
|
1768
|
-
});
|
|
1769
|
-
const enterpriseScimIdentityListByEnterprise = query({
|
|
1770
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1771
|
-
returns: v.array(vEnterpriseScimIdentityDoc),
|
|
1772
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1773
|
-
return await ctx.db.query("EnterpriseScimIdentity").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).collect();
|
|
1774
|
-
}
|
|
1775
|
-
});
|
|
1776
|
-
const enterpriseScimIdentityUpsert = mutation({
|
|
1777
|
-
args: {
|
|
1778
|
-
enterpriseId: v.id("Enterprise"),
|
|
1779
|
-
groupId: v.id("Group"),
|
|
1780
|
-
resourceType: vScimResourceType,
|
|
1781
|
-
externalId: v.string(),
|
|
1782
|
-
userId: v.optional(v.id("User")),
|
|
1783
|
-
mappedGroupId: v.optional(v.id("Group")),
|
|
1784
|
-
lastProvisionedAt: v.optional(v.number()),
|
|
1785
|
-
active: v.optional(v.boolean()),
|
|
1786
|
-
raw: v.optional(v.any())
|
|
1787
|
-
},
|
|
1788
|
-
returns: v.id("EnterpriseScimIdentity"),
|
|
1789
|
-
handler: async (ctx, args) => {
|
|
1790
|
-
const existing = await ctx.db.query("EnterpriseScimIdentity").withIndex("enterprise_id_resource_type_external_id", (idx) => idx.eq("enterpriseId", args.enterpriseId).eq("resourceType", args.resourceType).eq("externalId", args.externalId)).first();
|
|
1791
|
-
if (existing) {
|
|
1792
|
-
await ctx.db.patch(existing._id, args);
|
|
1793
|
-
return existing._id;
|
|
1794
|
-
}
|
|
1795
|
-
return await ctx.db.insert("EnterpriseScimIdentity", args);
|
|
1796
|
-
}
|
|
1797
|
-
});
|
|
1798
|
-
const enterpriseScimIdentityDelete = mutation({
|
|
1799
|
-
args: { identityId: v.id("EnterpriseScimIdentity") },
|
|
1800
|
-
returns: v.null(),
|
|
1801
|
-
handler: async (ctx, { identityId }) => {
|
|
1802
|
-
await ctx.db.delete(identityId);
|
|
1803
|
-
return null;
|
|
1804
|
-
}
|
|
1805
|
-
});
|
|
1806
|
-
const enterpriseAuditEventCreate = mutation({
|
|
1807
|
-
args: {
|
|
1808
|
-
enterpriseId: v.id("Enterprise"),
|
|
1809
|
-
groupId: v.id("Group"),
|
|
1810
|
-
eventType: v.string(),
|
|
1811
|
-
actorType: vAuditActorType,
|
|
1812
|
-
actorId: v.optional(v.string()),
|
|
1813
|
-
subjectType: v.string(),
|
|
1814
|
-
subjectId: v.optional(v.string()),
|
|
1815
|
-
status: vAuditStatus,
|
|
1816
|
-
occurredAt: v.number(),
|
|
1817
|
-
requestId: v.optional(v.string()),
|
|
1818
|
-
ip: v.optional(v.string()),
|
|
1819
|
-
metadata: v.optional(v.any())
|
|
1820
|
-
},
|
|
1821
|
-
returns: v.id("EnterpriseAuditEvent"),
|
|
1822
|
-
handler: async (ctx, args) => {
|
|
1823
|
-
return await ctx.db.insert("EnterpriseAuditEvent", args);
|
|
1824
|
-
}
|
|
1825
|
-
});
|
|
1826
|
-
const enterpriseAuditEventList = query({
|
|
1827
|
-
args: {
|
|
1828
|
-
enterpriseId: v.optional(v.id("Enterprise")),
|
|
1829
|
-
groupId: v.optional(v.id("Group")),
|
|
1830
|
-
limit: v.optional(v.number())
|
|
1831
|
-
},
|
|
1832
|
-
returns: v.array(vEnterpriseAuditEventDoc),
|
|
1833
|
-
handler: async (ctx, args) => {
|
|
1834
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
1835
|
-
if (args.enterpriseId !== void 0) return await ctx.db.query("EnterpriseAuditEvent").withIndex("enterprise_id_occurred_at", (idx) => idx.eq("enterpriseId", args.enterpriseId)).order("desc").take(limit);
|
|
1836
|
-
if (args.groupId !== void 0) return await ctx.db.query("EnterpriseAuditEvent").withIndex("group_id_occurred_at", (idx) => idx.eq("groupId", args.groupId)).order("desc").take(limit);
|
|
1837
|
-
return await ctx.db.query("EnterpriseAuditEvent").order("desc").take(limit);
|
|
1838
|
-
}
|
|
1839
|
-
});
|
|
1840
|
-
const enterpriseWebhookEndpointCreate = mutation({
|
|
1841
|
-
args: {
|
|
1842
|
-
enterpriseId: v.id("Enterprise"),
|
|
1843
|
-
groupId: v.id("Group"),
|
|
1844
|
-
url: v.string(),
|
|
1845
|
-
status: v.optional(vWebhookEndpointStatus),
|
|
1846
|
-
secretHash: v.string(),
|
|
1847
|
-
subscriptions: v.array(v.string()),
|
|
1848
|
-
createdByUserId: v.optional(v.id("User")),
|
|
1849
|
-
extend: v.optional(v.any())
|
|
1850
|
-
},
|
|
1851
|
-
returns: v.id("EnterpriseWebhookEndpoint"),
|
|
1852
|
-
handler: async (ctx, args) => {
|
|
1853
|
-
return await ctx.db.insert("EnterpriseWebhookEndpoint", {
|
|
1854
|
-
...args,
|
|
1855
|
-
status: args.status ?? "active",
|
|
1856
|
-
failureCount: 0
|
|
1857
|
-
});
|
|
1858
|
-
}
|
|
1859
|
-
});
|
|
1860
|
-
const enterpriseWebhookEndpointList = query({
|
|
1861
|
-
args: { enterpriseId: v.id("Enterprise") },
|
|
1862
|
-
returns: v.array(vEnterpriseWebhookEndpointDoc),
|
|
1863
|
-
handler: async (ctx, { enterpriseId }) => {
|
|
1864
|
-
return await ctx.db.query("EnterpriseWebhookEndpoint").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).collect();
|
|
1865
|
-
}
|
|
1866
|
-
});
|
|
1867
|
-
const enterpriseWebhookEndpointUpdate = mutation({
|
|
1868
|
-
args: {
|
|
1869
|
-
endpointId: v.id("EnterpriseWebhookEndpoint"),
|
|
1870
|
-
data: v.any()
|
|
1871
|
-
},
|
|
1872
|
-
returns: v.null(),
|
|
1873
|
-
handler: async (ctx, { endpointId, data }) => {
|
|
1874
|
-
await ctx.db.patch(endpointId, data);
|
|
1875
|
-
return null;
|
|
1876
|
-
}
|
|
1877
|
-
});
|
|
1878
|
-
const enterpriseWebhookDeliveryEnqueue = mutation({
|
|
1879
|
-
args: {
|
|
1880
|
-
enterpriseId: v.id("Enterprise"),
|
|
1881
|
-
endpointId: v.id("EnterpriseWebhookEndpoint"),
|
|
1882
|
-
auditEventId: v.optional(v.id("EnterpriseAuditEvent")),
|
|
1883
|
-
eventType: v.string(),
|
|
1884
|
-
payload: v.any(),
|
|
1885
|
-
nextAttemptAt: v.number()
|
|
1886
|
-
},
|
|
1887
|
-
returns: v.id("EnterpriseWebhookDelivery"),
|
|
1888
|
-
handler: async (ctx, args) => {
|
|
1889
|
-
return await ctx.db.insert("EnterpriseWebhookDelivery", {
|
|
1890
|
-
...args,
|
|
1891
|
-
status: "pending",
|
|
1892
|
-
attemptCount: 0
|
|
1893
|
-
});
|
|
1894
|
-
}
|
|
1895
|
-
});
|
|
1896
|
-
const enterpriseWebhookDeliveryListReady = query({
|
|
1897
|
-
args: {
|
|
1898
|
-
now: v.number(),
|
|
1899
|
-
limit: v.optional(v.number())
|
|
1900
|
-
},
|
|
1901
|
-
returns: v.array(vEnterpriseWebhookDeliveryDoc),
|
|
1902
|
-
handler: async (ctx, { now, limit }) => {
|
|
1903
|
-
return await ctx.db.query("EnterpriseWebhookDelivery").withIndex("status_next_attempt_at", (idx) => idx.eq("status", "pending").lte("nextAttemptAt", now)).take(Math.min(Math.max(limit ?? 50, 1), 100));
|
|
1904
|
-
}
|
|
1905
|
-
});
|
|
1906
|
-
const enterpriseWebhookDeliveryList = query({
|
|
1907
|
-
args: {
|
|
1908
|
-
enterpriseId: v.id("Enterprise"),
|
|
1909
|
-
limit: v.optional(v.number())
|
|
1910
|
-
},
|
|
1911
|
-
returns: v.array(vEnterpriseWebhookDeliveryDoc),
|
|
1912
|
-
handler: async (ctx, { enterpriseId, limit }) => {
|
|
1913
|
-
return await ctx.db.query("EnterpriseWebhookDelivery").withIndex("enterprise_id", (idx) => idx.eq("enterpriseId", enterpriseId)).order("desc").take(Math.min(Math.max(limit ?? 50, 1), 100));
|
|
1914
|
-
}
|
|
1915
|
-
});
|
|
1916
|
-
const enterpriseWebhookDeliveryPatch = mutation({
|
|
1917
|
-
args: {
|
|
1918
|
-
deliveryId: v.id("EnterpriseWebhookDelivery"),
|
|
1919
|
-
data: v.any()
|
|
1920
|
-
},
|
|
1921
|
-
returns: v.null(),
|
|
1922
|
-
handler: async (ctx, { deliveryId, data }) => {
|
|
1923
|
-
await ctx.db.patch(deliveryId, data);
|
|
1924
|
-
return null;
|
|
1925
|
-
}
|
|
1926
|
-
});
|
|
1927
|
-
/**
|
|
1928
|
-
* Insert a new API key record.
|
|
1929
|
-
*
|
|
1930
|
-
* The caller is responsible for hashing the raw key before passing it here —
|
|
1931
|
-
* this function only stores the hash and metadata.
|
|
1932
|
-
*/
|
|
1933
|
-
const keyInsert = mutation({
|
|
1934
|
-
args: {
|
|
1935
|
-
userId: v.id("User"),
|
|
1936
|
-
prefix: v.string(),
|
|
1937
|
-
hashedKey: v.string(),
|
|
1938
|
-
name: v.string(),
|
|
1939
|
-
scopes: v.array(v.object({
|
|
1940
|
-
resource: v.string(),
|
|
1941
|
-
actions: v.array(v.string())
|
|
1942
|
-
})),
|
|
1943
|
-
rateLimit: v.optional(vApiKeyRateLimit),
|
|
1944
|
-
expiresAt: v.optional(v.number()),
|
|
1945
|
-
metadata: v.optional(v.any())
|
|
1946
|
-
},
|
|
1947
|
-
returns: v.id("ApiKey"),
|
|
1948
|
-
handler: async (ctx, args) => {
|
|
1949
|
-
return await ctx.db.insert("ApiKey", {
|
|
1950
|
-
...args,
|
|
1951
|
-
createdAt: Date.now(),
|
|
1952
|
-
revoked: false
|
|
1953
|
-
});
|
|
1954
|
-
}
|
|
1955
|
-
});
|
|
1956
|
-
/**
|
|
1957
|
-
* Look up an API key by its SHA-256 hash.
|
|
1958
|
-
*
|
|
1959
|
-
* Used during Bearer token verification. Returns the full key record
|
|
1960
|
-
* (including rate limit state) or `null` if not found.
|
|
1961
|
-
*/
|
|
1962
|
-
const keyGetByHashedKey = query({
|
|
1963
|
-
args: { hashedKey: v.string() },
|
|
1964
|
-
returns: v.union(vApiKeyDoc, v.null()),
|
|
1965
|
-
handler: async (ctx, { hashedKey }) => {
|
|
1966
|
-
return await ctx.db.query("ApiKey").withIndex("hashed_key", (q) => q.eq("hashedKey", hashedKey)).first();
|
|
1967
|
-
}
|
|
1968
|
-
});
|
|
1969
|
-
/**
|
|
1970
|
-
* @deprecated Use `keyList` with `where: { userId }` instead.
|
|
1971
|
-
* Kept for backward compatibility with generated component types.
|
|
1972
|
-
*/
|
|
1973
|
-
const keyListByUserId = query({
|
|
1974
|
-
args: { userId: v.id("User") },
|
|
1975
|
-
returns: v.array(vApiKeyDoc),
|
|
1976
|
-
handler: async (ctx, { userId }) => {
|
|
1977
|
-
return await ctx.db.query("ApiKey").withIndex("user_id", (q) => q.eq("userId", userId)).collect();
|
|
1978
|
-
}
|
|
1979
|
-
});
|
|
1980
|
-
/**
|
|
1981
|
-
* List API keys with optional filtering, sorting, and pagination.
|
|
1982
|
-
*
|
|
1983
|
-
* Returns `{ items, nextCursor }`. Supports filtering by `userId`,
|
|
1984
|
-
* `revoked`, `name`, and `prefix`.
|
|
1985
|
-
*/
|
|
1986
|
-
const keyList = query({
|
|
1987
|
-
args: {
|
|
1988
|
-
where: v.optional(v.object({
|
|
1989
|
-
userId: v.optional(v.id("User")),
|
|
1990
|
-
revoked: v.optional(v.boolean()),
|
|
1991
|
-
name: v.optional(v.string()),
|
|
1992
|
-
prefix: v.optional(v.string())
|
|
1993
|
-
})),
|
|
1994
|
-
limit: v.optional(v.number()),
|
|
1995
|
-
cursor: v.optional(v.union(v.string(), v.null())),
|
|
1996
|
-
orderBy: v.optional(v.union(v.literal("_creationTime"), v.literal("name"), v.literal("lastUsedAt"), v.literal("expiresAt"), v.literal("revoked"))),
|
|
1997
|
-
order: v.optional(v.union(v.literal("asc"), v.literal("desc")))
|
|
1998
|
-
},
|
|
1999
|
-
returns: vPaginated(vApiKeyDoc),
|
|
2000
|
-
handler: async (ctx, args) => {
|
|
2001
|
-
const where = args.where ?? {};
|
|
2002
|
-
const limit = Math.min(Math.max(args.limit ?? 50, 1), 100);
|
|
2003
|
-
const order = args.order ?? "desc";
|
|
2004
|
-
let q;
|
|
2005
|
-
if (where.userId !== void 0) q = ctx.db.query("ApiKey").withIndex("user_id", (idx) => idx.eq("userId", where.userId));
|
|
2006
|
-
else q = ctx.db.query("ApiKey");
|
|
2007
|
-
if (where.revoked !== void 0) q = q.filter((f) => f.eq(f.field("revoked"), where.revoked));
|
|
2008
|
-
if (where.name !== void 0) q = q.filter((f) => f.eq(f.field("name"), where.name));
|
|
2009
|
-
if (where.prefix !== void 0) q = q.filter((f) => f.eq(f.field("prefix"), where.prefix));
|
|
2010
|
-
q = q.order(order);
|
|
2011
|
-
const all = await q.collect();
|
|
2012
|
-
let startIdx = 0;
|
|
2013
|
-
if (args.cursor) {
|
|
2014
|
-
const cursorIdx = all.findIndex((doc) => doc._id === args.cursor);
|
|
2015
|
-
if (cursorIdx !== -1) startIdx = cursorIdx + 1;
|
|
2016
|
-
}
|
|
2017
|
-
const page = all.slice(startIdx, startIdx + limit + 1);
|
|
2018
|
-
const hasMore = page.length > limit;
|
|
2019
|
-
const items = hasMore ? page.slice(0, limit) : page;
|
|
2020
|
-
return {
|
|
2021
|
-
items,
|
|
2022
|
-
nextCursor: hasMore ? items[items.length - 1]._id : null
|
|
2023
|
-
};
|
|
2024
|
-
}
|
|
2025
|
-
});
|
|
2026
|
-
/** Get a single API key by document ID. */
|
|
2027
|
-
const keyGetById = query({
|
|
2028
|
-
args: { keyId: v.id("ApiKey") },
|
|
2029
|
-
returns: v.union(vApiKeyDoc, v.null()),
|
|
2030
|
-
handler: async (ctx, { keyId }) => {
|
|
2031
|
-
return await ctx.db.get("ApiKey", keyId);
|
|
2032
|
-
}
|
|
2033
|
-
});
|
|
2034
|
-
/**
|
|
2035
|
-
* Patch an API key record. Used for updating name, scopes, rate limit config,
|
|
2036
|
-
* revocation, and lastUsedAt / rate limit state tracking.
|
|
2037
|
-
*/
|
|
2038
|
-
const keyPatch = mutation({
|
|
2039
|
-
args: {
|
|
2040
|
-
keyId: v.id("ApiKey"),
|
|
2041
|
-
data: v.object({
|
|
2042
|
-
name: v.optional(v.string()),
|
|
2043
|
-
scopes: v.optional(v.array(vApiKeyScope)),
|
|
2044
|
-
rateLimit: v.optional(vApiKeyRateLimit),
|
|
2045
|
-
rateLimitState: v.optional(vApiKeyRateLimitState),
|
|
2046
|
-
revoked: v.optional(v.boolean()),
|
|
2047
|
-
lastUsedAt: v.optional(v.number())
|
|
2048
|
-
})
|
|
2049
|
-
},
|
|
2050
|
-
returns: v.null(),
|
|
2051
|
-
handler: async (ctx, { keyId, data }) => {
|
|
2052
|
-
if (await ctx.db.get("ApiKey", keyId) === null) throw new ConvexError({
|
|
2053
|
-
code: "KEY_NOT_FOUND",
|
|
2054
|
-
message: "API key not found",
|
|
2055
|
-
keyId
|
|
2056
|
-
});
|
|
2057
|
-
await ctx.db.patch("ApiKey", keyId, data);
|
|
2058
|
-
return null;
|
|
2059
|
-
}
|
|
2060
|
-
});
|
|
2061
|
-
/** Hard delete an API key record. */
|
|
2062
|
-
const keyDelete = mutation({
|
|
2063
|
-
args: { keyId: v.id("ApiKey") },
|
|
2064
|
-
returns: v.null(),
|
|
2065
|
-
handler: async (ctx, { keyId }) => {
|
|
2066
|
-
if (await ctx.db.get("ApiKey", keyId) === null) throw new ConvexError({
|
|
2067
|
-
code: "KEY_NOT_FOUND",
|
|
2068
|
-
message: "API key not found",
|
|
2069
|
-
keyId
|
|
2070
|
-
});
|
|
2071
|
-
await ctx.db.delete("ApiKey", keyId);
|
|
2072
|
-
return null;
|
|
2073
|
-
}
|
|
2074
|
-
});
|
|
2075
|
-
/** Insert a new device authorization record. */
|
|
2076
|
-
const deviceInsert = mutation({
|
|
2077
|
-
args: {
|
|
2078
|
-
deviceCodeHash: v.string(),
|
|
2079
|
-
userCode: v.string(),
|
|
2080
|
-
expiresAt: v.number(),
|
|
2081
|
-
interval: v.number(),
|
|
2082
|
-
status: vDeviceStatus
|
|
2083
|
-
},
|
|
2084
|
-
returns: v.id("DeviceCode"),
|
|
2085
|
-
handler: async (ctx, args) => {
|
|
2086
|
-
return await ctx.db.insert("DeviceCode", args);
|
|
2087
|
-
}
|
|
2088
|
-
});
|
|
2089
|
-
/** Look up a device authorization by its hashed device code. */
|
|
2090
|
-
const deviceGetByCodeHash = query({
|
|
2091
|
-
args: { deviceCodeHash: v.string() },
|
|
2092
|
-
returns: v.union(vDeviceCodeDoc, v.null()),
|
|
2093
|
-
handler: async (ctx, { deviceCodeHash }) => {
|
|
2094
|
-
return await ctx.db.query("DeviceCode").withIndex("device_code_hash", (q) => q.eq("deviceCodeHash", deviceCodeHash)).first();
|
|
2095
|
-
}
|
|
2096
|
-
});
|
|
2097
|
-
/** Look up a pending device authorization by its user code. */
|
|
2098
|
-
const deviceGetByUserCode = query({
|
|
2099
|
-
args: { userCode: v.string() },
|
|
2100
|
-
returns: v.union(vDeviceCodeDoc, v.null()),
|
|
2101
|
-
handler: async (ctx, { userCode }) => {
|
|
2102
|
-
return await ctx.db.query("DeviceCode").withIndex("user_code_status", (q) => q.eq("userCode", userCode).eq("status", "pending")).first();
|
|
2103
|
-
}
|
|
2104
|
-
});
|
|
2105
|
-
/** Authorize a device code — link it to a user and session. */
|
|
2106
|
-
const deviceAuthorize = mutation({
|
|
2107
|
-
args: {
|
|
2108
|
-
deviceId: v.id("DeviceCode"),
|
|
2109
|
-
userId: v.id("User"),
|
|
2110
|
-
sessionId: v.id("Session")
|
|
2111
|
-
},
|
|
2112
|
-
returns: v.null(),
|
|
2113
|
-
handler: async (ctx, { deviceId, userId, sessionId }) => {
|
|
2114
|
-
await ctx.db.patch("DeviceCode", deviceId, {
|
|
2115
|
-
status: "authorized",
|
|
2116
|
-
userId,
|
|
2117
|
-
sessionId
|
|
2118
|
-
});
|
|
2119
|
-
return null;
|
|
2120
|
-
}
|
|
2121
|
-
});
|
|
2122
|
-
/** Update the last-polled timestamp on a device authorization record. */
|
|
2123
|
-
const deviceUpdateLastPolled = mutation({
|
|
2124
|
-
args: {
|
|
2125
|
-
deviceId: v.id("DeviceCode"),
|
|
2126
|
-
lastPolledAt: v.number()
|
|
2127
|
-
},
|
|
2128
|
-
returns: v.null(),
|
|
2129
|
-
handler: async (ctx, { deviceId, lastPolledAt }) => {
|
|
2130
|
-
await ctx.db.patch("DeviceCode", deviceId, { lastPolledAt });
|
|
2131
|
-
return null;
|
|
2132
|
-
}
|
|
2133
|
-
});
|
|
2134
|
-
/** Delete a device authorization record (cleanup after use or expiry). */
|
|
2135
|
-
const deviceDelete = mutation({
|
|
2136
|
-
args: { deviceId: v.id("DeviceCode") },
|
|
2137
|
-
returns: v.null(),
|
|
2138
|
-
handler: async (ctx, { deviceId }) => {
|
|
2139
|
-
await ctx.db.delete("DeviceCode", deviceId);
|
|
2140
|
-
return null;
|
|
2141
|
-
}
|
|
2142
|
-
});
|
|
2143
|
-
|
|
2144
|
-
//#endregion
|
|
2145
|
-
export { accountDelete, accountGet, accountGetById, accountInsert, accountListByUser, accountPatch, deviceAuthorize, deviceDelete, deviceGetByCodeHash, deviceGetByUserCode, deviceInsert, deviceUpdateLastPolled, enterpriseAuditEventCreate, enterpriseAuditEventList, enterpriseCreate, enterpriseDelete, enterpriseDomainAdd, enterpriseDomainDelete, enterpriseDomainList, enterpriseGet, enterpriseGetByDomain, enterpriseGetByGroup, enterpriseList, enterpriseScimConfigGetByEnterprise, enterpriseScimConfigGetByTokenHash, enterpriseScimConfigUpsert, enterpriseScimIdentityDelete, enterpriseScimIdentityGet, enterpriseScimIdentityGetByMappedGroup, enterpriseScimIdentityGetByUser, enterpriseScimIdentityListByEnterprise, enterpriseScimIdentityUpsert, enterpriseUpdate, enterpriseWebhookDeliveryEnqueue, enterpriseWebhookDeliveryList, enterpriseWebhookDeliveryListReady, enterpriseWebhookDeliveryPatch, enterpriseWebhookEndpointCreate, enterpriseWebhookEndpointList, enterpriseWebhookEndpointUpdate, groupCreate, groupDelete, groupGet, groupList, groupUpdate, inviteAccept, inviteAcceptByToken, inviteCreate, inviteGet, inviteGetByTokenHash, inviteList, inviteRevoke, keyDelete, keyGetByHashedKey, keyGetById, keyInsert, keyList, keyListByUserId, keyPatch, memberAdd, memberGet, memberGetByGroupAndUser, memberList, memberListByUser, memberRemove, memberUpdate, passkeyDelete, passkeyGetByCredentialId, passkeyInsert, passkeyListByUserId, passkeyUpdateCounter, passkeyUpdateMeta, rateLimitCreate, rateLimitDelete, rateLimitGet, rateLimitPatch, refreshTokenCreate, refreshTokenDeleteAll, refreshTokenGetActive, refreshTokenGetById, refreshTokenGetChildren, refreshTokenListBySession, refreshTokenPatch, sessionCreate, sessionDelete, sessionGetById, sessionList, sessionListByUser, totpDelete, totpGetById, totpGetVerifiedByUserId, totpInsert, totpListByUserId, totpMarkVerified, totpUpdateLastUsed, userDelete, userFindByVerifiedEmail, userFindByVerifiedPhone, userGetById, userInsert, userList, userPatch, userUpsert, verificationCodeCreate, verificationCodeDelete, verificationCodeGetByAccountId, verificationCodeGetByCode, verifierCreate, verifierDelete, verifierGetById, verifierGetBySignature, verifierPatch };
|
|
2146
|
-
//# sourceMappingURL=public.js.map
|
|
7
|
+
export { accountDelete, accountGet, accountGetById, accountInsert, accountListByUser, accountPatch, deviceAuthorize, deviceDelete, deviceGetByCodeHash, deviceGetByUserCode, deviceInsert, deviceUpdateLastPolled, enterpriseAuditEventCreate, enterpriseAuditEventList, enterpriseCreate, enterpriseDelete, enterpriseDomainAdd, enterpriseDomainDelete, enterpriseDomainList, enterpriseDomainVerificationDelete, enterpriseDomainVerificationGet, enterpriseDomainVerificationUpsert, enterpriseDomainVerify, enterpriseGet, enterpriseGetByDomain, enterpriseGetByGroup, enterpriseList, enterpriseScimConfigGetByEnterprise, enterpriseScimConfigGetByTokenHash, enterpriseScimConfigUpsert, enterpriseScimIdentityDelete, enterpriseScimIdentityGet, enterpriseScimIdentityGetByEnterpriseAndUser, enterpriseScimIdentityGetByMappedGroup, enterpriseScimIdentityGetByUser, enterpriseScimIdentityListByEnterprise, enterpriseScimIdentityUpsert, enterpriseSecretDelete, enterpriseSecretGet, enterpriseSecretUpsert, enterpriseUpdate, enterpriseWebhookDeliveryEnqueue, enterpriseWebhookDeliveryList, enterpriseWebhookDeliveryListReady, enterpriseWebhookDeliveryPatch, enterpriseWebhookEndpointCreate, enterpriseWebhookEndpointGet, enterpriseWebhookEndpointList, enterpriseWebhookEndpointUpdate, groupCreate, groupDelete, groupGet, groupList, groupUpdate, inviteAccept, inviteAcceptByToken, inviteCreate, inviteGet, inviteGetByTokenHash, inviteList, inviteRevoke, keyDelete, keyGetByHashedKey, keyGetById, keyInsert, keyList, keyListByUserId, keyPatch, memberAdd, memberGet, memberGetByGroupAndUser, memberList, memberListByUser, memberRemove, memberUpdate, passkeyDelete, passkeyGetByCredentialId, passkeyInsert, passkeyListByUserId, passkeyUpdateCounter, passkeyUpdateMeta, rateLimitCreate, rateLimitDelete, rateLimitGet, rateLimitPatch, refreshTokenCreate, refreshTokenDeleteAll, refreshTokenGetActive, refreshTokenGetById, refreshTokenGetChildren, refreshTokenListBySession, refreshTokenPatch, sessionCreate, sessionDelete, sessionGetById, sessionList, sessionListByUser, totpDelete, totpGetById, totpGetVerifiedByUserId, totpInsert, totpListByUserId, totpMarkVerified, totpUpdateLastUsed, userDelete, userFindByVerifiedEmail, userFindByVerifiedPhone, userGetById, userInsert, userList, userPatch, userUpsert, verificationCodeCreate, verificationCodeDelete, verificationCodeGetByAccountId, verificationCodeGetByCode, verifierCreate, verifierDelete, verifierGetById, verifierGetBySignature, verifierPatch };
|