Skip to content

Commit

Permalink
One referral per user
Browse files Browse the repository at this point in the history
  • Loading branch information
youngkidwarrior committed Oct 27, 2024
1 parent 3dda71a commit 8f12337
Show file tree
Hide file tree
Showing 15 changed files with 1,079 additions and 1,384 deletions.
2 changes: 2 additions & 0 deletions packages/api/src/routers/_app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { tagRouter } from './tag'
import { secretShopRouter } from './secretShop'
import { sendAccountRouter } from './sendAccount'
import { accountRecoveryRouter } from './account-recovery/router'
import { referralsRouter } from './referrals'

export const appRouter = createTRPCRouter({
chainAddress: chainAddressRouter,
Expand All @@ -16,6 +17,7 @@ export const appRouter = createTRPCRouter({
distribution: distributionRouter,
secretShop: secretShopRouter,
sendAccount: sendAccountRouter,
referrals: referralsRouter,
})

export type AppRouter = typeof appRouter
Expand Down
44 changes: 44 additions & 0 deletions packages/api/src/routers/referrals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TRPCError } from '@trpc/server'
import { createTRPCRouter, protectedProcedure } from '../trpc'
import debug from 'debug'
import { supabaseAdmin } from 'app/utils/supabase/admin'
const log = debug('api:routers:referrals')

export const referralsRouter = createTRPCRouter({
getReferred: protectedProcedure.query(async ({ ctx }) => {
const { user } = ctx.session
if (!user) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'Unauthorized',
})
}
const { data: referred, error } = await supabaseAdmin
.from('referrals')
.select('*')
.eq('referred_id', user.id)
.single()
if (error && error.code !== 'PGRST116') {
log('referrals error', error)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: error.message,
})
}
if (!referred) return null
const { data: referrerSendAccount, error: referrerError } = await supabaseAdmin
.from('send_accounts')
.select('*')
.eq('id', referred.referrer_id)
.single()
if (referrerError && referrerError.code !== 'PGRST116') {
log('referrals error', referrerError)
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: referrerError.message,
})
}

return { referred, referrerSendAccount }
}),
})
Loading

0 comments on commit 8f12337

Please sign in to comment.