MCPcopy
hub / github.com/anomalyco/opencode / createFromAccount

Function createFromAccount

packages/console/core/src/referral.ts:288–363  ·  view source on GitHub ↗
(input: { accountID: string; referralCode?: string })

Source from the content-addressed store, hash-verified

286 })
287
288 export async function createFromAccount(input: { accountID: string; referralCode?: string }) {
289 const referralCode = normalizeCode(input.referralCode)
290 if (!referralCode) return
291
292 return Database.transaction(async (tx) => {
293 const code = await tx
294 .select({ workspaceID: ReferralCodeTable.workspaceID })
295 .from(ReferralCodeTable)
296 .innerJoin(WorkspaceTable, eq(WorkspaceTable.id, ReferralCodeTable.workspaceID))
297 .where(and(eq(ReferralCodeTable.code, referralCode), isNull(WorkspaceTable.timeDeleted)))
298 .then((rows) => rows[0])
299 if (!code) throw new Error("Referral code invalid")
300
301 const existingReferral = await tx
302 .select({ id: ReferralTable.id })
303 .from(ReferralTable)
304 .where(and(eq(ReferralTable.inviteeAccountID, input.accountID), isNull(ReferralTable.timeDeleted)))
305 .then((rows) => rows[0])
306 if (existingReferral) throw new Error("Referral already redeemed")
307
308 const selfReferral = await tx
309 .select({ id: UserTable.id })
310 .from(UserTable)
311 .where(
312 and(
313 eq(UserTable.workspaceID, code.workspaceID),
314 eq(UserTable.accountID, input.accountID),
315 isNull(UserTable.timeDeleted),
316 ),
317 )
318 .then((rows) => rows[0])
319 if (selfReferral) throw new Error("Self-referral is not allowed")
320
321 const workspaceIDs = await tx
322 .select({ workspaceID: UserTable.workspaceID })
323 .from(UserTable)
324 .where(and(eq(UserTable.accountID, input.accountID), isNull(UserTable.timeDeleted)))
325 .then((rows) => rows.map((row) => row.workspaceID))
326 if (workspaceIDs.length === 0) return
327
328 const activeLite = await tx
329 .select({ id: LiteTable.id })
330 .from(LiteTable)
331 .where(and(inArray(LiteTable.workspaceID, workspaceIDs), isNull(LiteTable.timeDeleted)))
332 .then((rows) => rows[0])
333 if (activeLite) return
334
335 const litePayment = await tx
336 .select({ id: PaymentTable.id })
337 .from(PaymentTable)
338 .where(
339 and(
340 inArray(PaymentTable.workspaceID, workspaceIDs),
341 isNull(PaymentTable.timeDeleted),
342 sql`JSON_UNQUOTE(JSON_EXTRACT(${PaymentTable.enrichment}, '$.type')) = 'lite'`,
343 ),
344 )
345 .then((rows) => rows[0])

Callers

nothing calls this directly

Calls 9

normalizeCodeFunction · 0.85
eqFunction · 0.85
transactionMethod · 0.80
insertMethod · 0.80
createMethod · 0.65
whereMethod · 0.45
fromMethod · 0.45
selectMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected