Skip to content
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
18 changes: 9 additions & 9 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ APP_URL=http://localhost:3000
# openssl rand -base64 48
SESSION_SECRET=

# ---- database and photos (npm run setup fills these in) ----
# ---- database and photos (bun run setup fills these in) ----
# TigerRetail development PostgreSQL database (ask the project lead)
DATABASE_URL=postgresql://<user>:<password>@<host>:5432/<database>?sslmode=require
# Cloudflare R2 bucket for listing photos and avatars
R2_ACCOUNT_ID=
R2_BUCKET=tigerretail
R2_ACCESS_KEY_ID=
R2_SECRET_ACCESS_KEY=
# the bucket's Public Development URL (R2 → bucket → Settings)
R2_PUBLIC_URL=https://pub-<id>.r2.dev
# Private S3 bucket for listing photos and avatars; CloudFront serves reads.
AWS_REGION=us-east-1
S3_BUCKET=tigerretail-photos-104733724423-us-east-1
S3_PUBLIC_URL=https://d1ugkt2f7zwiiq.cloudfront.net
# Local development only: AWS CLI profile with access to the photo bucket.
# Run aws sso login --profile <profile>. Omit on EC2; its IAM role supplies credentials.
# AWS_PROFILE=TigerApps

# ---- listserv import ----
# netids allowed to run imports and approve them, comma-separated
Expand All @@ -22,7 +22,7 @@ ANTHROPIC_API_KEY=
# InboxEngine finds the sale emails (ask TigerApps for a token)
INBOX_ENGINE_URL=https://inbox-engine.tigerapps.org
INBOX_ENGINE_TOKEN=
# protects /api/cron/import (the daily scheduled job sends it; npm run import uses it). openssl rand -hex 24
# protects /api/cron/import (the daily scheduled job sends it; bun run import uses it). openssl rand -hex 24
CRON_SECRET=

# Invites to people whose listserv sales were imported (admin clicks "send invite").
Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: CI and deploy
on:
pull_request:
push:
branches: [main]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: tigerretail-${{ github.ref }}
cancel-in-progress: false

jobs:
build:
runs-on: ubuntu-24.04-arm
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
- uses: oven-sh/setup-bun@v2
with:
bun-version: '1.3.5'
- run: bun install --frozen-lockfile
- run: bun run lint
- run: bun run test
- run: bun run build
env:
NEXT_TELEMETRY_DISABLED: '1'
- run: bash deploy/package.sh
- uses: actions/upload-artifact@v4
with:
name: release
path: |
release.tar.gz
release.tar.gz.sha256
retention-days: 7

deploy:
needs: build
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: release
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }}
aws-region: us-east-1
- name: Upload and activate release
env:
INSTANCE_ID: ${{ vars.EC2_INSTANCE_ID }}
RELEASE_ID: ${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}
run: |
set -euo pipefail
sha256sum -c release.tar.gz.sha256
checksum="$(cut -d ' ' -f1 release.tar.gz.sha256)"
aws s3 cp release.tar.gz "s3://tigerretail-deploy-104733724423-us-east-1/releases/$RELEASE_ID.tar.gz" --only-show-errors
command_id="$(aws ssm send-command \
--instance-ids "$INSTANCE_ID" \
--document-name TigerRetailDeploy \
--parameters "Release=$RELEASE_ID,Sha256=$checksum" \
--timeout-seconds 600 --query Command.CommandId --output text)"
for attempt in $(seq 1 120); do
status="$(aws ssm get-command-invocation --command-id "$command_id" --instance-id "$INSTANCE_ID" --query Status --output text 2>/dev/null || true)"
case "$status" in
Success) break ;;
Failed|Cancelled|TimedOut|Cancelling) break ;;
esac
sleep 5
done
aws ssm get-command-invocation --command-id "$command_id" --instance-id "$INSTANCE_ID" --query '{Status:Status,Output:StandardOutputContent,Error:StandardErrorContent}'
[[ "$status" == Success ]]
curl --retry 3 --retry-delay 5 --fail --silent --show-error https://retail.tigerapps.org/api/health
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

