All files / src/models user.ts

99.31% Statements 288/290
98.76% Branches 80/81
100% Functions 9/9
99.31% Lines 288/290

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 2911x 1x 1x 1x 1x 1x 1x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 7x 7x 7x 6x 5x 3x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 5x 5x 5x 4x 3x 2x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 7x 7x 7x 6x 5x 4x 3x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 6x 6x 6x 4x 2x 8x 8x 1x 7x 7x 6x 6x 7x 7x 1x 1x 7x 1x 28x 28x 26x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 29x 28x 28x 28x 27x 26x 24x 23x 22x 21x 20x 29x 9x 9x 19x 29x 29x 5x 5x 5x 3x 3x 5x 5x 29x 7x 7x 7x 4x 3x 7x 7x 7x 7x 7x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 11x 12x 1x 1x 10x 10x 3x 12x 2x 2x 12x 12x 12x 5x 5x  
import { isJobAnalysisPayload } from "./job";
 
/** Freemium entitlement tier (data-model.md `Users.tier`). */
export const TIERS = ["free", "premium"] as const;
export type Tier = (typeof TIERS)[number];
 
export function isTier(value: unknown): value is Tier {
  return typeof value === "string" && (TIERS as readonly string[]).includes(value);
}
 
/** Identity extracted from a verified Google ID token (auth contract). */
export interface AuthenticatedUser {
  sub: string;
  email: string;
  /** Entitlement tier, attached by withAuth from the Users row. */
  tier: Tier;
}
 
// Sized for a full pasted resume; must match the extension's PROFILE_TEXT_MAX.
export const PROFILE_TEXT_MAX = 20_000;
export const NOTES_MAX = 10_000;
export const SAVED_JOBS_SOFT_CAP = 1_000;
 
/** The only place per-tier limits live (data-model.md "Per-tier entitlements"). */
export const MONTHLY_ANALYSES: Record<Tier, number> = { free: 50, premium: 300 };
export const SAVED_JOBS_CAP: Record<Tier, number> = {
  free: 100,
  premium: SAVED_JOBS_SOFT_CAP,
};
 
/** `Users` row — PK "User", RK lowercased email (data-model.md). */
export interface UserEntity {
  partitionKey: "User";
  rowKey: string;
  /** Google stable id; absent for migrated rows that have never signed in. */
  sub?: string;
  tier: Tier;
  /** Admin override (CLI): true ⇒ 403 in withAuth. */
  blocked?: boolean;
  createdAt: string;
  migratedFromAllowlist?: boolean;
  paddleCustomerId?: string;
  paddleSubscriptionId?: string;
  subscriptionStatus?: "active" | "past_due" | "paused" | "canceled";
  renewsAt?: string;
  endsAt?: string;
  /** Stale guard: events with an older occurred_at are ignored (R4). */
  paddleEventOccurredAt?: string;
}
 
export function isUserEntity(value: unknown): value is UserEntity {
  if (typeof value !== "object" || value === null) return false;
  const v = value as Record<string, unknown>;
  return (
    v.partitionKey === "User" &&
    typeof v.rowKey === "string" &&
    isTier(v.tier) &&
    typeof v.createdAt === "string"
  );
}
 
/** `Usage` row — PK sub, RK "usage-" + YYYY-MM (data-model.md). */
export interface UsageEntity {
  partitionKey: string;
  rowKey: string;
  count: number;
  limit: number;
}
 
export function isUsageEntity(value: unknown): value is UsageEntity {
  if (typeof value !== "object" || value === null) return false;
  const v = value as Record<string, unknown>;
  return (
    typeof v.partitionKey === "string" &&
    typeof v.rowKey === "string" &&
    typeof v.count === "number" &&
    typeof v.limit === "number"
  );
}
 
/** `PaddleEvents` row — PK "PaddleEvent", RK event id (data-model.md). */
export interface PaddleEventEntity {
  partitionKey: "PaddleEvent";
  rowKey: string;
  eventType: string;
  occurredAt: string;
  processedAt: string;
  sub?: string;
}
 
export function isPaddleEventEntity(value: unknown): value is PaddleEventEntity {
  if (typeof value !== "object" || value === null) return false;
  const v = value as Record<string, unknown>;
  return (
    v.partitionKey === "PaddleEvent" &&
    typeof v.rowKey === "string" &&
    typeof v.eventType === "string" &&
    typeof v.occurredAt === "string" &&
    typeof v.processedAt === "string"
  );
}
 
export const JOB_STATUSES = [
  "interested",
  "applied",
  "interviewing",
  "rejected",
  "ghosted",
  "archived",
] as const;
export type JobStatus = (typeof JOB_STATUSES)[number];
 
export const ARRANGEMENTS = ["remote", "hybrid", "onsite", "unspecified"] as const;
export type Arrangement = (typeof ARRANGEMENTS)[number];
 
/** `Profiles` row — PK sub, RK "profile" (data-model.md). */
export interface ProfileEntity {
  partitionKey: string;
  rowKey: "profile";
  text: string;
  /** JSON-encoded string[] */
  dealbreakers: string;
  updatedAt: string;
  schemaVersion: number;
}
 
/** Discriminates a saved posting's origin (data-model.md §2; "paste" added by 005 FR-020/FR-021). */
export type PostingSource = "url" | "document" | "paste";
 
