(data: {
name: string;
environment: "development" | "production";
})
| 12 | import { hashKey } from "@/lib/developer-key-hash"; |
| 13 | |
| 14 | export async function createDeveloperApp(data: { |
| 15 | name: string; |
| 16 | environment: "development" | "production"; |
| 17 | }) { |
| 18 | const user = await getCurrentUser(); |
| 19 | if (!user) throw new Error("Unauthorized"); |
| 20 | |
| 21 | if (!data.name.trim()) throw new Error("App name is required"); |
| 22 | |
| 23 | const appId = nanoId(); |
| 24 | const publicKeyRaw = `cpk_${nanoIdLong()}`; |
| 25 | const secretKeyRaw = `csk_${nanoIdLong()}`; |
| 26 | const publicKeyHash = await hashKey(publicKeyRaw); |
| 27 | const secretKeyHash = await hashKey(secretKeyRaw); |
| 28 | |
| 29 | const encryptedPublicKey = await encrypt(publicKeyRaw); |
| 30 | const encryptedSecretKey = await encrypt(secretKeyRaw); |
| 31 | |
| 32 | await db().transaction(async (tx) => { |
| 33 | await tx.insert(developerApps).values({ |
| 34 | id: appId, |
| 35 | ownerId: user.id, |
| 36 | name: data.name.trim(), |
| 37 | environment: data.environment, |
| 38 | }); |
| 39 | |
| 40 | await tx.insert(developerApiKeys).values([ |
| 41 | { |
| 42 | id: nanoId(), |
| 43 | appId, |
| 44 | keyType: "public", |
| 45 | keyPrefix: publicKeyRaw.slice(0, 12), |
| 46 | keyHash: publicKeyHash, |
| 47 | encryptedKey: encryptedPublicKey, |
| 48 | }, |
| 49 | { |
| 50 | id: nanoId(), |
| 51 | appId, |
| 52 | keyType: "secret", |
| 53 | keyPrefix: secretKeyRaw.slice(0, 12), |
| 54 | keyHash: secretKeyHash, |
| 55 | encryptedKey: encryptedSecretKey, |
| 56 | }, |
| 57 | ]); |
| 58 | |
| 59 | await tx.insert(developerCreditAccounts).values({ |
| 60 | id: nanoId(), |
| 61 | appId, |
| 62 | ownerId: user.id, |
| 63 | }); |
| 64 | }); |
| 65 | |
| 66 | return { |
| 67 | appId, |
| 68 | publicKey: publicKeyRaw, |
| 69 | secretKey: secretKeyRaw, |
| 70 | }; |
| 71 | } |
no test coverage detected