@nordsym/apiclaw 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/add-api.yml +123 -0
- package/BRIEFING.md +30 -0
- package/CONCEPT.md +494 -0
- package/README.md +272 -0
- package/backend/convex/README.md +90 -0
- package/backend/convex/_generated/api.d.ts +55 -0
- package/backend/convex/_generated/api.js +23 -0
- package/backend/convex/_generated/dataModel.d.ts +60 -0
- package/backend/convex/_generated/server.d.ts +143 -0
- package/backend/convex/_generated/server.js +93 -0
- package/backend/convex/apiKeys.ts +75 -0
- package/backend/convex/purchases.ts +74 -0
- package/backend/convex/schema.ts +45 -0
- package/backend/convex/transactions.ts +57 -0
- package/backend/convex/tsconfig.json +25 -0
- package/backend/convex/users.ts +94 -0
- package/backend/package-lock.json +521 -0
- package/backend/package.json +15 -0
- package/dist/credits.d.ts +54 -0
- package/dist/credits.d.ts.map +1 -0
- package/dist/credits.js +209 -0
- package/dist/credits.js.map +1 -0
- package/dist/discovery.d.ts +37 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +109 -0
- package/dist/discovery.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +355 -0
- package/dist/index.js.map +1 -0
- package/dist/registry/apis.json +20894 -0
- package/dist/registry/parse_apis.py +146 -0
- package/dist/revenuecat.d.ts +61 -0
- package/dist/revenuecat.d.ts.map +1 -0
- package/dist/revenuecat.js +166 -0
- package/dist/revenuecat.js.map +1 -0
- package/dist/test.d.ts +6 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +81 -0
- package/dist/test.js.map +1 -0
- package/dist/types.d.ts +96 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/webhooks/revenuecat.d.ts +48 -0
- package/dist/webhooks/revenuecat.d.ts.map +1 -0
- package/dist/webhooks/revenuecat.js +119 -0
- package/dist/webhooks/revenuecat.js.map +1 -0
- package/docs/revenuecat-setup.md +89 -0
- package/landing/next-env.d.ts +5 -0
- package/landing/next.config.mjs +6 -0
- package/landing/package-lock.json +1666 -0
- package/landing/package.json +27 -0
- package/landing/postcss.config.js +6 -0
- package/landing/src/app/api/keys/route.ts +71 -0
- package/landing/src/app/api/log/route.ts +37 -0
- package/landing/src/app/api/stats/route.ts +37 -0
- package/landing/src/app/globals.css +261 -0
- package/landing/src/app/layout.tsx +37 -0
- package/landing/src/app/page.tsx +753 -0
- package/landing/src/app/page.tsx.bak +567 -0
- package/landing/src/components/AddKeyModal.tsx +159 -0
- package/landing/tailwind.config.ts +34 -0
- package/landing/tsconfig.json +20 -0
- package/newsletter-template.html +71 -0
- package/outreach/OUTREACH-SYSTEM.md +211 -0
- package/outreach/email-template.html +179 -0
- package/outreach/targets.md +133 -0
- package/package.json +39 -0
- package/src/credits.ts +261 -0
- package/src/discovery.ts +147 -0
- package/src/index.ts +396 -0
- package/src/registry/apis.json +20894 -0
- package/src/registry/parse_apis.py +146 -0
- package/src/revenuecat.ts +239 -0
- package/src/test.ts +97 -0
- package/src/types.ts +110 -0
- package/src/webhooks/revenuecat.ts +187 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
actionGeneric,
|
|
13
|
+
httpActionGeneric,
|
|
14
|
+
queryGeneric,
|
|
15
|
+
mutationGeneric,
|
|
16
|
+
internalActionGeneric,
|
|
17
|
+
internalMutationGeneric,
|
|
18
|
+
internalQueryGeneric,
|
|
19
|
+
} from "convex/server";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Define a query in this Convex app's public API.
|
|
23
|
+
*
|
|
24
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
25
|
+
*
|
|
26
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
27
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
28
|
+
*/
|
|
29
|
+
export const query = queryGeneric;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
33
|
+
*
|
|
34
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
35
|
+
*
|
|
36
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
37
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
38
|
+
*/
|
|
39
|
+
export const internalQuery = internalQueryGeneric;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Define a mutation in this Convex app's public API.
|
|
43
|
+
*
|
|
44
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
45
|
+
*
|
|
46
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
47
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
48
|
+
*/
|
|
49
|
+
export const mutation = mutationGeneric;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
53
|
+
*
|
|
54
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
55
|
+
*
|
|
56
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
57
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
58
|
+
*/
|
|
59
|
+
export const internalMutation = internalMutationGeneric;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Define an action in this Convex app's public API.
|
|
63
|
+
*
|
|
64
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
65
|
+
* code and code with side-effects, like calling third-party services.
|
|
66
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
67
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
68
|
+
*
|
|
69
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
70
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
71
|
+
*/
|
|
72
|
+
export const action = actionGeneric;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
76
|
+
*
|
|
77
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
78
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
79
|
+
*/
|
|
80
|
+
export const internalAction = internalActionGeneric;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Define an HTTP action.
|
|
84
|
+
*
|
|
85
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
86
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
87
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
88
|
+
*
|
|
89
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
90
|
+
* and a Fetch API `Request` object as its second.
|
|
91
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
92
|
+
*/
|
|
93
|
+
export const httpAction = httpActionGeneric;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation, query } from "./_generated/server";
|
|
3
|
+
|
|
4
|
+
// Add a new API key
|
|
5
|
+
export const add = mutation({
|
|
6
|
+
args: {
|
|
7
|
+
userEmail: v.string(),
|
|
8
|
+
apiId: v.string(),
|
|
9
|
+
apiName: v.string(),
|
|
10
|
+
keyName: v.string(),
|
|
11
|
+
keyValue: v.string(),
|
|
12
|
+
},
|
|
13
|
+
handler: async (ctx, args) => {
|
|
14
|
+
const id = await ctx.db.insert("apiKeys", {
|
|
15
|
+
userEmail: args.userEmail,
|
|
16
|
+
apiId: args.apiId,
|
|
17
|
+
apiName: args.apiName,
|
|
18
|
+
keyName: args.keyName,
|
|
19
|
+
keyValue: args.keyValue,
|
|
20
|
+
createdAt: Date.now(),
|
|
21
|
+
});
|
|
22
|
+
return id;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// Get all keys for a user
|
|
27
|
+
export const listByEmail = query({
|
|
28
|
+
args: { userEmail: v.string() },
|
|
29
|
+
handler: async (ctx, args) => {
|
|
30
|
+
const keys = await ctx.db
|
|
31
|
+
.query("apiKeys")
|
|
32
|
+
.withIndex("by_email", (q) => q.eq("userEmail", args.userEmail))
|
|
33
|
+
.collect();
|
|
34
|
+
// Return without exposing full key values
|
|
35
|
+
return keys.map((k) => ({
|
|
36
|
+
...k,
|
|
37
|
+
keyValue: k.keyValue.slice(0, 4) + "..." + k.keyValue.slice(-4),
|
|
38
|
+
_fullKey: undefined, // never expose
|
|
39
|
+
}));
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Get a specific key (for agent use - requires auth in prod)
|
|
44
|
+
export const get = query({
|
|
45
|
+
args: {
|
|
46
|
+
userEmail: v.string(),
|
|
47
|
+
apiId: v.string(),
|
|
48
|
+
},
|
|
49
|
+
handler: async (ctx, args) => {
|
|
50
|
+
const keys = await ctx.db
|
|
51
|
+
.query("apiKeys")
|
|
52
|
+
.withIndex("by_email_and_api", (q) =>
|
|
53
|
+
q.eq("userEmail", args.userEmail).eq("apiId", args.apiId)
|
|
54
|
+
)
|
|
55
|
+
.collect();
|
|
56
|
+
return keys;
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Delete a key
|
|
61
|
+
export const remove = mutation({
|
|
62
|
+
args: { id: v.id("apiKeys") },
|
|
63
|
+
handler: async (ctx, args) => {
|
|
64
|
+
await ctx.db.delete(args.id);
|
|
65
|
+
return { success: true };
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Update last used timestamp
|
|
70
|
+
export const markUsed = mutation({
|
|
71
|
+
args: { id: v.id("apiKeys") },
|
|
72
|
+
handler: async (ctx, args) => {
|
|
73
|
+
await ctx.db.patch(args.id, { lastUsed: Date.now() });
|
|
74
|
+
},
|
|
75
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { query, mutation } from "./_generated/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
// Create new API purchase
|
|
5
|
+
export const create = mutation({
|
|
6
|
+
args: {
|
|
7
|
+
userId: v.id("users"),
|
|
8
|
+
apiId: v.string(),
|
|
9
|
+
credentials: v.any(),
|
|
10
|
+
expiresAt: v.optional(v.number()),
|
|
11
|
+
},
|
|
12
|
+
handler: async (ctx, args) => {
|
|
13
|
+
const purchaseId = await ctx.db.insert("purchases", {
|
|
14
|
+
userId: args.userId,
|
|
15
|
+
apiId: args.apiId,
|
|
16
|
+
credentials: args.credentials,
|
|
17
|
+
status: "active",
|
|
18
|
+
purchasedAt: Date.now(),
|
|
19
|
+
expiresAt: args.expiresAt,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return purchaseId;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// List user's active purchases
|
|
27
|
+
export const listByUser = query({
|
|
28
|
+
args: {
|
|
29
|
+
userId: v.id("users"),
|
|
30
|
+
},
|
|
31
|
+
handler: async (ctx, args) => {
|
|
32
|
+
const purchases = await ctx.db
|
|
33
|
+
.query("purchases")
|
|
34
|
+
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
35
|
+
.collect();
|
|
36
|
+
|
|
37
|
+
// Filter active only
|
|
38
|
+
return purchases.filter((p) => p.status === "active");
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
// Get purchase by user and API
|
|
43
|
+
export const getByUserAndApi = query({
|
|
44
|
+
args: {
|
|
45
|
+
userId: v.id("users"),
|
|
46
|
+
apiId: v.string(),
|
|
47
|
+
},
|
|
48
|
+
handler: async (ctx, args) => {
|
|
49
|
+
return await ctx.db
|
|
50
|
+
.query("purchases")
|
|
51
|
+
.withIndex("by_user_and_api", (q) =>
|
|
52
|
+
q.eq("userId", args.userId).eq("apiId", args.apiId)
|
|
53
|
+
)
|
|
54
|
+
.first();
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
// Update purchase status
|
|
59
|
+
export const updateStatus = mutation({
|
|
60
|
+
args: {
|
|
61
|
+
purchaseId: v.id("purchases"),
|
|
62
|
+
status: v.string(),
|
|
63
|
+
},
|
|
64
|
+
handler: async (ctx, args) => {
|
|
65
|
+
await ctx.db.patch(args.purchaseId, { status: args.status });
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Get all purchases (for admin)
|
|
70
|
+
export const listAll = query({
|
|
71
|
+
handler: async (ctx) => {
|
|
72
|
+
return await ctx.db.query("purchases").collect();
|
|
73
|
+
},
|
|
74
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export default defineSchema({
|
|
5
|
+
// Users
|
|
6
|
+
users: defineTable({
|
|
7
|
+
clerkId: v.optional(v.string()),
|
|
8
|
+
email: v.string(),
|
|
9
|
+
credits: v.number(), // in cents
|
|
10
|
+
createdAt: v.number(),
|
|
11
|
+
}).index("by_email", ["email"])
|
|
12
|
+
.index("by_clerkId", ["clerkId"]),
|
|
13
|
+
|
|
14
|
+
// API Purchases
|
|
15
|
+
purchases: defineTable({
|
|
16
|
+
userId: v.id("users"),
|
|
17
|
+
apiId: v.string(), // "46elks", "resend", etc
|
|
18
|
+
credentials: v.any(), // encrypted or reference
|
|
19
|
+
status: v.string(), // "active", "expired"
|
|
20
|
+
purchasedAt: v.number(),
|
|
21
|
+
expiresAt: v.optional(v.number()),
|
|
22
|
+
}).index("by_user", ["userId"])
|
|
23
|
+
.index("by_user_and_api", ["userId", "apiId"]),
|
|
24
|
+
|
|
25
|
+
// Transactions
|
|
26
|
+
transactions: defineTable({
|
|
27
|
+
userId: v.id("users"),
|
|
28
|
+
type: v.string(), // "credit_add", "api_purchase"
|
|
29
|
+
amount: v.number(),
|
|
30
|
+
description: v.string(),
|
|
31
|
+
createdAt: v.number(),
|
|
32
|
+
}).index("by_user", ["userId"]),
|
|
33
|
+
|
|
34
|
+
// BYOK - Bring Your Own Keys
|
|
35
|
+
apiKeys: defineTable({
|
|
36
|
+
userEmail: v.string(),
|
|
37
|
+
apiId: v.string(),
|
|
38
|
+
apiName: v.string(),
|
|
39
|
+
keyName: v.string(), // "API Key", "Secret Key", etc
|
|
40
|
+
keyValue: v.string(), // the actual key (encrypted in prod)
|
|
41
|
+
createdAt: v.number(),
|
|
42
|
+
lastUsed: v.optional(v.number()),
|
|
43
|
+
}).index("by_email", ["userEmail"])
|
|
44
|
+
.index("by_email_and_api", ["userEmail", "apiId"]),
|
|
45
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { query, mutation } from "./_generated/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
// Log a transaction
|
|
5
|
+
export const log = mutation({
|
|
6
|
+
args: {
|
|
7
|
+
userId: v.id("users"),
|
|
8
|
+
type: v.string(), // "credit_add", "api_purchase", "refund"
|
|
9
|
+
amount: v.number(),
|
|
10
|
+
description: v.string(),
|
|
11
|
+
},
|
|
12
|
+
handler: async (ctx, args) => {
|
|
13
|
+
const transactionId = await ctx.db.insert("transactions", {
|
|
14
|
+
userId: args.userId,
|
|
15
|
+
type: args.type,
|
|
16
|
+
amount: args.amount,
|
|
17
|
+
description: args.description,
|
|
18
|
+
createdAt: Date.now(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
return transactionId;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Get transaction history for user
|
|
26
|
+
export const listByUser = query({
|
|
27
|
+
args: {
|
|
28
|
+
userId: v.id("users"),
|
|
29
|
+
limit: v.optional(v.number()),
|
|
30
|
+
},
|
|
31
|
+
handler: async (ctx, args) => {
|
|
32
|
+
const query = ctx.db
|
|
33
|
+
.query("transactions")
|
|
34
|
+
.withIndex("by_user", (q) => q.eq("userId", args.userId))
|
|
35
|
+
.order("desc");
|
|
36
|
+
|
|
37
|
+
if (args.limit) {
|
|
38
|
+
return await query.take(args.limit);
|
|
39
|
+
}
|
|
40
|
+
return await query.collect();
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Get all transactions (for admin)
|
|
45
|
+
export const listAll = query({
|
|
46
|
+
args: {
|
|
47
|
+
limit: v.optional(v.number()),
|
|
48
|
+
},
|
|
49
|
+
handler: async (ctx, args) => {
|
|
50
|
+
const query = ctx.db.query("transactions").order("desc");
|
|
51
|
+
|
|
52
|
+
if (args.limit) {
|
|
53
|
+
return await query.take(args.limit);
|
|
54
|
+
}
|
|
55
|
+
return await query.collect();
|
|
56
|
+
},
|
|
57
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
/* This TypeScript project config describes the environment that
|
|
3
|
+
* Convex functions run in and is used to typecheck them.
|
|
4
|
+
* You can modify it, but some settings are required to use Convex.
|
|
5
|
+
*/
|
|
6
|
+
"compilerOptions": {
|
|
7
|
+
/* These settings are not required by Convex and can be modified. */
|
|
8
|
+
"allowJs": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
"jsx": "react-jsx",
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"allowSyntheticDefaultImports": true,
|
|
14
|
+
|
|
15
|
+
/* These compiler options are required by Convex */
|
|
16
|
+
"target": "ESNext",
|
|
17
|
+
"lib": ["ES2021", "dom"],
|
|
18
|
+
"forceConsistentCasingInFileNames": true,
|
|
19
|
+
"module": "ESNext",
|
|
20
|
+
"isolatedModules": true,
|
|
21
|
+
"noEmit": true
|
|
22
|
+
},
|
|
23
|
+
"include": ["./**/*"],
|
|
24
|
+
"exclude": ["./_generated"]
|
|
25
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { query, mutation } from "./_generated/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
// Get user by email, create if not exists
|
|
5
|
+
export const getOrCreate = mutation({
|
|
6
|
+
args: {
|
|
7
|
+
email: v.string(),
|
|
8
|
+
clerkId: v.optional(v.string()),
|
|
9
|
+
},
|
|
10
|
+
handler: async (ctx, args) => {
|
|
11
|
+
// Try to find existing user
|
|
12
|
+
const existing = await ctx.db
|
|
13
|
+
.query("users")
|
|
14
|
+
.withIndex("by_email", (q) => q.eq("email", args.email))
|
|
15
|
+
.first();
|
|
16
|
+
|
|
17
|
+
if (existing) {
|
|
18
|
+
// Update clerkId if provided and not set
|
|
19
|
+
if (args.clerkId && !existing.clerkId) {
|
|
20
|
+
await ctx.db.patch(existing._id, { clerkId: args.clerkId });
|
|
21
|
+
}
|
|
22
|
+
return existing._id;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Create new user
|
|
26
|
+
const userId = await ctx.db.insert("users", {
|
|
27
|
+
email: args.email,
|
|
28
|
+
clerkId: args.clerkId,
|
|
29
|
+
credits: 0,
|
|
30
|
+
createdAt: Date.now(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return userId;
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Add credits to user
|
|
38
|
+
export const addCredits = mutation({
|
|
39
|
+
args: {
|
|
40
|
+
userId: v.id("users"),
|
|
41
|
+
amount: v.number(), // in cents, positive or negative
|
|
42
|
+
},
|
|
43
|
+
handler: async (ctx, args) => {
|
|
44
|
+
const user = await ctx.db.get(args.userId);
|
|
45
|
+
if (!user) {
|
|
46
|
+
throw new Error("User not found");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const newBalance = user.credits + args.amount;
|
|
50
|
+
if (newBalance < 0) {
|
|
51
|
+
throw new Error("Insufficient credits");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
await ctx.db.patch(args.userId, { credits: newBalance });
|
|
55
|
+
return newBalance;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Get current balance
|
|
60
|
+
export const getBalance = query({
|
|
61
|
+
args: {
|
|
62
|
+
userId: v.id("users"),
|
|
63
|
+
},
|
|
64
|
+
handler: async (ctx, args) => {
|
|
65
|
+
const user = await ctx.db.get(args.userId);
|
|
66
|
+
if (!user) {
|
|
67
|
+
throw new Error("User not found");
|
|
68
|
+
}
|
|
69
|
+
return user.credits;
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Get user by ID
|
|
74
|
+
export const get = query({
|
|
75
|
+
args: {
|
|
76
|
+
userId: v.id("users"),
|
|
77
|
+
},
|
|
78
|
+
handler: async (ctx, args) => {
|
|
79
|
+
return await ctx.db.get(args.userId);
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Get user by email
|
|
84
|
+
export const getByEmail = query({
|
|
85
|
+
args: {
|
|
86
|
+
email: v.string(),
|
|
87
|
+
},
|
|
88
|
+
handler: async (ctx, args) => {
|
|
89
|
+
return await ctx.db
|
|
90
|
+
.query("users")
|
|
91
|
+
.withIndex("by_email", (q) => q.eq("email", args.email))
|
|
92
|
+
.first();
|
|
93
|
+
},
|
|
94
|
+
});
|