-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dda71a
commit 8f12337
Showing
15 changed files
with
1,079 additions
and
1,384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
}), | ||
}) |
Oops, something went wrong.