# deployment artifacts
/release.tar.gz
/release.tar.gz.sha256
58 changes: 40 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# TigerRetail

Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRetail development PostgreSQL database, and Cloudflare R2 for photos.
Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRetail development PostgreSQL database, and AWS S3 + CloudFront for photos.

## Run it on your machine

1. Install Node 22+ and Google Chrome (the e2e tests drive Chrome).
2. `git clone https://github.com/TigerAppsOrg/TigerRetail.git && cd TigerRetail && npm install`
3. `npm run setup` asks for the database and R2 keys one at a time and saves them to `.env.local`.
1. Install Node 22, Bun, AWS CLI, and Google Chrome (the e2e tests drive Chrome).
2. `git clone https://github.com/TigerAppsOrg/TigerRetail.git && cd TigerRetail && bun install --frozen-lockfile`
3. `bun run setup` asks for the database and S3 settings one at a time and saves them to `.env.local`.
4. Copy the remaining keys you need from `.env.example` into `.env.local` (see the table below).
5. `npm run dev`, open http://localhost:3000 and sign in with your netid.
5. `bun run dev`, open http://localhost:3000 and sign in with your netid.

`.env.local` is gitignored. Never commit keys, and never paste them in Slack or GitHub.

Expand All @@ -17,35 +17,57 @@ Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRe
| Key | Needed for | Where to get it |
|---|---|---|
| `DATABASE_URL` | everything | Ask Raj (shared dev database) |
| `R2_ACCOUNT_ID`, `R2_BUCKET`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_PUBLIC_URL` | photos | Ask Raj for an R2 API token (Object Read & Write on bucket `tigerretail`) |
| `AWS_REGION`, `S3_BUCKET`, `S3_PUBLIC_URL` | photos | See [AWS hosting](deploy/README.md); ask TigerApps for AWS access, then `aws sso login --profile <profile>` and set `AWS_PROFILE` locally |
| `SESSION_SECRET` | sign-in | Make your own: `openssl rand -base64 48` |
| `APP_URL` | sign-in | `http://localhost:3000` |
| `ADMIN_NETIDS` | the import review page (`/admin/imports`) | Your netid |
| `ANTHROPIC_API_KEY` | reading sale slide decks | Your own key from console.anthropic.com |
| `INBOX_ENGINE_URL`, `INBOX_ENGINE_TOKEN` | finding listserv sale emails | Ask TigerApps |
| `CRON_SECRET` | `npm run import` | Make your own: `openssl rand -hex 24` |
| `CRON_SECRET` | `bun run import` | Make your own: `openssl rand -hex 24` |
| `SENDGRID_API_KEY`, `MAIL_FROM` | sending seller invites | Ask Raj |
| `INVITE_TEST_RECIPIENT` | sending seller invites | Your own `netid@princeton.edu`; every test invite goes here |

Only the first four rows are needed to run the app. Leave `INVITES_TO_SELLERS` unset: it sends invites to real sellers.
Only the first four rows are needed to run the app. S3 uses the AWS SDK default credential chain: your local AWS profile or the EC2 instance role. No R2 keys are needed. Leave `INVITES_TO_SELLERS` unset: it sends invites to real sellers.

## Commands

- `npm test`: unit tests.
- `npm run lint`: lint.
- `npm run e2e`, `npm run e2e:import`, `npm run e2e:map`: browser tests. Run them with `npm run dev` going in another terminal.
- `npm run migrate -- --status`: lists the database migrations that have been applied.
- `npm run migrate`: applies new files from `db/migrations/`.
- `npm run seed -- --owner <netid>`: adds the demo marketplace.
- `npm run seed:clear`: removes the demo marketplace.
- `bun run test`: unit tests.
- `bun run lint`: lint.
- `bun run e2e`, `bun run e2e:import`, `bun run e2e:map`: browser tests. Run them with `bun run dev` going in another terminal.
- `bun run migrate -- --status`: lists the database migrations that have been applied.
- `bun run migrate`: applies new files from `db/migrations/`.
- `bun run seed -- --owner <netid>`: adds the demo marketplace.
- `bun run seed:clear`: removes the demo marketplace.

