Toolkit
Beta

Service Layer

Core business logic layer with clean, typed methods for handling signups and referrals.

WaitlistService

Core implementation of the waitlist service. It can be extended with additional features.

waitlist_service.ts
import { randomBytes } from 'crypto';
 
export class WaitlistService {
  /**
   * Adds an email to the waitlist with the provided referral code and returns entry details.
   * If email already exists in the waitlist, it returns the existing entry details.
   */
  async addToWaitlist({ email, referredByCode }: { email: string; referredByCode?: string }) {
    // Find the signup record belonging to the provided referredByCode if exists
    const referredByRecord = referredByCode
      ? await prisma.waitlistSignUps.findFirst({ where: { referralCode: referredByCode } })
      : undefined;
 
    // Insert the new signup table if the email does not already exists in our waitlist
    const newReferralCode = this.generateReferralCode();
    const signUpRecord = await prisma.waitlistSignUps.upsert({
      where: { email },
      create: {
        email,
        referralCode: newReferralCode,
        referredById: referredByRecord?.id,
      },
      update: {}, // Do nothing if email already exists in the database
    });
 
    return {
      id: signUpRecord.id,
      referralCode: signUpRecord.referralCode,
      createdAt: signUpRecord.createdAt,
    };
  }
 
  /**
   * Generates a referral URL for the provided referral code.
   */
  getReferralUrl(referralCode: string) {
    const url = new URL(BASE_URL);
    url.pathname = REFERRAL_ENTRY_PATH;
    url.searchParams.set(REFERRAL_CODE_QUERY_KEY, referralCode);
    return url.toString();
  }
 
  /**
   * Generates an 8-character, URL-safe, cryptographically random referral code.
   */
  private generateReferralCode() {
    return randomBytes(8).toString('base64url');
  }
}

Required Constants

The below constants are used in WaitlistService. Replace them or adjust their values based on your application setup and needs.

constants.ts
/**
 * The base URL of your client application.
 * This should be adjusted depending on the environment (e.g. http://localhost:3000).
 */
export const BASE_URL = 'https://example.com';
 
/**
 * The query key used to pass the referral code in the URL.
 * Example: https://example.com/signup?ref=abc123
 */
export const REFERRAL_CODE_QUERY_KEY = 'ref';
/**
 * The path where the referred users will land on in your website or app.
 * Example: /signup
 */
export const REFERRAL_ENTRY_PATH = '/';

Key Design Decisions

  1. Uses upsert operation to handle both new signups and duplicate attempts gracefully.
  2. Generates URL-safe, cryptographically secure referral codes using base64url encoding.
  3. Separates referral URL generation from core signup logic for flexibility.
  4. Returns minimal response data to maintain clean service boundaries.
  5. Handles optional referral codes without requiring conditional logic in consumers.
  6. Uses 8-byte random values for referral codes, balancing uniqueness and usability.
  7. Implements URL generation with standard Web APIs for consistent formatting.

On this page