Skip to content

Commit

Permalink
fix: make it possible to refer multiple people
Browse files Browse the repository at this point in the history
classic copy/paste schema error.

updating D1 columns is apparently very hard, so I'm taking advantage of not having launched one more time...
  • Loading branch information
travis committed Dec 12, 2024
1 parent 3fb9f7b commit 8e1323a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion migrations/0001_create_referrals_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS users (
-- "reward" tracks whether the referee has paid long enough for the referrer to be rewarded
CREATE TABLE IF NOT EXISTS referrals (
email TEXT PRIMARY KEY,
refcode TEXT UNIQUE,
refcode TEXT,
referred_at DATETIME DEFAULT CURRENT_TIMESTAMP,
selected_plan TEXT,
rewarded BOOLEAN DEFAULT false,
Expand Down
23 changes: 23 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,30 @@ describe('the referral service', () => {
const referredByResponseJson = await referredByResponse.json() as any
expect(referredByResponseJson).toHaveProperty('refcode')

// create a second referral
const secondNewUserEmail = 'secondnewuser@example.com'
await SELF.fetch('http://example.com/referrals/create',
{
method: 'POST',
body: (() => {
const form = new FormData()
form.append('refcode', refcode)
form.append('email', secondNewUserEmail)
return form
})()
}
);

// now there should be two referrals
referralsResponse = await SELF.fetch(`http://example.com/referrals/${encodeURIComponent(refcode)}`);
referralsResponseJson = await referralsResponse.json() as any
expect(referralsResponseJson).toHaveProperty('referrals');
expect(referralsResponseJson.referrals).toBeInstanceOf(Array);
expect(referralsResponseJson.referrals).toHaveLength(2)

const secondReferredByResponse = await SELF.fetch(`http://example.com/referredby/${encodeURIComponent(secondNewUserEmail)}`);
const secondReferredByResponseJson = await secondReferredByResponse.json() as any
expect(secondReferredByResponseJson).toHaveProperty('refcode')
});

it('will not create a referral for the user who created the refcode', async () => {
Expand Down

0 comments on commit 8e1323a

Please sign in to comment.