Phase 1 foundation: backend, web profile, and mobile app
CI / policy (push) Successful in 0s
CI / mobile (push) Successful in 47s
CI / profile (push) Successful in 49s
CI / backend (push) Failing after 49s

Greenfield implementation of CardClaws Phase 1 across three surfaces.

Backend (Rust/Axum workspace, 67 tests):
- cardclaws-types/config/db/auth/api/wallet crates
- Auth: register, login + lockout, magic link, refresh rotation, Apple verify
- Cards: CRUD, tier-limited publish, duplicate, public handle lookup
- Assets: R2 presigned uploads; vCard export
- Apple Wallet .pkpass pipeline (PKCS#7 signer behind apple-signing feature)
- Analytics ingest + summary with daily-salted IP hashing
- Migrations 0001 (incl. cardclaws_sessions) + 0002 analytics

Web profile (Astro SSR): cardclaws.com/[handle] hero + flip, contact actions,
client-built vCard, visit attribution. Verified end-to-end.

Mobile (Expo SDK 51): auth, card list/create, builder v1 (bg/text/logo,
palette, undo/redo), Skia/Reanimated viewer (flip + ambient). 22 logic tests.

CI: policy/backend/profile/mobile jobs; LOC + no-placeholder lint.

Review fixes baked in: `back` (not `cardclaws`) key; strip image is a bundled
manifest file (not a URL); sessions table added; NFC reframed; test doubles
allowed for external services.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
This commit is contained in:
Omar Sobh
2026-06-04 10:17:26 -05:00
co-authored by Claude Opus 4.8
commit c30d3afeec
128 changed files with 36279 additions and 0 deletions
@@ -0,0 +1,50 @@
//! Analytics event writes and summary aggregation (PRD §18).
//!
//! Phase 1 writes events directly to `analytics_events`. The Redis write-buffer
//! + hourly rollup path (PRD §18.1/§18.2) is a Phase 2 optimization.
use uuid::Uuid;
use crate::models::analytics::AnalyticsSummary;
use crate::Db;
pub struct NewEvent<'a> {
pub card_id: Uuid,
pub event_type: &'a str,
pub ip_hash: Option<&'a str>,
pub user_agent: Option<&'a str>,
}
pub async fn insert_event(db: &Db, ev: NewEvent<'_>) -> Result<(), sqlx::Error> {
sqlx::query(
r#"INSERT INTO analytics_events (card_id, event_type, ip_hash, user_agent)
VALUES ($1, $2, $3, $4)"#,
)
.bind(ev.card_id)
.bind(ev.event_type)
.bind(ev.ip_hash)
.bind(ev.user_agent)
.execute(db)
.await?;
Ok(())
}
/// Aggregate metrics for one card across the standard windows.
pub async fn summary(db: &Db, card_id: Uuid) -> Result<AnalyticsSummary, sqlx::Error> {
sqlx::query_as(
r#"
SELECT
COUNT(*) FILTER (WHERE event_type = 'profile_visit') AS total_visits,
COUNT(*) FILTER (WHERE event_type = 'profile_visit' AND occurred_at > now() - interval '7 days') AS visits_7d,
COUNT(*) FILTER (WHERE event_type = 'profile_visit' AND occurred_at > now() - interval '24 hours') AS visits_24h,
COUNT(*) FILTER (WHERE event_type = 'qr_scan') AS qr_scans,
COUNT(*) FILTER (WHERE event_type = 'contact_save') AS contact_saves,
COUNT(*) FILTER (WHERE event_type = 'link_click') AS link_clicks
FROM analytics_events
WHERE card_id = $1
"#,
)
.bind(card_id)
.fetch_one(db)
.await
}
@@ -0,0 +1,104 @@
//! Card table queries.
use uuid::Uuid;
use crate::models::card::CardRow;
use crate::Db;
pub struct NewCard<'a> {
pub owner_id: Uuid,
pub handle: &'a str,
pub definition: &'a serde_json::Value,
}
pub async fn insert(db: &Db, new: NewCard<'_>) -> Result<CardRow, sqlx::Error> {
sqlx::query_as(
r#"
INSERT INTO cards (owner_id, handle, definition)
VALUES ($1, $2, $3)
RETURNING id, owner_id, handle, status, definition, version,
created_at, updated_at
"#,
)
.bind(new.owner_id)
.bind(new.handle)
.bind(new.definition)
.fetch_one(db)
.await
}
pub async fn find_by_id(db: &Db, id: Uuid) -> Result<Option<CardRow>, sqlx::Error> {
sqlx::query_as(
r#"SELECT id, owner_id, handle, status, definition, version, created_at, updated_at
FROM cards WHERE id = $1"#,
)
.bind(id)
.fetch_optional(db)
.await
}
pub async fn list_by_owner(db: &Db, owner_id: Uuid) -> Result<Vec<CardRow>, sqlx::Error> {
sqlx::query_as(
r#"SELECT id, owner_id, handle, status, definition, version, created_at, updated_at
FROM cards WHERE owner_id = $1 ORDER BY created_at DESC"#,
)
.bind(owner_id)
.fetch_all(db)
.await
}
/// Public lookup: only `active` cards are resolvable by handle (PRD §6.6,
/// review A2 — the profile resolves the active card's handle).
pub async fn find_active_by_handle(db: &Db, handle: &str) -> Result<Option<CardRow>, sqlx::Error> {
sqlx::query_as(
r#"SELECT id, owner_id, handle, status, definition, version, created_at, updated_at
FROM cards WHERE handle = $1 AND status = 'active'"#,
)
.bind(handle)
.fetch_optional(db)
.await
}
/// Count a user's `active` cards — used to enforce per-tier limits (§6.8.2).
pub async fn count_active_for_owner(db: &Db, owner_id: Uuid) -> Result<i64, sqlx::Error> {
let (count,): (i64,) =
sqlx::query_as("SELECT COUNT(*) FROM cards WHERE owner_id = $1 AND status = 'active'")
.bind(owner_id)
.fetch_one(db)
.await?;
Ok(count)
}
/// Replace the definition and bump the version atomically.
pub async fn update_definition(
db: &Db,
id: Uuid,
definition: &serde_json::Value,
) -> Result<CardRow, sqlx::Error> {
sqlx::query_as(
r#"
UPDATE cards
SET definition = $2, version = version + 1, updated_at = now()
WHERE id = $1
RETURNING id, owner_id, handle, status, definition, version, created_at, updated_at
"#,
)
.bind(id)
.bind(definition)
.fetch_one(db)
.await
}
pub async fn set_status(db: &Db, id: Uuid, status: &str) -> Result<CardRow, sqlx::Error> {
sqlx::query_as(
r#"
UPDATE cards SET status = $2, updated_at = now()
WHERE id = $1
RETURNING id, owner_id, handle, status, definition, version, created_at, updated_at
"#,
)
.bind(id)
.bind(status)
.fetch_one(db)
.await
}
@@ -0,0 +1,7 @@
//! SQL query functions, one module per table. Functions take `&Db` (or a
//! transaction) and return domain types or row models.
pub mod analytics;
pub mod cards;
pub mod sessions;
pub mod users;
@@ -0,0 +1,66 @@
//! Session (refresh-token) queries. Tokens are stored only as SHA-256 hashes.
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::models::session::SessionRow;
use crate::Db;
pub struct NewSession<'a> {
pub user_id: Uuid,
pub refresh_token_hash: &'a str,
pub device_fingerprint: Option<&'a str>,
pub expires_at: DateTime<Utc>,
}
pub async fn insert(db: &Db, new: NewSession<'_>) -> Result<SessionRow, sqlx::Error> {
sqlx::query_as(
r#"
INSERT INTO cardclaws_sessions
(user_id, refresh_token_hash, device_fingerprint, expires_at)
VALUES ($1, $2, $3, $4)
RETURNING id, user_id, refresh_token_hash, device_fingerprint,
last_active_at, expires_at, created_at
"#,
)
.bind(new.user_id)
.bind(new.refresh_token_hash)
.bind(new.device_fingerprint)
.bind(new.expires_at)
.fetch_one(db)
.await
}
/// Look up a live (non-expired) session by its token hash.
pub async fn find_live_by_hash(
db: &Db,
token_hash: &str,
) -> Result<Option<SessionRow>, sqlx::Error> {
sqlx::query_as(
r#"SELECT id, user_id, refresh_token_hash, device_fingerprint,
last_active_at, expires_at, created_at
FROM cardclaws_sessions
WHERE refresh_token_hash = $1 AND expires_at > now()"#,
)
.bind(token_hash)
.fetch_optional(db)
.await
}
/// Delete a session by id (used both for rotation and explicit logout).
pub async fn delete(db: &Db, id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM cardclaws_sessions WHERE id = $1")
.bind(id)
.execute(db)
.await?;
Ok(())
}
/// Delete every session for a user (logout-everywhere / account deletion).
pub async fn delete_all_for_user(db: &Db, user_id: Uuid) -> Result<(), sqlx::Error> {
sqlx::query("DELETE FROM cardclaws_sessions WHERE user_id = $1")
.bind(user_id)
.execute(db)
.await?;
Ok(())
}
@@ -0,0 +1,85 @@
//! User table queries.
use cardclaws_types::User;
use uuid::Uuid;
use crate::models::user::UserRow;
use crate::Db;
/// Parameters for inserting a new user.
pub struct NewUser<'a> {
pub email: &'a str,
pub handle: &'a str,
pub display_name: &'a str,
pub password_hash: Option<&'a str>,
}
/// Insert a user. Returns a `Conflict`-style sqlx error if email/handle is taken
/// (the caller maps unique-violation to a 409).
pub async fn insert(db: &Db, new: NewUser<'_>) -> Result<User, sqlx::Error> {
let row: UserRow = sqlx::query_as(
r#"
INSERT INTO users (email, handle, display_name, password_hash)
VALUES ($1, $2, $3, $4)
RETURNING id, email, handle, display_name, tier, password_hash,
avatar_r2_key, created_at, updated_at
"#,
)
.bind(new.email)
.bind(new.handle)
.bind(new.display_name)
.bind(new.password_hash)
.fetch_one(db)
.await?;
Ok(row.into_domain())
}
pub async fn find_by_email(db: &Db, email: &str) -> Result<Option<User>, sqlx::Error> {
let row: Option<UserRow> = sqlx::query_as(
r#"SELECT id, email, handle, display_name, tier, password_hash,
avatar_r2_key, created_at, updated_at
FROM users WHERE email = $1"#,
)
.bind(email)
.fetch_optional(db)
.await?;
Ok(row.map(UserRow::into_domain))
}
pub async fn find_by_id(db: &Db, id: Uuid) -> Result<Option<User>, sqlx::Error> {
let row: Option<UserRow> = sqlx::query_as(
r#"SELECT id, email, handle, display_name, tier, password_hash,
avatar_r2_key, created_at, updated_at
FROM users WHERE id = $1"#,
)
.bind(id)
.fetch_optional(db)
.await?;
Ok(row.map(UserRow::into_domain))
}
/// Returns true if the handle is already taken (any user). Used when
/// auto-generating a unique handle for OAuth sign-ups.
pub async fn handle_taken(db: &Db, handle: &str) -> Result<bool, sqlx::Error> {
let exists: Option<(i32,)> = sqlx::query_as("SELECT 1 FROM users WHERE handle = $1 LIMIT 1")
.bind(handle)
.fetch_optional(db)
.await?;
Ok(exists.is_some())
}
/// Returns true if either the email or the handle is already taken. Used to give
/// a clear 409 before attempting the insert.
pub async fn email_or_handle_taken(
db: &Db,
email: &str,
handle: &str,
) -> Result<bool, sqlx::Error> {
let exists: Option<(i32,)> =
sqlx::query_as("SELECT 1 FROM users WHERE email = $1 OR handle = $2 LIMIT 1")
.bind(email)
.bind(handle)
.fetch_optional(db)
.await?;
Ok(exists.is_some())
}