## The shared database

Everyone works on the same dev database and photo bucket, so:

- Tell the team before you run `npm run migrate`, `npm run seed` or `npm run seed:clear`. They change everyone's data.
- Tell the team before you run `bun run migrate`, `bun run seed` or `bun run seed:clear`. They change everyone's data.
- The e2e tests create `e2e-*` users and delete them when they finish.

## Hosting
## Hosted MVP (AWS)

- Princeton CAS only accepts `*.tigerapps.org` addresses, so the hosted site lives on one.
The deployment targets https://retail.tigerapps.org. Pushes to `main` run CI and
then deploy to the dedicated EC2 instance through GitHub OIDC and AWS Systems Manager.
See [AWS hosting](deploy/README.md) for SSH, secrets, release management, and
shared database migration details.

Princeton CAS requires the custom `*.tigerapps.org` hostname for sign-in.

## Where to work

`main` is the shared source of truth and the branch deployed to AWS. Start new
work from an up-to-date `main` and merge changes through a pull request.

- `app/(site)/`: the live marketplace pages, actions, and shared UI styles.
- `components/campus-map/`: the live map and its filtering/rendering logic.
- `lib/`: authentication, database access, storage, messaging, and imports.
- `db/migrations/`: database history; keep applied migrations intact.
- `deploy/` and `.github/workflows/`: AWS release configuration.
- `fixtures/seed/`: development seed data and illustrations, used by `bun run seed`.
- `app/dev/map-demo/` and `fixtures/demoMarketplace.ts`: the development-only
map stress test, exercised by `bun run e2e:map`. It returns 404 in production.

The standalone HTML prototype has been retired. Update the Next.js app directly;
its shared marketplace styles live in `app/(site)/marketplace.css`.
15 changes: 15 additions & 0 deletions app/(site)/_components/AutoRefresh.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use client";
import { useRouter } from "next/navigation";
import { useEffect } from "react";

/* Re-renders the page from the server every few seconds while it's on screen.
For work that finishes in the background (reading a deck, searching the
listserv): the page renders this only while something is still going. */
export function AutoRefresh({ seconds = 4 }: { seconds?: number }) {
const router = useRouter();
useEffect(() => {
const t = setInterval(() => router.refresh(), seconds * 1000);
return () => clearInterval(t);
}, [router, seconds]);
return null;
}
2 changes: 1 addition & 1 deletion app/(site)/_components/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Icons copied from the wireframe (wireframe/index.html, const I). Static,
/* Marketplace icons. Static,
trusted SVG strings, so they render through innerHTML unchanged. */
const SVG = {
search: '<svg width="17" height="17" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round"><circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/></svg>',
Expand Down
8 changes: 6 additions & 2 deletions app/(site)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
a Server Action is a public POST endpoint, so the page that rendered the
button proves nothing about who is calling it. */
import { redirect } from "next/navigation";
import { after } from "next/server";
import { revalidatePath } from "next/cache";
import { safeNext } from "@/lib/cas";
import { getViewer, requireUser } from "@/lib/auth";
import * as data from "@/lib/data";
import { claim, isAdmin, publishOwn, takeDown } from "@/lib/importer/admin";
import { importOwnDeck, isResidentialBuilding } from "@/lib/importer/importer";
import { isResidentialBuilding, startOwnDeck } from "@/lib/importer/importer";
import { deckIdsIn, linksIn } from "@/lib/importer/rules";
import { isBlankItem, parseContact, parseDollars, parseItem, parseProfile, type ContactChoice, type ItemDraft, type ItemInput } from "@/lib/rules";
import type { CardListing } from "./_components/ListingCard";
Expand Down Expand Up @@ -213,8 +214,11 @@ export async function importOwnDeckAction(_: DeckState, f: FormData): Promise<De
const text = String(f.get("url") ?? "").trim();
const [deckId] = deckIdsIn([text, ...linksIn(text)]);
if (!deckId) return { error: "That isn't a Google Slides link. It looks like docs.google.com/presentation/d/…" };
const r = await importOwnDeck(deckId, user);
const r = await startOwnDeck(deckId, user);
if ("error" in r) return { error: r.error };
// reading takes minutes, longer than Cloudflare keeps a request open: the sale
// page says "reading" and refreshes itself until it's done
if (r.read) after(r.read);
revalidatePath("/", "layout");
redirect(`/welcome/${r.sourceId}`);
}
Expand Down
26 changes: 15 additions & 11 deletions app/(site)/admin/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
the admin list itself: a Server Action is a public endpoint. */
import { notFound, redirect } from "next/navigation";
import { revalidatePath } from "next/cache";
import { after } from "next/server";
import { requireUser } from "@/lib/auth";
import { approve, isAdmin, reject, sendInvite } from "@/lib/importer/admin";
import { importDeck, runListservImport } from "@/lib/importer/importer";
import { finishListservRun, readDeck, startDeck, startListservRun } from "@/lib/importer/importer";
import { deckIdsIn } from "@/lib/importer/rules";