/** `SavedJobs` row — PK sub, RK sha256(canonicalUrl) (data-model.md). */
export interface SavedJobEntity {
  partitionKey: string;
  rowKey: string;
  canonicalUrl: string;
  sourceUrl: string;
  title: string;
  company: string;
  arrangement: string;
  status: string;
  notes: string;
  /** JSON-encoded JobAnalysis snapshot */
  analysisJson: string;
  savedAt: string;
  updatedAt: string;
  schemaVersion: number;
  /** Absent on rows written before 004 — the repository defaults to "url" on read. */
  source?: string;
  /** Absent on rows written before 004 — the repository defaults to "" on read. */
  filename?: string;
}
 
/** Wire shape of a saved job (matches the extension's `SavedJob`). */
export interface SavedJobPayload {
  schemaVersion: number;
  canonicalUrl: string;
  sourceUrl: string;
  /** Discriminator (data-model.md §2). Absent/"url" on legacy PUT bodies. */
  source: PostingSource;
  /** The uploaded document's original filename; "" for source="url". */
  filename: string;
  analysis: SavedJobAnalysis;
  status: JobStatus;
  notes: string;
  savedAt: string;
  updatedAt: string;
}
 
/** The persisted analysis snapshot: model output plus server-appended fields. */
export interface SavedJobAnalysis extends Record<string, unknown> {
  isJobPosting: boolean;
  title: string | null;
  company: string | null;
  arrangement: Arrangement;
  model: string;
  analyzedAt: string;
}
 
export interface ProfilePutBody {
  text: string;
  dealbreakers: string[];
}
 
export function isProfilePutBody(body: unknown): body is ProfilePutBody {
  if (typeof body !== "object" || body === null) return false;
  const v = body as Record<string, unknown>;
  return (
    typeof v.text === "string" &&
    Array.isArray(v.dealbreakers) &&
    v.dealbreakers.every((d) => typeof d === "string")
  );
}
 
function isHttpUrl(value: unknown): value is string {
  if (typeof value !== "string" || value.length === 0) return false;
  try {
    const url = new URL(value);
    return url.protocol === "http:" || url.protocol === "https:";
  } catch {
    return false;
  }
}
 
function isSavedJobAnalysis(value: unknown): value is SavedJobAnalysis {
  if (!isJobAnalysisPayload(value)) return false;
  const v = value as unknown as Record<string, unknown>;
  return typeof v.model === "string" && typeof v.analyzedAt === "string";
}
 
const DOC_CANONICAL_URL_RE = /^doc:[0-9a-f]{64}$/;
 
/** 005 FR-021: a paste-sourced posting's stable, content-derived identity. */
export const PASTE_CANONICAL_URL_RE = /^paste:[0-9a-f]{64}$/;
 
/**
 * Discriminated on `source` (data-model.md §2.2): omitted/"url" validates
 * exactly as before (isHttpUrl canonicalUrl); "document" requires
 * canonicalUrl to match `doc:<sha256 hex>` and a non-empty filename, with
 * sourceUrl allowed to be empty. "paste" (005 FR-020/FR-021) mirrors
 * "document" structurally: canonicalUrl matches `paste:<sha256 hex>`, and
 * sourceUrl/filename are both empty — a pasted posting has neither a URL nor
 * a file.
 */
export function isSavedJobPutBody(body: unknown): body is SavedJobPayload {
  if (typeof body !== "object" || body === null) return false;
  const v = body as Record<string, unknown>;
  if (
    typeof v.schemaVersion !== "number" ||
    typeof v.sourceUrl !== "string" ||
    !isSavedJobAnalysis(v.analysis) ||
    !JOB_STATUSES.includes(v.status as JobStatus) ||
    typeof v.notes !== "string" ||
    v.notes.length > NOTES_MAX ||
    typeof v.savedAt !== "string" ||
    typeof v.updatedAt !== "string"
  ) {
    return false;
  }
 
  const source = v.source === undefined ? "url" : v.source;
  if (source === "document") {
    return (
      typeof v.canonicalUrl === "string" &&
      DOC_CANONICAL_URL_RE.test(v.canonicalUrl) &&
      typeof v.filename === "string" &&
      v.filename.length > 0
    );
  }
  if (source === "paste") {
    return (
      typeof v.canonicalUrl === "string" &&
      PASTE_CANONICAL_URL_RE.test(v.canonicalUrl) &&
      v.sourceUrl === "" &&
      (v.filename === undefined || v.filename === "")
    );
  }
  if (source === "url") {
    return isHttpUrl(v.canonicalUrl);
  }
  return false;
}
 
/**
 * PATCH body: any of status / notes / analysis. `canonicalUrl` and `savedAt`
 * may be echoed back but are immutable — the handler rejects changed values.
 */
export interface SavedJobPatchBody {
  status?: JobStatus;
  notes?: string;
  analysis?: SavedJobAnalysis;
  canonicalUrl?: string;
  savedAt?: string;
}
 
export function isSavedJobPatchBody(body: unknown): body is SavedJobPatchBody {
  if (typeof body !== "object" || body === null) return false;
  const v = body as Record<string, unknown>;
  if (v.status !== undefined && !JOB_STATUSES.includes(v.status as JobStatus)) {
    return false;
  }
  if (
    v.notes !== undefined &&
    (typeof v.notes !== "string" || v.notes.length > NOTES_MAX)
  ) {
    return false;
  }
  if (v.analysis !== undefined && !isSavedJobAnalysis(v.analysis)) return false;
  if (v.canonicalUrl !== undefined && typeof v.canonicalUrl !== "string") return false;
  if (v.savedAt !== undefined && typeof v.savedAt !== "string") return false;
  return true;
}