Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial spike on referrals service #1

Merged
merged 12 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test
description: 'Setup and test'

runs:
using: "composite"
steps:
- name: Install pnpm
uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v4
with:
registry-url: 'https://registry.npmjs.org'
node-version: 20
cache: 'pnpm'
- run: pnpm install
shell: bash
- run: pnpm test
shell: bash
39 changes: 39 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
on:
push:
branches:
- main
name: Release
jobs:
release-staging:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/test
- name: Deploy to Staging
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{secrets.CF_TOKEN }}
environment: 'staging'
release-production:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: google-github-actions/release-please-action@v3
id: release
with:
release-type: node
package-name: referrals-service
- uses: actions/checkout@v4
- uses: ./.github/actions/test
- name: Deploy to Production
uses: cloudflare/wrangler-action@2.0.0
with:
apiToken: ${{ secrets.CF_TOKEN }}
environment: 'production'
if: ${{ steps.release.outputs.release_created }}

16 changes: 16 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Test
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: ./.github/actions/test
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Storacha Referrals Service

Simple RESTful service for tracking Storacha referrals.

## Run

To get started, clone this repo and run:

```
pnpm install
pnpm dev
```

## Domain Model

A `refcode` is a 16 character string drawn from
the [`nolookalikesSafe` nanoid dictionary](https://github.com/CyberAP/nanoid-dictionary).
It is associated with an email address.

A `referral` is a record of a different email address using a refcode to sign up. It
records an email, a refcode and referral time.

## API

The API does not use any sort of authorization at the moment as it needs to be usable by
users who have not yet established an identity relationship with us. As a result it's important
that it not return sensitive information like email addresses in any responses.

### `POST /refcode/create'`

Create a refcode by posting form data with `email` set to the email the refcode will
be attached to.

### `GET /refcode/:email'`

Get the refcode associated with an email. Returns a JSON object like:

```
{
refcode: 'abc123'
}
```

### `POST /referrals/create'`

Create a referral by posting form data with `email` set to the email of the referred
user and `refcode` set to the refcode they used to sign up.

### `GET /referredby/:email'`

Get the refcode used when an email address signed up. Returns a JSON object like:

```
{
refcode: 'abc123'
}
```

### `GET /referrals/:refcode'`

Get the referrals associated with a refcode. Returns a list of JSON objects like:


```
[
{ referredAt: '2024-11-21 00:45:12', rewarded: false }
]
```



18 changes: 18 additions & 0 deletions migrations/0001_create_referrals_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Migration number: 0001 2024-10-21T15:14:46.268Z

-- users identified by email. each user has a refcode they can use to invite other users
CREATE TABLE IF NOT EXISTS users (
email TEXT PRIMARY KEY,
refcode TEXT UNIQUE
);

-- referrals identified by email. each referral tracks the refcode it was referred by.
-- "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,
referred_at DATETIME DEFAULT CURRENT_TIMESTAMP,
selected_plan TEXT,
rewarded BOOLEAN DEFAULT false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work - is there some external process that's updating it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep exactly - I have the beginnings of a cronjob in this PR that will go look at Stripe and update these referrals after we've granted the referrer credits.

FOREIGN KEY(refcode) REFERENCES users(refcode)
);
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"dev": "wrangler dev --port 4000",
"start": "wrangler dev",
"test": "vitest",
"cf-typegen": "wrangler types"
"cf-typegen": "wrangler types",
"db:local:clear": "rm -rf .wrangler/state/v3/d1",
"db:local:init": "pnpm wrangler d1 migrations apply referrals --local",
"db:local:reset": "pnpm db:local:clear && pnpm db:local:init"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.5.2",
"@cloudflare/workers-types": "^4.20241106.0",
"typescript": "^5.5.2",
"vitest": "2.0.5",
"wrangler": "^3.60.3"
},
"dependencies": {
"@tsndr/cloudflare-worker-router": "^3.2.10",
"nanoid": "^5.0.8",
"stripe": "^17.3.1"
}
}
Loading