async function requireAdmin() {
Expand All @@ -16,35 +17,38 @@ async function requireAdmin() {

export type ImportState = { message?: string; error?: string } | undefined;

/* Reading decks takes minutes, longer than Cloudflare keeps a request open, so
these start the work, answer at once, and finish it after responding. The
imports page shows what's still going and refreshes itself until it's done. */

/* paste a deck link (works without InboxEngine) */
export async function importLinkAction(_: ImportState, f: FormData): Promise<ImportState> {
await requireAdmin();
const [deckId] = deckIdsIn([String(f.get("url") ?? "").trim()]);
if (!deckId) return { error: "That isn't a Google Slides link." };
const r = await importDeck(deckId, {
const email = {
messageId: null,
subject: String(f.get("subject") ?? "").trim(),
text: "",
listserv: null,
authorName: String(f.get("authorName") ?? "").trim() || null,
authorEmail: String(f.get("authorEmail") ?? "").trim() || null,
sentAt: null,
});
};
const started = await startDeck(deckId, email);
if ("skipped" in started) return { message: `Already imported: ${started.skipped.detail}` };
after(() => readDeck(started.sourceId, deckId, email));
revalidatePath("/admin/imports");
return r.status === "failed"
? { error: `Couldn't import that deck: ${r.detail}` }
: { message: `${r.status === "skipped" ? "Already imported" : "Imported"}: ${r.detail}` };
return { message: "Reading the deck. Its items show up below when it's done; a big deck takes a few minutes." };
}

export async function runNowAction(): Promise<ImportState> {
await requireAdmin();
try {
const r = await runListservImport();
const run = await startListservRun();
after(() => finishListservRun(run));
revalidatePath("/admin/imports");
const created = [...r.decks, ...r.emailSales].reduce((n, d) => n + d.created, 0);
return {
message: `Checked ${r.emails} sale emails since ${new Date(r.since).toLocaleDateString()}: ${r.decks.length} decks, ${r.emailSales.length} sales written in the email, ${created} items for review.`,
};
return { message: "Searching the listserv. The result shows above the queue when it's done." };
} catch (e) {
return { error: e instanceof Error ? e.message : String(e) };
}
Expand Down
2 changes: 1 addition & 1 deletion app/(site)/admin/imports/ImportForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function ImportForms({ engineConfigured }: { engineConfigured: boolean })
<h3 style={{ margin: 0 }}>search the listserv</h3>
<p className="sub" style={{ margin: 0 }}>
sale emails across the residential listservs: slides links, and sales written in the email with photos. the first run covers the last 30 days; after that each run picks up where the
last one ended. once deployed this also runs on its own three times a day.
last one ended. it doesn&rsquo;t run on its own yet. a search takes a few minutes; the result shows above the queue.
</p>
<button className="btn btn-outline" disabled={runPending}>
{runPending ? "searching…" : "run now"}
Expand Down
13 changes: 11 additions & 2 deletions app/(site)/admin/imports/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { notFound } from "next/navigation";
import { Suspense } from "react";
import { requireUser } from "@/lib/auth";
import { isAdmin, recentRuns, reviewQueue } from "@/lib/importer/admin";
import { olderThan, STALE_PENDING_MS, STALE_RUN_MS } from "@/lib/importer/importer";
import { netidFromEmail } from "@/lib/importer/rules";
import { formatCents } from "@/lib/rules";
import { AutoRefresh } from "../../_components/AutoRefresh";
import { reviewAction } from "../actions";
import { ImportForms, InviteButton } from "./ImportForms";

Expand All @@ -30,8 +32,13 @@ async function Imports() {
const [queue, runs] = await Promise.all([reviewQueue(), recentRuns()]);
const engineConfigured = !!(process.env.INBOX_ENGINE_URL && process.env.INBOX_ENGINE_TOKEN);
const waiting = queue.reduce((n, q) => n + q.items.length, 0);
// reads and searches finish in the background; keep the page current until they're done
const lastRun = runs[0];
const runGoing = !!lastRun && !lastRun.finished_at && !olderThan(lastRun.started_at as string, STALE_RUN_MS);
const reading = queue.some(({ source }) => source.status === "pending" && !olderThan(source.createdAt, STALE_PENDING_MS));
return (
<>
{(runGoing || reading) && <AutoRefresh />}
<h1 className="sec-title">listserv imports</h1>
<p className="sub" style={{ marginTop: 4 }}>
items from sale decks, and from sales written in an email with photos, wait here until you approve them. approving one publishes it with the
Expand All @@ -45,7 +52,7 @@ async function Imports() {
{runs.length > 0 && (
<p className="sub">
last run {new Date(runs[0].started_at as string).toLocaleString("en-US", { timeZone: "America/New_York" }).toLowerCase()}:{" "}
{String(runs[0].summary ?? (runs[0].finished_at ? "" : "still running"))}
{String(lastRun.summary ?? (lastRun.finished_at ? "" : runGoing ? "still running…" : "cut off before it finished (the server restarted); run it again"))}
</p>
)}
<h2 className="sec-title" style={{ fontSize: 20, margin: "18px 0 10px" }}>
Expand All @@ -70,7 +77,9 @@ async function Imports() {
)}
</span>
</div>
<span className={`ostat ${source.status === "done" ? "accepted" : ""}`}>{source.status}</span>
<span className={`ostat ${source.status === "done" ? "accepted" : ""}`}>
{source.status === "pending" ? (!olderThan(source.createdAt, STALE_PENDING_MS) ? "reading…" : "cut off") : source.status}
</span>
</div>
{source.error && <p className={source.status === "failed" ? "err" : "sub"}>{source.status === "failed" ? source.error : `skipped: ${source.error}`}</p>}
<div style={{ display: "flex", gap: 10, alignItems: "center", flexWrap: "wrap", marginTop: 6 }}>
Expand Down
4 changes: 2 additions & 2 deletions app/(site)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Suspense } from "react";
import { getViewer } from "@/lib/auth";
import { unreadCount } from "@/lib/data";
import { Nav } from "./_components/Nav";
import "./wireframe.css";
import "./marketplace.css";
import "./site.css";

/* `.wf` scopes the wireframe's element styles (see wireframe.css). The nav
/* `.wf` scopes the marketplace element styles (see marketplace.css). The nav
and every page wear it; the map page doesn't, so the ArcGIS widgets keep
their own look. */
export default function SiteLayout({ children }: { children: React.ReactNode }) {
Expand Down
Loading
Loading