Skip to content

Commit

Permalink
fix: subscription not found in generate referral fn
Browse files Browse the repository at this point in the history
  • Loading branch information
lui7henrique committed Oct 16, 2024
1 parent bf1cc35 commit f424533
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@
6. **Documentation and Deployment**
- [x] Document API endpoints using Swagger/OpenAPI.
- [x] Set up deployment using Docker.
- [ ] Ensure proper security and performance settings in production.
- [x] Ensure proper security and performance settings in production.
5 changes: 5 additions & 0 deletions src/app/errors/subscription-not-found-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class SubscriptionNotFound extends Error {
constructor() {
super('Subscription not found.')
}
}
77 changes: 39 additions & 38 deletions src/app/functions/generate-referral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { schema } from '@/db/schema'
import { and, eq } from 'drizzle-orm'
import { ReferralLinkAlreadyExists } from '../errors/referral-link-already-exists'
import { randomBytes } from 'node:crypto'
import { SubscriptionNotFound } from '../errors/subscription-not-found-error'

type GenerateReferralInput = {
email: string
Expand All @@ -14,47 +15,47 @@ export async function generateReferral({
email,
eventId,
}: GenerateReferralInput) {
try {
const [existingReferralLink] = await db
.select()
.from(schema.referral)
.where(
and(
eq(schema.referral.email, email),
eq(schema.referral.eventId, eventId)
)
const [existingReferralLink] = await db
.select()
.from(schema.referral)
.where(
and(
eq(schema.referral.email, email),
eq(schema.referral.eventId, eventId)
)
)

if (existingReferralLink) {
return makeLeft(new ReferralLinkAlreadyExists())
}

const token = randomBytes(16).toString('hex')
const url = `${process.env.BASE_URL}/referral?token=${token}`

const [subscription] = await db
.select()
.from(schema.subscriptions)
.where(
and(
eq(schema.subscriptions.email, email),
eq(schema.subscriptions.eventId, eventId)
)
if (existingReferralLink) {
return makeLeft(new ReferralLinkAlreadyExists())
}

const token = randomBytes(16).toString('hex')
const url = `${process.env.BASE_URL}/referral?token=${token}`

const [subscription] = await db
.select()
.from(schema.subscriptions)
.where(
and(
eq(schema.subscriptions.email, email),
eq(schema.subscriptions.eventId, eventId)
)
)

const [referral] = await db
.insert(schema.referral)
.values({
email,
eventId,
link: url,
token: token,
parentId: subscription.referralId || null,
})
.returning()

return makeRight({ referral })
} catch (error) {
throw error
if (!subscription) {
return makeLeft(new SubscriptionNotFound())
}

const [referral] = await db
.insert(schema.referral)
.values({
email,
eventId,
link: url,
token: token,
parentId: subscription.referralId || null,
})
.returning()

return makeRight({ referral })
}

0 comments on commit f424533

Please sign in to comment.