Compare commits
95 Commits
67f6fc3aba
...
6e36704bb1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6e36704bb1 | ||
|
|
7d72ee271f | ||
|
|
fbc42f11fd | ||
|
|
9d0beed02f | ||
|
|
89e637843a | ||
|
|
a1c293028a | ||
|
|
765bdf9f9e | ||
|
|
48d29d4a6b | ||
|
|
90dcb47c25 | ||
|
|
35f46c3e34 | ||
|
|
e0f6b7e741 | ||
|
|
31b98f6f1e | ||
|
|
df95867465 | ||
|
|
ec24d404c5 | ||
|
|
618def6174 | ||
|
|
bbfe2d8097 | ||
|
|
051dea4d0e | ||
|
|
939a13c0e8 | ||
|
|
ec00942620 | ||
|
|
6fcabc89d7 | ||
|
|
d4e5d54de2 | ||
|
|
829a7e457a | ||
|
|
05b0412534 | ||
|
|
a671bb853c | ||
|
|
d779959e54 | ||
|
|
9e14775f08 | ||
|
|
06b171b0d4 | ||
|
|
1f24f5539c | ||
|
|
7da4200e72 | ||
|
|
1a0afd8c6e | ||
|
|
cdb18cc3d1 | ||
|
|
e16039142e | ||
|
|
1a58b3db1a | ||
|
|
eb19cb11a1 | ||
|
|
2f59b87e4f | ||
|
|
78992a493a | ||
|
|
62ab27a05a | ||
|
|
030db533e1 | ||
|
|
7824b00ff4 | ||
|
|
46a78c3a74 | ||
|
|
fe630e0e2d | ||
|
|
7c86e42413 | ||
|
|
0e104e0b6f | ||
|
|
bdfd99874a | ||
|
|
289903c8bd | ||
|
|
6e5f607425 | ||
|
|
ff355ee10e | ||
|
|
903ec2401f | ||
|
|
a6284e5c66 | ||
|
|
5b642c3d50 | ||
|
|
3d8aab46f1 | ||
|
|
3bc1cc14c7 | ||
|
|
5bdb65181d | ||
|
|
e706913a57 | ||
|
|
6487f4b209 | ||
|
|
57ec28edad | ||
|
|
d1f29a149a | ||
|
|
b1e6eb81eb | ||
|
|
497145b983 | ||
|
|
88548cbea3 | ||
|
|
95055e0dae | ||
|
|
437bed2326 | ||
|
|
14a81cd6ec | ||
|
|
19ef364c71 | ||
|
|
895be93678 | ||
|
|
3ea36296b9 | ||
|
|
53a1e62614 | ||
|
|
dff18b17f7 | ||
|
|
d0058b46ed | ||
|
|
11ab0943f6 | ||
|
|
e37f3a5874 | ||
|
|
26ff8ed111 | ||
|
|
70a9752d73 | ||
|
|
6475d5c418 | ||
|
|
432470083c | ||
|
|
0c2b2d1f96 | ||
|
|
cedd188328 | ||
|
|
75c8829c3f | ||
|
|
08829df54d | ||
|
|
34bd267c32 | ||
|
|
a0a2c5f06a | ||
|
|
f9bffabf05 | ||
|
|
64668b047e | ||
|
|
2b07c12c18 | ||
|
|
ddae34c8f5 | ||
|
|
4874491b18 | ||
|
|
c29410fd4e | ||
|
|
b867c45114 | ||
|
|
16156111a6 | ||
|
|
2e7b545a1b | ||
|
|
dd48db5eea | ||
|
|
0222da79e0 | ||
|
|
6ef0e50081 | ||
|
|
0c35531b87 | ||
|
|
305b35f3a8 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -62,3 +62,4 @@ build-output.txt
|
||||
# Private keys and secrets
|
||||
private/
|
||||
public/build-id.json
|
||||
.remember/
|
||||
|
||||
399
docs/superpowers/plans/2026-04-28-pr1-jury-preferences-filter.md
Normal file
399
docs/superpowers/plans/2026-04-28-pr1-jury-preferences-filter.md
Normal file
@@ -0,0 +1,399 @@
|
||||
# PR 1 — Jury Preferences Filter (§E)
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Filter the juror "Confirm Your Evaluation Preferences" banner so it only shows jury group memberships whose linked rounds include at least one review-type round (INTAKE/FILTERING/EVALUATION/SUBMISSION/MENTORING). Memberships in groups whose only rounds are LIVE_FINAL or DELIBERATION must be hidden — those ceremonies don't use cap+category preferences.
|
||||
|
||||
**Architecture:** Single-procedure change. `getOnboardingContext` in `src/server/routers/user.ts` adds a Prisma `juryGroup.rounds: { some: { roundType: { in: [...] } } }` filter to the `juryGroupMember.findMany` query. No schema migration. No frontend change (the banner consumes the same return shape).
|
||||
|
||||
**Tech Stack:** Prisma 6, tRPC 11, Vitest 4. Tests use `prisma` directly + `createCaller(userRouter, user)` from `tests/setup.ts`.
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-04-28-mentor-round-readiness-design.md` §E.
|
||||
|
||||
---
|
||||
|
||||
## File map
|
||||
|
||||
| File | Action | Responsibility |
|
||||
|------|--------|----------------|
|
||||
| `src/server/routers/user.ts` (`getOnboardingContext`, lines 1395-1422) | Modify | Add `juryGroup.rounds.some` filter to membership query |
|
||||
| `tests/unit/jury-preferences-filter.test.ts` | Create | Three test cases covering the filter behavior |
|
||||
|
||||
No new files beyond the test. No schema changes. No client change.
|
||||
|
||||
---
|
||||
|
||||
## Task 1: Orient on the current implementation
|
||||
|
||||
**Files:**
|
||||
- Read: `src/server/routers/user.ts:1395-1422`
|
||||
- Read: `src/components/jury/preferences-banner.tsx:17-62`
|
||||
- Read: `prisma/schema.prisma` (lines 2249-2280 for `JuryGroup`, lines 2149-2200 for `Round`)
|
||||
|
||||
- [ ] **Step 1: Read the current procedure**
|
||||
|
||||
```bash
|
||||
sed -n '1395,1425p' /Users/matt/Repos/MOPC/src/server/routers/user.ts
|
||||
```
|
||||
|
||||
Expected: see the `getOnboardingContext: protectedProcedure.query(...)` definition that calls `prisma.juryGroupMember.findMany({ where: { userId: ctx.user.id }, include: { juryGroup: { select: ... } } })`.
|
||||
|
||||
- [ ] **Step 2: Confirm the JuryGroup ↔ Round relation field**
|
||||
|
||||
```bash
|
||||
sed -n '2249,2280p' /Users/matt/Repos/MOPC/prisma/schema.prisma
|
||||
```
|
||||
|
||||
Expected: see `model JuryGroup { ... rounds Round[] ... }`. The relation field name is **`rounds`** (plural). This is the field name we'll use in the Prisma `where` filter.
|
||||
|
||||
- [ ] **Step 3: Inspect the consumer to confirm return shape stays identical**
|
||||
|
||||
```bash
|
||||
sed -n '17,62p' /Users/matt/Repos/MOPC/src/components/jury/preferences-banner.tsx
|
||||
```
|
||||
|
||||
Expected: see that the banner reads `(ctx?.memberships ?? []).filter(m => m.selfServiceCap === null)`. We are only narrowing the rows returned — the row shape is unchanged — so the banner needs no edit.
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Write the failing tests
|
||||
|
||||
**Files:**
|
||||
- Create: `tests/unit/jury-preferences-filter.test.ts`
|
||||
|
||||
- [ ] **Step 1: Create the test file**
|
||||
|
||||
Write the file at `tests/unit/jury-preferences-filter.test.ts`:
|
||||
|
||||
```ts
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
||||
import { prisma, createCaller } from '../setup'
|
||||
import {
|
||||
createTestUser, createTestProgram, createTestCompetition, createTestRound,
|
||||
cleanupTestData, uid,
|
||||
} from '../helpers'
|
||||
import { userRouter } from '../../src/server/routers/user'
|
||||
|
||||
describe('user.getOnboardingContext — preferences filter excludes LIVE_FINAL/DELIBERATION-only groups', () => {
|
||||
let programId: string
|
||||
let competitionId: string
|
||||
let juror: { id: string; email: string; role: 'JURY_MEMBER' }
|
||||
let observerOnlyGroupId: string
|
||||
let reviewGroupId: string
|
||||
let mixedGroupId: string
|
||||
const userIds: string[] = []
|
||||
|
||||
beforeAll(async () => {
|
||||
const program = await createTestProgram({ name: `prefs-filter-${uid()}` })
|
||||
programId = program.id
|
||||
const competition = await createTestCompetition(programId)
|
||||
competitionId = competition.id
|
||||
|
||||
const reviewRound = await createTestRound(competitionId, {
|
||||
name: 'Review Round', slug: `review-${uid()}`, roundType: 'EVALUATION', sortOrder: 0,
|
||||
})
|
||||
const liveFinalRound = await createTestRound(competitionId, {
|
||||
name: 'Final Round', slug: `final-${uid()}`, roundType: 'LIVE_FINAL', sortOrder: 1,
|
||||
})
|
||||
const deliberationRound = await createTestRound(competitionId, {
|
||||
name: 'Delib Round', slug: `delib-${uid()}`, roundType: 'DELIBERATION', sortOrder: 2,
|
||||
})
|
||||
|
||||
const reviewOnlyGroup = await prisma.juryGroup.create({
|
||||
data: {
|
||||
id: uid('jg-rev'), competitionId, name: 'Review Only Group',
|
||||
slug: uid('rev'), defaultMaxAssignments: 30,
|
||||
},
|
||||
})
|
||||
reviewGroupId = reviewOnlyGroup.id
|
||||
const liveFinalOnlyGroup = await prisma.juryGroup.create({
|
||||
data: {
|
||||
id: uid('jg-fin'), competitionId, name: 'Finals Only Group',
|
||||
slug: uid('fin'), defaultMaxAssignments: 10,
|
||||
},
|
||||
})
|
||||
observerOnlyGroupId = liveFinalOnlyGroup.id
|
||||
const mixedGroup = await prisma.juryGroup.create({
|
||||
data: {
|
||||
id: uid('jg-mix'), competitionId, name: 'Mixed Group',
|
||||
slug: uid('mix'), defaultMaxAssignments: 20,
|
||||
},
|
||||
})
|
||||
mixedGroupId = mixedGroup.id
|
||||
|
||||
await prisma.round.update({ where: { id: reviewRound.id }, data: { juryGroupId: reviewOnlyGroup.id } })
|
||||
await prisma.round.update({ where: { id: liveFinalRound.id }, data: { juryGroupId: liveFinalOnlyGroup.id } })
|
||||
const mixedReview = await createTestRound(competitionId, {
|
||||
name: 'Mixed Review', slug: `mixed-rev-${uid()}`, roundType: 'EVALUATION', sortOrder: 3,
|
||||
})
|
||||
const mixedFinal = await createTestRound(competitionId, {
|
||||
name: 'Mixed Final', slug: `mixed-fin-${uid()}`, roundType: 'LIVE_FINAL', sortOrder: 4,
|
||||
})
|
||||
await prisma.round.update({ where: { id: mixedReview.id }, data: { juryGroupId: mixedGroup.id } })
|
||||
await prisma.round.update({ where: { id: mixedFinal.id }, data: { juryGroupId: mixedGroup.id } })
|
||||
|
||||
void deliberationRound // referenced for cleanup; not attached to a group in these scenarios
|
||||
|
||||
const u = await createTestUser('JURY_MEMBER')
|
||||
userIds.push(u.id)
|
||||
juror = { id: u.id, email: u.email, role: 'JURY_MEMBER' }
|
||||
|
||||
await prisma.juryGroupMember.createMany({
|
||||
data: [
|
||||
{ id: uid('jgm-rev'), juryGroupId: reviewGroupId, userId: u.id, role: 'MEMBER' },
|
||||
{ id: uid('jgm-fin'), juryGroupId: observerOnlyGroupId, userId: u.id, role: 'MEMBER' },
|
||||
{ id: uid('jgm-mix'), juryGroupId: mixedGroupId, userId: u.id, role: 'MEMBER' },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanupTestData(programId, userIds)
|
||||
})
|
||||
|
||||
it('returns the review-only group membership', async () => {
|
||||
const caller = createCaller(userRouter, juror)
|
||||
const ctx = await caller.getOnboardingContext()
|
||||
const names = ctx.memberships.map((m) => m.juryGroupName).sort()
|
||||
expect(names).toContain('Review Only Group')
|
||||
})
|
||||
|
||||
it('omits the LIVE_FINAL-only group membership', async () => {
|
||||
const caller = createCaller(userRouter, juror)
|
||||
const ctx = await caller.getOnboardingContext()
|
||||
const names = ctx.memberships.map((m) => m.juryGroupName)
|
||||
expect(names).not.toContain('Finals Only Group')
|
||||
})
|
||||
|
||||
it('keeps the mixed group (has at least one review round)', async () => {
|
||||
const caller = createCaller(userRouter, juror)
|
||||
const ctx = await caller.getOnboardingContext()
|
||||
const names = ctx.memberships.map((m) => m.juryGroupName)
|
||||
expect(names).toContain('Mixed Group')
|
||||
})
|
||||
|
||||
it('returns hasSelfServiceOptions=true when at least one membership remains', async () => {
|
||||
const caller = createCaller(userRouter, juror)
|
||||
const ctx = await caller.getOnboardingContext()
|
||||
expect(ctx.hasSelfServiceOptions).toBe(true)
|
||||
expect(ctx.memberships.length).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('user.getOnboardingContext — juror with only LIVE_FINAL membership', () => {
|
||||
let programId: string
|
||||
let juror: { id: string; email: string; role: 'JURY_MEMBER' }
|
||||
const userIds: string[] = []
|
||||
|
||||
beforeAll(async () => {
|
||||
const program = await createTestProgram({ name: `prefs-only-fin-${uid()}` })
|
||||
programId = program.id
|
||||
const competition = await createTestCompetition(programId)
|
||||
const liveFinalRound = await createTestRound(competition.id, {
|
||||
name: 'Solo Final', slug: `solo-fin-${uid()}`, roundType: 'LIVE_FINAL', sortOrder: 0,
|
||||
})
|
||||
const liveFinalOnlyGroup = await prisma.juryGroup.create({
|
||||
data: {
|
||||
id: uid('jg-only-fin'), competitionId: competition.id, name: 'Solo Finals Group',
|
||||
slug: uid('solo-fin'), defaultMaxAssignments: 10,
|
||||
},
|
||||
})
|
||||
await prisma.round.update({ where: { id: liveFinalRound.id }, data: { juryGroupId: liveFinalOnlyGroup.id } })
|
||||
|
||||
const u = await createTestUser('JURY_MEMBER')
|
||||
userIds.push(u.id)
|
||||
juror = { id: u.id, email: u.email, role: 'JURY_MEMBER' }
|
||||
await prisma.juryGroupMember.create({
|
||||
data: { id: uid('jgm-only-fin'), juryGroupId: liveFinalOnlyGroup.id, userId: u.id, role: 'MEMBER' },
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanupTestData(programId, userIds)
|
||||
})
|
||||
|
||||
it('returns no memberships and hasSelfServiceOptions=false', async () => {
|
||||
const caller = createCaller(userRouter, juror)
|
||||
const ctx = await caller.getOnboardingContext()
|
||||
expect(ctx.memberships).toEqual([])
|
||||
expect(ctx.hasSelfServiceOptions).toBe(false)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run the new tests and confirm they FAIL**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && npx vitest run tests/unit/jury-preferences-filter.test.ts 2>&1 | tail -30
|
||||
```
|
||||
|
||||
Expected: at least one of these failures:
|
||||
- "omits the LIVE_FINAL-only group membership" → `expected [...] not to contain 'Finals Only Group'` (today the procedure returns ALL memberships, so it WILL contain that name).
|
||||
- "returns no memberships and hasSelfServiceOptions=false" → `expected [{ ... 'Solo Finals Group' ... }] to equal []` (today returns the lone Finals membership).
|
||||
|
||||
If all four tests pass with no code change, STOP — that means the filter is already in place or the test fixtures aren't exercising the procedure correctly. Re-read Task 1 outputs.
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Apply the Prisma filter
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/server/routers/user.ts` (the `findMany` call inside `getOnboardingContext`)
|
||||
|
||||
- [ ] **Step 1: Read the current procedure to anchor the edit**
|
||||
|
||||
```bash
|
||||
sed -n '1397,1410p' /Users/matt/Repos/MOPC/src/server/routers/user.ts
|
||||
```
|
||||
|
||||
Expected: lines look like
|
||||
|
||||
```ts
|
||||
getOnboardingContext: protectedProcedure.query(async ({ ctx }) => {
|
||||
const memberships = await ctx.prisma.juryGroupMember.findMany({
|
||||
where: { userId: ctx.user.id },
|
||||
include: {
|
||||
juryGroup: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
defaultMaxAssignments: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Add the round-type filter to the `where` clause**
|
||||
|
||||
Edit `src/server/routers/user.ts`. Replace the `findMany` call's `where` clause:
|
||||
|
||||
```ts
|
||||
// before
|
||||
where: { userId: ctx.user.id },
|
||||
|
||||
// after
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
juryGroup: {
|
||||
rounds: {
|
||||
some: {
|
||||
roundType: {
|
||||
in: ['INTAKE', 'FILTERING', 'EVALUATION', 'SUBMISSION', 'MENTORING'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
```
|
||||
|
||||
(The `include` block stays unchanged. The `return` block stays unchanged.)
|
||||
|
||||
- [ ] **Step 3: Re-run the tests and confirm they all PASS**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && npx vitest run tests/unit/jury-preferences-filter.test.ts 2>&1 | tail -30
|
||||
```
|
||||
|
||||
Expected: 5 passing, 0 failing across the two `describe` blocks.
|
||||
|
||||
If any test fails:
|
||||
- Re-read the procedure: did the edit save? `sed -n '1397,1415p' src/server/routers/user.ts`
|
||||
- Did the relation field name change? Re-confirm via `grep "rounds " prisma/schema.prisma`
|
||||
- Did the test cleanup run from a previous failed test leave stale data? Try `npx vitest run -t 'returns the review-only group membership'` in isolation.
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Run the full unit suite to check for regressions
|
||||
|
||||
- [ ] **Step 1: Run all unit tests**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && npx vitest run tests/unit 2>&1 | tail -20
|
||||
```
|
||||
|
||||
Expected: all unit tests pass. The new file should appear in the output as `tests/unit/jury-preferences-filter.test.ts ... ✓`. No previously-passing test should now fail.
|
||||
|
||||
If any other test fails: read the failure. The most likely cause is that the Prisma filter unintentionally hides memberships from a test fixture that happens to use a jury group with no attached rounds. If so, the test fixture (not our change) is the problem — flag it and fix the fixture to attach a review-type round.
|
||||
|
||||
---
|
||||
|
||||
## Task 5: Run typecheck
|
||||
|
||||
- [ ] **Step 1: Run the project typecheck**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && npm run typecheck 2>&1 | tail -10
|
||||
```
|
||||
|
||||
Expected: `tsc --noEmit` exits with code 0, no output.
|
||||
|
||||
---
|
||||
|
||||
## Task 6: Commit
|
||||
|
||||
- [ ] **Step 1: Stage the changes**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && git add src/server/routers/user.ts tests/unit/jury-preferences-filter.test.ts
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Verify staged diff is what we expect**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && git diff --cached --stat
|
||||
```
|
||||
|
||||
Expected:
|
||||
```
|
||||
src/server/routers/user.ts | ~10 +-
|
||||
tests/unit/jury-preferences-filter.test.ts | ~140 ++++
|
||||
2 files changed, ~150 insertions(+), ~3 deletions(-)
|
||||
```
|
||||
|
||||
(Numbers approximate. If anything else is staged, unstage it: `git restore --staged <unwanted-file>`.)
|
||||
|
||||
- [ ] **Step 3: Commit**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && git commit -m "$(cat <<'EOF'
|
||||
fix: filter juror preferences banner to review-round groups
|
||||
|
||||
The "Confirm Your Evaluation Preferences" banner was including jury
|
||||
group memberships whose only rounds are LIVE_FINAL or DELIBERATION.
|
||||
Those ceremonies don't use cap+category preferences, so the sliders
|
||||
were meaningless. Filter getOnboardingContext to memberships in
|
||||
groups with at least one INTAKE/FILTERING/EVALUATION/SUBMISSION/
|
||||
MENTORING round.
|
||||
|
||||
Spec: docs/superpowers/specs/2026-04-28-mentor-round-readiness-design.md §E
|
||||
|
||||
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
||||
EOF
|
||||
)"
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Verify clean status**
|
||||
|
||||
```bash
|
||||
cd /Users/matt/Repos/MOPC && git status --short && git log -1 --oneline
|
||||
```
|
||||
|
||||
Expected: empty status, latest commit is the one just created.
|
||||
|
||||
---
|
||||
|
||||
## Acceptance criteria
|
||||
|
||||
- [ ] `npx vitest run tests/unit/jury-preferences-filter.test.ts` → 5 pass
|
||||
- [ ] `npx vitest run tests/unit` → no regressions
|
||||
- [ ] `npm run typecheck` → no errors
|
||||
- [ ] Commit message references §E of the spec
|
||||
- [ ] No frontend changes
|
||||
- [ ] No Prisma migration files changed
|
||||
|
||||
## Out of scope (verified)
|
||||
|
||||
- The `preferences-banner.tsx` component is NOT modified — the return shape from `getOnboardingContext` is unchanged, only the row count differs.
|
||||
- Existing tests are NOT modified — the change is additive.
|
||||
- Prisma schema is NOT touched.
|
||||
1088
docs/superpowers/plans/2026-04-28-pr2-mentor-workspace-files.md
Normal file
1088
docs/superpowers/plans/2026-04-28-pr2-mentor-workspace-files.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,39 @@
|
||||
# PR 3 — MENTORING Round Config Completeness (§A)
|
||||
|
||||
> **For agentic workers:** Use superpowers:executing-plans for inline execution.
|
||||
|
||||
**Goal:** Surface every `MentoringConfigSchema` field on the round Config tab; hide the empty General Settings card on MENTORING rounds; relax the "File requirements set" Launch Readiness gate when no file promotion is configured.
|
||||
|
||||
**Architecture:** UI-only changes. No schema, no API. Three files touched.
|
||||
|
||||
**Spec:** `docs/superpowers/specs/2026-04-28-mentor-round-readiness-design.md` §A.
|
||||
|
||||
## File map
|
||||
|
||||
| File | Action | Why |
|
||||
|------|--------|-----|
|
||||
| `src/components/admin/rounds/config/mentoring-config.tsx` | Modify | Add `mentoringRequestDeadlineDays` numeric input + `passThroughIfNoRequest` toggle; add help-text to Eligibility |
|
||||
| `src/app/(admin)/admin/rounds/[roundId]/page.tsx` | Modify | Hide General Settings card when `round.roundType === 'MENTORING'`; relax File-requirements readiness gate for MENTORING rounds without file promotion configured |
|
||||
|
||||
## Tasks
|
||||
|
||||
### Task 1: Add the two missing inputs to `mentoring-config.tsx`
|
||||
|
||||
- [ ] **Step 1: Patch the file** — append a new "Mentoring Request Window" card BETWEEN the existing two cards, and add help-text to Eligibility. Code in execution.
|
||||
|
||||
- [ ] **Step 2: Typecheck** — `npm run typecheck`. Expect 0 errors.
|
||||
|
||||
### Task 2: Hide General Settings card + relax readiness on MENTORING rounds
|
||||
|
||||
- [ ] **Step 1: Patch `(admin)/admin/rounds/[roundId]/page.tsx`** — wrap the General Settings card in `{!isMentoring && (...)}` and extend the file-requirements bypass condition.
|
||||
|
||||
- [ ] **Step 2: Typecheck + build** — confirm clean.
|
||||
|
||||
### Task 3: Smoke + commit
|
||||
|
||||
- [ ] **Step 1: `npm run build`** — confirm clean.
|
||||
- [ ] **Step 2: Commit** — message references §A.
|
||||
|
||||
## Out of scope
|
||||
|
||||
Form unit tests (heavy render setup; existing config-save mutation already verified by other PRs). Manual smoke covers the UI work.
|
||||
269
docs/superpowers/plans/2026-04-28-pr4-visa-tracking.md
Normal file
269
docs/superpowers/plans/2026-04-28-pr4-visa-tracking.md
Normal file
@@ -0,0 +1,269 @@
|
||||
# PR 4: Visa Tracking Implementation Plan
|
||||
|
||||
> **For agentic workers:** Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Track the visa-application lifecycle for grand-finale attendees who need a visa, without ever holding sensitive documents (passport scans, visa letters). Documents continue to flow over email; the platform records process metadata only.
|
||||
|
||||
**Architecture:** A new `VisaApplication` model 1:1 with `AttendingMember`, auto-created when `needsVisa=true` flips on. Compact 5-stage status enum. A `Program.visaStatusVisibleToMembers` toggle gates whether teams see their own status. Auto-create / auto-cleanup runs in the same writes as `confirm` / `adminConfirm` / `editAttendees` so the lifecycle stays in sync.
|
||||
|
||||
**Tech Stack:** Prisma 6 (additive migration), tRPC (router additions to `logistics` + `applicant`), Vitest 4 for TDD, shadcn/ui for the admin tab + member badge.
|
||||
|
||||
---
|
||||
|
||||
## Task 1: Schema migration (additive)
|
||||
|
||||
**Files:**
|
||||
- Modify: `prisma/schema.prisma`
|
||||
- Create: `prisma/migrations/<timestamp>_add_visa_tracking/migration.sql`
|
||||
|
||||
- [ ] **Step 1: Add the enum + model + program toggle**
|
||||
|
||||
```prisma
|
||||
enum VisaStatus {
|
||||
NOT_NEEDED
|
||||
REQUESTED
|
||||
INVITATION_SENT
|
||||
APPOINTMENT_BOOKED
|
||||
GRANTED
|
||||
DENIED
|
||||
}
|
||||
|
||||
model VisaApplication {
|
||||
id String @id @default(cuid())
|
||||
attendingMemberId String @unique
|
||||
status VisaStatus @default(REQUESTED)
|
||||
nationality String? // self-declared, optional
|
||||
invitationSentAt DateTime?
|
||||
appointmentAt DateTime?
|
||||
decisionAt DateTime? // GRANTED or DENIED date
|
||||
notes String? @db.Text
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
attendingMember AttendingMember @relation(fields: [attendingMemberId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([status])
|
||||
}
|
||||
```
|
||||
|
||||
Add the back-reference on `AttendingMember`:
|
||||
|
||||
```prisma
|
||||
visaApplication VisaApplication?
|
||||
```
|
||||
|
||||
Add to `Program`:
|
||||
|
||||
```prisma
|
||||
visaStatusVisibleToMembers Boolean @default(true)
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Generate migration with `--create-only`** and inspect the SQL for any non-additive operations (DROP COLUMN / ALTER COLUMN / RENAME). Should be only `CREATE TYPE`, `CREATE TABLE`, `ALTER TABLE Program ADD COLUMN`, foreign keys.
|
||||
|
||||
Run: `npx prisma migrate dev --name add_visa_tracking --create-only`
|
||||
Then: read migration SQL, verify it's safe.
|
||||
|
||||
- [ ] **Step 3: Apply migration + regenerate client**
|
||||
|
||||
Run: `npx prisma migrate dev` (apply pending) and `npx prisma generate`.
|
||||
|
||||
- [ ] **Step 4: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Auto-create / cleanup VisaApplication on attendee writes (TDD)
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/server/routers/finalist.ts` (`confirm`, `adminConfirm`, `editAttendees`)
|
||||
- Create: `tests/unit/visa-application-lifecycle.test.ts`
|
||||
|
||||
- [ ] **Step 1: Write failing tests**
|
||||
|
||||
```ts
|
||||
describe('VisaApplication lifecycle', () => {
|
||||
it('public confirm creates a VisaApplication for each needsVisa=true attendee', async () => {
|
||||
// setup: PENDING confirmation, 2 team members
|
||||
// call confirm with both attending, visaFlags { lead: false, member: true }
|
||||
// assert: 1 VisaApplication with status=REQUESTED for member
|
||||
})
|
||||
|
||||
it('adminConfirm creates a VisaApplication for each needsVisa=true attendee', async () => {
|
||||
// same as above but via adminConfirm
|
||||
})
|
||||
|
||||
it('editAttendees creates a VisaApplication when an attendee flips to needsVisa=true', async () => {
|
||||
// setup: CONFIRMED with 1 attendee (lead) needsVisa=false, no VisaApp
|
||||
// call editAttendees with same attendees but visaFlags { lead: true }
|
||||
// assert: 1 VisaApplication for lead
|
||||
})
|
||||
|
||||
it('editAttendees deletes the VisaApplication when an attendee flips to needsVisa=false', async () => {
|
||||
// setup: CONFIRMED with 1 attendee needsVisa=true + VisaApp exists
|
||||
// call editAttendees same roster but visaFlags { lead: false }
|
||||
// assert: 0 VisaApplications
|
||||
})
|
||||
|
||||
it('editAttendees preserves an existing VisaApplication when needsVisa stays true', async () => {
|
||||
// setup: VisaApp with notes='abc', status=APPOINTMENT_BOOKED
|
||||
// call editAttendees same roster + visaFlags unchanged
|
||||
// assert: VisaApp unchanged (notes still 'abc', status still APPOINTMENT_BOOKED)
|
||||
})
|
||||
|
||||
it('removing an attendee cascades the VisaApplication', async () => {
|
||||
// setup: CONFIRMED with 2 attendees, both needsVisa=true with VisaApp rows
|
||||
// call editAttendees roster of just the lead
|
||||
// assert: only 1 VisaApp left (for lead)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run tests, expect 6 failures**.
|
||||
|
||||
- [ ] **Step 3: Wire auto-create in `confirm` (public)**
|
||||
|
||||
After the existing `attendingMember.createMany`, add a follow-up createMany for any user with `visaFlags[uid] === true`:
|
||||
|
||||
```ts
|
||||
// inside the same $transaction
|
||||
ctx.prisma.visaApplication.createMany({
|
||||
data: input.attendingUserIds
|
||||
.filter((uid) => input.visaFlags[uid] === true)
|
||||
.map((uid) => /* will need attendingMemberId — use a separate post-tx pass */),
|
||||
})
|
||||
```
|
||||
|
||||
Note: `createMany` won't have the `attendingMemberId` until after the AttendingMember rows are created. Easiest: run the visa creation in a follow-up step (after the transaction commits) by re-querying the attendee rows. The transaction guarantees the attendees are committed; if the visa step fails the worst case is missing visa rows that the admin can manually re-create — but to be safer wrap both in a single Prisma transaction using `$transaction(async (tx) => {...})` callback form.
|
||||
|
||||
- [ ] **Step 4: Wire auto-create in `adminConfirm`** — same pattern.
|
||||
|
||||
- [ ] **Step 5: Wire diff-aware sync in `editAttendees`**
|
||||
|
||||
After the existing diff transaction, derive the desired `needsVisa=true` set, the existing VisaApplication set, and:
|
||||
- Create rows for new needsVisa=true attendees with no VisaApp
|
||||
- Delete rows for attendees whose needsVisa flipped to false (or whose AttendingMember was deleted — already cascaded)
|
||||
- Leave alone rows where needsVisa stays true (preserves notes / status)
|
||||
|
||||
- [ ] **Step 6: Run tests, expect green**.
|
||||
|
||||
- [ ] **Step 7: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Admin visa CRUD procedures (TDD)
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/server/routers/logistics.ts` (add 3 procedures)
|
||||
- Create: `tests/unit/visa-admin.test.ts`
|
||||
|
||||
- [ ] **Step 1: Write failing tests**
|
||||
|
||||
```ts
|
||||
describe('logistics.listVisaApplications', () => {
|
||||
it('returns rows joined with project + attendee for the program, sorted by status priority', async () => {
|
||||
// 3 visa apps: GRANTED, REQUESTED, APPOINTMENT_BOOKED
|
||||
// expect order: REQUESTED, INVITATION_SENT (none), APPOINTMENT_BOOKED, GRANTED
|
||||
})
|
||||
})
|
||||
|
||||
describe('logistics.updateVisaApplication', () => {
|
||||
it('updates status + dates + notes + nationality', async () => {
|
||||
// setup: REQUESTED app
|
||||
// update -> APPOINTMENT_BOOKED + appointmentAt + notes
|
||||
// assert: row updated, audit log VISA_UPDATE written
|
||||
})
|
||||
|
||||
it('rejects an unknown application id', async () => {
|
||||
// expect throw /not found/i
|
||||
})
|
||||
})
|
||||
|
||||
describe('logistics.setVisaVisibility', () => {
|
||||
it('flips Program.visaStatusVisibleToMembers', async () => {
|
||||
// default true -> set false -> verify
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Implement the three procedures** in `logistics.ts`.
|
||||
|
||||
- [ ] **Step 3: Run tests, expect green**.
|
||||
|
||||
- [ ] **Step 4: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Member visa query (TDD)
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/server/routers/applicant.ts`
|
||||
- Modify: `tests/unit/visa-admin.test.ts` (add a describe block) OR new file `tests/unit/visa-member-visibility.test.ts`
|
||||
|
||||
- [ ] **Step 1: Write failing tests**
|
||||
|
||||
```ts
|
||||
describe('applicant.getMyVisaApplications', () => {
|
||||
it('returns the caller-team visa apps when toggle is true', async () => {
|
||||
// setup: program toggle=true, member with VisaApp
|
||||
// assert: returns array with that app
|
||||
})
|
||||
|
||||
it('returns null when toggle is false', async () => {
|
||||
// assert: returns null
|
||||
})
|
||||
|
||||
it('returns empty array when caller has no visa apps', async () => {
|
||||
// assert: []
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Implement** — query AttendingMember rows where `userId = caller`, include VisaApplication. Return null if `program.visaStatusVisibleToMembers === false`.
|
||||
|
||||
- [ ] **Step 3: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 5: Admin Visas tab UI
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/app/(admin)/admin/logistics/page.tsx` (un-disable the Visas tab)
|
||||
- Create: `src/components/admin/logistics/visas-tab.tsx`
|
||||
- Create: `src/components/admin/logistics/visa-edit-dialog.tsx`
|
||||
|
||||
- [ ] **Step 1: Build the tab**
|
||||
|
||||
Table columns: Project · Member · Nationality · Status · Next date · Notes (truncated) · Actions. Status filter pills mirror the Confirmations tab. Header has a "Visible to teams" Switch wired to `setVisaVisibility`.
|
||||
|
||||
- [ ] **Step 2: Build the edit dialog**
|
||||
|
||||
Status dropdown, nationality input, three date pickers (invitation / appointment / decision), notes textarea. Save calls `updateVisaApplication`.
|
||||
|
||||
- [ ] **Step 3: Activate the tab** — remove the `disabled` prop and wire `<VisasTab programId={programId} />`.
|
||||
|
||||
- [ ] **Step 4: Live smoke** — confirm a finalist with one needsVisa attendee, open Visas tab, edit → verify persistence.
|
||||
|
||||
- [ ] **Step 5: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 6: Member visa surface on AttendingMembersCard
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/components/applicant/attending-members-card.tsx`
|
||||
|
||||
- [ ] **Step 1: Wire the query**
|
||||
|
||||
Call `applicant.getMyVisaApplications` alongside the existing confirmation query. If the result is null (toggle off), don't render any visa info. Otherwise, render a small status badge + the next upcoming date next to each attendee whose `needsVisa=true`.
|
||||
|
||||
- [ ] **Step 2: Live smoke** — toggle visibility off in the admin tab, confirm member can no longer see status.
|
||||
|
||||
- [ ] **Step 3: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 7: Final verification
|
||||
|
||||
- [ ] **Step 1: Full vitest** — `npx vitest run`. Expect 148 + new tests, all green.
|
||||
- [ ] **Step 2: Typecheck** — `npm run typecheck`.
|
||||
- [ ] **Step 3: Build** — `npm run build`.
|
||||
- [ ] **Step 4: Live smoke** end-to-end — confirm an attendee with visa, edit status as admin, verify member view, toggle off, verify hidden.
|
||||
142
docs/superpowers/plans/2026-04-28-pr5-settings-consolidation.md
Normal file
142
docs/superpowers/plans/2026-04-28-pr5-settings-consolidation.md
Normal file
@@ -0,0 +1,142 @@
|
||||
# PR 5: Settings Consolidation Implementation Plan
|
||||
|
||||
> **For agentic workers:** Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Consolidate per-edition logistics configuration onto `/admin/settings` so admins find every knob in one place. Remove dead Logistics tabs and move the visa-visibility toggle out of the Visas tab.
|
||||
|
||||
**Architecture:** A new "Edition" tab on `/admin/settings` reads + writes a merged view: `Program` fields (defaultAttendeeCap, visaStatusVisibleToMembers) + the LIVE_FINAL round's `configJson` (attendeeEditCutoffHours, confirmationWindowHours). One reader (`program.getEditionSettings`) + one writer (`program.updateEditionSettings`) so the UI stays simple.
|
||||
|
||||
**Tech Stack:** Prisma 6 (no migration — purely a UX layer over existing fields), tRPC, Vitest 4 for the procedure tests, shadcn/ui for the tab + sub-sections.
|
||||
|
||||
---
|
||||
|
||||
## Task 1: tRPC procedures for edition settings (TDD)
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/server/routers/program.ts`
|
||||
- Create: `tests/unit/program-edition-settings.test.ts`
|
||||
|
||||
- [ ] **Step 1: Failing tests**
|
||||
|
||||
```ts
|
||||
describe('program.getEditionSettings', () => {
|
||||
it('returns the merged view (program fields + LIVE_FINAL round config)', async () => {
|
||||
// setup: program with defaultAttendeeCap=5, visaStatusVisibleToMembers=false
|
||||
// + LIVE_FINAL round with configJson { attendeeEditCutoffHours: 24, confirmationWindowHours: 12 }
|
||||
// assert: shape { defaultAttendeeCap, visaStatusVisibleToMembers, attendeeEditCutoffHours, confirmationWindowHours }
|
||||
})
|
||||
|
||||
it('falls back to defaults when LIVE_FINAL round has no config', async () => {
|
||||
// assert: attendeeEditCutoffHours = 48, confirmationWindowHours = 24
|
||||
})
|
||||
|
||||
it('returns nulls for round-derived fields if no LIVE_FINAL round exists', async () => {
|
||||
// assert: attendeeEditCutoffHours = null, confirmationWindowHours = null
|
||||
})
|
||||
})
|
||||
|
||||
describe('program.updateEditionSettings', () => {
|
||||
it('writes program fields + round configJson + audit-logs', async () => {
|
||||
// call with partial { defaultAttendeeCap: 4, attendeeEditCutoffHours: 36 }
|
||||
// assert: program.defaultAttendeeCap=4, round.configJson.attendeeEditCutoffHours=36
|
||||
// assert: AuditLog action=PROGRAM_EDITION_SETTINGS_UPDATE
|
||||
})
|
||||
|
||||
it('preserves untouched configJson keys', async () => {
|
||||
// round.configJson initially { foo: 'bar', attendeeEditCutoffHours: 48 }
|
||||
// call with { attendeeEditCutoffHours: 24 }
|
||||
// assert: round.configJson = { foo: 'bar', attendeeEditCutoffHours: 24 }
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run failing tests**.
|
||||
|
||||
- [ ] **Step 3: Implement getEditionSettings**
|
||||
|
||||
```ts
|
||||
getEditionSettings: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const program = await ctx.prisma.program.findUniqueOrThrow({
|
||||
where: { id: input.programId },
|
||||
select: { id: true, defaultAttendeeCap: true, visaStatusVisibleToMembers: true },
|
||||
})
|
||||
const round = await ctx.prisma.round.findFirst({
|
||||
where: { competition: { programId: input.programId }, roundType: 'LIVE_FINAL' },
|
||||
orderBy: { sortOrder: 'desc' },
|
||||
select: { id: true, configJson: true },
|
||||
})
|
||||
const cfg = (round?.configJson ?? {}) as Record<string, unknown>
|
||||
return {
|
||||
programId: program.id,
|
||||
defaultAttendeeCap: program.defaultAttendeeCap,
|
||||
visaStatusVisibleToMembers: program.visaStatusVisibleToMembers,
|
||||
liveFinalRoundId: round?.id ?? null,
|
||||
attendeeEditCutoffHours: round
|
||||
? ((cfg.attendeeEditCutoffHours as number | undefined) ?? 48)
|
||||
: null,
|
||||
confirmationWindowHours: round
|
||||
? ((cfg.confirmationWindowHours as number | undefined) ?? 24)
|
||||
: null,
|
||||
}
|
||||
}),
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Implement updateEditionSettings** with merge semantics on configJson + audit log.
|
||||
|
||||
- [ ] **Step 5: Run tests, expect green**.
|
||||
|
||||
- [ ] **Step 6: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 2: Edition Settings tab UI
|
||||
|
||||
**Files:**
|
||||
- Create: `src/components/admin/settings/edition-settings-tab.tsx`
|
||||
- Modify: `src/components/settings/settings-content.tsx` (add the new tab + entry)
|
||||
|
||||
- [ ] **Step 1: Build the Edition Settings tab**
|
||||
|
||||
Three sub-sections (Card per section):
|
||||
1. **Grand-finale logistics** — `defaultAttendeeCap` (number input), `attendeeEditCutoffHours` (number input with "hours before LIVE_FINAL" hint), `confirmationWindowHours` (number input with "hours from email send" hint).
|
||||
2. **Visa** — `visaStatusVisibleToMembers` Switch + caption.
|
||||
3. **Coming soon** — placeholder for Lunch + Email Templates that PRs 6 + 7 will fill.
|
||||
|
||||
Each input fires `program.updateEditionSettings.useMutation` debounced or on-blur. Toast on success.
|
||||
|
||||
- [ ] **Step 2: Wire into `/admin/settings`** — add `<TabsTrigger value="edition">` and `<TabsContent value="edition">` in settings-content. Place before existing tabs.
|
||||
|
||||
- [ ] **Step 3: Live smoke** — open settings, verify it renders, toggle visa visibility, verify it persists in the DB and the applicant dashboard hides the surface.
|
||||
|
||||
- [ ] **Step 4: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 3: Cleanup — remove dead Logistics tabs + visibility toggle
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/app/(admin)/admin/logistics/page.tsx`
|
||||
- Modify: `src/components/admin/logistics/visas-tab.tsx`
|
||||
|
||||
- [ ] **Step 1: Remove disabled tabs**
|
||||
|
||||
Drop the `<TabsTrigger value="documents" disabled>` and `<TabsTrigger value="settings" disabled>` blocks. Also drop their unused imports (`FileText`, `Settings`).
|
||||
|
||||
- [ ] **Step 2: Replace visibility toggle with a hint**
|
||||
|
||||
In `visas-tab.tsx`, swap the inline Switch + Label for a small banner: "Configure visibility in Settings → Edition" with a Link to `/admin/settings?tab=edition`. Drop the `getVisaVisibility` query and `setVisaVisibility` mutation usage from this component (keep the procedures — used by the new settings page).
|
||||
|
||||
- [ ] **Step 3: Live smoke** — confirm Logistics tab bar shows only the active tabs, visas tab no longer has a toggle, settings page does.
|
||||
|
||||
- [ ] **Step 4: Commit**.
|
||||
|
||||
---
|
||||
|
||||
## Task 4: Final verification
|
||||
|
||||
- [ ] **Step 1: Full vitest** — `npx vitest run`. Expect 161 + new tests (~5).
|
||||
- [ ] **Step 2: Typecheck** — clean.
|
||||
- [ ] **Step 3: Build** — clean.
|
||||
- [ ] **Step 4: E2E smoke** — open `/admin/settings` → Edition tab, change knobs, verify persistence; confirm the Visas tab no longer shows the toggle and the hint links to settings.
|
||||
182
docs/superpowers/plans/2026-04-28-pr7-email-team-from-project.md
Normal file
182
docs/superpowers/plans/2026-04-28-pr7-email-team-from-project.md
Normal file
@@ -0,0 +1,182 @@
|
||||
# PR 7 — "Email Team" Modal on Project Detail Page
|
||||
|
||||
> **For agentic workers:** Use superpowers:executing-plans for inline execution.
|
||||
|
||||
**Goal:** Add an "Email Team" button to `/admin/projects/[id]` that opens a modal composing a custom email to that project's team. Same look + features as `/admin/messages` (subject, body, live HTML preview, send-to-all). Body pre-filled with `Hello [Project Title] team,\n\n` so admin can edit and add their custom content beneath.
|
||||
|
||||
**Architecture:** Adds `PROJECT_TEAM` to the existing `RecipientType` enum (5 places) + a resolver branch. New self-contained `<ProjectEmailDialog>` component reuses the existing `message.previewEmail` and `message.send` procedures. No template-system rewrite.
|
||||
|
||||
**Spec:** New ask added during PR review (2026-04-28). Not in original spec but adjacent to §B (admin views).
|
||||
|
||||
## File map
|
||||
|
||||
| File | Action | Why |
|
||||
|------|--------|-----|
|
||||
| `src/server/routers/message.ts` | Modify | Add `PROJECT_TEAM` to 4 input enums + 1 resolver case |
|
||||
| `src/components/admin/project-email-dialog.tsx` | Create | Self-contained modal with subject + body + live preview + send |
|
||||
| `src/app/(admin)/admin/projects/[id]/page.tsx` | Modify | Add "Email Team" button next to Edit button; render the dialog |
|
||||
| `tests/unit/message-recipient-project-team.test.ts` | Create | Resolver returns team members + submittedByUserId |
|
||||
|
||||
## Tasks
|
||||
|
||||
### Task 1: Backend — `PROJECT_TEAM` recipient type
|
||||
|
||||
- [ ] **Step 1: Write failing test**
|
||||
|
||||
```ts
|
||||
// tests/unit/message-recipient-project-team.test.ts
|
||||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
|
||||
import { prisma, createCaller } from '../setup'
|
||||
import {
|
||||
createTestUser, createTestProgram, createTestProject, cleanupTestData, uid,
|
||||
} from '../helpers'
|
||||
import { messageRouter } from '../../src/server/routers/message'
|
||||
|
||||
describe('message.previewRecipients — PROJECT_TEAM', () => {
|
||||
let programId: string
|
||||
let admin: { id: string; email: string; role: 'SUPER_ADMIN' }
|
||||
let projectId: string
|
||||
const userIds: string[] = []
|
||||
|
||||
beforeAll(async () => {
|
||||
const program = await createTestProgram({ name: `proj-team-${uid()}` })
|
||||
programId = program.id
|
||||
|
||||
const lead = await createTestUser('APPLICANT')
|
||||
userIds.push(lead.id)
|
||||
const project = await createTestProject(programId, { title: 'TestProj' })
|
||||
projectId = project.id
|
||||
await prisma.project.update({ where: { id: projectId }, data: { submittedByUserId: lead.id } })
|
||||
|
||||
const member1 = await createTestUser('APPLICANT')
|
||||
const member2 = await createTestUser('APPLICANT')
|
||||
userIds.push(member1.id, member2.id)
|
||||
await prisma.teamMember.createMany({
|
||||
data: [
|
||||
{ id: uid('tm'), projectId, userId: member1.id, role: 'MEMBER' },
|
||||
{ id: uid('tm'), projectId, userId: member2.id, role: 'MEMBER' },
|
||||
],
|
||||
})
|
||||
|
||||
const a = await createTestUser('SUPER_ADMIN')
|
||||
userIds.push(a.id)
|
||||
admin = { id: a.id, email: a.email, role: 'SUPER_ADMIN' }
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanupTestData(programId, userIds)
|
||||
})
|
||||
|
||||
it('counts the lead + 2 team members', async () => {
|
||||
const caller = createCaller(messageRouter, admin)
|
||||
const result = await caller.previewRecipients({
|
||||
recipientType: 'PROJECT_TEAM',
|
||||
recipientFilter: { projectId },
|
||||
})
|
||||
expect(result.totalApplicants).toBe(3)
|
||||
})
|
||||
|
||||
it('returns 0 when projectId is missing', async () => {
|
||||
const caller = createCaller(messageRouter, admin)
|
||||
const result = await caller.previewRecipients({
|
||||
recipientType: 'PROJECT_TEAM',
|
||||
recipientFilter: {},
|
||||
})
|
||||
expect(result.totalApplicants).toBe(0)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2: Run, expect FAIL** — `'PROJECT_TEAM'` not in enum.
|
||||
|
||||
- [ ] **Step 3: Patch `src/server/routers/message.ts`**
|
||||
|
||||
Replace ALL FIVE enum literal lines:
|
||||
|
||||
```ts
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'ALL']),
|
||||
```
|
||||
|
||||
with:
|
||||
|
||||
```ts
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'PROJECT_TEAM', 'ALL']),
|
||||
```
|
||||
|
||||
(Use Edit's `replace_all: true` since the line is identical at all 5 occurrences.)
|
||||
|
||||
Add a new case in `resolveRecipients` (after `'PROGRAM_TEAM'`):
|
||||
|
||||
```ts
|
||||
case 'PROJECT_TEAM': {
|
||||
const projectId = filter?.projectId as string
|
||||
if (!projectId) return []
|
||||
const [teamMembers, project] = await Promise.all([
|
||||
prisma.teamMember.findMany({
|
||||
where: { projectId },
|
||||
select: { userId: true },
|
||||
}),
|
||||
prisma.project.findUnique({
|
||||
where: { id: projectId },
|
||||
select: { submittedByUserId: true },
|
||||
}),
|
||||
])
|
||||
const ids = new Set<string>()
|
||||
for (const tm of teamMembers) ids.add(tm.userId)
|
||||
if (project?.submittedByUserId) ids.add(project.submittedByUserId)
|
||||
return [...ids]
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 4: Re-run, expect PASS.**
|
||||
|
||||
### Task 2: Build `<ProjectEmailDialog>`
|
||||
|
||||
- [ ] **Step 1: Create the component** (full code in execution)
|
||||
|
||||
Behaviour:
|
||||
- Open via `open: boolean; onClose: () => void; projectId: string; projectTitle: string` props.
|
||||
- On open, body field is pre-filled: ``Hello ${projectTitle} team,\n\n``. Cursor placed at the end.
|
||||
- Subject field default: empty (admin types).
|
||||
- Live HTML preview pane below the form, calling `message.previewEmail` with `{ subject, body }` (debounced 300ms).
|
||||
- Recipient count line: calls `message.previewRecipients` with `PROJECT_TEAM` + `{ projectId }`. Shows "Will email N team members."
|
||||
- "Send Test" button: sends to the admin only via `message.sendTest`.
|
||||
- "Send" button: calls `message.send` with `recipientType: 'PROJECT_TEAM'`, `recipientFilter: { projectId }`, `deliveryChannels: ['EMAIL']`, `linkType: 'NONE'`.
|
||||
- On success: toast + close dialog. On error: toast.
|
||||
|
||||
### Task 3: Wire the button on project detail page
|
||||
|
||||
- [ ] **Step 1: Add button next to Edit** in `src/app/(admin)/admin/projects/[id]/page.tsx`:
|
||||
|
||||
```tsx
|
||||
<Button variant="outline" onClick={() => setEmailDialogOpen(true)}>
|
||||
<Mail className="mr-2 h-4 w-4" />
|
||||
Email Team
|
||||
</Button>
|
||||
```
|
||||
|
||||
(Add `Mail` to the lucide imports. Add `useState` for `emailDialogOpen`.)
|
||||
|
||||
Render the dialog at the bottom of the page:
|
||||
|
||||
```tsx
|
||||
{project && (
|
||||
<ProjectEmailDialog
|
||||
open={emailDialogOpen}
|
||||
onClose={() => setEmailDialogOpen(false)}
|
||||
projectId={project.id}
|
||||
projectTitle={project.title}
|
||||
/>
|
||||
)}
|
||||
```
|
||||
|
||||
### Task 4: Verify + commit
|
||||
|
||||
- [ ] `npx vitest run tests/unit` → all pass.
|
||||
- [ ] `npm run typecheck` → clean.
|
||||
- [ ] `npm run build` → clean.
|
||||
- [ ] Commit with message referencing PR 7.
|
||||
|
||||
## Out of scope
|
||||
|
||||
Template picker (admins can use plain body for now; integration with template system can land later). HTML editor (plain textarea — same UX as messages page composer).
|
||||
3476
docs/superpowers/plans/2026-04-29-pr6-lunch-event.md
Normal file
3476
docs/superpowers/plans/2026-04-29-pr6-lunch-event.md
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,520 @@
|
||||
# Mentor Round Readiness — End-to-End Design
|
||||
|
||||
**Date:** 2026-04-28
|
||||
**Author:** Matt + Claude (brainstorming session)
|
||||
**Status:** Draft, awaiting review
|
||||
|
||||
## Motivation
|
||||
|
||||
R5 (Semi-Final Evaluation) is about to close. Next is R6 (Mentoring) for projects that request or are assigned a mentor, then R7 (Grand Final). The MENTORING backend exists but has gaps that block operational use:
|
||||
|
||||
- Admin Config form omits two `MentoringConfigSchema` fields (`mentoringRequestDeadlineDays`, `passThroughIfNoRequest`)
|
||||
- Round Overview shows generic stats only — no mentor-specific dashboard
|
||||
- `/admin/projects/[id]/mentor` exposes only AI suggestions; manual mentor selection is missing entirely from the UI
|
||||
- File uploads (`mentor.workspaceUploadFile`) accept client-controlled `bucket` / `objectKey` — security/consistency hole
|
||||
- Juror "Confirm Your Evaluation Preferences" banner pulls in LIVE_FINAL groups (not appropriate for a live ceremony)
|
||||
- Multi-role users (juror + mentor) land on primary role's dashboard only; no quick path for an admin to bulk-promote jurors
|
||||
- Zero tests for MENTORING round behavior
|
||||
|
||||
This spec covers all of the above plus workspace messaging/file UX polish, in one design with phased PRs.
|
||||
|
||||
## Goals
|
||||
|
||||
1. Admin can fully configure a MENTORING round from the UI (no DB-direct edits needed for any `MentoringConfigSchema` field).
|
||||
2. Admin can see at a glance: who requested mentoring, who has a mentor, who doesn't, who's mentoring whom, what the mentor pool looks like.
|
||||
3. Admin can manually assign a mentor to any project, AND auto-fill all unassigned projects in one action.
|
||||
4. Files uploaded in the mentor workspace land at `<projectName>/mentorship/<file>` in the configured bucket, with paths constructed server-side.
|
||||
5. Mentors and applicant teams see recent messages on their respective dashboards.
|
||||
6. A juror who is also a mentor can switch dashboards in one click, without seeing irrelevant LIVE_FINAL preference cards.
|
||||
7. The MENTORING round behavior (pass-through, eligibility, advancement) is covered by integration tests.
|
||||
|
||||
## Non-goals
|
||||
|
||||
- Redesigning messaging or notifications from scratch.
|
||||
- Replacing the AI mentor-matching service with a different model.
|
||||
- Building a mentor scheduling/calendar feature.
|
||||
- Bulk-promoting jurors to mentors via CSV import (per-row checkbox + bulk action is enough for this iteration).
|
||||
- Migrating any existing mentor file objects in MinIO (none exist yet — spec asserts a pre-flight check).
|
||||
|
||||
## Out-of-scope but adjacent
|
||||
|
||||
- Grand Finale (R7 LIVE_FINAL) UX — explicitly deferred per user direction (handled separately, much further build-out planned).
|
||||
- Mentor pool capacity / load-balancing algorithm changes — covered only by surfacing existing fields in the admin view.
|
||||
|
||||
---
|
||||
|
||||
## High-level architecture
|
||||
|
||||
No new top-level architecture. Extending existing patterns:
|
||||
|
||||
- **Storage path:** new helper `generateMentorObjectKey(projectTitle, fileName)` in `src/lib/minio.ts` that returns `<sanitizedProjectName>/mentorship/<timestamp>-<sanitizedFileName>` — exact same shape as `generateObjectKey()` with `roundName="mentorship"`. Server-side only.
|
||||
- **Config schema:** no Prisma migration. The two missing fields (`mentoringRequestDeadlineDays`, `passThroughIfNoRequest`) already exist in `MentoringConfigSchema` and are read by `round-engine.ts` and `applicant.ts` — only the form needs updating.
|
||||
- **Multi-role dashboards:** existing `User.roles UserRole[]` array drives everything; logic-only changes (post-login redirect priority, bulk-promote bulk action, fix CSS layering on impersonation banner).
|
||||
- **Preferences filter:** single Prisma query change in `getOnboardingContext`.
|
||||
- **Workspace dashboards:** reuse existing `MentorMessage` table; new tRPC procedures return last-N message previews.
|
||||
|
||||
## Phasing / PR plan
|
||||
|
||||
Six PRs, ordered smallest-blast-radius first:
|
||||
|
||||
| PR | Section | Risk | What ships |
|
||||
|----|---------|------|------------|
|
||||
| 1 | §E | Low | Filter `getOnboardingContext` to review-only rounds |
|
||||
| 2 | §F.1 | Low | Server-side `objectKey` enforcement + `generateMentorObjectKey` helper |
|
||||
| 3 | §A | Med | Config form completeness (2 missing inputs + General Settings cleanup + Launch Readiness gate relax) |
|
||||
| 4 | §C | Med | Manual mentor picker + bulk auto-fill + AI fallback |
|
||||
| 5 | §B | Med | Mentor-specific Round Overview + un-redirect `/admin/mentors` |
|
||||
| 6 | §D + §F.2 | Med | Multi-role redirect priority + bulk-promote + impersonation banner fix + dashboard message previews |
|
||||
| (continuous) | §G | Low | Tests added in each PR for the surface changing in that PR |
|
||||
|
||||
A standalone test PR is *not* planned — tests ride with the change they cover.
|
||||
|
||||
---
|
||||
|
||||
## §A. MENTORING round Config form
|
||||
|
||||
**Files:**
|
||||
- `src/components/admin/round-config/mentoring-config.tsx` (likely path; locate the round-type-specific config component used by `(admin)/admin/rounds/[roundId]` Config tab)
|
||||
- `src/components/admin/round-config/launch-readiness.tsx` (or similar — the component that renders the 0/3 readiness checklist)
|
||||
|
||||
**Changes:**
|
||||
|
||||
1. Add **"Mentoring Request Window"** section to the Config form:
|
||||
- Numeric input bound to `configJson.mentoringRequestDeadlineDays` — int, min 1, max 90, default 14.
|
||||
- Help text: "Number of days from round opening during which teams may request mentoring. After this window, no new requests are accepted."
|
||||
2. Add **"Pass-through behavior"** toggle bound to `configJson.passThroughIfNoRequest`:
|
||||
- Default `true` (matches schema default).
|
||||
- Off-state label: "Hold all projects in PENDING until mentor is assigned (manual gate)"
|
||||
- On-state label: "Auto-PASS projects that don't request mentoring (default)"
|
||||
3. Replace empty **"General Settings"** section header. Either:
|
||||
- Delete the empty header (preferred — fewer questions); OR
|
||||
- Move the eligibility dropdown into it (so the section has content).
|
||||
4. Relax Launch Readiness "File requirements set" gate for MENTORING rounds:
|
||||
- Required only when `configJson.filePromotionEnabled === true` AND `configJson.promotionTargetWindowId` is set (i.e., the round is configured to promote mentor-authored files into a downstream submission window).
|
||||
- Otherwise treat the readiness item as N/A and don't count it against the 0/3 (it becomes 0/2 for mentoring rounds without promotion configured).
|
||||
5. Help-text added to the existing **Eligibility** dropdown explaining each option:
|
||||
- `requested_only` — only projects that flag `mentoringRequested` participate (default).
|
||||
- `all_advancing` — every project advancing into this round gets a mentor.
|
||||
- `admin_selected` — admin manually picks which projects participate.
|
||||
|
||||
**Tests** (in PR 3): one per `MentoringConfigSchema` field — render with default config, change input, submit, assert config persisted via the existing config-save mutation.
|
||||
|
||||
---
|
||||
|
||||
## §B. Mentoring-specific admin views
|
||||
|
||||
**Files:**
|
||||
- `src/app/(admin)/admin/rounds/[roundId]/page.tsx` (Round Overview tab)
|
||||
- `src/app/(admin)/admin/rounds/[roundId]/projects-tab.tsx` (Projects tab — exact filename to confirm during impl)
|
||||
- `src/app/(admin)/admin/mentors/page.tsx` (currently a redirect stub — replace with a real list page)
|
||||
- `src/app/(admin)/admin/mentors/[id]/page.tsx` (also a stub today; replace with mentor detail)
|
||||
- New tRPC procedures on `mentor` router (admin-gated): `getRoundStats`, `getMentorPool`, `getMentorDetail`
|
||||
|
||||
**Round Overview — replace generic Round Details with a mentoring-specific stats card** when `round.roundType === 'MENTORING'`:
|
||||
|
||||
- **Top-line counts** (single row of stat cards):
|
||||
- Total projects in round
|
||||
- Requested mentoring (count + % of total)
|
||||
- Mentor assigned (count + % of total)
|
||||
- Awaiting assignment (= requested - assigned)
|
||||
- **Request window** card:
|
||||
- Deadline (computed from `windowOpenAt + mentoringRequestDeadlineDays`)
|
||||
- Time remaining (live countdown, using existing `formatCountdown` helper)
|
||||
- "Closes in N days" pill, turns amber within 48 hours, red within 12 hours
|
||||
- **Mentor pool** card:
|
||||
- Pool size (count of users with MENTOR role in the program)
|
||||
- Average load (assigned projects ÷ pool size)
|
||||
- Capacity remaining (sum of `User.maxAssignmentsOverride` minus current load, where overrides exist)
|
||||
- Link → `/admin/mentors`
|
||||
- **Workspace activity** card:
|
||||
- Total messages exchanged (sum across all assignments in round)
|
||||
- Total files uploaded
|
||||
- Total milestones completed
|
||||
- "Last activity" timestamp
|
||||
|
||||
**Round Details panel** stays at the bottom of the Overview tab when round is MENTORING (the existing panel is still useful for type/status/position/dates), but with these field-level adjustments:
|
||||
- Replace "Jury Group: —" row with "Mentor Pool: N members" (link to `/admin/mentors`).
|
||||
- Keep "Type", "Status", "Position", "Opens", "Closes" rows unchanged.
|
||||
- The new "mentoring stats card" (top-line counts, request window, mentor pool, workspace activity) renders **above** the Round Details panel, not in place of it.
|
||||
|
||||
**Projects tab — when round is MENTORING**, the per-project row shows:
|
||||
- Project title + team lead
|
||||
- "Requested mentoring" badge (yes/no)
|
||||
- "Mentor assigned" cell — mentor name + expertise overlap chip, OR "Unassigned" with inline "Assign" button → opens the manual-pick drawer (see §C)
|
||||
- "Workspace activity" small-text summary (msgs / files / milestones)
|
||||
- Bulk action bar (when ≥1 project selected): "Auto-fill mentors for selected" → calls `mentor.autoAssignBulk`
|
||||
|
||||
**`/admin/mentors` — un-redirect, replace stub with a real list page:**
|
||||
- Searchable/filterable list of all users with MENTOR role in the current edition.
|
||||
- Columns: name, email, country, expertise tags (chips), assigned-projects count, completed count, capacity remaining, last activity.
|
||||
- Row → `/admin/mentors/[id]` detail page (existing route, replace stub):
|
||||
- Mentor profile + expertise + bio
|
||||
- List of assigned projects (link to per-project workspace)
|
||||
- Per-project status (in_progress / completed / paused)
|
||||
- Recent activity feed (messages / file uploads / milestone completions across all assignments)
|
||||
- Admin actions: reassign / unassign
|
||||
|
||||
**Tests** (in PR 5): integration test for `getRoundStats` returning correct counts; render-test for round overview when round.roundType=MENTORING.
|
||||
|
||||
---
|
||||
|
||||
## §C. Manual + auto-fill mentor assignment
|
||||
|
||||
**Files:**
|
||||
- `src/app/(admin)/admin/projects/[id]/mentor/page.tsx` (rewrite)
|
||||
- `src/server/services/mentor-matching.ts` (add expertise-tag fallback)
|
||||
- `src/server/routers/mentor.ts` (`getCandidates` new procedure for manual picker; ensure `autoAssignBulk` exposes a "skip already assigned" param — confirm and document)
|
||||
|
||||
**Page rewrite — three sections, all visible at once (not tabs):**
|
||||
|
||||
1. **Project Context** card (top):
|
||||
- Project title, ocean issue, country, team size, expertise needs (project tags)
|
||||
- Round being assigned for (linked)
|
||||
- Mentoring requested? Yes/no
|
||||
2. **Currently Assigned** card:
|
||||
- If assigned: mentor name, email, country, expertise overlap chips, "Assigned by [admin], 3 days ago, method: MANUAL/AUTO", actions: Unassign | Swap
|
||||
- If unassigned: empty state with copy "No mentor assigned yet — pick one below or use AI"
|
||||
3. **Pick a mentor** card with a tab strip:
|
||||
- **Tab 1 — Manual picker** (default selected):
|
||||
- Searchable input
|
||||
- Sortable table of all MENTOR-role users in the program: name, expertise tags, country, current load, capacity, **expertise overlap with this project** (computed: count of shared tags / total project tags, displayed as a percentage chip)
|
||||
- Default sort: highest expertise overlap first
|
||||
- Per-row "Assign" button → calls `mentor.assign({ projectId, mentorId, method: 'MANUAL' })`
|
||||
- **Tab 2 — AI suggestions**:
|
||||
- Existing pane (loads `getSuggestions`).
|
||||
- **Fallback**: if AI fails (no `OPENAI_API_KEY`, network error, or returns empty) — show expertise-tag-overlap ranking as the suggestion source instead, with a banner: "AI matching unavailable — showing expertise-tag overlap instead". (The fallback ranking is the same algorithm as Tab 1's default sort, so the lists may look similar — that's fine.)
|
||||
|
||||
**Auto-fill remainder** (bulk action):
|
||||
- On round Projects tab + Round Overview, button: "Auto-fill mentors for unassigned projects".
|
||||
- Call `mentor.autoAssignBulk` with the round ID; the service filters to projects-in-round-without-MentorAssignment, scoped further by the round's `eligibility` config:
|
||||
- `requested_only` → only projects with `mentoringRequested=true`
|
||||
- `all_advancing` → every project in the round
|
||||
- `admin_selected` → button disabled (admins must pick manually for this mode)
|
||||
- Confirm the existing service already skips projects with a MentorAssignment (any method); if it doesn't, fix in the same PR.
|
||||
- Result toast: "Assigned N projects, skipped M already-assigned, K unassignable (no matching mentor)".
|
||||
|
||||
**Tests** (in PR 4):
|
||||
- `mentor.assign` round-trips with method=MANUAL
|
||||
- `mentor.autoAssignBulk` skips manually-assigned projects
|
||||
- `getCandidates` returns expected expertise-overlap ordering
|
||||
- Fallback path used when AI unavailable
|
||||
|
||||
---
|
||||
|
||||
## §D. Juror→mentor multi-role UX
|
||||
|
||||
**Files:**
|
||||
- `src/app/page.tsx` (post-login redirect)
|
||||
- `src/app/(admin)/admin/members/page.tsx` (bulk action)
|
||||
- `src/components/layouts/role-nav.tsx` (no change — switcher already correct)
|
||||
- `src/components/layouts/impersonation-banner.tsx` (or wherever the banner lives — find via grep)
|
||||
- `src/server/routers/user.ts` (new `bulkUpdateRoles` mutation if not exists)
|
||||
- `src/lib/email/templates/mentor-onboarding.tsx` (new)
|
||||
- `src/server/services/notifications.ts` (or equivalent — call site to send mentor-onboarding email when MENTOR role is freshly added to a user)
|
||||
|
||||
**1. Post-login redirect — context-aware "go where the work is":**
|
||||
|
||||
Replace single-`role` switch in `src/app/page.tsx` with a priority list that is *filtered by actionable work*. The user lands on the highest-priority role for which they have something to do right now; if no role has active work, fall back to the static priority order.
|
||||
|
||||
New tRPC query: `user.getDefaultDashboard()` (server-side, called from `src/app/page.tsx`):
|
||||
|
||||
```ts
|
||||
// Static priority — used as fallback ordering AND as the order we check for work.
|
||||
const ROLE_DASHBOARD_PRIORITY: Array<[UserRole, Route]> = [
|
||||
['SUPER_ADMIN', '/admin'],
|
||||
['PROGRAM_ADMIN', '/admin'],
|
||||
['AWARD_MASTER', '/award-master'],
|
||||
['JURY_MEMBER', '/jury'],
|
||||
['MENTOR', '/mentor'],
|
||||
['APPLICANT', '/applicant'],
|
||||
['OBSERVER', '/observer'],
|
||||
['AUDIENCE', '/audience'],
|
||||
]
|
||||
```
|
||||
|
||||
For each role the user holds (in priority order), the server checks "does this user have actionable work in this role right now?":
|
||||
|
||||
| Role | "Has actionable work" predicate |
|
||||
|------|---------------------------------|
|
||||
| SUPER_ADMIN / PROGRAM_ADMIN | Always true (admin work is always present) |
|
||||
| AWARD_MASTER | Any unfinalized award decision in an active round in current edition |
|
||||
| JURY_MEMBER | Any `JuryAssignment` linked to a round whose `status = ROUND_ACTIVE` AND the user has at least one PENDING evaluation |
|
||||
| MENTOR | Any `MentorAssignment` whose linked round is `ROUND_ACTIVE` AND `workspaceEnabled = true` |
|
||||
| APPLICANT | Any `Project` led by user with at least one `ProjectRoundState` in a non-terminal state in an active round |
|
||||
| OBSERVER | Always false (observers have nothing to act on) |
|
||||
| AUDIENCE | Always false |
|
||||
|
||||
Algorithm:
|
||||
|
||||
1. Try roles in priority order. Return the first role whose predicate is true.
|
||||
2. If no role has actionable work, return the highest-priority role the user holds (static fallback).
|
||||
3. Always end with a non-null route (worst case: any signed-in user has at least their primary role).
|
||||
|
||||
**Why this matters (your example):** a juror+observer who logs in during an open jury round lands on `/jury` (because they have a pending evaluation), not `/observer`. A mentor+juror logs in during an active MENTORING round → `/mentor`. After both rounds close, same user logs in → static fallback (jury > mentor) → `/jury`. The role switcher in the user menu is always available to override.
|
||||
|
||||
**Decision: context-aware, not "remember last view".** "Remember last view" requires a new column and surprises users when their last context disappears (round closed, role removed). Context-aware is deterministic, explains itself, and handles the cross-role overlap cleanly. The role switcher dropdown is the user's escape hatch.
|
||||
|
||||
**Tests** (in PR 6):
|
||||
- Juror with pending evaluation in active round + Observer → `/jury`
|
||||
- Juror with no active assignments + Observer → `/jury` (fallback to static priority)
|
||||
- Mentor+Juror, MENTORING round active, no jury work → `/mentor`
|
||||
- Mentor+Juror, both rounds active with work in both → `/jury` (priority order breaks the tie)
|
||||
- Observer-only user → `/observer`
|
||||
- Multi-role with no active work anywhere → static-priority fallback
|
||||
|
||||
**2. Bulk juror→mentor promotion** on `/admin/members`:
|
||||
- Add row checkboxes to the Members table (already a table — confirm during impl).
|
||||
- When ≥1 row selected, surface a bulk action toolbar with "Add role…" dropdown (OBSERVER / MENTOR / AWARD_MASTER) and "Remove role…".
|
||||
- Call new `user.bulkUpdateRoles({ userIds, addRole?, removeRole? })` mutation. Server-side: only SUPER_ADMIN/PROGRAM_ADMIN, log a `DecisionAuditLog` entry per user changed.
|
||||
- After success, refresh the table and toast "Added MENTOR role to N users; M already had it (no-op)".
|
||||
|
||||
**3. Mentor-onboarding email** (one-shot):
|
||||
- New email template at `src/lib/email/templates/mentor-onboarding.tsx`: brief welcome, explanation of mentor responsibilities, link to `/mentor`, link to "Switch View" doc/walkthrough.
|
||||
- Trigger: in `user.bulkUpdateRoles` and the existing single-user `updateRoles` mutation, when MENTOR is **newly** added (i.e., wasn't in `roles[]` before this update) → enqueue the email. Idempotent on subsequent edits that keep MENTOR in `roles`.
|
||||
- Add a `User.mentorOnboardingSentAt: DateTime?` column for idempotency. Migration: nullable column, no backfill needed.
|
||||
|
||||
**4. Fix impersonation banner pointer-events:**
|
||||
- Locate the banner component (grep `Impersonating` / `bg-red-600 fixed top-0`).
|
||||
- Restructure: banner sits in a flex container above the header rather than being `position: fixed` over it. The header height stays unchanged; the banner pushes content down.
|
||||
- Alternative (smaller change): keep `position: fixed` but `pointer-events: none` on the banner div and re-enable `pointer-events: auto` on the inner "Return to Admin" button only. Either fixes the menu intercept.
|
||||
- Pick the simpler diff at impl time; document choice in PR.
|
||||
|
||||
**5. Banner shows all roles:**
|
||||
- When `session.user.roles.length > 1`, render comma-separated list: "Impersonating Dr. Sophie Laurent (JURY MEMBER, MENTOR)".
|
||||
|
||||
**6. Standardize the role-switcher (location + presentation):**
|
||||
|
||||
Today's state:
|
||||
- Header layouts (`role-nav.tsx`) — used by jury, mentor, applicant, observer, award-master — put the user menu **top-right** with role-switcher items inside the dropdown.
|
||||
- Admin layout (`admin-sidebar.tsx`) puts the user menu **bottom-left of the sidebar** with its own duplicate `ROLE_SWITCH_OPTIONS` constant + `switchableRoles` filter (lines 161, 191, 377-401).
|
||||
|
||||
Two problems: (a) duplicated logic across two files; (b) different physical placement, so a multi-role user has to learn two patterns to find "Switch View".
|
||||
|
||||
Changes:
|
||||
|
||||
- **Extract a shared module** at `src/components/layouts/role-switcher.tsx` exporting:
|
||||
- `useRoleSwitcher()` hook returning `{ switchableRoles: Array<{ label, path, icon }>, currentBasePath }`. Both `role-nav.tsx` and `admin-sidebar.tsx` import this. Source of truth for `ROLE_SWITCH_OPTIONS` lives here only.
|
||||
- `RoleSwitcherMenuItems` component — renders the dropdown items (used inside both layouts' user menus). Keeps rendering inline-consistent.
|
||||
- `RoleSwitcherPill` component — a standalone visible button that renders just outside the user-menu dropdown, with label "Switch View" + the icon of the next-best alternate role. Visible only when `switchableRoles.length > 0`. Click opens a small popover listing alternates.
|
||||
|
||||
- **Place the `RoleSwitcherPill` in a consistent location across all layouts**: top-right of the header, immediately to the LEFT of the notifications bell. For the admin layout (sidebar-based), add a top-right header strip that hosts the pill + notifications + theme toggle, mirroring the other dashboards. (The admin sidebar keeps everything else; just the top-bar is added.)
|
||||
|
||||
Why top-right: that's where the existing role-nav layouts already put switching/profile actions. Admins gain the pill in the same spot — no learning curve when switching from /admin to /jury.
|
||||
|
||||
- **Pill behavior:**
|
||||
- Hidden if `switchableRoles.length === 0` (single-role users see nothing — clean default).
|
||||
- Hidden when `isImpersonating` (impersonator UX is already different; the existing impersonation banner with "Return to Admin" handles role-switching for that path).
|
||||
- On hover/focus: shows tooltip "Switch dashboard view".
|
||||
- Keyboard: `Cmd+Shift+V` shortcut opens the popover (nice-to-have; ship if it doesn't add much code).
|
||||
|
||||
- **Admin sidebar bottom user pill stays** (so admin users can still sign out / open settings from there). The role-switcher items are removed from that menu — they live exclusively in the new pill + the user-dropdown's switch list. (Avoids three places to switch view.)
|
||||
|
||||
**Acceptance for §D.6:** any signed-in user with `roles.length > 1` sees a "Switch View" pill in the same screen position regardless of which dashboard they're currently in.
|
||||
|
||||
**Tests** (in PR 6):
|
||||
- `user.getDefaultDashboard` test cases enumerated above (juror+observer with active jury round → /jury; etc.).
|
||||
- `bulkUpdateRoles` adds MENTOR to N users and sends N onboarding emails.
|
||||
- Idempotency: second `bulkUpdateRoles` with same input does NOT resend email.
|
||||
- Impersonation banner does not intercept clicks on user dropdown (Playwright e2e if available).
|
||||
- `RoleSwitcherPill` renders in the top-right of every dashboard for multi-role users; renders nothing for single-role users.
|
||||
- Single shared `useRoleSwitcher` source means changing `ROLE_SWITCH_OPTIONS` updates both layouts simultaneously.
|
||||
|
||||
---
|
||||
|
||||
## §E. Filter juror preferences to review-only rounds (PR 1)
|
||||
|
||||
**File:** `src/server/routers/user.ts:1397-1422` (`getOnboardingContext`)
|
||||
|
||||
**Change:** Query the membership's jury group, including its linked rounds. Filter out memberships where every linked round is LIVE_FINAL or DELIBERATION. Keep memberships where at least one linked round is INTAKE / FILTERING / EVALUATION / SUBMISSION / MENTORING.
|
||||
|
||||
```ts
|
||||
const memberships = await ctx.prisma.juryGroupMember.findMany({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
juryGroup: {
|
||||
rounds: {
|
||||
some: {
|
||||
roundType: {
|
||||
in: ['INTAKE', 'FILTERING', 'EVALUATION', 'SUBMISSION', 'MENTORING'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
include: { juryGroup: { select: { id: true, name: true, defaultMaxAssignments: true } } },
|
||||
})
|
||||
```
|
||||
|
||||
(Confirm the relation field name `rounds` on `JuryGroup` during impl — Prisma schema field may be `Round[]` named differently.)
|
||||
|
||||
**Tests** (in PR 1):
|
||||
- Juror with memberships in (Screening: FILTERING) + (Finals: LIVE_FINAL) → only Screening returned.
|
||||
- Juror with memberships in (Mixed: EVALUATION + LIVE_FINAL) → returned (group has at least one review round).
|
||||
- Juror with only (Finals: LIVE_FINAL) → no memberships returned.
|
||||
|
||||
**Risk:** very low. Single procedure, additive Prisma filter, easy to revert.
|
||||
|
||||
---
|
||||
|
||||
## §F. Workspace messaging + files end-to-end
|
||||
|
||||
### §F.1 — Server-side path enforcement (PR 2)
|
||||
|
||||
**Files:**
|
||||
- `src/lib/minio.ts` (add helper)
|
||||
- `src/server/routers/mentor.ts` (`workspaceUploadFile` procedure + presign procedure)
|
||||
- `src/server/services/mentor-workspace.ts` (`uploadFile` service)
|
||||
|
||||
**New helper** in `src/lib/minio.ts`:
|
||||
|
||||
```ts
|
||||
export function generateMentorObjectKey(projectTitle: string, fileName: string): string {
|
||||
return generateObjectKey(projectTitle, fileName, 'mentorship')
|
||||
}
|
||||
```
|
||||
|
||||
This produces `<sanitizedProjectName>/mentorship/<timestamp>-<sanitizedFileName>`, matching the existing project-file scheme.
|
||||
|
||||
**Procedure changes:**
|
||||
|
||||
1. Add a presign procedure (if not present): `mentor.presignWorkspaceUpload({ mentorAssignmentId, fileName, mimeType, size })` →
|
||||
- Loads the `MentorAssignment` + linked `Project` (server-side).
|
||||
- Authorizes: user is the assigned mentor OR a project team member (mentorProcedure for mentors; protectedProcedure with project-team check for applicants).
|
||||
- Constructs `objectKey = generateMentorObjectKey(project.title, fileName)`.
|
||||
- Returns `{ uploadUrl, bucket, objectKey }` — the presigned PUT URL is short-lived (1h).
|
||||
2. Change `workspaceUploadFile` to accept ONLY `{ uploadToken, description? }` (where `uploadToken` is an opaque value returned by the presign call). The presign procedure stores `{ token → { mentorAssignmentId, fileName, mimeType, size, bucket, objectKey } }` in a short-lived cache (in-memory or Redis if configured, 1h TTL). The upload procedure looks up the token, validates that the user is the same one who called presign, then writes the `MentorFile` row using the cached values. This eliminates any client-controlled path entirely.
|
||||
3. Mirror the same change for applicant-side uploads to mentor workspace (if a separate procedure exists).
|
||||
|
||||
**Migration:** Pre-flight — confirm `MentorFile` table is empty (or only test data) in production. If it has any rows, migrate `objectKey`s to the new scheme via a one-shot script; otherwise skip migration.
|
||||
|
||||
**Tests** (in PR 2):
|
||||
- Presign returns key matching `<projectName>/mentorship/<timestamp>-<file>` shape.
|
||||
- `workspaceUploadFile` rejects payloads that include `bucket` or `objectKey` (input schema rejects unknown fields via Zod).
|
||||
- Authorization: mentor uploading to a workspace they're NOT assigned to → throws TRPCError UNAUTHORIZED.
|
||||
|
||||
### §F.2 — Dashboard message previews (PR 6)
|
||||
|
||||
**Files:**
|
||||
- New component: `src/components/mentor/recent-messages-card.tsx`
|
||||
- New component: `src/components/applicant/mentor-conversation-card.tsx`
|
||||
- `src/app/(mentor)/mentor/page.tsx` — embed RecentMessagesCard
|
||||
- `src/app/(applicant)/applicant/page.tsx` — embed MentorConversationCard (only render when project has mentorAssignment + workspace enabled)
|
||||
- `src/server/routers/mentor.ts` — new procedure `getRecentMessagesForMentor` (returns last N msgs across all assignments)
|
||||
- `src/server/routers/applicant.ts` — new procedure `getMentorConversationPreview({ projectId })` (returns last 3 msgs + unread count for one project)
|
||||
|
||||
**Mentor dashboard preview**:
|
||||
- Card title: "Recent Messages"
|
||||
- Shows last 5 unread messages across ALL assignments (sender name + project + first 100 chars + relative timestamp).
|
||||
- Each row links to `/mentor/workspace/<projectId>` (jumps to that conversation).
|
||||
- "View all" link → `/mentor/messages` (existing or new index — confirm during impl).
|
||||
- Empty state: "No new messages. Your mentees will appear here when they reach out."
|
||||
|
||||
**Applicant dashboard preview** (only when project has assigned mentor + workspace enabled):
|
||||
- Card title: "Conversation with [Mentor Name]"
|
||||
- Shows last 3 messages (sender name + content + timestamp).
|
||||
- Unread count badge.
|
||||
- "Send a message" inline composer or "Open chat" button → `/applicant/mentor`.
|
||||
- Empty state: "Say hi to your mentor — they're here to help you sharpen your project."
|
||||
|
||||
**Performance:** both queries use indexed lookups on `MentorMessage(workspaceId, createdAt)`. Add an index migration if not present.
|
||||
|
||||
**Tests** (in PR 6):
|
||||
- `getRecentMessagesForMentor` returns N most-recent unread messages across assignments.
|
||||
- `getMentorConversationPreview` returns 3 most-recent messages + correct unread count.
|
||||
- Renders gracefully when no assignment / no messages.
|
||||
|
||||
### §F.3 — End-to-end verification scenario (covered in §G)
|
||||
|
||||
A single integration test walking through the full happy path. See §G.
|
||||
|
||||
---
|
||||
|
||||
## §G. Tests
|
||||
|
||||
**New test files:**
|
||||
- `tests/unit/mentor-config.test.ts` (PR 3) — Config form persistence per field
|
||||
- `tests/unit/mentor-key-construction.test.ts` (PR 2) — `generateMentorObjectKey` shape + sanitization
|
||||
- `tests/integration/mentor-assignment.test.ts` (PR 4) — manual + auto + bulk + skip
|
||||
- `tests/integration/mentor-round-engine.test.ts` (NEW for PR 3 or PR 5) — pass-through behavior, eligibility variants, advancement
|
||||
- `tests/integration/mentor-workspace.test.ts` (PR 6) — message + file lifecycle, dashboard previews, milestone auto-complete
|
||||
- `tests/unit/jury-preferences-filter.test.ts` (PR 1) — `getOnboardingContext` filter
|
||||
|
||||
**End-to-end happy path** (`tests/integration/mentor-round-e2e.test.ts`, ships with PR 6):
|
||||
|
||||
1. Admin creates a MENTORING round, sets dates + eligibility=requested_only + 14-day deadline.
|
||||
2. Admin activates round.
|
||||
3. Project A has `mentoringRequested=true`, project B does not.
|
||||
4. Round-engine activation: B auto-PASSED (pass-through), A stays PENDING.
|
||||
5. Admin manually assigns mentor M1 to project A. A flips PENDING → IN_PROGRESS. Mentor + team get assignment notification.
|
||||
6. M1 sends a message in workspace; team replies. Both messages appear in respective dashboard previews.
|
||||
7. M1 uploads a file. ObjectKey matches `<projectA-title>/mentorship/<timestamp>-...`. Team comments on the file.
|
||||
8. M1 marks all required milestones complete → assignment.completionStatus = "completed".
|
||||
9. Admin closes round. A and B both PASSED; A also COMPLETED.
|
||||
|
||||
This single test covers the operational path the user actually cares about for the upcoming round.
|
||||
|
||||
---
|
||||
|
||||
## Open questions
|
||||
|
||||
1. **`generateMentorObjectKey` — which "project name" field do we pass?** `Project.title` is the obvious choice (it's what `generateObjectKey` for submission files uses). Confirm during impl that there's no team-name-specific field we should prefer.
|
||||
2. **Does `JuryGroup` have a direct `rounds` Prisma relation?** Spec assumes it; confirm field name during impl. If it's `Round.juryGroupId` only (no back-relation), use a nested `Round` query.
|
||||
3. **Mentor-onboarding email content** — copy needs writing. Owned by admin, not blocking impl; can ship with placeholder copy and finalize before going live.
|
||||
4. **`mentor.autoAssignBulk` — does it already skip manually-assigned?** Spec assumes yes; confirm by reading source during PR 4. If no, change is small (add `where: { method: { not: 'MANUAL' } }` to its query).
|
||||
5. **Pre-flight check on existing mentor files in prod MinIO before §F.1** — must be empty or migrated, not orphaned. Confirm via `prisma db query` against prod read replica before deploying PR 2.
|
||||
|
||||
## Risks
|
||||
|
||||
| Risk | Severity | Mitigation |
|
||||
|------|----------|------------|
|
||||
| Existing mentor files in prod use legacy keys | High if hit | Pre-flight check; migration script ready before deploy |
|
||||
| `bulkUpdateRoles` accidentally removes a critical role | Med | Server-side guard: SUPER_ADMIN cannot be self-demoted; audit log all changes |
|
||||
| Multi-role redirect priority surprises some users | Low | Document the priority order; role switcher exists for override |
|
||||
| AI fallback ordering doesn't match prior AI suggestions | Low | UX banner clearly states fallback is in use; keep logic simple |
|
||||
| Filter on `getOnboardingContext` accidentally hides valid memberships | Low | Tests cover the three cases; ship behind no flag, easy to revert |
|
||||
|
||||
## Migration plan
|
||||
|
||||
- §A: no migration.
|
||||
- §B: no migration.
|
||||
- §C: no migration.
|
||||
- §D: one Prisma migration adding nullable `User.mentorOnboardingSentAt: DateTime?`. No backfill (treat all existing users as not-yet-onboarded; on next role edit, email fires once).
|
||||
- §E: no migration.
|
||||
- §F.1: optional one-shot script to rewrite legacy `MentorFile.objectKey` rows to the new scheme. Only runs if pre-flight check finds rows. The script copies objects to the new key path then updates DB rows in a transaction; old keys remain readable until manual cleanup.
|
||||
- §F.2: optional Prisma index on `MentorMessage(workspaceId, createdAt DESC)` if not present.
|
||||
|
||||
## Rollback
|
||||
|
||||
Each PR independently revertable. PRs 1, 2, 4 ship with no migration → straight git revert. PR 6 has a migration → revert PR + one-line down migration to drop the column. PR 3 has no migration; PR 5 has no migration.
|
||||
|
||||
## Acceptance criteria (per phase)
|
||||
|
||||
**PR 1 (§E):**
|
||||
- Sophie Laurent (member of Screening, Expert, Finals jury groups) sees Screening + Expert preferences only — not Finals.
|
||||
|
||||
**PR 2 (§F.1):**
|
||||
- New mentor file uploads write to `<projectName>/mentorship/<timestamp>-<file>` in MinIO.
|
||||
- Removing `bucket` / `objectKey` from a `workspaceUploadFile` call still succeeds.
|
||||
- Old `objectKey` upload payloads now fail Zod validation.
|
||||
|
||||
**PR 3 (§A):**
|
||||
- All `MentoringConfigSchema` fields are editable from the Config tab.
|
||||
- A draft MENTORING round with no document-promotion configured can pass Launch Readiness without a "File requirements set" check.
|
||||
|
||||
**PR 4 (§C):**
|
||||
- Admin can manually assign any MENTOR-role user to any project from `/admin/projects/[id]/mentor`.
|
||||
- Round Projects tab "Auto-fill remaining" assigns to all `mentoringRequested=true` projects without a mentor.
|
||||
- Page renders sensibly with no `OPENAI_API_KEY` set (expertise-tag fallback).
|
||||
|
||||
**PR 5 (§B):**
|
||||
- MENTORING round Overview shows live counts (requested / assigned / unassigned), deadline countdown, mentor pool size, workspace activity totals.
|
||||
- `/admin/mentors` shows real list of MENTOR-role users with current assignments.
|
||||
|
||||
**PR 6 (§D + §F.2):**
|
||||
- Juror+observer logging in during an active jury round lands on `/jury` (context-aware default). Same user logging in after the round closes lands on `/jury` via static fallback (still highest-priority role they hold).
|
||||
- Mentor+juror with active mentoring assignments and no jury work lands on `/mentor`.
|
||||
- `RoleSwitcherPill` ("Switch View") renders in the top-right of the header on every dashboard for multi-role users, in the same screen position regardless of layout. Single-role users don't see it.
|
||||
- Admin sidebar still has the user pill at the bottom-left for sign-out / settings; role-switcher entries are removed from that menu (live in the new pill instead).
|
||||
- `/admin/members` allows multi-select + "Add MENTOR role to selected" → all selected users get email + role.
|
||||
- Impersonation banner doesn't intercept clicks on the user dropdown.
|
||||
- Mentor `/mentor` dashboard shows "Recent Messages" card; applicant `/applicant` dashboard shows "Conversation with [Mentor]" card.
|
||||
348
docs/superpowers/specs/2026-04-29-pr6-lunch-event-design.md
Normal file
348
docs/superpowers/specs/2026-04-29-pr6-lunch-event-design.md
Normal file
@@ -0,0 +1,348 @@
|
||||
# PR 6 — Lunch event (design)
|
||||
|
||||
Date: 2026-04-29
|
||||
Status: design locked, ready for implementation plan
|
||||
|
||||
## 1. Goal & scope
|
||||
|
||||
Replace the Lunch tab placeholder on `/admin/logistics` with a working flow: admins configure a single per-edition lunch event, attendees pick a dish + log allergies, and the manifest is reviewable in-app, exportable to CSV, and emailed at the change deadline.
|
||||
|
||||
**In scope:**
|
||||
|
||||
- New models: `LunchEvent` (1:1 per program), `Dish` (per event), `MemberLunchPick` (1:1 per `AttendingMember`), `ExternalAttendee` (per program, optionally team-attached).
|
||||
- Enums: `DietaryTag`, `Allergen`.
|
||||
- Admin UI on the existing Logistics → Lunch tab: event config card, dishes CRUD, manifest table, externals CRUD, recap preview/send, audit logging.
|
||||
- Team-lead UX: dish/allergy editing for any `AttendingMember` on their project, on the existing applicant dashboard.
|
||||
- Member self-serve UX: dish/allergy editing for own `AttendingMember`, on the same dashboard.
|
||||
- Single reminder email (configurable hours before deadline).
|
||||
- Recap email (manual button + cron auto-send, both toggleable; admin recipients + free-form extras).
|
||||
- Removal of the Lunch placeholders from Edition settings and from the disabled Logistics tab trigger.
|
||||
|
||||
**Out of scope:**
|
||||
|
||||
- No caterer-facing email integration. Admins forward the recap manually.
|
||||
- No multi-event per edition (1:1 with `Program`).
|
||||
- No public token-gated picker. Members must have an account; team leads or admins fill in for non-active members.
|
||||
- Editable email templates (lands in PR 7).
|
||||
|
||||
## 2. Permission matrix
|
||||
|
||||
| Editor | Can edit |
|
||||
| --- | --- |
|
||||
| Member (logged in) | Their own dish + allergies, until deadline |
|
||||
| Team lead | Any `AttendingMember` on their project, until deadline |
|
||||
| Admin | Everything — all `AttendingMember` picks + all `ExternalAttendee` records, no deadline cap |
|
||||
|
||||
External (off-platform) attendees are admin-only to manage; team leads see them on the project page read-only when an external is attached to their team.
|
||||
|
||||
*"Team lead"* throughout this spec means a user with a `TeamMember` row on the project where `TeamMember.role === 'LEAD'` (existing enum value, defined at `schema.prisma:273-277`).
|
||||
|
||||
*"Admins of the edition"* (used by recap recipients and audit-log actor scoping) means all users with `role === 'SUPER_ADMIN'` plus all users with `role === 'PROGRAM_ADMIN'`. There is no per-program admin scoping today, so all program admins receive the recap.
|
||||
|
||||
## 3. Data model
|
||||
|
||||
```prisma
|
||||
enum DietaryTag {
|
||||
VEGETARIAN
|
||||
VEGAN
|
||||
GLUTEN_FREE
|
||||
PESCATARIAN
|
||||
}
|
||||
|
||||
enum Allergen {
|
||||
GLUTEN // cereals containing gluten
|
||||
CRUSTACEANS
|
||||
EGGS
|
||||
FISH
|
||||
PEANUTS
|
||||
SOYBEANS
|
||||
MILK
|
||||
TREE_NUTS
|
||||
CELERY
|
||||
MUSTARD
|
||||
SESAME
|
||||
SULPHITES
|
||||
LUPIN
|
||||
MOLLUSCS
|
||||
}
|
||||
|
||||
model LunchEvent {
|
||||
id String @id @default(cuid())
|
||||
programId String @unique // 1:1 — one lunch per edition
|
||||
enabled Boolean @default(false)
|
||||
eventAt DateTime? // nullable until admin sets it
|
||||
endAt DateTime?
|
||||
venue String?
|
||||
notes String? @db.Text
|
||||
changeCutoffHours Int @default(48)
|
||||
reminderHoursBeforeDeadline Int? // null = no reminder
|
||||
cronEnabled Boolean @default(true) // auto-recap at deadline
|
||||
extraRecipients String[] @default([]) // off-platform recap recipients
|
||||
reminderSentAt DateTime? // cron idempotency
|
||||
recapSentAt DateTime? // gates "send updated recap?" prompt
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
dishes Dish[]
|
||||
externalAttendees ExternalAttendee[]
|
||||
}
|
||||
|
||||
model Dish {
|
||||
id String @id @default(cuid())
|
||||
lunchEventId String
|
||||
name String
|
||||
sortOrder Int @default(0)
|
||||
dietaryTags DietaryTag[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
lunchEvent LunchEvent @relation(fields: [lunchEventId], references: [id], onDelete: Cascade)
|
||||
memberPicks MemberLunchPick[]
|
||||
externals ExternalAttendee[]
|
||||
|
||||
@@index([lunchEventId])
|
||||
}
|
||||
|
||||
model MemberLunchPick {
|
||||
id String @id @default(cuid())
|
||||
attendingMemberId String @unique // 1:1, mirrors FlightDetail/VisaApplication
|
||||
dishId String? // null = not picked yet
|
||||
allergens Allergen[] @default([])
|
||||
allergenOther String? // "other" free-text
|
||||
pickedAt DateTime? // null until first pick made
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
attendingMember AttendingMember @relation(fields: [attendingMemberId], references: [id], onDelete: Cascade)
|
||||
dish Dish? @relation(fields: [dishId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([dishId])
|
||||
}
|
||||
|
||||
model ExternalAttendee {
|
||||
id String @id @default(cuid())
|
||||
lunchEventId String
|
||||
projectId String? // optional — null = standalone (jury/dignitary/etc.)
|
||||
name String
|
||||
email String?
|
||||
roleNote String?
|
||||
dishId String?
|
||||
allergens Allergen[] @default([])
|
||||
allergenOther String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
lunchEvent LunchEvent @relation(fields: [lunchEventId], references: [id], onDelete: Cascade)
|
||||
project Project? @relation(fields: [projectId], references: [id], onDelete: SetNull)
|
||||
dish Dish? @relation(fields: [dishId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([lunchEventId])
|
||||
@@index([projectId])
|
||||
}
|
||||
```
|
||||
|
||||
**Back-references on existing models:**
|
||||
|
||||
```prisma
|
||||
model Program {
|
||||
// ...existing fields...
|
||||
lunchEvent LunchEvent?
|
||||
}
|
||||
|
||||
model AttendingMember {
|
||||
// ...existing fields...
|
||||
lunchPick MemberLunchPick?
|
||||
}
|
||||
|
||||
model Project {
|
||||
// ...existing fields...
|
||||
externalLunchAttendees ExternalAttendee[]
|
||||
}
|
||||
```
|
||||
|
||||
**Auto-create hook.** When an `AttendingMember` is created, if a `LunchEvent` exists for the parent program, also create an empty `MemberLunchPick` (`dishId=null`, `pickedAt=null`). When the `AttendingMember` is deleted, the cascade handles the pick. Mirrors the visa-application auto-sync added in commit `bdfd998`.
|
||||
|
||||
**Migrations are additive.** Nothing existing changes shape. `pickedAt` is set on the first `upsertPick` call where `dishId` is non-null; subsequent edits update `updatedAt` only.
|
||||
|
||||
## 4. API surface
|
||||
|
||||
New router `src/server/routers/lunch.ts`, mounted as `trpc.lunch.*`. Logistics router unchanged.
|
||||
|
||||
### Admin-only (`adminProcedure`)
|
||||
|
||||
| Procedure | Purpose |
|
||||
| --- | --- |
|
||||
| `getEvent` | Get-or-create the `LunchEvent` for the current program (lazy create, mirrors hotel's pattern). |
|
||||
| `updateEvent` | Patch any subset of: `enabled, eventAt, endAt, venue, notes, changeCutoffHours, reminderHoursBeforeDeadline, cronEnabled, extraRecipients[]`. |
|
||||
| `createDish` / `updateDish` / `deleteDish` / `reorderDishes` | Dish CRUD. Delete sets `dishId=null` on picks via Prisma `SetNull`. |
|
||||
| `listExternals` / `createExternal` / `updateExternal` / `deleteExternal` | External-attendee CRUD. |
|
||||
| `getManifest` | Full manifest: attending members (filtered to `FinalistConfirmation.status === CONFIRMED`) + externals, each with project, name, dish, allergens. Backs the Lunch tab table and CSV export. |
|
||||
| `exportManifestCsv` | Server-side CSV generation; returns string for client-side download. |
|
||||
| `getRecapPreview` | Returns the recap email payload (counts + table) for in-app preview. |
|
||||
| `sendRecap` | Manual send. Input `{ forceUpdate?: boolean }`. If `recapSentAt` is set and `forceUpdate=false`, throws `PRECONDITION_FAILED` so the UI can show the "send updated?" confirm. Sends to admins of the edition + `extraRecipients[]`. Updates `recapSentAt`. Audit-logged. |
|
||||
|
||||
### Mixed permission (`protectedProcedure` with role guard inside)
|
||||
|
||||
| Procedure | Purpose |
|
||||
| --- | --- |
|
||||
| `upsertPick` | Single procedure for member-self / team-lead / admin. Input: `{ attendingMemberId, dishId, allergens, allergenOther }`. Guard: caller is (a) the `AttendingMember.userId`, OR (b) team lead of the parent project, OR (c) admin. After `changeCutoffHours` cutoff, only admins pass. Audit-logged on every write with actor role. |
|
||||
|
||||
### Member read (`protectedProcedure`)
|
||||
|
||||
| Procedure | Purpose |
|
||||
| --- | --- |
|
||||
| `getEventForMember` | Public-ish event view: `{ enabled, eventAt, endAt, venue, notes, changeDeadline }` for the dashboard banner. Returns `null` when `enabled=false`. |
|
||||
| `getTeamPicks` | All picks for the caller's team (resolved via `TeamMember → project`). Returns `[{ attendingMemberId, memberName, dish, allergens, hasPicked }]` for the team-wide-read visibility. |
|
||||
|
||||
### Cron endpoints (REST, `CRON_SECRET` guarded)
|
||||
|
||||
| Endpoint | Behavior |
|
||||
| --- | --- |
|
||||
| `POST /api/cron/lunch-reminders` | Single fire per event: scans enabled `LunchEvent`s with `reminderHoursBeforeDeadline` set and `reminderSentAt` null. If `now ∈ [reminderAt, deadline)`, emails attending members with `pickedAt=null` whose parent `FinalistConfirmation.status === CONFIRMED`, then stamps `reminderSentAt`. Idempotent. |
|
||||
| `POST /api/cron/lunch-recap` | Single fire per event: scans enabled `LunchEvent`s with `cronEnabled=true`, `recapSentAt` null, and `now >= deadline`. Sends recap to admins + `extraRecipients[]`, stamps `recapSentAt`. Idempotent. |
|
||||
|
||||
Both endpoints follow the CLAUDE.md rule "Round notifications never throw — all errors caught and logged" with per-event `try/catch` so one failure does not poison the sweep.
|
||||
|
||||
## 5. UI
|
||||
|
||||
### Admin: `/admin/logistics → Lunch tab`
|
||||
|
||||
Stack of cards on the existing tab content area:
|
||||
|
||||
1. **Event config card** — enabled toggle (master switch), `eventAt` + `endAt` date pickers, `venue`, `notes`, `changeCutoffHours`, `reminderHoursBeforeDeadline`, `cronEnabled`, `extraRecipients[]` (chip-input for emails). Same blur-to-commit pattern used by the Edition settings tab.
|
||||
2. **Dishes card** — list of dishes (name, dietary-tag pills, drag handle for `sortOrder`), inline add row, edit/delete buttons. Empty state: *"Add at least one dish to open picks."*
|
||||
3. **Manifest card** — table: `Team | Attendee | Type (member/external) | Dish | Allergens | Picked at`. Filters: team dropdown, "Missing picks only" toggle. Header summary chip: *"23/30 picked · 3 vegan · 2 nut-allergic · 1 missing"*. Externals appear inline (team column shows "—" for standalone). Edit-pencil opens a slide-over on any row (admin override).
|
||||
4. **Externals card** — table of external attendees with add button → dialog (name, email, project (optional), `roleNote`, `dishId`, `allergens`, `allergenOther`). Edits use the same dialog.
|
||||
5. **Recap actions card** — two buttons: *"Preview recap"* (modal showing email body) and *"Send recap now"* (with the post-deadline "you already sent — resend updated?" confirm); plus *"Download CSV"*. Footer text: *"Last sent: 2026-06-25 14:02. Recipients: 4 admins + 2 extra."*
|
||||
|
||||
When `enabled=false`, cards 2–5 collapse to a single empty state: *"Lunch is disabled — toggle on to configure."*
|
||||
|
||||
### Applicant dashboard (`/applicant`) — extend `AttendingMembersCard`
|
||||
|
||||
Each attending-member row (already shows visa + flight) gets a new collapsible **Lunch** subsection:
|
||||
|
||||
- Dish dropdown (grouped by dietary tag — *"Vegetarian options"*, *"All options"*).
|
||||
- Allergen checklist (EU 14 inline grid) + "other" textarea.
|
||||
- "Picked" chip with timestamp once `pickedAt` is set.
|
||||
|
||||
Edit affordance:
|
||||
|
||||
- **Member viewing own row:** editable until deadline.
|
||||
- **Team lead viewing teammates' rows:** editable until deadline, with a clear *"Editing on behalf of [Name]"* label.
|
||||
- **Past deadline:** read-only, with note *"Past change deadline. Contact an admin for changes."*
|
||||
|
||||
Above `AttendingMembersCard`, a thin **lunch banner** (only when `enabled=true`) shows event date/time, venue, change-deadline countdown, and a *"Notes from organizers"* expander.
|
||||
|
||||
### Project page
|
||||
|
||||
Read-only **External attendees for your team** strip — only when externals with `projectId === thisProject` exist, so the team knows who's joining them. No edits — admin-only.
|
||||
|
||||
### Removals
|
||||
|
||||
- Drop the Lunch line from the "Coming soon" card on `edition-settings-tab.tsx:212-216`.
|
||||
- Remove `disabled` from the Lunch tab trigger in `logistics/page.tsx:55-58` and wire it to a new `<LunchTab>` component.
|
||||
|
||||
## 6. Email + cron details
|
||||
|
||||
**Email templates** live inline in `src/lib/email.ts` (the existing single-file pattern); no new infrastructure.
|
||||
|
||||
**Reminder.** Subject: *"Pick your lunch dish — deadline in [Xh]"*. Body: greeting, event date/venue/notes, deadline timestamp, button → applicant dashboard. Sent only to attending members with `pickedAt=null` whose confirmation is `CONFIRMED`.
|
||||
|
||||
**Recap.** Subject: *"Lunch manifest — [event date]"*. Body: aggregate counts (dishes, dietary tags, allergens), per-attendee table grouped by team (externals as a final group), event metadata. Plain HTML; no CSV attachment in v1 — admins use the in-app *"Download CSV"* button when needed.
|
||||
|
||||
**Time formatting.** Same approach as the confirmation page: format with `Intl.DateTimeFormat` in the recipient's email-client locale, plus a hardcoded `"Europe/Monaco"` zone label and the ISO timestamp for unambiguous parsing.
|
||||
|
||||
**Audit log entries** (new `eventType` string literals on the existing `DecisionAuditLog.eventType` field — no schema change since the column is free-form):
|
||||
|
||||
- `LUNCH_EVENT_UPDATED`
|
||||
- `LUNCH_DISH_CREATED` / `LUNCH_DISH_UPDATED` / `LUNCH_DISH_DELETED`
|
||||
- `LUNCH_PICK_UPDATED` (records actor role: `SELF` / `TEAM_LEAD` / `ADMIN`)
|
||||
- `LUNCH_EXTERNAL_CREATED` / `LUNCH_EXTERNAL_UPDATED` / `LUNCH_EXTERNAL_DELETED`
|
||||
- `LUNCH_RECAP_SENT` (with recipient count)
|
||||
|
||||
## 7. Edge cases & error handling
|
||||
|
||||
| Case | Behavior |
|
||||
| --- | --- |
|
||||
| `LunchEvent` does not yet exist for the program | `getEvent` lazily creates it with defaults; member/team-lead reads return `null` (banner hidden). |
|
||||
| Admin disables lunch after picks made | Picks remain in the database; UI hides them; recap still works if re-enabled. |
|
||||
| `FinalistConfirmation` flips from `CONFIRMED` to `SUPERSEDED` after a pick was made | Pick row stays; manifest filters the team out. If the team is later re-promoted via waitlist, picks are visible again. |
|
||||
| Dish is deleted | `dishId` becomes `null` on picks/externals; rows show as "not picked"; admins re-pick. Audit logged. |
|
||||
| `eventAt` is moved | Deadline (`eventAt - changeCutoffHours`) and reminder window recalculate automatically — no manual adjustment needed. |
|
||||
| `eventAt` is set in the past | Admin sees a UI warning; cron treats deadline as already-past (so a manual send is the only recourse, since `recapSentAt` may already be moot). |
|
||||
| `changeCutoffHours = 0` | Deadline equals `eventAt`. Allowed. |
|
||||
| Admin edits a pick after `recapSentAt` is set | UI surfaces a confirm dialog: *"This will not auto-resend the recap. Send updated recap?"* ─ "Yes" calls `sendRecap` with `forceUpdate=true`. Audit logged regardless. |
|
||||
| Member with no `AttendingMember` row | Cannot edit. UI hides the lunch subsection (no row exists). |
|
||||
| External with `projectId` that points to a project no longer in the edition | `onDelete: SetNull` on the relation already covers cascades; standalone-display fallback. |
|
||||
|
||||
## 8. Testing strategy
|
||||
|
||||
Vitest, sequential pool (per CLAUDE.md). Tests grouped by router/service:
|
||||
|
||||
**`tests/lunch/lunch-router.test.ts`**
|
||||
|
||||
- `getEvent` lazily creates the row on first call.
|
||||
- `updateEvent` patches an arbitrary subset.
|
||||
- Dish CRUD (`createDish`, `updateDish`, `deleteDish`, `reorderDishes`) — delete sets `dishId=null` on existing picks.
|
||||
- External CRUD covers the standalone (`projectId=null`) and team-attached cases.
|
||||
- `getManifest` filters out non-`CONFIRMED` confirmations and merges externals.
|
||||
|
||||
**`tests/lunch/upsert-pick.test.ts`**
|
||||
|
||||
- Member edits own row: succeeds before deadline, fails after.
|
||||
- Team lead edits teammate row: succeeds before deadline, fails after.
|
||||
- Team lead edits a non-team member's row: fails with `FORBIDDEN`.
|
||||
- Admin edits any row before/after deadline: succeeds in both cases.
|
||||
- Audit log records actor role correctly per case.
|
||||
|
||||
**`tests/lunch/recap.test.ts`**
|
||||
|
||||
- `sendRecap` with `recapSentAt=null` succeeds and stamps the timestamp.
|
||||
- `sendRecap` with `recapSentAt` set and `forceUpdate=false` throws `PRECONDITION_FAILED`.
|
||||
- `sendRecap` with `forceUpdate=true` succeeds and re-stamps.
|
||||
- Recap aggregation is correct (dish counts, dietary-tag counts, allergen counts).
|
||||
|
||||
**`tests/lunch/cron.test.ts`**
|
||||
|
||||
- `lunch-reminders` is idempotent (second call within window does not double-send).
|
||||
- `lunch-reminders` skips events with `reminderSentAt` already set.
|
||||
- `lunch-recap` skips events with `cronEnabled=false`.
|
||||
- `lunch-recap` skips events with `recapSentAt` already set.
|
||||
- Per-event try/catch — a failing send for one event does not stop the next from being processed.
|
||||
|
||||
**`tests/lunch/auto-create.test.ts`**
|
||||
|
||||
- Creating an `AttendingMember` while a `LunchEvent` exists also creates an empty `MemberLunchPick`.
|
||||
- Creating an `AttendingMember` while no `LunchEvent` exists does not error and does not create a pick.
|
||||
|
||||
Build (`npm run build`), typecheck (`npm run typecheck`), and full test suite must be green before commit.
|
||||
|
||||
## 9. File-level work surface (informative — drives the implementation plan)
|
||||
|
||||
- `prisma/schema.prisma` — add models, enums, back-references; new migration.
|
||||
- `src/server/routers/lunch.ts` (new) — router as designed.
|
||||
- `src/server/routers/_app.ts` — mount `lunch` router.
|
||||
- `src/server/services/lunch-pick-sync.ts` (new) — `ensureLunchPickForAttendingMember` helper called from existing attendee-creation paths.
|
||||
- `src/server/services/lunch-recap.ts` (new) — manifest aggregation + email body builder, used by `sendRecap` and the recap cron.
|
||||
- `src/lib/email.ts` — append two new template functions (reminder + recap).
|
||||
- `src/app/api/cron/lunch-reminders/route.ts` (new).
|
||||
- `src/app/api/cron/lunch-recap/route.ts` (new).
|
||||
- `src/app/(admin)/admin/logistics/page.tsx` — un-disable the Lunch tab trigger; mount new tab content.
|
||||
- `src/components/admin/logistics/lunch-tab.tsx` (new) — orchestrates the five cards.
|
||||
- `src/components/admin/logistics/lunch-event-config.tsx` (new) — config card.
|
||||
- `src/components/admin/logistics/lunch-dishes.tsx` (new) — dishes card.
|
||||
- `src/components/admin/logistics/lunch-manifest.tsx` (new) — manifest card.
|
||||
- `src/components/admin/logistics/lunch-externals.tsx` (new) — externals card.
|
||||
- `src/components/admin/logistics/lunch-recap-actions.tsx` (new) — recap actions card.
|
||||
- `src/components/applicant/attending-members-card.tsx` — extend each row with the lunch subsection.
|
||||
- `src/components/applicant/lunch-banner.tsx` (new) — the dashboard banner above the attending-members card.
|
||||
- `src/components/admin/settings/edition-settings-tab.tsx` — drop the Lunch line from the "Coming soon" card.
|
||||
|
||||
## 10. Non-goals reminder
|
||||
|
||||
- No keyboard shortcuts anywhere — visible UI affordances only (per existing user feedback memory).
|
||||
- No editable email templates in this PR (PR 7).
|
||||
- No public token-gated picker.
|
||||
- No multi-event support.
|
||||
- No caterer email integration.
|
||||
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "mopc-platform",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "mopc-platform",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.78.0",
|
||||
"@auth/prisma-adapter": "^2.7.4",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "mopc-platform",
|
||||
"version": "0.1.0",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "User" ADD COLUMN "mentorOnboardingSentAt" TIMESTAMP(3);
|
||||
@@ -0,0 +1,119 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "WaitlistEntryStatus" AS ENUM ('WAITING', 'PROMOTED', 'USED');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "FinalistConfirmationStatus" AS ENUM ('PENDING', 'CONFIRMED', 'DECLINED', 'EXPIRED', 'SUPERSEDED');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Program" ADD COLUMN "defaultAttendeeCap" INTEGER NOT NULL DEFAULT 3;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "FinalistSlotQuota" (
|
||||
"id" TEXT NOT NULL,
|
||||
"programId" TEXT NOT NULL,
|
||||
"category" "CompetitionCategory" NOT NULL,
|
||||
"quota" INTEGER NOT NULL,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "FinalistSlotQuota_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "WaitlistEntry" (
|
||||
"id" TEXT NOT NULL,
|
||||
"programId" TEXT NOT NULL,
|
||||
"projectId" TEXT NOT NULL,
|
||||
"category" "CompetitionCategory" NOT NULL,
|
||||
"rank" INTEGER NOT NULL,
|
||||
"status" "WaitlistEntryStatus" NOT NULL DEFAULT 'WAITING',
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "WaitlistEntry_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "FinalistConfirmation" (
|
||||
"id" TEXT NOT NULL,
|
||||
"projectId" TEXT NOT NULL,
|
||||
"category" "CompetitionCategory" NOT NULL,
|
||||
"status" "FinalistConfirmationStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"deadline" TIMESTAMP(3) NOT NULL,
|
||||
"token" TEXT NOT NULL,
|
||||
"confirmedAt" TIMESTAMP(3),
|
||||
"declinedAt" TIMESTAMP(3),
|
||||
"declineReason" TEXT,
|
||||
"expiredAt" TIMESTAMP(3),
|
||||
"promotedFromWaitlistEntryId" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "FinalistConfirmation_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "AttendingMember" (
|
||||
"id" TEXT NOT NULL,
|
||||
"confirmationId" TEXT NOT NULL,
|
||||
"userId" TEXT NOT NULL,
|
||||
"needsVisa" BOOLEAN NOT NULL DEFAULT false,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "AttendingMember_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "FinalistSlotQuota_programId_idx" ON "FinalistSlotQuota"("programId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "FinalistSlotQuota_programId_category_key" ON "FinalistSlotQuota"("programId", "category");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "WaitlistEntry_projectId_key" ON "WaitlistEntry"("projectId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "WaitlistEntry_programId_category_status_idx" ON "WaitlistEntry"("programId", "category", "status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "WaitlistEntry_programId_category_rank_key" ON "WaitlistEntry"("programId", "category", "rank");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "FinalistConfirmation_projectId_key" ON "FinalistConfirmation"("projectId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "FinalistConfirmation_token_key" ON "FinalistConfirmation"("token");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "FinalistConfirmation_promotedFromWaitlistEntryId_key" ON "FinalistConfirmation"("promotedFromWaitlistEntryId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "FinalistConfirmation_status_deadline_idx" ON "FinalistConfirmation"("status", "deadline");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "FinalistConfirmation_category_status_idx" ON "FinalistConfirmation"("category", "status");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "AttendingMember_userId_idx" ON "AttendingMember"("userId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "AttendingMember_confirmationId_userId_key" ON "AttendingMember"("confirmationId", "userId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "FinalistSlotQuota" ADD CONSTRAINT "FinalistSlotQuota_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "WaitlistEntry" ADD CONSTRAINT "WaitlistEntry_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "WaitlistEntry" ADD CONSTRAINT "WaitlistEntry_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "FinalistConfirmation" ADD CONSTRAINT "FinalistConfirmation_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "AttendingMember" ADD CONSTRAINT "AttendingMember_confirmationId_fkey" FOREIGN KEY ("confirmationId") REFERENCES "FinalistConfirmation"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "AttendingMember" ADD CONSTRAINT "AttendingMember_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,49 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "FlightDetailStatus" AS ENUM ('PENDING', 'CONFIRMED');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Hotel" (
|
||||
"id" TEXT NOT NULL,
|
||||
"programId" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"address" TEXT,
|
||||
"link" TEXT,
|
||||
"notes" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Hotel_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "FlightDetail" (
|
||||
"id" TEXT NOT NULL,
|
||||
"attendingMemberId" TEXT NOT NULL,
|
||||
"arrivalAt" TIMESTAMP(3),
|
||||
"arrivalFlightNumber" TEXT,
|
||||
"arrivalAirport" TEXT,
|
||||
"departureAt" TIMESTAMP(3),
|
||||
"departureFlightNumber" TEXT,
|
||||
"departureAirport" TEXT,
|
||||
"status" "FlightDetailStatus" NOT NULL DEFAULT 'PENDING',
|
||||
"adminNotes" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "FlightDetail_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "Hotel_programId_key" ON "Hotel"("programId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "FlightDetail_attendingMemberId_key" ON "FlightDetail"("attendingMemberId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "FlightDetail_status_idx" ON "FlightDetail"("status");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Hotel" ADD CONSTRAINT "Hotel_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "FlightDetail" ADD CONSTRAINT "FlightDetail_attendingMemberId_fkey" FOREIGN KEY ("attendingMemberId") REFERENCES "AttendingMember"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,4 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "MentorAssignment" ADD COLUMN "droppedAt" TIMESTAMP(3),
|
||||
ADD COLUMN "droppedBy" TEXT,
|
||||
ADD COLUMN "droppedReason" TEXT;
|
||||
@@ -0,0 +1,30 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "VisaStatus" AS ENUM ('NOT_NEEDED', 'REQUESTED', 'INVITATION_SENT', 'APPOINTMENT_BOOKED', 'GRANTED', 'DENIED');
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Program" ADD COLUMN "visaStatusVisibleToMembers" BOOLEAN NOT NULL DEFAULT true;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "VisaApplication" (
|
||||
"id" TEXT NOT NULL,
|
||||
"attendingMemberId" TEXT NOT NULL,
|
||||
"status" "VisaStatus" NOT NULL DEFAULT 'REQUESTED',
|
||||
"nationality" TEXT,
|
||||
"invitationSentAt" TIMESTAMP(3),
|
||||
"appointmentAt" TIMESTAMP(3),
|
||||
"decisionAt" TIMESTAMP(3),
|
||||
"notes" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "VisaApplication_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "VisaApplication_attendingMemberId_key" ON "VisaApplication"("attendingMemberId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "VisaApplication_status_idx" ON "VisaApplication"("status");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "VisaApplication" ADD CONSTRAINT "VisaApplication_attendingMemberId_fkey" FOREIGN KEY ("attendingMemberId") REFERENCES "AttendingMember"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
109
prisma/migrations/20260429002325_add_lunch_event/migration.sql
Normal file
109
prisma/migrations/20260429002325_add_lunch_event/migration.sql
Normal file
@@ -0,0 +1,109 @@
|
||||
-- CreateEnum
|
||||
CREATE TYPE "DietaryTag" AS ENUM ('VEGETARIAN', 'VEGAN', 'GLUTEN_FREE', 'PESCATARIAN');
|
||||
|
||||
-- CreateEnum
|
||||
CREATE TYPE "Allergen" AS ENUM ('GLUTEN', 'CRUSTACEANS', 'EGGS', 'FISH', 'PEANUTS', 'SOYBEANS', 'MILK', 'TREE_NUTS', 'CELERY', 'MUSTARD', 'SESAME', 'SULPHITES', 'LUPIN', 'MOLLUSCS');
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "LunchEvent" (
|
||||
"id" TEXT NOT NULL,
|
||||
"programId" TEXT NOT NULL,
|
||||
"enabled" BOOLEAN NOT NULL DEFAULT false,
|
||||
"eventAt" TIMESTAMP(3),
|
||||
"endAt" TIMESTAMP(3),
|
||||
"venue" TEXT,
|
||||
"notes" TEXT,
|
||||
"changeCutoffHours" INTEGER NOT NULL DEFAULT 48,
|
||||
"reminderHoursBeforeDeadline" INTEGER,
|
||||
"cronEnabled" BOOLEAN NOT NULL DEFAULT true,
|
||||
"extraRecipients" TEXT[] DEFAULT ARRAY[]::TEXT[],
|
||||
"reminderSentAt" TIMESTAMP(3),
|
||||
"recapSentAt" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "LunchEvent_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Dish" (
|
||||
"id" TEXT NOT NULL,
|
||||
"lunchEventId" TEXT NOT NULL,
|
||||
"name" TEXT NOT NULL,
|
||||
"sortOrder" INTEGER NOT NULL DEFAULT 0,
|
||||
"dietaryTags" "DietaryTag"[],
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "Dish_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "MemberLunchPick" (
|
||||
"id" TEXT NOT NULL,
|
||||
"attendingMemberId" TEXT NOT NULL,
|
||||
"dishId" TEXT,
|
||||
"allergens" "Allergen"[] DEFAULT ARRAY[]::"Allergen"[],
|
||||
"allergenOther" TEXT,
|
||||
"pickedAt" TIMESTAMP(3),
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "MemberLunchPick_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "ExternalAttendee" (
|
||||
"id" TEXT NOT NULL,
|
||||
"lunchEventId" TEXT NOT NULL,
|
||||
"projectId" TEXT,
|
||||
"name" TEXT NOT NULL,
|
||||
"email" TEXT,
|
||||
"roleNote" TEXT,
|
||||
"dishId" TEXT,
|
||||
"allergens" "Allergen"[] DEFAULT ARRAY[]::"Allergen"[],
|
||||
"allergenOther" TEXT,
|
||||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||
|
||||
CONSTRAINT "ExternalAttendee_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "LunchEvent_programId_key" ON "LunchEvent"("programId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Dish_lunchEventId_idx" ON "Dish"("lunchEventId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX "MemberLunchPick_attendingMemberId_key" ON "MemberLunchPick"("attendingMemberId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "MemberLunchPick_dishId_idx" ON "MemberLunchPick"("dishId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ExternalAttendee_lunchEventId_idx" ON "ExternalAttendee"("lunchEventId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "ExternalAttendee_projectId_idx" ON "ExternalAttendee"("projectId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "LunchEvent" ADD CONSTRAINT "LunchEvent_programId_fkey" FOREIGN KEY ("programId") REFERENCES "Program"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Dish" ADD CONSTRAINT "Dish_lunchEventId_fkey" FOREIGN KEY ("lunchEventId") REFERENCES "LunchEvent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "MemberLunchPick" ADD CONSTRAINT "MemberLunchPick_attendingMemberId_fkey" FOREIGN KEY ("attendingMemberId") REFERENCES "AttendingMember"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "MemberLunchPick" ADD CONSTRAINT "MemberLunchPick_dishId_fkey" FOREIGN KEY ("dishId") REFERENCES "Dish"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ExternalAttendee" ADD CONSTRAINT "ExternalAttendee_lunchEventId_fkey" FOREIGN KEY ("lunchEventId") REFERENCES "LunchEvent"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ExternalAttendee" ADD CONSTRAINT "ExternalAttendee_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "ExternalAttendee" ADD CONSTRAINT "ExternalAttendee_dishId_fkey" FOREIGN KEY ("dishId") REFERENCES "Dish"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -302,6 +302,9 @@ model User {
|
||||
institution String? // User's institution/organization
|
||||
metadataJson Json? @db.JsonB
|
||||
|
||||
// Mentor onboarding email idempotency: stamped once when MENTOR role is first added.
|
||||
mentorOnboardingSentAt DateTime?
|
||||
|
||||
// Profile
|
||||
bio String? // User bio for matching with project descriptions
|
||||
profileImageKey String? // Storage key (e.g., "avatars/user123/1234567890.jpg")
|
||||
@@ -423,6 +426,9 @@ model User {
|
||||
// AI Ranking
|
||||
rankingSnapshots RankingSnapshot[] @relation("TriggeredRankingSnapshots")
|
||||
|
||||
// Grand-finale logistics
|
||||
finalistAttendances AttendingMember[]
|
||||
|
||||
@@index([role])
|
||||
@@index([status])
|
||||
}
|
||||
@@ -480,6 +486,10 @@ model Program {
|
||||
description String?
|
||||
settingsJson Json? @db.JsonB
|
||||
|
||||
// Grand-finale logistics
|
||||
defaultAttendeeCap Int @default(3) // Max team members allowed at the grand finale per finalist team
|
||||
visaStatusVisibleToMembers Boolean @default(true) // Whether team members see their own visa status on the applicant dashboard
|
||||
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@ -493,6 +503,12 @@ model Program {
|
||||
mentorMilestones MentorMilestone[]
|
||||
competitions Competition[]
|
||||
|
||||
// Grand-finale logistics
|
||||
finalistSlotQuotas FinalistSlotQuota[]
|
||||
waitlistEntries WaitlistEntry[]
|
||||
hotel Hotel?
|
||||
lunchEvent LunchEvent?
|
||||
|
||||
@@unique([name, year])
|
||||
@@index([status])
|
||||
}
|
||||
@@ -634,6 +650,11 @@ model Project {
|
||||
submissionPromotions SubmissionPromotionEvent[]
|
||||
notificationLogs NotificationLog[]
|
||||
|
||||
// Grand-finale logistics
|
||||
waitlistEntry WaitlistEntry?
|
||||
finalistConfirmation FinalistConfirmation?
|
||||
externalLunchAttendees ExternalAttendee[]
|
||||
|
||||
@@index([programId])
|
||||
@@index([status])
|
||||
@@index([tags])
|
||||
@@ -1267,6 +1288,11 @@ model MentorAssignment {
|
||||
completionStatus String @default("in_progress") // 'in_progress' | 'completed' | 'paused'
|
||||
lastViewedAt DateTime?
|
||||
|
||||
// Drop tracking — null while assignment is active
|
||||
droppedAt DateTime?
|
||||
droppedReason String? @db.Text
|
||||
droppedBy String? // 'mentor' | 'admin' | 'finalist_unconfirmed'
|
||||
|
||||
// ── Competition/Round architecture — workspace activation ──
|
||||
workspaceEnabled Boolean @default(false)
|
||||
workspaceOpenAt DateTime?
|
||||
@@ -2623,3 +2649,273 @@ model ResultUnlockEvent {
|
||||
@@index([resultLockId])
|
||||
@@index([unlockedById])
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Grand-finale logistics (PR 1: finalist confirmation flow)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
enum WaitlistEntryStatus {
|
||||
WAITING // available for promotion
|
||||
PROMOTED // moved into a finalist slot
|
||||
USED // promoted and confirmation flow completed (declined or accepted)
|
||||
}
|
||||
|
||||
enum FinalistConfirmationStatus {
|
||||
PENDING // sent, awaiting team response
|
||||
CONFIRMED // team accepted, attendees selected
|
||||
DECLINED // team explicitly declined
|
||||
EXPIRED // deadline passed without response
|
||||
SUPERSEDED // admin manually overrode (e.g. unconfirmed to allow quota decrease)
|
||||
}
|
||||
|
||||
model FinalistSlotQuota {
|
||||
id String @id @default(cuid())
|
||||
programId String
|
||||
category CompetitionCategory
|
||||
quota Int
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([programId, category])
|
||||
@@index([programId])
|
||||
}
|
||||
|
||||
model WaitlistEntry {
|
||||
id String @id @default(cuid())
|
||||
programId String
|
||||
projectId String @unique
|
||||
category CompetitionCategory
|
||||
rank Int
|
||||
status WaitlistEntryStatus @default(WAITING)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@unique([programId, category, rank])
|
||||
@@index([programId, category, status])
|
||||
}
|
||||
|
||||
model FinalistConfirmation {
|
||||
id String @id @default(cuid())
|
||||
projectId String @unique
|
||||
category CompetitionCategory
|
||||
status FinalistConfirmationStatus @default(PENDING)
|
||||
deadline DateTime
|
||||
token String @unique
|
||||
confirmedAt DateTime?
|
||||
declinedAt DateTime?
|
||||
declineReason String? // optional free-text on decline
|
||||
expiredAt DateTime?
|
||||
promotedFromWaitlistEntryId String? @unique // null for original finalists
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
|
||||
attendingMembers AttendingMember[]
|
||||
|
||||
@@index([status, deadline]) // for cron scan
|
||||
@@index([category, status])
|
||||
}
|
||||
|
||||
model AttendingMember {
|
||||
id String @id @default(cuid())
|
||||
confirmationId String
|
||||
userId String // must be a TeamMember of the same project (validated at write time)
|
||||
needsVisa Boolean @default(false)
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
confirmation FinalistConfirmation @relation(fields: [confirmationId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
flightDetail FlightDetail?
|
||||
visaApplication VisaApplication?
|
||||
lunchPick MemberLunchPick?
|
||||
|
||||
@@unique([confirmationId, userId])
|
||||
@@index([userId])
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Grand-finale logistics (PR 2: hotels + flight tracking)
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
enum FlightDetailStatus {
|
||||
PENDING // team submitted details, admin not yet reviewed
|
||||
CONFIRMED // admin verified booking
|
||||
}
|
||||
|
||||
model Hotel {
|
||||
id String @id @default(cuid())
|
||||
programId String @unique // 1:1 — one hotel per edition
|
||||
name String
|
||||
address String? @db.Text
|
||||
link String? // external URL to hotel page / booking confirmation
|
||||
notes String? @db.Text
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
}
|
||||
|
||||
model FlightDetail {
|
||||
id String @id @default(cuid())
|
||||
attendingMemberId String @unique // 1:1
|
||||
arrivalAt DateTime?
|
||||
arrivalFlightNumber String?
|
||||
arrivalAirport String?
|
||||
departureAt DateTime?
|
||||
departureFlightNumber String?
|
||||
departureAirport String?
|
||||
status FlightDetailStatus @default(PENDING)
|
||||
adminNotes String? @db.Text
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
attendingMember AttendingMember @relation(fields: [attendingMemberId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([status])
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Grand-finale visa tracking (PR 4)
|
||||
// Process metadata only — no document storage. Passport scans / invitation
|
||||
// letters / decision documents are exchanged over email; this model just
|
||||
// records what stage the application is at, key dates, and free-text notes.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
enum VisaStatus {
|
||||
NOT_NEEDED
|
||||
REQUESTED
|
||||
INVITATION_SENT
|
||||
APPOINTMENT_BOOKED
|
||||
GRANTED
|
||||
DENIED
|
||||
}
|
||||
|
||||
model VisaApplication {
|
||||
id String @id @default(cuid())
|
||||
attendingMemberId String @unique // 1:1
|
||||
status VisaStatus @default(REQUESTED)
|
||||
nationality String? // self-declared, optional
|
||||
invitationSentAt DateTime?
|
||||
appointmentAt DateTime?
|
||||
decisionAt DateTime? // GRANTED or DENIED date
|
||||
notes String? @db.Text
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
attendingMember AttendingMember @relation(fields: [attendingMemberId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([status])
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Grand-finale lunch event (PR 6)
|
||||
// Single configurable lunch event per edition. Each attending member has a
|
||||
// 1:1 MemberLunchPick (auto-created via lunch-pick-sync). External attendees
|
||||
// can be standalone or attached to a finalist project. Allergens use the
|
||||
// EU 14 regulated list; dishes carry dietary tags.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
enum DietaryTag {
|
||||
VEGETARIAN
|
||||
VEGAN
|
||||
GLUTEN_FREE
|
||||
PESCATARIAN
|
||||
}
|
||||
|
||||
enum Allergen {
|
||||
GLUTEN
|
||||
CRUSTACEANS
|
||||
EGGS
|
||||
FISH
|
||||
PEANUTS
|
||||
SOYBEANS
|
||||
MILK
|
||||
TREE_NUTS
|
||||
CELERY
|
||||
MUSTARD
|
||||
SESAME
|
||||
SULPHITES
|
||||
LUPIN
|
||||
MOLLUSCS
|
||||
}
|
||||
|
||||
model LunchEvent {
|
||||
id String @id @default(cuid())
|
||||
programId String @unique
|
||||
enabled Boolean @default(false)
|
||||
eventAt DateTime?
|
||||
endAt DateTime?
|
||||
venue String?
|
||||
notes String? @db.Text
|
||||
changeCutoffHours Int @default(48)
|
||||
reminderHoursBeforeDeadline Int?
|
||||
cronEnabled Boolean @default(true)
|
||||
extraRecipients String[] @default([])
|
||||
reminderSentAt DateTime?
|
||||
recapSentAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
program Program @relation(fields: [programId], references: [id], onDelete: Cascade)
|
||||
dishes Dish[]
|
||||
externalAttendees ExternalAttendee[]
|
||||
}
|
||||
|
||||
model Dish {
|
||||
id String @id @default(cuid())
|
||||
lunchEventId String
|
||||
name String
|
||||
sortOrder Int @default(0)
|
||||
dietaryTags DietaryTag[]
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
lunchEvent LunchEvent @relation(fields: [lunchEventId], references: [id], onDelete: Cascade)
|
||||
memberPicks MemberLunchPick[]
|
||||
externals ExternalAttendee[]
|
||||
|
||||
@@index([lunchEventId])
|
||||
}
|
||||
|
||||
model MemberLunchPick {
|
||||
id String @id @default(cuid())
|
||||
attendingMemberId String @unique
|
||||
dishId String?
|
||||
allergens Allergen[] @default([])
|
||||
allergenOther String?
|
||||
pickedAt DateTime?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
attendingMember AttendingMember @relation(fields: [attendingMemberId], references: [id], onDelete: Cascade)
|
||||
dish Dish? @relation(fields: [dishId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([dishId])
|
||||
}
|
||||
|
||||
model ExternalAttendee {
|
||||
id String @id @default(cuid())
|
||||
lunchEventId String
|
||||
projectId String?
|
||||
name String
|
||||
email String?
|
||||
roleNote String?
|
||||
dishId String?
|
||||
allergens Allergen[] @default([])
|
||||
allergenOther String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
lunchEvent LunchEvent @relation(fields: [lunchEventId], references: [id], onDelete: Cascade)
|
||||
project Project? @relation(fields: [projectId], references: [id], onDelete: SetNull)
|
||||
dish Dish? @relation(fields: [dishId], references: [id], onDelete: SetNull)
|
||||
|
||||
@@index([lunchEventId])
|
||||
@@index([projectId])
|
||||
}
|
||||
|
||||
101
scripts/cleanup-test-pollution.ts
Normal file
101
scripts/cleanup-test-pollution.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* One-shot: remove leaked test data from dev DB.
|
||||
*
|
||||
* Test runs that crashed before reaching `afterAll` left orphan test users +
|
||||
* programs. This mirrors `tests/helpers.ts#cleanupTestData` with the same
|
||||
* reverse-dependency order, applied to all programs whose name matches the
|
||||
* test patterns.
|
||||
*
|
||||
* Run: npx tsx scripts/cleanup-test-pollution.ts
|
||||
*/
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
const TEST_PROGRAM_PATTERNS = [
|
||||
'Test Program prog-%',
|
||||
'getCandidates-%',
|
||||
'bulk-%',
|
||||
'source-flag-%',
|
||||
'mentor-files-%',
|
||||
'mentor-config-%',
|
||||
]
|
||||
|
||||
async function main() {
|
||||
const programs = await prisma.program.findMany({
|
||||
where: {
|
||||
OR: TEST_PROGRAM_PATTERNS.map((p) => ({ name: { startsWith: p.replace('%', '') } })),
|
||||
},
|
||||
select: { id: true, name: true },
|
||||
})
|
||||
|
||||
console.log(`Found ${programs.length} test programs:`)
|
||||
programs.forEach((p) => console.log(` - ${p.id} ${p.name}`))
|
||||
|
||||
for (const program of programs) {
|
||||
const programId = program.id
|
||||
console.log(`\nCleaning ${program.name}...`)
|
||||
|
||||
// MentorAssignment isn't in cleanupTestData — kill it first
|
||||
const ma = await prisma.mentorAssignment.deleteMany({
|
||||
where: { project: { programId } },
|
||||
})
|
||||
if (ma.count > 0) console.log(` ${ma.count} MentorAssignment`)
|
||||
|
||||
// Mirror tests/helpers.ts#cleanupTestData order
|
||||
await prisma.cohortProject.deleteMany({ where: { cohort: { round: { competition: { programId } } } } })
|
||||
await prisma.cohort.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.liveProgressCursor.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.filteringResult.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.filteringRule.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.filteringJob.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.assignmentJob.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.conflictOfInterest.deleteMany({ where: { assignment: { round: { competition: { programId } } } } })
|
||||
await prisma.evaluation.deleteMany({ where: { assignment: { round: { competition: { programId } } } } })
|
||||
await prisma.assignment.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.evaluationForm.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.fileRequirement.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.gracePeriod.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.reminderLog.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.evaluationSummary.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.evaluationDiscussion.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.projectRoundState.deleteMany({ where: { round: { competition: { programId } } } })
|
||||
await prisma.awardEligibility.deleteMany({ where: { award: { program: { id: programId } } } })
|
||||
await prisma.awardVote.deleteMany({ where: { award: { program: { id: programId } } } })
|
||||
await prisma.awardJuror.deleteMany({ where: { award: { program: { id: programId } } } })
|
||||
await prisma.specialAward.deleteMany({ where: { programId } })
|
||||
await prisma.round.deleteMany({ where: { competition: { programId } } })
|
||||
await prisma.competition.deleteMany({ where: { programId } })
|
||||
await prisma.projectStatusHistory.deleteMany({ where: { project: { programId } } })
|
||||
await prisma.projectFile.deleteMany({ where: { project: { programId } } })
|
||||
await prisma.projectTag.deleteMany({ where: { project: { programId } } })
|
||||
await prisma.project.deleteMany({ where: { programId } })
|
||||
await prisma.program.deleteMany({ where: { id: programId } })
|
||||
console.log(' cascade complete')
|
||||
}
|
||||
|
||||
// Delete test users (@test.local). Catch any audit-log refs first.
|
||||
const testUsers = await prisma.user.findMany({
|
||||
where: { email: { endsWith: '@test.local' } },
|
||||
select: { id: true },
|
||||
})
|
||||
const testUserIds = testUsers.map((u) => u.id)
|
||||
console.log(`\nDeleting ${testUserIds.length} @test.local users...`)
|
||||
if (testUserIds.length > 0) {
|
||||
await prisma.decisionAuditLog.deleteMany({ where: { actorId: { in: testUserIds } } })
|
||||
await prisma.auditLog.deleteMany({ where: { userId: { in: testUserIds } } })
|
||||
// Any remaining MentorAssignments referencing these users (e.g., from other tests)
|
||||
await prisma.mentorAssignment.deleteMany({ where: { mentorId: { in: testUserIds } } })
|
||||
await prisma.user.deleteMany({ where: { id: { in: testUserIds } } })
|
||||
}
|
||||
|
||||
console.log('\nDone.')
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => prisma.$disconnect())
|
||||
.catch(async (e) => {
|
||||
console.error(e)
|
||||
await prisma.$disconnect()
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -513,6 +513,13 @@ export default function AwardDetailPage({
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
const notifyJurors = trpc.specialAward.notifyJurors.useMutation({
|
||||
onSuccess: (data) => {
|
||||
const failedNote = data.failed > 0 ? ` (${data.failed} failed)` : ''
|
||||
toast.success(`Reminder sent to ${data.sent} of ${data.targeted} juror(s)${failedNote}`)
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
const setWinner = trpc.specialAward.setWinner.useMutation({
|
||||
onSuccess: invalidateAward,
|
||||
})
|
||||
@@ -1335,7 +1342,7 @@ export default function AwardDetailPage({
|
||||
|
||||
{/* Jurors Tab */}
|
||||
<TabsContent value="jurors" className="space-y-4">
|
||||
<div className="flex gap-2">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<Select value={selectedJurorId} onValueChange={setSelectedJurorId}>
|
||||
<SelectTrigger className="w-64">
|
||||
<SelectValue placeholder="Select a juror..." />
|
||||
@@ -1355,6 +1362,19 @@ export default function AwardDetailPage({
|
||||
<UserPlus className="mr-2 h-4 w-4" />
|
||||
Add Juror
|
||||
</Button>
|
||||
{jurors && jurors.length > 0 && (
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => notifyJurors.mutate({ awardId })}
|
||||
disabled={notifyJurors.isPending}
|
||||
className="ml-auto"
|
||||
>
|
||||
<Mail className="mr-2 h-4 w-4" />
|
||||
{notifyJurors.isPending
|
||||
? 'Sending...'
|
||||
: `Send reminder to all (${jurors.length})`}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Import from Jury Group */}
|
||||
@@ -1549,11 +1569,23 @@ export default function AwardDetailPage({
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
notifyJurors.mutate({ awardId, userIds: [j.userId] })
|
||||
}
|
||||
disabled={notifyJurors.isPending}
|
||||
title="Send reminder email"
|
||||
>
|
||||
<Mail className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => handleRemoveJuror(j.userId)}
|
||||
disabled={removeJuror.isPending}
|
||||
title="Remove juror"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
83
src/app/(admin)/admin/logistics/page.tsx
Normal file
83
src/app/(admin)/admin/logistics/page.tsx
Normal file
@@ -0,0 +1,83 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useEdition } from '@/contexts/edition-context'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import {
|
||||
CheckCircle2,
|
||||
Hotel as HotelIcon,
|
||||
Plane,
|
||||
Salad,
|
||||
ScrollText,
|
||||
Stamp,
|
||||
} from 'lucide-react'
|
||||
import { ConfirmationsTab } from '@/components/admin/logistics/confirmations-tab'
|
||||
import { TravelTab } from '@/components/admin/logistics/travel-tab'
|
||||
import { HotelsTab } from '@/components/admin/logistics/hotels-tab'
|
||||
import { VisasTab } from '@/components/admin/logistics/visas-tab'
|
||||
import { LunchTab } from '@/components/admin/logistics/lunch-tab'
|
||||
|
||||
export default function LogisticsPage() {
|
||||
const { currentEdition } = useEdition()
|
||||
const [tab, setTab] = useState('confirmations')
|
||||
|
||||
if (!currentEdition) {
|
||||
return (
|
||||
<p className="text-muted-foreground py-12 text-center text-sm">
|
||||
Select an edition to view logistics.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
const programId = currentEdition.id
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Logistics</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Operational hub for the grand finale: confirmations, travel, hotels, and more.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Tabs value={tab} onValueChange={setTab} className="space-y-6">
|
||||
<TabsList className="h-auto w-full justify-start overflow-x-auto pb-2">
|
||||
<TabsTrigger value="confirmations">
|
||||
<CheckCircle2 className="mr-2 h-4 w-4" /> Confirmations
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="travel">
|
||||
<Plane className="mr-2 h-4 w-4" /> Travel
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="hotels">
|
||||
<HotelIcon className="mr-2 h-4 w-4" /> Hotels
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="visas">
|
||||
<Stamp className="mr-2 h-4 w-4" /> Visas
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="lunch">
|
||||
<Salad className="mr-2 h-4 w-4" /> Lunch
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="email-templates" disabled>
|
||||
<ScrollText className="mr-2 h-4 w-4" /> Email Templates
|
||||
<span className="text-muted-foreground ml-1 text-xs">(soon)</span>
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="confirmations">
|
||||
<ConfirmationsTab programId={programId} />
|
||||
</TabsContent>
|
||||
<TabsContent value="travel">
|
||||
<TravelTab programId={programId} />
|
||||
</TabsContent>
|
||||
<TabsContent value="hotels">
|
||||
<HotelsTab programId={programId} />
|
||||
</TabsContent>
|
||||
<TabsContent value="visas">
|
||||
<VisasTab programId={programId} />
|
||||
</TabsContent>
|
||||
<TabsContent value="lunch">
|
||||
<LunchTab programId={programId} />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,470 @@
|
||||
import { redirect } from 'next/navigation'
|
||||
'use client'
|
||||
|
||||
export default function MentorsPage() {
|
||||
redirect('/admin/members')
|
||||
import { useMemo, useState } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { ArrowUpDown, GraduationCap, Search, Users } from 'lucide-react'
|
||||
import { formatEnumLabel } from '@/lib/utils'
|
||||
import { MentorDetailSheet } from '@/components/admin/mentor/mentor-detail-sheet'
|
||||
|
||||
type SortKey = 'name' | 'load' | 'capacity' | 'lastActivity'
|
||||
|
||||
function formatRelativePast(date: Date | string | null): string {
|
||||
if (!date) return '—'
|
||||
const d = typeof date === 'string' ? new Date(date) : date
|
||||
const ms = Date.now() - d.getTime()
|
||||
const days = Math.floor(ms / 86_400_000)
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
if (days >= 1) return `${days}d ago`
|
||||
if (hours >= 1) return `${hours}h ago`
|
||||
const minutes = Math.floor(ms / 60_000)
|
||||
return `${Math.max(0, minutes)}m ago`
|
||||
}
|
||||
|
||||
const STATUS_BADGE: Record<
|
||||
'unassigned' | 'assigned' | 'active' | 'stalled',
|
||||
{ label: string; variant: 'default' | 'secondary' | 'destructive' | 'outline' }
|
||||
> = {
|
||||
unassigned: { label: 'Unassigned', variant: 'outline' },
|
||||
assigned: { label: 'Assigned', variant: 'secondary' },
|
||||
active: { label: 'Active', variant: 'default' },
|
||||
stalled: { label: 'Stalled', variant: 'destructive' },
|
||||
}
|
||||
|
||||
type Mentor = {
|
||||
id: string
|
||||
name: string | null
|
||||
email: string
|
||||
country: string | null
|
||||
expertiseTags: string[]
|
||||
currentAssignments: number
|
||||
completedAssignments: number
|
||||
maxAssignments: number | null
|
||||
capacityRemaining: number | null
|
||||
lastActivityAt: Date | string | null
|
||||
activeTeams: { id: string; title: string }[]
|
||||
}
|
||||
|
||||
function MentorListPanel() {
|
||||
const [search, setSearch] = useState('')
|
||||
const [sortKey, setSortKey] = useState<SortKey>('load')
|
||||
const [sortDir, setSortDir] = useState<'asc' | 'desc'>('asc')
|
||||
const [detailMentorId, setDetailMentorId] = useState<string | null>(null)
|
||||
|
||||
const { data, isLoading } = trpc.mentor.getMentorPool.useQuery({})
|
||||
|
||||
const filtered = useMemo<Mentor[]>(() => {
|
||||
if (!data) return []
|
||||
const q = search.trim().toLowerCase()
|
||||
let rows: Mentor[] = data.mentors
|
||||
if (q) {
|
||||
rows = rows.filter((m) =>
|
||||
[m.name ?? '', m.email, m.country ?? '', ...m.expertiseTags]
|
||||
.join(' ')
|
||||
.toLowerCase()
|
||||
.includes(q),
|
||||
)
|
||||
}
|
||||
rows = [...rows].sort((a, b) => {
|
||||
let av: string | number = 0
|
||||
let bv: string | number = 0
|
||||
switch (sortKey) {
|
||||
case 'name':
|
||||
av = (a.name ?? '').toLowerCase()
|
||||
bv = (b.name ?? '').toLowerCase()
|
||||
break
|
||||
case 'load':
|
||||
av = a.currentAssignments
|
||||
bv = b.currentAssignments
|
||||
break
|
||||
case 'capacity':
|
||||
av = a.capacityRemaining ?? Number.MAX_SAFE_INTEGER
|
||||
bv = b.capacityRemaining ?? Number.MAX_SAFE_INTEGER
|
||||
break
|
||||
case 'lastActivity':
|
||||
av = a.lastActivityAt ? new Date(a.lastActivityAt).getTime() : 0
|
||||
bv = b.lastActivityAt ? new Date(b.lastActivityAt).getTime() : 0
|
||||
break
|
||||
}
|
||||
if (av < bv) return sortDir === 'asc' ? -1 : 1
|
||||
if (av > bv) return sortDir === 'asc' ? 1 : -1
|
||||
return 0
|
||||
})
|
||||
return rows
|
||||
}, [data, search, sortKey, sortDir])
|
||||
|
||||
const toggleSort = (key: SortKey) => {
|
||||
if (sortKey === key) setSortDir((d) => (d === 'asc' ? 'desc' : 'asc'))
|
||||
else {
|
||||
setSortKey(key)
|
||||
setSortDir(key === 'name' ? 'asc' : 'desc')
|
||||
}
|
||||
}
|
||||
|
||||
const SortHeader = ({
|
||||
k,
|
||||
children,
|
||||
align = 'left',
|
||||
}: {
|
||||
k: SortKey
|
||||
children: React.ReactNode
|
||||
align?: 'left' | 'right'
|
||||
}) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => toggleSort(k)}
|
||||
className={`flex items-center gap-1 text-sm font-medium ${align === 'right' ? 'ml-auto' : ''}`}
|
||||
>
|
||||
{children}
|
||||
<ArrowUpDown
|
||||
className={`h-3 w-3 ${sortKey === k ? 'text-foreground' : 'text-muted-foreground/50'}`}
|
||||
/>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader className="space-y-4">
|
||||
<CardTitle className="text-base">Mentor list</CardTitle>
|
||||
<div className="relative">
|
||||
<Search className="text-muted-foreground absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2" />
|
||||
<Input
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search by name, email, country, or expertise tag…"
|
||||
className="pl-9"
|
||||
/>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3, 4, 5].map((i) => (
|
||||
<Skeleton key={i} className="h-12 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||||
{search ? 'No matching mentors.' : 'No mentors yet.'}
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>
|
||||
<SortHeader k="name">Mentor</SortHeader>
|
||||
</TableHead>
|
||||
<TableHead>Expertise</TableHead>
|
||||
<TableHead>Country</TableHead>
|
||||
<TableHead className="text-right">
|
||||
<SortHeader k="capacity" align="right">
|
||||
Capacity
|
||||
</SortHeader>
|
||||
</TableHead>
|
||||
<TableHead>
|
||||
<SortHeader k="lastActivity">Last activity</SortHeader>
|
||||
</TableHead>
|
||||
<TableHead>
|
||||
<SortHeader k="load">Teams</SortHeader>
|
||||
</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filtered.map((m) => (
|
||||
<TableRow
|
||||
key={m.id}
|
||||
className="cursor-pointer"
|
||||
onClick={() => setDetailMentorId(m.id)}
|
||||
>
|
||||
<TableCell>
|
||||
<div className="font-medium">{m.name ?? 'Unnamed'}</div>
|
||||
<div className="text-muted-foreground text-xs">{m.email}</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{m.expertiseTags.slice(0, 4).map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
{m.expertiseTags.length > 4 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{m.expertiseTags.length - 4}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">{m.country ?? '—'}</TableCell>
|
||||
<TableCell className="text-right text-sm tabular-nums">
|
||||
{m.capacityRemaining != null ? m.capacityRemaining : '∞'}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
{formatRelativePast(m.lastActivityAt)}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{m.activeTeams.length === 0 ? (
|
||||
<span className="text-muted-foreground text-xs">—</span>
|
||||
) : (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{m.activeTeams.slice(0, 2).map((t) => (
|
||||
<Badge
|
||||
key={t.id}
|
||||
variant="outline"
|
||||
className="max-w-[12rem] truncate text-xs"
|
||||
title={t.title}
|
||||
>
|
||||
{t.title}
|
||||
</Badge>
|
||||
))}
|
||||
{m.activeTeams.length > 2 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{m.activeTeams.length - 2}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<MentorDetailSheet
|
||||
mentorId={detailMentorId}
|
||||
open={!!detailMentorId}
|
||||
onOpenChange={(next) => {
|
||||
if (!next) setDetailMentorId(null)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
type StatusFilter = 'all' | 'unassigned' | 'assigned' | 'active' | 'stalled'
|
||||
|
||||
function MenteeActivityPanel() {
|
||||
const [search, setSearch] = useState('')
|
||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all')
|
||||
|
||||
const { data, isLoading } = trpc.mentor.getMenteeActivity.useQuery({})
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!data) return []
|
||||
const q = search.trim().toLowerCase()
|
||||
return data.rows.filter((r) => {
|
||||
if (statusFilter !== 'all' && r.status !== statusFilter) return false
|
||||
if (!q) return true
|
||||
const hay = [
|
||||
r.project.title,
|
||||
r.project.country ?? '',
|
||||
r.teamLead?.name ?? '',
|
||||
r.teamLead?.email ?? '',
|
||||
r.mentor?.name ?? '',
|
||||
r.mentor?.email ?? '',
|
||||
]
|
||||
.join(' ')
|
||||
.toLowerCase()
|
||||
return hay.includes(q)
|
||||
})
|
||||
}, [data, search, statusFilter])
|
||||
|
||||
const totals = data?.totals ?? { unassigned: 0, assigned: 0, active: 0, stalled: 0 }
|
||||
|
||||
const StatusPill = ({ value, label, count }: { value: StatusFilter; label: string; count: number }) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStatusFilter(value)}
|
||||
className={`rounded-md border px-2.5 py-1 text-xs font-medium transition-colors ${
|
||||
statusFilter === value
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'bg-background hover:bg-muted'
|
||||
}`}
|
||||
>
|
||||
{label} <span className="tabular-nums opacity-80">({count})</span>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Card>
|
||||
<CardHeader className="space-y-4">
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<CardTitle className="text-base">Mentee teams</CardTitle>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
<StatusPill
|
||||
value="all"
|
||||
label="All"
|
||||
count={
|
||||
totals.unassigned + totals.assigned + totals.active + totals.stalled
|
||||
}
|
||||
/>
|
||||
<StatusPill value="unassigned" label="Unassigned" count={totals.unassigned} />
|
||||
<StatusPill value="assigned" label="Assigned" count={totals.assigned} />
|
||||
<StatusPill value="active" label="Active" count={totals.active} />
|
||||
<StatusPill value="stalled" label="Stalled" count={totals.stalled} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="relative">
|
||||
<Search className="text-muted-foreground absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2" />
|
||||
<Input
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search by project, team lead, or mentor…"
|
||||
className="pl-9"
|
||||
/>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3, 4, 5].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||||
{search || statusFilter !== 'all'
|
||||
? 'No matching teams.'
|
||||
: 'No teams have requested mentorship yet.'}
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Project</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Mentor</TableHead>
|
||||
<TableHead className="text-right">Messages</TableHead>
|
||||
<TableHead className="text-right">Files</TableHead>
|
||||
<TableHead>Last activity</TableHead>
|
||||
<TableHead></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filtered.map((r) => {
|
||||
const badge = STATUS_BADGE[r.status]
|
||||
return (
|
||||
<TableRow key={r.project.id}>
|
||||
<TableCell>
|
||||
<div className="font-medium">{r.project.title}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{r.teamLead?.name ?? r.teamLead?.email ?? '—'}
|
||||
{r.project.oceanIssue && (
|
||||
<>
|
||||
{' · '}
|
||||
{formatEnumLabel(r.project.oceanIssue)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={badge.variant} className="text-xs">
|
||||
{badge.label}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{r.mentor ? (
|
||||
<div className="text-sm">
|
||||
<div>{r.mentor.name ?? r.mentor.email}</div>
|
||||
<div className="text-muted-foreground text-xs tabular-nums">
|
||||
{r.mentor.currentLoad}
|
||||
{r.mentor.maxAssignments != null
|
||||
? `/${r.mentor.maxAssignments}`
|
||||
: ''}
|
||||
{' load'}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-muted-foreground text-sm">—</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">
|
||||
{r.messageCount}
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">{r.fileCount}</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
{formatRelativePast(r.lastActivityAt as unknown as Date | null)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button asChild size="sm" variant="outline">
|
||||
<Link href={`/admin/projects/${r.project.id}/mentor`}>
|
||||
{r.mentor ? 'Open' : 'Assign'}
|
||||
</Link>
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function MentorsListPage() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Mentors</h1>
|
||||
<p className="text-muted-foreground">
|
||||
Manage the mentor pool and track mentee teams across the program.
|
||||
</p>
|
||||
</div>
|
||||
<Button asChild variant="outline">
|
||||
<Link href="/admin/members">
|
||||
<Users className="mr-2 h-4 w-4" />
|
||||
Manage Members
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<Tabs defaultValue="mentors" className="space-y-6">
|
||||
<TabsList>
|
||||
<TabsTrigger value="mentors">
|
||||
<GraduationCap className="mr-2 h-4 w-4" /> Mentors
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="mentees">
|
||||
<Users className="mr-2 h-4 w-4" /> Mentees & Activity
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
<TabsContent value="mentors">
|
||||
<MentorListPanel />
|
||||
</TabsContent>
|
||||
<TabsContent value="mentees">
|
||||
<MenteeActivityPanel />
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,378 +1,550 @@
|
||||
'use client'
|
||||
|
||||
import { Suspense, use, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import {
|
||||
ArrowLeft,
|
||||
Bot,
|
||||
Loader2,
|
||||
Users,
|
||||
Check,
|
||||
RefreshCw,
|
||||
} from 'lucide-react'
|
||||
import { getInitials } from '@/lib/utils'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
}
|
||||
|
||||
// Type for mentor suggestion from the API
|
||||
interface MentorSuggestion {
|
||||
mentorId: string
|
||||
confidenceScore: number
|
||||
expertiseMatchScore: number
|
||||
reasoning: string
|
||||
mentor: {
|
||||
id: string
|
||||
name: string | null
|
||||
email: string
|
||||
expertiseTags: string[]
|
||||
assignmentCount: number
|
||||
} | null
|
||||
}
|
||||
|
||||
function MentorAssignmentContent({ projectId }: { projectId: string }) {
|
||||
const router = useRouter()
|
||||
const [selectedMentorId, setSelectedMentorId] = useState<string | null>(null)
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
// Fetch project
|
||||
const { data: project, isLoading: projectLoading } = trpc.project.get.useQuery({
|
||||
id: projectId,
|
||||
})
|
||||
|
||||
// Fetch suggestions
|
||||
const { data: suggestions, isLoading: suggestionsLoading, refetch } = trpc.mentor.getSuggestions.useQuery(
|
||||
{ projectId, limit: 5 },
|
||||
{ enabled: !!project && !project.mentorAssignment }
|
||||
)
|
||||
|
||||
// Assign mentor mutation
|
||||
const assignMutation = trpc.mentor.assign.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Mentor assigned!')
|
||||
utils.project.get.invalidate({ id: projectId })
|
||||
utils.mentor.getSuggestions.invalidate({ projectId })
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message)
|
||||
},
|
||||
})
|
||||
|
||||
// Auto-assign mutation
|
||||
const autoAssignMutation = trpc.mentor.autoAssign.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Mentor auto-assigned!')
|
||||
utils.project.get.invalidate({ id: projectId })
|
||||
utils.mentor.getSuggestions.invalidate({ projectId })
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message)
|
||||
},
|
||||
})
|
||||
|
||||
// Unassign mutation
|
||||
const unassignMutation = trpc.mentor.unassign.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Mentor removed')
|
||||
utils.project.get.invalidate({ id: projectId })
|
||||
utils.mentor.getSuggestions.invalidate({ projectId })
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message)
|
||||
},
|
||||
})
|
||||
|
||||
const handleAssign = (mentorId: string, suggestion?: MentorSuggestion) => {
|
||||
assignMutation.mutate({
|
||||
projectId,
|
||||
mentorId,
|
||||
method: suggestion ? 'AI_SUGGESTED' : 'MANUAL',
|
||||
aiConfidenceScore: suggestion?.confidenceScore,
|
||||
expertiseMatchScore: suggestion?.expertiseMatchScore,
|
||||
aiReasoning: suggestion?.reasoning,
|
||||
})
|
||||
}
|
||||
|
||||
if (projectLoading) {
|
||||
return <MentorAssignmentSkeleton />
|
||||
}
|
||||
|
||||
if (!project) {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="py-12 text-center">
|
||||
<p>Project not found</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
const hasMentor = !!project.mentorAssignment
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Header */}
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" className="-ml-4" onClick={() => router.back()}>
|
||||
<ArrowLeft className="mr-2 h-4 w-4" />
|
||||
Back
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Mentor Assignment</h1>
|
||||
<p className="text-muted-foreground">{project.title}</p>
|
||||
</div>
|
||||
|
||||
{/* Current Assignment */}
|
||||
{hasMentor && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Current Mentor</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<Avatar className="h-12 w-12">
|
||||
<AvatarFallback>
|
||||
{getInitials(project.mentorAssignment!.mentor.name || project.mentorAssignment!.mentor.email)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div>
|
||||
<p className="font-medium">{project.mentorAssignment!.mentor.name || 'Unnamed'}</p>
|
||||
<p className="text-sm text-muted-foreground">{project.mentorAssignment!.mentor.email}</p>
|
||||
{project.mentorAssignment!.mentor.expertiseTags && project.mentorAssignment!.mentor.expertiseTags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-1 mt-1">
|
||||
{project.mentorAssignment!.mentor.expertiseTags.slice(0, 3).map((tag: string) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">{tag}</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-right">
|
||||
<Badge variant="outline" className="mb-2">
|
||||
{project.mentorAssignment!.method.replace(/_/g, ' ')}
|
||||
</Badge>
|
||||
<div>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => unassignMutation.mutate({ projectId })}
|
||||
disabled={unassignMutation.isPending}
|
||||
>
|
||||
{unassignMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Remove'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* AI Suggestions */}
|
||||
{!hasMentor && (
|
||||
<>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<CardTitle className="text-lg flex items-center gap-2">
|
||||
<Users className="h-5 w-5 text-primary" />
|
||||
AI-Suggested Mentors
|
||||
<Badge variant="outline" className="text-xs gap-1 shrink-0 ml-1">
|
||||
<Bot className="h-3 w-3" />
|
||||
AI Recommended
|
||||
</Badge>
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Mentors matched based on expertise and project needs
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => refetch()}
|
||||
disabled={suggestionsLoading}
|
||||
>
|
||||
{suggestionsLoading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Refresh'
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => autoAssignMutation.mutate({ projectId, useAI: true })}
|
||||
disabled={autoAssignMutation.isPending}
|
||||
>
|
||||
{autoAssignMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Auto-Assign Best Match
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{suggestionsLoading ? (
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-24 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : suggestions?.suggestions.length === 0 ? (
|
||||
<p className="text-muted-foreground text-center py-8">
|
||||
No mentor suggestions available. Try adding more users with expertise tags.
|
||||
</p>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{suggestions?.suggestions.map((suggestion, index) => (
|
||||
<div
|
||||
key={suggestion.mentorId}
|
||||
className={`p-4 rounded-lg border-2 transition-colors ${
|
||||
selectedMentorId === suggestion.mentorId
|
||||
? 'border-primary bg-primary/5'
|
||||
: 'border-border hover:border-primary/50'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div className="flex items-start gap-4 flex-1">
|
||||
<div className="relative">
|
||||
<Avatar className="h-12 w-12">
|
||||
<AvatarFallback>
|
||||
{suggestion.mentor ? getInitials(suggestion.mentor.name || suggestion.mentor.email) : '?'}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
{index === 0 && (
|
||||
<div className="absolute -top-1 -right-1 h-5 w-5 rounded-full bg-primary text-primary-foreground flex items-center justify-center text-xs font-bold">
|
||||
1
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="font-medium">{suggestion.mentor?.name || 'Unnamed'}</p>
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{suggestion.mentor?.assignmentCount || 0} projects
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">{suggestion.mentor?.email}</p>
|
||||
|
||||
{/* Expertise tags */}
|
||||
{suggestion.mentor?.expertiseTags && suggestion.mentor.expertiseTags.length > 0 && (
|
||||
<div className="flex flex-wrap gap-1 mt-2">
|
||||
{suggestion.mentor.expertiseTags.map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Match scores */}
|
||||
<div className="mt-3 space-y-2">
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground w-28">Confidence:</span>
|
||||
<Progress value={suggestion.confidenceScore * 100} className="flex-1 h-2" />
|
||||
<span className="w-12 text-right">{(suggestion.confidenceScore * 100).toFixed(0)}%</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground w-28">Expertise Match:</span>
|
||||
<Progress value={suggestion.expertiseMatchScore * 100} className="flex-1 h-2" />
|
||||
<span className="w-12 text-right">{(suggestion.expertiseMatchScore * 100).toFixed(0)}%</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* AI Reasoning */}
|
||||
{suggestion.reasoning && (
|
||||
<p className="mt-2 text-sm text-muted-foreground italic">
|
||||
"{suggestion.reasoning}"
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
onClick={() => handleAssign(suggestion.mentorId, suggestion)}
|
||||
disabled={assignMutation.isPending}
|
||||
variant={selectedMentorId === suggestion.mentorId ? 'default' : 'outline'}
|
||||
>
|
||||
{assignMutation.isPending && selectedMentorId === suggestion.mentorId ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Check className="mr-2 h-4 w-4" />
|
||||
Assign
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function MentorAssignmentSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-9 w-36" />
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-8 w-64" />
|
||||
<Skeleton className="h-4 w-48" />
|
||||
</div>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-6 w-48" />
|
||||
<Skeleton className="h-4 w-64" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-4">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-24 w-full" />
|
||||
))}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function MentorAssignmentPage({ params }: PageProps) {
|
||||
const { id } = use(params)
|
||||
|
||||
return (
|
||||
<Suspense fallback={<MentorAssignmentSkeleton />}>
|
||||
<MentorAssignmentContent projectId={id} />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
'use client'
|
||||
|
||||
import { Suspense, use, useMemo, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import {
|
||||
AlertTriangle,
|
||||
ArrowLeft,
|
||||
Bot,
|
||||
Check,
|
||||
Loader2,
|
||||
Search,
|
||||
Sparkles,
|
||||
Users,
|
||||
} from 'lucide-react'
|
||||
import { getInitials, formatEnumLabel } from '@/lib/utils'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ id: string }>
|
||||
}
|
||||
|
||||
function MentorAssignmentContent({ projectId }: { projectId: string }) {
|
||||
const router = useRouter()
|
||||
const utils = trpc.useUtils()
|
||||
const [search, setSearch] = useState('')
|
||||
const [pendingMentorId, setPendingMentorId] = useState<string | null>(null)
|
||||
|
||||
const { data: project, isLoading: projectLoading } = trpc.project.get.useQuery({ id: projectId })
|
||||
|
||||
const { data: candidatesData, isLoading: candidatesLoading } =
|
||||
trpc.mentor.getCandidates.useQuery(
|
||||
{ projectId },
|
||||
{ enabled: !!project && !project.mentorAssignment },
|
||||
)
|
||||
|
||||
const {
|
||||
data: suggestionsData,
|
||||
isLoading: suggestionsLoading,
|
||||
refetch: refetchSuggestions,
|
||||
} = trpc.mentor.getSuggestions.useQuery(
|
||||
{ projectId, limit: 5 },
|
||||
{ enabled: !!project && !project.mentorAssignment },
|
||||
)
|
||||
|
||||
const assignMutation = trpc.mentor.assign.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Mentor assigned')
|
||||
utils.project.get.invalidate({ id: projectId })
|
||||
utils.mentor.getCandidates.invalidate({ projectId })
|
||||
utils.mentor.getSuggestions.invalidate({ projectId })
|
||||
setPendingMentorId(null)
|
||||
},
|
||||
onError: (err) => {
|
||||
toast.error(err.message)
|
||||
setPendingMentorId(null)
|
||||
},
|
||||
})
|
||||
|
||||
const unassignMutation = trpc.mentor.unassign.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Mentor removed')
|
||||
utils.project.get.invalidate({ id: projectId })
|
||||
utils.mentor.getCandidates.invalidate({ projectId })
|
||||
utils.mentor.getSuggestions.invalidate({ projectId })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const filteredCandidates = useMemo(() => {
|
||||
if (!candidatesData) return []
|
||||
const q = search.trim().toLowerCase()
|
||||
if (!q) return candidatesData.candidates
|
||||
return candidatesData.candidates.filter((c) => {
|
||||
const hay = [c.name ?? '', c.email, ...(c.expertiseTags ?? []), c.country ?? '']
|
||||
.join(' ')
|
||||
.toLowerCase()
|
||||
return hay.includes(q)
|
||||
})
|
||||
}, [candidatesData, search])
|
||||
|
||||
if (projectLoading) return <MentorAssignmentSkeleton />
|
||||
if (!project) {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="py-12 text-center">
|
||||
<p>Project not found</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
const hasMentor = !!project.mentorAssignment
|
||||
const teamSize = project.teamMembers?.length ?? 0
|
||||
const aiSource = suggestionsData?.source ?? 'ai'
|
||||
|
||||
const handleAssignManual = (mentorId: string) => {
|
||||
setPendingMentorId(mentorId)
|
||||
assignMutation.mutate({ projectId, mentorId, method: 'MANUAL' })
|
||||
}
|
||||
|
||||
const handleAssignFromSuggestion = (
|
||||
mentorId: string,
|
||||
suggestion: {
|
||||
confidenceScore: number
|
||||
expertiseMatchScore: number
|
||||
reasoning: string
|
||||
},
|
||||
) => {
|
||||
setPendingMentorId(mentorId)
|
||||
assignMutation.mutate({
|
||||
projectId,
|
||||
mentorId,
|
||||
method: aiSource === 'ai' ? 'AI_SUGGESTED' : 'ALGORITHM',
|
||||
aiConfidenceScore: suggestion.confidenceScore,
|
||||
expertiseMatchScore: suggestion.expertiseMatchScore,
|
||||
aiReasoning: suggestion.reasoning,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center gap-4">
|
||||
<Button variant="ghost" className="-ml-4" onClick={() => router.back()}>
|
||||
<ArrowLeft className="mr-2 h-4 w-4" /> Back
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h1 className="text-2xl font-semibold tracking-tight">Mentor Assignment</h1>
|
||||
<p className="text-muted-foreground">{project.title}</p>
|
||||
</div>
|
||||
|
||||
{/* ─── Project Context ─── */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Project Context</CardTitle>
|
||||
<CardDescription>What this project needs from a mentor</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid grid-cols-2 gap-x-6 gap-y-3 text-sm md:grid-cols-3">
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs uppercase tracking-wide">Ocean Issue</div>
|
||||
<div className="font-medium">
|
||||
{project.oceanIssue ? formatEnumLabel(project.oceanIssue) : '—'}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs uppercase tracking-wide">Category</div>
|
||||
<div className="font-medium">
|
||||
{project.competitionCategory ? formatEnumLabel(project.competitionCategory) : '—'}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs uppercase tracking-wide">Country</div>
|
||||
<div className="font-medium">{project.country ?? '—'}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs uppercase tracking-wide">Team Size</div>
|
||||
<div className="font-medium">{teamSize}</div>
|
||||
</div>
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs uppercase tracking-wide">
|
||||
Mentoring Requested
|
||||
</div>
|
||||
<div className="font-medium">{project.wantsMentorship ? 'Yes' : 'No'}</div>
|
||||
</div>
|
||||
</div>
|
||||
{project.tags && project.tags.length > 0 && (
|
||||
<div className="mt-4">
|
||||
<div className="text-muted-foreground mb-1.5 text-xs uppercase tracking-wide">
|
||||
Project Tags
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{project.tags.map((tag: string) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* ─── Currently Assigned ─── */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Currently Assigned</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{hasMentor ? (
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<Avatar className="h-12 w-12">
|
||||
<AvatarFallback>
|
||||
{getInitials(
|
||||
project.mentorAssignment!.mentor.name ||
|
||||
project.mentorAssignment!.mentor.email,
|
||||
)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div>
|
||||
<Link
|
||||
href={`/admin/mentors/${project.mentorAssignment!.mentor.id}`}
|
||||
className="font-medium hover:underline"
|
||||
>
|
||||
{project.mentorAssignment!.mentor.name || 'Unnamed'}
|
||||
</Link>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{project.mentorAssignment!.mentor.email}
|
||||
</p>
|
||||
{project.mentorAssignment!.mentor.expertiseTags &&
|
||||
project.mentorAssignment!.mentor.expertiseTags.length > 0 && (
|
||||
<div className="mt-1 flex flex-wrap gap-1">
|
||||
{project.mentorAssignment!.mentor.expertiseTags
|
||||
.slice(0, 5)
|
||||
.map((tag: string) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col items-end gap-2">
|
||||
<Badge variant="outline" className="text-xs">
|
||||
{project.mentorAssignment!.method.replace(/_/g, ' ')}
|
||||
</Badge>
|
||||
<Button
|
||||
variant="destructive"
|
||||
size="sm"
|
||||
onClick={() => unassignMutation.mutate({ projectId })}
|
||||
disabled={unassignMutation.isPending}
|
||||
>
|
||||
{unassignMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Unassign'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
No mentor assigned yet — pick one below.
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* ─── Pick a Mentor ─── */}
|
||||
{!hasMentor && (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-lg">Pick a Mentor</CardTitle>
|
||||
<CardDescription>
|
||||
Browse all eligible mentors or use AI to surface the best fits.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Tabs defaultValue="manual" className="space-y-4">
|
||||
<TabsList>
|
||||
<TabsTrigger value="manual">
|
||||
<Users className="mr-2 h-4 w-4" /> Manual Picker
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="ai">
|
||||
<Sparkles className="mr-2 h-4 w-4" /> AI Suggestions
|
||||
</TabsTrigger>
|
||||
</TabsList>
|
||||
|
||||
<TabsContent value="manual" className="space-y-4">
|
||||
<div className="relative">
|
||||
<Search className="text-muted-foreground absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2" />
|
||||
<Input
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
placeholder="Search by name, email, country, or expertise tag…"
|
||||
className="pl-9"
|
||||
/>
|
||||
</div>
|
||||
{candidatesLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filteredCandidates.length === 0 ? (
|
||||
<div className="text-muted-foreground py-8 text-center text-sm">
|
||||
No matching mentors. Try a different search.
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Mentor</TableHead>
|
||||
<TableHead>Expertise</TableHead>
|
||||
<TableHead>Country</TableHead>
|
||||
<TableHead className="text-right">Load</TableHead>
|
||||
<TableHead className="text-right">Overlap</TableHead>
|
||||
<TableHead></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filteredCandidates.map((c) => (
|
||||
<TableRow key={c.id}>
|
||||
<TableCell>
|
||||
<div className="font-medium">{c.name ?? 'Unnamed'}</div>
|
||||
<div className="text-muted-foreground text-xs">{c.email}</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{c.expertiseTags.slice(0, 4).map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
{c.expertiseTags.length > 4 && (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
+{c.expertiseTags.length - 4}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">{c.country ?? '—'}</TableCell>
|
||||
<TableCell className="text-right text-sm tabular-nums">
|
||||
{c.currentAssignments}
|
||||
{c.maxAssignments != null ? `/${c.maxAssignments}` : ''}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Badge
|
||||
variant={
|
||||
c.overlapScore >= 0.5
|
||||
? 'default'
|
||||
: c.overlapScore > 0
|
||||
? 'secondary'
|
||||
: 'outline'
|
||||
}
|
||||
className="text-xs tabular-nums"
|
||||
>
|
||||
{Math.round(c.overlapScore * 100)}%
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleAssignManual(c.id)}
|
||||
disabled={assignMutation.isPending}
|
||||
>
|
||||
{assignMutation.isPending && pendingMentorId === c.id ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Check className="mr-1 h-3.5 w-3.5" /> Assign
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="ai" className="space-y-4">
|
||||
{aiSource === 'fallback' && (
|
||||
<div className="flex items-start gap-3 rounded-md border border-amber-300 bg-amber-50 p-3 text-sm dark:border-amber-700 dark:bg-amber-950/40">
|
||||
<AlertTriangle className="mt-0.5 h-4 w-4 shrink-0 text-amber-600" />
|
||||
<div>
|
||||
<p className="font-medium">AI matching unavailable</p>
|
||||
<p className="text-muted-foreground">
|
||||
Showing expertise-tag overlap instead. Configure{' '}
|
||||
<code>OPENAI_API_KEY</code> to enable AI matching.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<div className="flex items-center justify-end">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => refetchSuggestions()}
|
||||
disabled={suggestionsLoading}
|
||||
>
|
||||
{suggestionsLoading ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Refresh'
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
{suggestionsLoading ? (
|
||||
<div className="space-y-3">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-24 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : !suggestionsData || suggestionsData.suggestions.length === 0 ? (
|
||||
<p className="text-muted-foreground py-8 text-center text-sm">
|
||||
No suggestions available.
|
||||
</p>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{suggestionsData.suggestions.map((s, i) => (
|
||||
<div
|
||||
key={s.mentorId}
|
||||
className="flex items-start justify-between rounded-md border p-4"
|
||||
>
|
||||
<div className="flex flex-1 gap-3">
|
||||
<div className="relative">
|
||||
<Avatar className="h-12 w-12">
|
||||
<AvatarFallback>
|
||||
{s.mentor
|
||||
? getInitials(s.mentor.name || s.mentor.email)
|
||||
: '?'}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
{i === 0 && (
|
||||
<div className="bg-primary text-primary-foreground absolute -right-1 -top-1 flex h-5 w-5 items-center justify-center rounded-full text-xs font-bold">
|
||||
1
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex items-center gap-2">
|
||||
<p className="font-medium">{s.mentor?.name || 'Unnamed'}</p>
|
||||
<Badge variant="outline" className="gap-1 text-xs">
|
||||
<Bot className="h-3 w-3" />{' '}
|
||||
{aiSource === 'ai' ? 'AI' : 'Tag overlap'}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-muted-foreground text-sm">{s.mentor?.email}</p>
|
||||
{s.mentor?.expertiseTags && s.mentor.expertiseTags.length > 0 && (
|
||||
<div className="mt-2 flex flex-wrap gap-1">
|
||||
{s.mentor.expertiseTags.slice(0, 5).map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="text-xs">
|
||||
{tag}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="mt-3 space-y-1.5">
|
||||
<div className="flex items-center gap-2 text-xs">
|
||||
<span className="text-muted-foreground w-28">Confidence:</span>
|
||||
<Progress value={s.confidenceScore * 100} className="h-1.5 flex-1" />
|
||||
<span className="w-10 text-right tabular-nums">
|
||||
{Math.round(s.confidenceScore * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-xs">
|
||||
<span className="text-muted-foreground w-28">
|
||||
Expertise Match:
|
||||
</span>
|
||||
<Progress
|
||||
value={s.expertiseMatchScore * 100}
|
||||
className="h-1.5 flex-1"
|
||||
/>
|
||||
<span className="w-10 text-right tabular-nums">
|
||||
{Math.round(s.expertiseMatchScore * 100)}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{s.reasoning && (
|
||||
<p className="text-muted-foreground mt-2 text-xs italic">
|
||||
"{s.reasoning}"
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => handleAssignFromSuggestion(s.mentorId, s)}
|
||||
disabled={assignMutation.isPending}
|
||||
>
|
||||
{assignMutation.isPending && pendingMentorId === s.mentorId ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Check className="mr-1 h-3.5 w-3.5" /> Assign
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</TabsContent>
|
||||
</Tabs>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function MentorAssignmentSkeleton() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-9 w-36" />
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-8 w-64" />
|
||||
<Skeleton className="h-4 w-48" />
|
||||
</div>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-6 w-48" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="h-32 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function MentorAssignmentPage({ params }: PageProps) {
|
||||
const { id } = use(params)
|
||||
return (
|
||||
<Suspense fallback={<MentorAssignmentSkeleton />}>
|
||||
<MentorAssignmentContent projectId={id} />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,7 +75,9 @@ import {
|
||||
Eye,
|
||||
Plus,
|
||||
X,
|
||||
Mail,
|
||||
} from 'lucide-react'
|
||||
import { ProjectEmailDialog } from '@/components/admin/project-email-dialog'
|
||||
import { toast } from 'sonner'
|
||||
import { formatDateOnly } from '@/lib/utils'
|
||||
import { getCountryName, getCountryFlag } from '@/lib/countries'
|
||||
@@ -161,6 +163,7 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
||||
|
||||
// State for remove member confirmation
|
||||
const [removingMemberId, setRemovingMemberId] = useState<string | null>(null)
|
||||
const [emailDialogOpen, setEmailDialogOpen] = useState(false)
|
||||
|
||||
const addTeamMember = trpc.project.addTeamMember.useMutation({
|
||||
onSuccess: () => {
|
||||
@@ -269,14 +272,29 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Button variant="outline" asChild>
|
||||
<Link href={`/admin/projects/${projectId}/edit`}>
|
||||
<Edit className="mr-2 h-4 w-4" />
|
||||
Edit
|
||||
</Link>
|
||||
</Button>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button variant="outline" onClick={() => setEmailDialogOpen(true)}>
|
||||
<Mail className="mr-2 h-4 w-4" />
|
||||
Email Team
|
||||
</Button>
|
||||
<Button variant="outline" asChild>
|
||||
<Link href={`/admin/projects/${projectId}/edit`}>
|
||||
<Edit className="mr-2 h-4 w-4" />
|
||||
Edit
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{project && (
|
||||
<ProjectEmailDialog
|
||||
open={emailDialogOpen}
|
||||
onClose={() => setEmailDialogOpen(false)}
|
||||
projectId={project.id}
|
||||
projectTitle={project.title}
|
||||
/>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
{/* Stats Grid */}
|
||||
|
||||
@@ -91,6 +91,9 @@ import { ProjectStatesTable } from '@/components/admin/round/project-states-tabl
|
||||
// SubmissionWindowManager removed — round dates + file requirements in Config are sufficient
|
||||
import { FileRequirementsEditor } from '@/components/admin/round/file-requirements-editor'
|
||||
import { FilteringDashboard } from '@/components/admin/round/filtering-dashboard'
|
||||
import { MentoringRoundOverview } from '@/components/admin/round/mentoring-round-overview'
|
||||
import { FinalistSlotsCard } from '@/components/admin/grand-finale/finalist-slots-card'
|
||||
import { WaitlistCard } from '@/components/admin/grand-finale/waitlist-card'
|
||||
import { RankingDashboard } from '@/components/admin/round/ranking-dashboard'
|
||||
import { CoverageReport } from '@/components/admin/assignment/coverage-report'
|
||||
import { AssignmentPreviewSheet } from '@/components/admin/assignment/assignment-preview-sheet'
|
||||
@@ -145,6 +148,73 @@ const stateColors: Record<string, string> = Object.fromEntries(
|
||||
Object.entries(projectStateConfig).map(([k, v]) => [k, v.bg])
|
||||
)
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Mentoring round: Auto-fill remaining toolbar (Projects tab)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
function MentoringBulkAssignToolbar({
|
||||
roundId,
|
||||
configJson,
|
||||
}: {
|
||||
roundId: string
|
||||
configJson: Record<string, unknown>
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const eligibility = (configJson.eligibility as string) ?? 'requested_only'
|
||||
const isAdminSelected = eligibility === 'admin_selected'
|
||||
|
||||
const { data: pending } = trpc.round.getProjectsNeedingMentor.useQuery(
|
||||
{ roundId },
|
||||
{ enabled: !isAdminSelected, refetchInterval: 30_000 },
|
||||
)
|
||||
const count = pending?.count ?? 0
|
||||
|
||||
const bulk = trpc.mentor.autoAssignBulkForRound.useMutation({
|
||||
onSuccess: (result) => {
|
||||
toast.success(result.message)
|
||||
utils.round.getProjectsNeedingMentor.invalidate({ roundId })
|
||||
utils.project.list.invalidate()
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const eligibilityLabel = eligibility.replace('_', ' ')
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-between rounded-md border bg-muted/30 px-4 py-2.5">
|
||||
<div className="text-sm">
|
||||
{isAdminSelected ? (
|
||||
<>
|
||||
<span className="font-medium">Eligibility: admin-selected</span>
|
||||
<span className="text-muted-foreground ml-2">
|
||||
— auto-fill is disabled. Assign each project manually.
|
||||
</span>
|
||||
</>
|
||||
) : count > 0 ? (
|
||||
<>
|
||||
<span className="font-medium">{count}</span>{' '}
|
||||
<span className="text-muted-foreground">
|
||||
project{count === 1 ? '' : 's'} eligible for auto-fill ({eligibilityLabel})
|
||||
</span>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-muted-foreground">
|
||||
All eligible projects have a mentor.
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
size="sm"
|
||||
onClick={() => bulk.mutate({ roundId })}
|
||||
disabled={isAdminSelected || count === 0 || bulk.isPending}
|
||||
>
|
||||
{bulk.isPending ? <Loader2 className="mr-2 h-4 w-4 animate-spin" /> : null}
|
||||
Auto-fill remaining
|
||||
</Button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Main Page Component
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
@@ -514,6 +584,16 @@ export default function RoundDetailPage() {
|
||||
|
||||
const isFiltering = round?.roundType === 'FILTERING'
|
||||
const isEvaluation = round?.roundType === 'EVALUATION'
|
||||
const isMentoring = round?.roundType === 'MENTORING'
|
||||
const isGrandFinale = round?.roundType === 'LIVE_FINAL'
|
||||
|
||||
// Mentor pool size — used by Round Details panel below to replace the
|
||||
// always-empty "Jury Group" row on MENTORING rounds.
|
||||
const { data: mentorPool } = trpc.mentor.getMentorPool.useQuery(
|
||||
{},
|
||||
{ enabled: isMentoring },
|
||||
)
|
||||
const mentorPoolSize = mentorPool?.poolSize ?? 0
|
||||
const hasJury = ['EVALUATION', 'LIVE_FINAL', 'DELIBERATION'].includes(round?.roundType ?? '')
|
||||
const hasAwards = roundAwards.length > 0
|
||||
const isSimpleAdvance = ['INTAKE', 'SUBMISSION', 'MENTORING'].includes(round?.roundType ?? '')
|
||||
@@ -589,7 +669,8 @@ export default function RoundDetailPage() {
|
||||
action: undefined as Route | undefined,
|
||||
actionLabel: undefined as string | undefined,
|
||||
},
|
||||
...((isEvaluation && !(config.requireDocumentUpload as boolean))
|
||||
...((isEvaluation && !(config.requireDocumentUpload as boolean)) ||
|
||||
(isMentoring && !(config.filePromotionEnabled as boolean) && !config.promotionTargetWindowId)
|
||||
? []
|
||||
: [{
|
||||
label: 'File requirements set',
|
||||
@@ -1400,6 +1481,17 @@ export default function RoundDetailPage() {
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Mentoring-specific stats \u2014 only on MENTORING rounds */}
|
||||
{isMentoring && <MentoringRoundOverview roundId={roundId} />}
|
||||
|
||||
{/* Grand-finale logistics \u2014 only on LIVE_FINAL rounds */}
|
||||
{isGrandFinale && programId && (
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<FinalistSlotsCard programId={programId} />
|
||||
<WaitlistCard programId={programId} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Round Info + Project Breakdown */}
|
||||
<div className="grid gap-4 sm:grid-cols-2">
|
||||
<AnimatedCard index={2}>
|
||||
@@ -1413,7 +1505,9 @@ export default function RoundDetailPage() {
|
||||
{ label: 'Status', value: <span className="font-medium">{statusCfg.label}</span> },
|
||||
{ label: 'Position', value: <span className="font-medium">{`Round ${(round.sortOrder ?? 0) + 1}${competition?.rounds ? ` of ${competition.rounds.length}` : ''}`}</span> },
|
||||
...(round.purposeKey ? [{ label: 'Purpose', value: <span className="font-medium">{round.purposeKey}</span> }] : []),
|
||||
{ label: 'Jury Group', value: <span className="font-medium">{juryGroup ? juryGroup.name : '\u2014'}</span> },
|
||||
isMentoring
|
||||
? { label: 'Mentor Pool', value: <Link href="/admin/mentors" className="font-medium hover:underline">{mentorPoolSize} member{mentorPoolSize === 1 ? '' : 's'}</Link> }
|
||||
: { label: 'Jury Group', value: <span className="font-medium">{juryGroup ? juryGroup.name : '\u2014'}</span> },
|
||||
{ label: 'Opens', value: <span className="font-medium">{round.windowOpenAt ? new Date(round.windowOpenAt).toLocaleString() : '\u2014'}</span> },
|
||||
{ label: 'Closes', value: <span className="font-medium">{round.windowCloseAt ? new Date(round.windowCloseAt).toLocaleString() : '\u2014'}</span> },
|
||||
].map((row, i) => (
|
||||
@@ -1475,6 +1569,9 @@ export default function RoundDetailPage() {
|
||||
|
||||
{/* ═══════════ PROJECTS TAB ═══════════ */}
|
||||
<TabsContent value="projects" className="space-y-4">
|
||||
{isMentoring && (
|
||||
<MentoringBulkAssignToolbar roundId={roundId} configJson={config} />
|
||||
)}
|
||||
<ProjectStatesTable
|
||||
competitionId={competitionId}
|
||||
roundId={roundId}
|
||||
@@ -2198,7 +2295,8 @@ export default function RoundDetailPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* General Round Settings */}
|
||||
{/* General Round Settings — hidden on MENTORING rounds (no advancement targets apply) */}
|
||||
{!isMentoring && (
|
||||
<Card>
|
||||
<CardHeader className="border-b">
|
||||
<ConfigSectionHeader
|
||||
@@ -2321,6 +2419,7 @@ export default function RoundDetailPage() {
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Round-type-specific config */}
|
||||
<RoundConfigForm
|
||||
|
||||
@@ -2,6 +2,7 @@ import { prisma } from '@/lib/prisma'
|
||||
import { requireRole } from '@/lib/auth-redirect'
|
||||
import { AdminSidebar } from '@/components/layouts/admin-sidebar'
|
||||
import { AdminEditionWrapper } from '@/components/layouts/admin-edition-wrapper'
|
||||
import { RoleSwitcherPill } from '@/components/layouts/role-switcher'
|
||||
|
||||
export default async function AdminLayout({
|
||||
children,
|
||||
@@ -34,6 +35,12 @@ export default async function AdminLayout({
|
||||
<main className="lg:pl-64">
|
||||
{/* Spacer for mobile header */}
|
||||
<div className="h-16 lg:hidden" />
|
||||
{/* Top-bar — hosts the RoleSwitcherPill so multi-role admins
|
||||
can switch dashboards from the same screen position used on
|
||||
every other layout. Pill auto-hides for single-role users. */}
|
||||
<div className="sticky top-0 z-30 flex h-12 items-center justify-end gap-2 border-b bg-card/80 backdrop-blur px-4">
|
||||
<RoleSwitcherPill currentBasePath="/admin" />
|
||||
</div>
|
||||
<div className="container-app py-6 lg:py-8">{children}</div>
|
||||
</main>
|
||||
</div>
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from '@/components/ui/card'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { MentorChat } from '@/components/shared/mentor-chat'
|
||||
import { WorkspaceFilesPanel } from '@/components/mentor/workspace-files-panel'
|
||||
import {
|
||||
MessageSquare,
|
||||
UserCircle,
|
||||
@@ -133,6 +134,14 @@ export default function ApplicantMentorPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{/* Files */}
|
||||
{dashboardData?.project?.mentorAssignment?.id && (
|
||||
<WorkspaceFilesPanel
|
||||
mentorAssignmentId={dashboardData.project.mentorAssignment.id}
|
||||
asApplicant
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,10 @@ import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { CompetitionTimelineSidebar } from '@/components/applicant/competition-timeline'
|
||||
import { MentoringRequestCard } from '@/components/applicant/mentoring-request-card'
|
||||
import { MentorConversationCard } from '@/components/applicant/mentor-conversation-card'
|
||||
import { AttendingMembersCard } from '@/components/applicant/attending-members-card'
|
||||
import { LunchBanner } from '@/components/applicant/lunch-banner'
|
||||
import { ExternalAttendeesStrip } from '@/components/applicant/external-attendees-strip'
|
||||
import { AnimatedCard } from '@/components/shared/animated-container'
|
||||
import { ProjectLogoUpload } from '@/components/shared/project-logo-upload'
|
||||
import { Progress } from '@/components/ui/progress'
|
||||
@@ -401,6 +405,19 @@ export default function ApplicantDashboardPage() {
|
||||
</AnimatedCard>
|
||||
))}
|
||||
|
||||
{/* Lunch banner (auto-hides when lunch event disabled or unconfigured) */}
|
||||
<LunchBanner programId={project.programId} />
|
||||
|
||||
{/* External lunch attendees attached to this team (auto-hides if none) */}
|
||||
<ExternalAttendeesStrip projectId={project.id} />
|
||||
|
||||
{/* Grand finale attendee roster (auto-hides until confirmation status is CONFIRMED) */}
|
||||
<AttendingMembersCard />
|
||||
|
||||
{/* Conversation with assigned mentor (auto-hides when no mentor assigned) */}
|
||||
<MentorConversationCard projectId={project.id} />
|
||||
|
||||
|
||||
{/* Jury Feedback Card */}
|
||||
{totalEvaluations > 0 && (
|
||||
<AnimatedCard index={4}>
|
||||
|
||||
@@ -41,6 +41,7 @@ import {
|
||||
import { formatDateOnly } from '@/lib/utils'
|
||||
import { AnimatedCard } from '@/components/shared/animated-container'
|
||||
import { CountryDisplay } from '@/components/shared/country-display'
|
||||
import { RecentMessagesCard } from '@/components/mentor/recent-messages-card'
|
||||
|
||||
// Status badge colors
|
||||
const statusColors: Record<string, 'default' | 'secondary' | 'destructive' | 'outline'> = {
|
||||
@@ -117,6 +118,9 @@ export default function MentorDashboard() {
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Recent unread messages from teams */}
|
||||
<RecentMessagesCard />
|
||||
|
||||
{/* Stats */}
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<AnimatedCard index={0}>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { Suspense, use, useState, useEffect } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
@@ -31,6 +32,7 @@ import { AnimatedCard } from '@/components/shared/animated-container'
|
||||
import { FileViewer } from '@/components/shared/file-viewer'
|
||||
import { MentorChat } from '@/components/shared/mentor-chat'
|
||||
import { ProjectLogoWithUrl } from '@/components/shared/project-logo-with-url'
|
||||
import { DropAssignmentDialog } from '@/components/mentor/drop-assignment-dialog'
|
||||
import {
|
||||
ArrowLeft,
|
||||
AlertCircle,
|
||||
@@ -76,6 +78,7 @@ const statusColors: Record<string, 'default' | 'secondary' | 'destructive' | 'ou
|
||||
|
||||
function ProjectDetailContent({ projectId }: { projectId: string }) {
|
||||
const router = useRouter()
|
||||
const { data: session } = useSession()
|
||||
const { data: project, isLoading, error } = trpc.mentor.getProjectDetail.useQuery({
|
||||
projectId,
|
||||
})
|
||||
@@ -132,8 +135,15 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
||||
|
||||
const teamLead = project.teamMembers?.find((m) => m.role === 'LEAD')
|
||||
const otherMembers = project.teamMembers?.filter((m) => m.role !== 'LEAD') || []
|
||||
const mentorAssignmentId = project.mentorAssignment?.id
|
||||
const mentorAssignment = project.mentorAssignment
|
||||
const mentorAssignmentId = mentorAssignment?.id
|
||||
const programId = project.program?.id
|
||||
const viewerIsAssignedMentor =
|
||||
!!mentorAssignment && session?.user?.id === mentorAssignment.mentor?.id
|
||||
const canDrop =
|
||||
viewerIsAssignedMentor &&
|
||||
!mentorAssignment.droppedAt &&
|
||||
mentorAssignment.completionStatus !== 'completed'
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
@@ -179,6 +189,12 @@ function ProjectDetailContent({ projectId }: { projectId: string }) {
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{canDrop && mentorAssignmentId && (
|
||||
<DropAssignmentDialog
|
||||
assignmentId={mentorAssignmentId}
|
||||
projectTitle={project.title}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{project.assignedAt && (
|
||||
|
||||
@@ -9,6 +9,7 @@ import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
|
||||
import { WorkspaceChat } from '@/components/mentor/workspace-chat'
|
||||
import { FilePromotionPanel } from '@/components/mentor/file-promotion-panel'
|
||||
import { WorkspaceFilesPanel } from '@/components/mentor/workspace-files-panel'
|
||||
import { ArrowLeft, MessageSquare, FileText, Upload } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
@@ -102,20 +103,16 @@ export default function MentorWorkspaceDetailPage() {
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="files" className="mt-6">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Workspace Files</CardTitle>
|
||||
<CardDescription>
|
||||
Files shared in the mentor workspace
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="text-center py-8">
|
||||
<FileText className="h-12 w-12 text-muted-foreground/50 mx-auto mb-3" />
|
||||
<p className="text-sm text-muted-foreground">
|
||||
File listing feature coming soon
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
{assignment ? (
|
||||
<WorkspaceFilesPanel mentorAssignmentId={assignment.id} />
|
||||
) : (
|
||||
<Card>
|
||||
<CardContent className="text-center py-8">
|
||||
<FileText className="h-12 w-12 text-muted-foreground/50 mx-auto mb-3" />
|
||||
<p className="text-sm text-muted-foreground">Loading workspace…</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)}
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="promotion" className="mt-6">
|
||||
|
||||
421
src/app/(public)/finalist/confirm/[token]/page.tsx
Normal file
421
src/app/(public)/finalist/confirm/[token]/page.tsx
Normal file
@@ -0,0 +1,421 @@
|
||||
'use client'
|
||||
|
||||
import { Suspense, use, useEffect, useMemo, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { AlertCircle, CheckCircle2, Loader2, PartyPopper, XCircle } from 'lucide-react'
|
||||
import { TRPCClientError } from '@trpc/client'
|
||||
|
||||
interface PageProps {
|
||||
params: Promise<{ token: string }>
|
||||
}
|
||||
|
||||
function formatDeadline(d: Date): string {
|
||||
const main = new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'long',
|
||||
timeStyle: 'short',
|
||||
}).format(d)
|
||||
const tzPart = new Intl.DateTimeFormat(undefined, { timeZoneName: 'short' })
|
||||
.formatToParts(d)
|
||||
.find((p) => p.type === 'timeZoneName')?.value
|
||||
return tzPart ? `${main} (${tzPart})` : main
|
||||
}
|
||||
|
||||
function CountdownLabel({ deadline }: { deadline: Date }) {
|
||||
const [now, setNow] = useState<number>(Date.now())
|
||||
useEffect(() => {
|
||||
const id = setInterval(() => setNow(Date.now()), 1000)
|
||||
return () => clearInterval(id)
|
||||
}, [])
|
||||
const ms = deadline.getTime() - now
|
||||
if (ms <= 0) return <span className="text-destructive font-medium">expired</span>
|
||||
const totalSec = Math.floor(ms / 1000)
|
||||
const hours = Math.floor(totalSec / 3600)
|
||||
const minutes = Math.floor((totalSec % 3600) / 60)
|
||||
const seconds = totalSec % 60
|
||||
if (hours >= 24) {
|
||||
const days = Math.floor(hours / 24)
|
||||
const remHours = hours % 24
|
||||
return (
|
||||
<span className="font-medium tabular-nums">
|
||||
{days}d {remHours}h remaining
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<span className="font-medium tabular-nums">
|
||||
{hours.toString().padStart(2, '0')}:{minutes.toString().padStart(2, '0')}:
|
||||
{seconds.toString().padStart(2, '0')} remaining
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function FriendlyError({
|
||||
title,
|
||||
message,
|
||||
icon: Icon,
|
||||
}: {
|
||||
title: string
|
||||
message: string
|
||||
icon: typeof AlertCircle
|
||||
}) {
|
||||
return (
|
||||
<Card className="mx-auto max-w-xl">
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<Icon className="text-muted-foreground h-5 w-5" />
|
||||
<CardTitle>{title}</CardTitle>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-muted-foreground">{message}</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
function FinalistConfirmContent({ token }: { token: string }) {
|
||||
const { data, isLoading, error } = trpc.finalist.getByToken.useQuery({ token }, { retry: false })
|
||||
const confirmMutation = trpc.finalist.confirm.useMutation()
|
||||
const declineMutation = trpc.finalist.decline.useMutation()
|
||||
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set())
|
||||
const [visa, setVisa] = useState<Record<string, boolean>>({})
|
||||
const [declineReason, setDeclineReason] = useState('')
|
||||
const [submitState, setSubmitState] = useState<'idle' | 'confirmed' | 'declined' | 'error'>(
|
||||
'idle',
|
||||
)
|
||||
const [submitError, setSubmitError] = useState<string | null>(null)
|
||||
|
||||
// Default-select all team members once data arrives
|
||||
useEffect(() => {
|
||||
if (data?.project.teamMembers && selected.size === 0 && submitState === 'idle') {
|
||||
const cap = data.project.program.defaultAttendeeCap
|
||||
const initial = new Set(
|
||||
data.project.teamMembers.slice(0, cap).map((tm) => tm.userId),
|
||||
)
|
||||
setSelected(initial)
|
||||
}
|
||||
}, [data, selected.size, submitState])
|
||||
|
||||
// ── Loading
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="mx-auto max-w-xl space-y-4">
|
||||
<Skeleton className="h-8 w-2/3" />
|
||||
<Skeleton className="h-32 w-full" />
|
||||
<Skeleton className="h-48 w-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
// ── Token errors → friendly states
|
||||
if (error) {
|
||||
const msg = error.message ?? ''
|
||||
if (/expired/i.test(msg)) {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="This link has expired"
|
||||
message="If your team still wants to attend, please contact us at info@monaco-opc.com."
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (/signature|malformed|payload/i.test(msg)) {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="This link is not valid"
|
||||
message="Please check your email or contact us at info@monaco-opc.com."
|
||||
/>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="Something went wrong"
|
||||
message={msg || 'Please try again or contact us at info@monaco-opc.com.'}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (!data) {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="Confirmation not found"
|
||||
message="Please check your email link or contact us at info@monaco-opc.com."
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// ── Status branches: only PENDING is interactive
|
||||
if (submitState === 'confirmed' || data.status === 'CONFIRMED') {
|
||||
return (
|
||||
<Card className="mx-auto max-w-xl">
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<PartyPopper className="text-primary h-5 w-5" />
|
||||
<CardTitle>You're in!</CardTitle>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="mb-2">
|
||||
Your team's attendance for <strong>{data.project.title}</strong> is confirmed.
|
||||
</p>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
We'll be in touch shortly with travel and lunch logistics. You can edit your team
|
||||
selection from your project page closer to the event.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
if (submitState === 'declined' || data.status === 'DECLINED') {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={XCircle}
|
||||
title="Your team has declined"
|
||||
message="If this was a mistake, please contact us at info@monaco-opc.com."
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (data.status === 'EXPIRED') {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="The confirmation deadline has passed"
|
||||
message="If your team still wants to attend, please contact us at info@monaco-opc.com."
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (data.status === 'SUPERSEDED') {
|
||||
return (
|
||||
<FriendlyError
|
||||
icon={AlertCircle}
|
||||
title="This confirmation is no longer active"
|
||||
message="Please contact us at info@monaco-opc.com for details."
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
// ── PENDING: render the form
|
||||
const cap = data.project.program.defaultAttendeeCap
|
||||
const deadline = new Date(data.deadline)
|
||||
const overCap = selected.size > cap
|
||||
const noneSelected = selected.size === 0
|
||||
|
||||
const toggle = (userId: string, checked: boolean) => {
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (checked) next.add(userId)
|
||||
else next.delete(userId)
|
||||
return next
|
||||
})
|
||||
}
|
||||
const toggleVisa = (userId: string, checked: boolean) => {
|
||||
setVisa((prev) => ({ ...prev, [userId]: checked }))
|
||||
}
|
||||
|
||||
const handleConfirm = async () => {
|
||||
setSubmitError(null)
|
||||
try {
|
||||
await confirmMutation.mutateAsync({
|
||||
token,
|
||||
attendingUserIds: Array.from(selected),
|
||||
visaFlags: Object.fromEntries(
|
||||
Array.from(selected).map((uid) => [uid, !!visa[uid]]),
|
||||
),
|
||||
})
|
||||
setSubmitState('confirmed')
|
||||
} catch (err) {
|
||||
setSubmitState('error')
|
||||
const msg =
|
||||
err instanceof TRPCClientError ? err.message : err instanceof Error ? err.message : 'Failed'
|
||||
setSubmitError(msg)
|
||||
}
|
||||
}
|
||||
const handleDecline = async () => {
|
||||
setSubmitError(null)
|
||||
try {
|
||||
await declineMutation.mutateAsync({
|
||||
token,
|
||||
reason: declineReason.trim() || undefined,
|
||||
})
|
||||
setSubmitState('declined')
|
||||
} catch (err) {
|
||||
setSubmitState('error')
|
||||
const msg =
|
||||
err instanceof TRPCClientError ? err.message : err instanceof Error ? err.message : 'Failed'
|
||||
setSubmitError(msg)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mx-auto max-w-xl space-y-6">
|
||||
<Card className="border-primary/40 bg-primary/5">
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<PartyPopper className="text-primary h-5 w-5" />
|
||||
<CardTitle>Congratulations!</CardTitle>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
<p>
|
||||
Your project <strong>{data.project.title}</strong> is a finalist for the Monaco Ocean
|
||||
Protection Challenge grand finale.
|
||||
</p>
|
||||
<div className="bg-background border-amber-300 mt-3 rounded-md border-l-4 p-3 dark:border-amber-700">
|
||||
<p className="text-sm">
|
||||
<strong>Confirm by {formatDeadline(deadline)}.</strong>
|
||||
</p>
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
<CountdownLabel deadline={deadline} />
|
||||
</p>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Who from your team will attend?</CardTitle>
|
||||
<p className="text-muted-foreground text-sm">
|
||||
You can select up to <strong>{cap}</strong> team members. Indicate who needs visa
|
||||
support so we can prepare documents in time.
|
||||
</p>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ul className="space-y-3">
|
||||
{data.project.teamMembers.map((tm) => {
|
||||
const checked = selected.has(tm.userId)
|
||||
return (
|
||||
<li key={tm.userId} className="flex items-start justify-between gap-4">
|
||||
<label className="flex flex-1 items-start gap-3 cursor-pointer">
|
||||
<Checkbox
|
||||
checked={checked}
|
||||
onCheckedChange={(c) => toggle(tm.userId, c === true)}
|
||||
className="mt-0.5"
|
||||
/>
|
||||
<div>
|
||||
<div className="font-medium">{tm.user.name ?? tm.user.email}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{tm.user.email}
|
||||
{tm.role && tm.role !== 'MEMBER' ? ` · ${tm.role.toLowerCase()}` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
{checked && (
|
||||
<label className="flex items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground">Needs visa?</span>
|
||||
<Switch
|
||||
checked={!!visa[tm.userId]}
|
||||
onCheckedChange={(c) => toggleVisa(tm.userId, c)}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
{overCap && (
|
||||
<p className="text-destructive mt-3 text-sm">
|
||||
Please select no more than {cap} member{cap === 1 ? '' : 's'}.
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{submitError && (
|
||||
<div className="border-destructive bg-destructive/10 text-destructive rounded-md border p-3 text-sm">
|
||||
{submitError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="ghost" className="text-muted-foreground">
|
||||
We can't attend
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Decline finalist slot?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
If your team can't attend, we'll offer the slot to a waitlisted team. This
|
||||
action can't be undone from this page.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<div className="space-y-2">
|
||||
<label className="text-muted-foreground text-sm" htmlFor="decline-reason">
|
||||
Reason (optional, helps us improve future editions)
|
||||
</label>
|
||||
<Textarea
|
||||
id="decline-reason"
|
||||
value={declineReason}
|
||||
onChange={(e) => setDeclineReason(e.target.value)}
|
||||
placeholder="e.g. logistics conflict, team disbanded, no longer interested"
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={handleDecline}
|
||||
disabled={declineMutation.isPending}
|
||||
className="bg-destructive hover:bg-destructive/90"
|
||||
>
|
||||
{declineMutation.isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Decline finalist slot'
|
||||
)}
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
|
||||
<Button
|
||||
size="lg"
|
||||
onClick={handleConfirm}
|
||||
disabled={overCap || noneSelected || confirmMutation.isPending}
|
||||
>
|
||||
{confirmMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" /> Confirming…
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<CheckCircle2 className="mr-2 h-4 w-4" />
|
||||
Confirm Attendance
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default function FinalistConfirmPage({ params }: PageProps) {
|
||||
const { token } = use(params)
|
||||
return (
|
||||
<Suspense fallback={<Skeleton className="h-64 w-full" />}>
|
||||
<FinalistConfirmContent token={token} />
|
||||
</Suspense>
|
||||
)
|
||||
}
|
||||
@@ -106,7 +106,6 @@ export default function ProfileSettingsPage() {
|
||||
const handleSaveProfile = async () => {
|
||||
try {
|
||||
await updateProfile.mutateAsync({
|
||||
email: email || undefined,
|
||||
name: name || undefined,
|
||||
bio,
|
||||
phoneNumber: phoneNumber || null,
|
||||
@@ -229,11 +228,13 @@ export default function ProfileSettingsPage() {
|
||||
id="email"
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
readOnly
|
||||
disabled
|
||||
placeholder="you@example.com"
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
This will be used for login and all notification emails.
|
||||
Used for login and notifications. Contact an administrator to
|
||||
change your email address.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
|
||||
17
src/app/api/cron/finalist-confirmations/route.ts
Normal file
17
src/app/api/cron/finalist-confirmations/route.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { NextResponse, type NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { expirePendingPastDeadline } from '@/server/services/finalist-confirmation'
|
||||
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
try {
|
||||
const result = await expirePendingPastDeadline(prisma)
|
||||
return NextResponse.json({ ok: true, ...result })
|
||||
} catch (error) {
|
||||
console.error('[Cron] finalist-confirmations failed:', error)
|
||||
return NextResponse.json({ error: 'Internal error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
69
src/app/api/cron/lunch-recap/route.ts
Normal file
69
src/app/api/cron/lunch-recap/route.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { NextResponse, type NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { sendLunchRecapEmail } from '@/lib/email'
|
||||
import { buildRecapPayload } from '@/server/services/lunch-recap'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
|
||||
/**
|
||||
* Cron: when a lunch event is past its change deadline and admins have
|
||||
* left auto-recap on (cronEnabled), send the recap to admins +
|
||||
* extraRecipients and stamp recapSentAt. Idempotent.
|
||||
*/
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
const now = new Date()
|
||||
const events = await prisma.lunchEvent.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
cronEnabled: true,
|
||||
recapSentAt: null,
|
||||
eventAt: { not: null },
|
||||
},
|
||||
})
|
||||
let sent = 0
|
||||
for (const event of events) {
|
||||
try {
|
||||
if (!event.eventAt) continue
|
||||
const deadline = new Date(
|
||||
event.eventAt.getTime() - event.changeCutoffHours * 3_600_000,
|
||||
)
|
||||
if (now < deadline) continue
|
||||
const payload = await buildRecapPayload(prisma, event.programId)
|
||||
const adminUsers = await prisma.user.findMany({
|
||||
where: {
|
||||
role: { in: ['SUPER_ADMIN', 'PROGRAM_ADMIN'] },
|
||||
email: { not: '' },
|
||||
},
|
||||
select: { email: true },
|
||||
})
|
||||
const recipients = [
|
||||
...adminUsers.map((u) => u.email).filter(Boolean),
|
||||
...event.extraRecipients,
|
||||
]
|
||||
try {
|
||||
await sendLunchRecapEmail(recipients, payload)
|
||||
} catch (e) {
|
||||
console.error('[lunch-recap] email send failed', event.id, e)
|
||||
}
|
||||
await prisma.lunchEvent.update({
|
||||
where: { id: event.id },
|
||||
data: { recapSentAt: new Date() },
|
||||
})
|
||||
await logAudit({
|
||||
prisma,
|
||||
userId: null,
|
||||
action: 'LUNCH_RECAP_SENT',
|
||||
entityType: 'LunchEvent',
|
||||
entityId: event.id,
|
||||
detailsJson: { recipientCount: recipients.length, source: 'cron' },
|
||||
})
|
||||
sent++
|
||||
} catch (e) {
|
||||
console.error('[lunch-recap] event failed', event.id, e)
|
||||
}
|
||||
}
|
||||
return NextResponse.json({ ok: true, sent, processedEvents: events.length })
|
||||
}
|
||||
73
src/app/api/cron/lunch-reminders/route.ts
Normal file
73
src/app/api/cron/lunch-reminders/route.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { NextResponse, type NextRequest } from 'next/server'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { sendLunchReminderEmail } from '@/lib/email'
|
||||
|
||||
/**
|
||||
* Cron: send a single reminder email per attending member who hasn't picked
|
||||
* a lunch dish yet, when we're inside the reminder window
|
||||
* (deadline - reminderHoursBeforeDeadline) <= now < deadline.
|
||||
*
|
||||
* Idempotent — `LunchEvent.reminderSentAt` blocks repeat sends.
|
||||
*/
|
||||
export async function GET(request: NextRequest): Promise<NextResponse> {
|
||||
const cronSecret = request.headers.get('x-cron-secret')
|
||||
if (!cronSecret || cronSecret !== process.env.CRON_SECRET) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
const now = new Date()
|
||||
const events = await prisma.lunchEvent.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
reminderSentAt: null,
|
||||
reminderHoursBeforeDeadline: { not: null },
|
||||
eventAt: { not: null },
|
||||
},
|
||||
})
|
||||
let sent = 0
|
||||
for (const event of events) {
|
||||
try {
|
||||
if (!event.eventAt || event.reminderHoursBeforeDeadline == null) continue
|
||||
const deadline = new Date(
|
||||
event.eventAt.getTime() - event.changeCutoffHours * 3_600_000,
|
||||
)
|
||||
const reminderAt = new Date(
|
||||
deadline.getTime() - event.reminderHoursBeforeDeadline * 3_600_000,
|
||||
)
|
||||
if (now < reminderAt || now >= deadline) continue
|
||||
|
||||
const ams = await prisma.attendingMember.findMany({
|
||||
where: {
|
||||
confirmation: {
|
||||
project: { programId: event.programId },
|
||||
status: 'CONFIRMED',
|
||||
},
|
||||
lunchPick: { is: { pickedAt: null } },
|
||||
},
|
||||
include: { user: { select: { name: true, email: true } } },
|
||||
})
|
||||
for (const am of ams) {
|
||||
if (!am.user.email) continue
|
||||
try {
|
||||
await sendLunchReminderEmail({
|
||||
to: am.user.email,
|
||||
memberName: am.user.name ?? am.user.email,
|
||||
eventAt: event.eventAt,
|
||||
venue: event.venue,
|
||||
changeDeadline: deadline,
|
||||
pickUrl: `${process.env.NEXTAUTH_URL ?? ''}/applicant`,
|
||||
})
|
||||
sent++
|
||||
} catch (e) {
|
||||
console.error('[lunch-reminders] send failed for', am.user.email, e)
|
||||
}
|
||||
}
|
||||
await prisma.lunchEvent.update({
|
||||
where: { id: event.id },
|
||||
data: { reminderSentAt: new Date() },
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('[lunch-reminders] event failed', event.id, e)
|
||||
}
|
||||
}
|
||||
return NextResponse.json({ ok: true, sent, processedEvents: events.length })
|
||||
}
|
||||
@@ -30,21 +30,21 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
// Authorization: must be admin or assigned jury/mentor for this project
|
||||
const isAdmin = userRole === 'SUPER_ADMIN' || userRole === 'PROGRAM_ADMIN'
|
||||
|
||||
// Per-round scope: jurors may only pull URLs for files in rounds with
|
||||
// sortOrder <= their assigned round. Mirrors file.getDownloadUrl. Without
|
||||
// this, a juror assigned to EVALUATION could bulk-download LIVE_FINAL
|
||||
// confidential files via this endpoint.
|
||||
let priorRoundIds: string[] | null = null
|
||||
|
||||
if (!isAdmin) {
|
||||
// Check if user is assigned as jury
|
||||
const juryAssignment = await prisma.assignment.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
projectId,
|
||||
},
|
||||
where: { userId, projectId },
|
||||
select: { id: true, roundId: true },
|
||||
})
|
||||
|
||||
// Check if user is assigned as mentor
|
||||
const mentorAssignment = await prisma.mentorAssignment.findFirst({
|
||||
where: {
|
||||
mentorId: userId,
|
||||
projectId,
|
||||
},
|
||||
where: { mentorId: userId, projectId },
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (!juryAssignment && !mentorAssignment) {
|
||||
@@ -53,14 +53,41 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
|
||||
{ status: 403 }
|
||||
)
|
||||
}
|
||||
|
||||
// Apply the per-round filter only when access is jury-only.
|
||||
if (juryAssignment && !mentorAssignment) {
|
||||
const assignedRound = await prisma.round.findUnique({
|
||||
where: { id: juryAssignment.roundId },
|
||||
select: { competitionId: true, sortOrder: true },
|
||||
})
|
||||
if (assignedRound) {
|
||||
const priorOrCurrent = await prisma.round.findMany({
|
||||
where: {
|
||||
competitionId: assignedRound.competitionId,
|
||||
sortOrder: { lte: assignedRound.sortOrder },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
priorRoundIds = priorOrCurrent.map((r) => r.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch file metadata from DB
|
||||
const fileWhere: Record<string, unknown> = {
|
||||
id: { in: fileIds },
|
||||
projectId,
|
||||
}
|
||||
if (priorRoundIds !== null) {
|
||||
fileWhere.OR = [
|
||||
{ requirement: { roundId: { in: priorRoundIds } } },
|
||||
{ requirementId: null, roundId: { in: priorRoundIds } },
|
||||
{ requirementId: null, roundId: null },
|
||||
]
|
||||
}
|
||||
|
||||
const files = await prisma.projectFile.findMany({
|
||||
where: {
|
||||
id: { in: fileIds },
|
||||
projectId,
|
||||
},
|
||||
where: fileWhere,
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
|
||||
@@ -4,28 +4,27 @@ import Image from 'next/image'
|
||||
import { auth } from '@/lib/auth'
|
||||
import { redirect } from 'next/navigation'
|
||||
import type { Route } from 'next'
|
||||
import type { UserRole } from '@prisma/client'
|
||||
|
||||
export const metadata: Metadata = { title: 'Monaco Ocean Protection Challenge' }
|
||||
|
||||
export default async function HomePage() {
|
||||
const session = await auth()
|
||||
|
||||
// Redirect authenticated users to their appropriate dashboard
|
||||
// Redirect authenticated users to their appropriate dashboard.
|
||||
// Reads the multi-role array (roles[]) so a user who is e.g. JURY_MEMBER+MENTOR
|
||||
// lands on /jury (their highest-priority role) rather than always falling
|
||||
// through on the singular `role` field. The context-aware variant —
|
||||
// user.getDefaultDashboard tRPC procedure — exists for surfaces that can call
|
||||
// tRPC; page.tsx uses static priority for simplicity.
|
||||
if (session?.user) {
|
||||
if (
|
||||
session.user.role === 'SUPER_ADMIN' ||
|
||||
session.user.role === 'PROGRAM_ADMIN'
|
||||
) {
|
||||
redirect('/admin')
|
||||
} else if (session.user.role === 'JURY_MEMBER') {
|
||||
redirect('/jury')
|
||||
} else if (session.user.role === 'MENTOR') {
|
||||
redirect('/mentor' as Route)
|
||||
} else if (session.user.role === 'OBSERVER') {
|
||||
redirect('/observer')
|
||||
} else if (session.user.role === 'APPLICANT') {
|
||||
redirect('/applicant' as Route)
|
||||
}
|
||||
const roles = (session.user.roles as UserRole[] | undefined) ?? [session.user.role as UserRole]
|
||||
if (roles.includes('SUPER_ADMIN') || roles.includes('PROGRAM_ADMIN')) redirect('/admin')
|
||||
if (roles.includes('AWARD_MASTER')) redirect('/award-master')
|
||||
if (roles.includes('JURY_MEMBER')) redirect('/jury')
|
||||
if (roles.includes('MENTOR')) redirect('/mentor' as Route)
|
||||
if (roles.includes('APPLICANT')) redirect('/applicant' as Route)
|
||||
if (roles.includes('OBSERVER')) redirect('/observer')
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
162
src/components/admin/grand-finale/finalist-slots-card.tsx
Normal file
162
src/components/admin/grand-finale/finalist-slots-card.tsx
Normal file
@@ -0,0 +1,162 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Loader2, Save, Trophy } from 'lucide-react'
|
||||
import { formatEnumLabel } from '@/lib/utils'
|
||||
import type { CompetitionCategory } from '@prisma/client'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
const CATEGORIES: CompetitionCategory[] = ['STARTUP', 'BUSINESS_CONCEPT']
|
||||
|
||||
type Row = {
|
||||
category: CompetitionCategory
|
||||
quota: number
|
||||
confirmed: number
|
||||
pending: number
|
||||
}
|
||||
|
||||
export function FinalistSlotsCard({ programId }: Props) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: quotas, isLoading: loadingQuotas } = trpc.finalist.listQuotas.useQuery({
|
||||
programId,
|
||||
})
|
||||
const { data: counts, isLoading: loadingCounts } = trpc.finalist.listCategoryCounts.useQuery({
|
||||
programId,
|
||||
})
|
||||
|
||||
const [draft, setDraft] = useState<Record<CompetitionCategory, string>>({
|
||||
STARTUP: '',
|
||||
BUSINESS_CONCEPT: '',
|
||||
})
|
||||
|
||||
// Sync draft from server response on first load / after save
|
||||
useEffect(() => {
|
||||
if (!quotas) return
|
||||
const next: Record<CompetitionCategory, string> = { STARTUP: '', BUSINESS_CONCEPT: '' }
|
||||
for (const cat of CATEGORIES) {
|
||||
const found = quotas.find((q) => q.category === cat)
|
||||
next[cat] = found ? String(found.quota) : ''
|
||||
}
|
||||
setDraft(next)
|
||||
}, [quotas])
|
||||
|
||||
const setQuotaMutation = trpc.finalist.setQuota.useMutation({
|
||||
onSuccess: (_, vars) => {
|
||||
toast.success(`${formatEnumLabel(vars.category)} quota saved`)
|
||||
utils.finalist.listQuotas.invalidate({ programId })
|
||||
utils.finalist.listCategoryCounts.invalidate({ programId })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
if (loadingQuotas || loadingCounts) {
|
||||
return <Skeleton className="h-44 w-full rounded-md" />
|
||||
}
|
||||
|
||||
const rows: Row[] = CATEGORIES.map((cat) => {
|
||||
const q = quotas?.find((x) => x.category === cat)
|
||||
const c = counts?.find((x) => x.category === cat)
|
||||
return {
|
||||
category: cat,
|
||||
quota: q?.quota ?? 0,
|
||||
confirmed: c?.confirmed ?? 0,
|
||||
pending: c?.pending ?? 0,
|
||||
}
|
||||
})
|
||||
|
||||
const handleSave = (category: CompetitionCategory) => {
|
||||
const raw = draft[category]
|
||||
const n = Number.parseInt(raw, 10)
|
||||
if (Number.isNaN(n) || n < 0) {
|
||||
toast.error('Quota must be a non-negative integer')
|
||||
return
|
||||
}
|
||||
setQuotaMutation.mutate({ programId, category, quota: n })
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<Trophy className="text-muted-foreground h-4 w-4" />
|
||||
<CardTitle className="text-base">Finalist slots</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
Per-category quotas. Reductions blocked when {`> `}confirmed count — un-confirm a team
|
||||
first if you need to shrink a category.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="space-y-3">
|
||||
{rows.map((r) => {
|
||||
const isPending =
|
||||
setQuotaMutation.isPending &&
|
||||
setQuotaMutation.variables?.category === r.category
|
||||
const dirty = String(r.quota) !== draft[r.category]
|
||||
return (
|
||||
<div
|
||||
key={r.category}
|
||||
className="flex items-center justify-between gap-3 rounded-md border p-3"
|
||||
>
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="font-medium">{formatEnumLabel(r.category)}</div>
|
||||
<div className="text-muted-foreground mt-0.5 flex flex-wrap items-center gap-x-3 gap-y-1 text-xs">
|
||||
<span>
|
||||
<Badge variant="default" className="text-xs">
|
||||
{r.confirmed}
|
||||
</Badge>{' '}
|
||||
confirmed
|
||||
</span>
|
||||
<span>
|
||||
<Badge variant="secondary" className="text-xs">
|
||||
{r.pending}
|
||||
</Badge>{' '}
|
||||
pending
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
type="number"
|
||||
inputMode="numeric"
|
||||
min={0}
|
||||
className="w-20 tabular-nums"
|
||||
value={draft[r.category]}
|
||||
onChange={(e) =>
|
||||
setDraft((d) => ({ ...d, [r.category]: e.target.value }))
|
||||
}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
variant={dirty ? 'default' : 'outline'}
|
||||
disabled={!dirty || isPending}
|
||||
onClick={() => handleSave(r.category)}
|
||||
>
|
||||
{isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<>
|
||||
<Save className="mr-1 h-3.5 w-3.5" />
|
||||
Save
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
167
src/components/admin/grand-finale/waitlist-card.tsx
Normal file
167
src/components/admin/grand-finale/waitlist-card.tsx
Normal file
@@ -0,0 +1,167 @@
|
||||
'use client'
|
||||
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { ListOrdered, Loader2 } from 'lucide-react'
|
||||
import { formatEnumLabel } from '@/lib/utils'
|
||||
import type { CompetitionCategory } from '@prisma/client'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
const STATUS_LABEL: Record<string, { label: string; variant: 'default' | 'secondary' | 'outline' | 'destructive' }> = {
|
||||
WAITING: { label: 'Waiting', variant: 'outline' },
|
||||
PROMOTED: { label: 'Promoted', variant: 'default' },
|
||||
USED: { label: 'Used', variant: 'secondary' },
|
||||
}
|
||||
|
||||
export function WaitlistCard({ programId }: Props) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data, isLoading } = trpc.finalist.listWaitlist.useQuery({ programId })
|
||||
|
||||
const promoteMutation = trpc.finalist.manualPromote.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Waitlist entry promoted — confirmation email sent')
|
||||
utils.finalist.listWaitlist.invalidate({ programId })
|
||||
utils.finalist.listCategoryCounts.invalidate({ programId })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
if (isLoading) return <Skeleton className="h-44 w-full rounded-md" />
|
||||
|
||||
const byCategory = new Map<CompetitionCategory, typeof data>()
|
||||
for (const entry of data ?? []) {
|
||||
const list = byCategory.get(entry.category) ?? []
|
||||
list.push(entry)
|
||||
byCategory.set(entry.category, list)
|
||||
}
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ListOrdered className="text-muted-foreground h-4 w-4" />
|
||||
<CardTitle className="text-base">Waitlist</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
Per-category ranked waitlist. Auto-cascades when a finalist declines or expires.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<p className="text-muted-foreground py-6 text-center text-sm">
|
||||
No waitlist entries yet.
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<ListOrdered className="text-muted-foreground h-4 w-4" />
|
||||
<CardTitle className="text-base">Waitlist</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
Per-category ranked waitlist. Auto-cascades when a finalist declines or expires. You can
|
||||
manually promote out of order — overrides are audit-logged.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-5">
|
||||
{Array.from(byCategory.entries()).map(([category, entries]) => (
|
||||
<div key={category}>
|
||||
<div className="text-muted-foreground mb-2 text-xs font-medium uppercase tracking-wide">
|
||||
{formatEnumLabel(category)}
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
{(entries ?? []).map((entry) => {
|
||||
const badge = STATUS_LABEL[entry.status] ?? { label: entry.status, variant: 'outline' as const }
|
||||
const canPromote = entry.status === 'WAITING'
|
||||
const isPending =
|
||||
promoteMutation.isPending && promoteMutation.variables?.waitlistEntryId === entry.id
|
||||
return (
|
||||
<div
|
||||
key={entry.id}
|
||||
className="flex items-center justify-between gap-3 rounded-md border p-3"
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="bg-muted flex h-8 w-8 items-center justify-center rounded-full text-sm font-semibold tabular-nums">
|
||||
{entry.rank}
|
||||
</div>
|
||||
<div className="min-w-0">
|
||||
<div className="font-medium">{entry.project.title}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{entry.project.country ?? '—'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Badge variant={badge.variant} className="text-xs">
|
||||
{badge.label}
|
||||
</Badge>
|
||||
{canPromote && (
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button size="sm" variant="outline" disabled={isPending}>
|
||||
{isPending ? (
|
||||
<Loader2 className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
'Promote'
|
||||
)}
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Promote this team out of order?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
{entry.project.title} (rank #{entry.rank}) will be promoted into a
|
||||
finalist slot. A confirmation email will be sent to the team lead
|
||||
with a 24-hour window. This override is audit-logged.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
onClick={() =>
|
||||
promoteMutation.mutate({
|
||||
waitlistEntryId: entry.id,
|
||||
windowHours: 24,
|
||||
})
|
||||
}
|
||||
>
|
||||
Promote
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
235
src/components/admin/logistics/admin-attendance-dialog.tsx
Normal file
235
src/components/admin/logistics/admin-attendance-dialog.tsx
Normal file
@@ -0,0 +1,235 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
export type AttendanceMode = 'confirm' | 'decline'
|
||||
|
||||
export function AdminAttendanceDialog({
|
||||
open,
|
||||
mode,
|
||||
confirmationId,
|
||||
programId,
|
||||
onOpenChange,
|
||||
}: {
|
||||
open: boolean
|
||||
mode: AttendanceMode
|
||||
confirmationId: string | null
|
||||
programId: string
|
||||
onOpenChange: (next: boolean) => void
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const enabled = open && !!confirmationId
|
||||
const { data: detail, isLoading } = trpc.finalist.getConfirmationDetail.useQuery(
|
||||
{ confirmationId: confirmationId ?? '' },
|
||||
{ enabled },
|
||||
)
|
||||
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set())
|
||||
const [visa, setVisa] = useState<Record<string, boolean>>({})
|
||||
const [reason, setReason] = useState('')
|
||||
|
||||
const invalidate = () => {
|
||||
utils.logistics.listConfirmations.invalidate({ programId })
|
||||
}
|
||||
|
||||
const confirmMutation = trpc.finalist.adminConfirm.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Attendance confirmed')
|
||||
invalidate()
|
||||
onOpenChange(false)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const declineMutation = trpc.finalist.adminDecline.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Marked as declined')
|
||||
invalidate()
|
||||
onOpenChange(false)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
// Reset form when the dialog opens for a new row
|
||||
useEffect(() => {
|
||||
if (!open) return
|
||||
setReason('')
|
||||
if (detail) {
|
||||
// Default-pre-select the team lead + up to cap members
|
||||
const cap = detail.project.program.defaultAttendeeCap
|
||||
const initial = new Set(
|
||||
detail.project.teamMembers.slice(0, cap).map((tm) => tm.userId),
|
||||
)
|
||||
setSelected(initial)
|
||||
setVisa({})
|
||||
}
|
||||
}, [open, detail])
|
||||
|
||||
const isPending = confirmMutation.isPending || declineMutation.isPending
|
||||
|
||||
const handleConfirm = () => {
|
||||
if (!confirmationId) return
|
||||
const ids = Array.from(selected)
|
||||
confirmMutation.mutate({
|
||||
confirmationId,
|
||||
attendingUserIds: ids,
|
||||
visaFlags: Object.fromEntries(ids.map((id) => [id, !!visa[id]])),
|
||||
})
|
||||
}
|
||||
const handleDecline = () => {
|
||||
if (!confirmationId) return
|
||||
declineMutation.mutate({
|
||||
confirmationId,
|
||||
reason: reason.trim() || undefined,
|
||||
})
|
||||
}
|
||||
|
||||
const cap = detail?.project.program.defaultAttendeeCap ?? 3
|
||||
const overCap = selected.size > cap
|
||||
const noneSelected = selected.size === 0
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!isPending) onOpenChange(next)
|
||||
}}
|
||||
>
|
||||
<DialogContent className="sm:max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{mode === 'confirm' ? 'Confirm attendance on team behalf' : 'Decline on team behalf'}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
{mode === 'confirm'
|
||||
? 'Use this when the team replied by email. The selected attendees will be locked in just like a public confirmation.'
|
||||
: 'Use this when the team has told us they cannot attend. The slot will cascade to the next waitlist entry.'}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{isLoading || !detail ? (
|
||||
<div className="space-y-2">
|
||||
<Skeleton className="h-6 w-3/4" />
|
||||
<Skeleton className="h-20 w-full" />
|
||||
</div>
|
||||
) : mode === 'confirm' ? (
|
||||
<>
|
||||
<div className="text-sm">
|
||||
<span className="text-muted-foreground">Project:</span>{' '}
|
||||
<strong>{detail.project.title}</strong>
|
||||
</div>
|
||||
<ul className="space-y-2 max-h-[50vh] overflow-y-auto pr-1">
|
||||
{detail.project.teamMembers.map((tm) => {
|
||||
const checked = selected.has(tm.userId)
|
||||
return (
|
||||
<li
|
||||
key={tm.userId}
|
||||
className="flex items-start justify-between gap-4 rounded-md border px-3 py-2"
|
||||
>
|
||||
<label className="flex flex-1 items-start gap-3 cursor-pointer">
|
||||
<Checkbox
|
||||
checked={checked}
|
||||
onCheckedChange={(c) => {
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (c === true) next.add(tm.userId)
|
||||
else next.delete(tm.userId)
|
||||
return next
|
||||
})
|
||||
}}
|
||||
className="mt-0.5"
|
||||
/>
|
||||
<div>
|
||||
<div className="text-sm font-medium">
|
||||
{tm.user.name ?? tm.user.email}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{tm.user.email}
|
||||
{tm.role && tm.role !== 'MEMBER' ? ` · ${tm.role.toLowerCase()}` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
{checked && (
|
||||
<label className="flex items-center gap-2 text-xs">
|
||||
<span className="text-muted-foreground">Visa?</span>
|
||||
<Switch
|
||||
checked={!!visa[tm.userId]}
|
||||
onCheckedChange={(c) =>
|
||||
setVisa((prev) => ({ ...prev, [tm.userId]: c }))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
{overCap && (
|
||||
<p className="text-destructive text-sm">
|
||||
Please select no more than {cap} member{cap === 1 ? '' : 's'}.
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div className="text-sm">
|
||||
<span className="text-muted-foreground">Project:</span>{' '}
|
||||
<strong>{detail.project.title}</strong>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-muted-foreground text-sm" htmlFor="admin-decline-reason">
|
||||
Reason (optional)
|
||||
</label>
|
||||
<Textarea
|
||||
id="admin-decline-reason"
|
||||
value={reason}
|
||||
onChange={(e) => setReason(e.target.value)}
|
||||
placeholder="e.g. logistics conflict, team disbanded, no longer interested"
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={isPending}>
|
||||
Cancel
|
||||
</Button>
|
||||
{mode === 'confirm' ? (
|
||||
<Button
|
||||
onClick={handleConfirm}
|
||||
disabled={!detail || overCap || noneSelected || isPending}
|
||||
>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Confirm attendance
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
onClick={handleDecline}
|
||||
disabled={!detail || isPending}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Mark as declined
|
||||
</Button>
|
||||
)}
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
246
src/components/admin/logistics/confirmations-tab.tsx
Normal file
246
src/components/admin/logistics/confirmations-tab.tsx
Normal file
@@ -0,0 +1,246 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { formatEnumLabel } from '@/lib/utils'
|
||||
import type { FinalistConfirmationStatus } from '@prisma/client'
|
||||
import { AdminAttendanceDialog, type AttendanceMode } from './admin-attendance-dialog'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
type StatusFilter = 'all' | FinalistConfirmationStatus
|
||||
|
||||
const STATUS_BADGE: Record<
|
||||
FinalistConfirmationStatus,
|
||||
{ label: string; variant: 'default' | 'secondary' | 'destructive' | 'outline' }
|
||||
> = {
|
||||
PENDING: { label: 'Pending', variant: 'secondary' },
|
||||
CONFIRMED: { label: 'Confirmed', variant: 'default' },
|
||||
DECLINED: { label: 'Declined', variant: 'destructive' },
|
||||
EXPIRED: { label: 'Expired', variant: 'outline' },
|
||||
SUPERSEDED: { label: 'Superseded', variant: 'outline' },
|
||||
}
|
||||
|
||||
function formatDeadline(d: Date): string {
|
||||
return new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
}).format(d)
|
||||
}
|
||||
|
||||
function relativeFromNow(d: Date): string {
|
||||
const ms = d.getTime() - Date.now()
|
||||
if (ms <= 0) return 'past deadline'
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
const days = Math.floor(hours / 24)
|
||||
if (days >= 1) return `in ${days}d`
|
||||
return `in ${hours}h`
|
||||
}
|
||||
|
||||
export function ConfirmationsTab({ programId }: Props) {
|
||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all')
|
||||
const [dialogState, setDialogState] = useState<{
|
||||
open: boolean
|
||||
mode: AttendanceMode
|
||||
confirmationId: string | null
|
||||
}>({ open: false, mode: 'confirm', confirmationId: null })
|
||||
const { data, isLoading } = trpc.logistics.listConfirmations.useQuery(
|
||||
{ programId },
|
||||
{ refetchInterval: 60_000 },
|
||||
)
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!data) return []
|
||||
return statusFilter === 'all' ? data : data.filter((r) => r.status === statusFilter)
|
||||
}, [data, statusFilter])
|
||||
|
||||
const totals = useMemo(() => {
|
||||
const counts: Record<FinalistConfirmationStatus, number> = {
|
||||
PENDING: 0,
|
||||
CONFIRMED: 0,
|
||||
DECLINED: 0,
|
||||
EXPIRED: 0,
|
||||
SUPERSEDED: 0,
|
||||
}
|
||||
for (const r of data ?? []) counts[r.status]++
|
||||
return counts
|
||||
}, [data])
|
||||
|
||||
const StatusPill = ({ value, label, count }: { value: StatusFilter; label: string; count: number }) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStatusFilter(value)}
|
||||
className={`rounded-md border px-2.5 py-1 text-xs font-medium transition-colors ${
|
||||
statusFilter === value
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'bg-background hover:bg-muted'
|
||||
}`}
|
||||
>
|
||||
{label} <span className="tabular-nums opacity-80">({count})</span>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Card>
|
||||
<CardHeader className="space-y-3">
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<CardTitle className="text-base">All confirmations</CardTitle>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
<StatusPill
|
||||
value="all"
|
||||
label="All"
|
||||
count={(data ?? []).length}
|
||||
/>
|
||||
<StatusPill value="PENDING" label="Pending" count={totals.PENDING} />
|
||||
<StatusPill value="CONFIRMED" label="Confirmed" count={totals.CONFIRMED} />
|
||||
<StatusPill value="DECLINED" label="Declined" count={totals.DECLINED} />
|
||||
<StatusPill value="EXPIRED" label="Expired" count={totals.EXPIRED} />
|
||||
<StatusPill value="SUPERSEDED" label="Superseded" count={totals.SUPERSEDED} />
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<p className="text-muted-foreground py-12 text-center text-sm">
|
||||
{statusFilter === 'all'
|
||||
? 'No finalists have been selected yet. Use the grand-finale round page to send confirmations.'
|
||||
: 'No confirmations match this filter.'}
|
||||
</p>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Project</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Deadline</TableHead>
|
||||
<TableHead className="text-right">Attendees</TableHead>
|
||||
<TableHead>Notes</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filtered.map((r) => {
|
||||
const badge = STATUS_BADGE[r.status]
|
||||
const isPending = r.status === 'PENDING'
|
||||
return (
|
||||
<TableRow key={r.id}>
|
||||
<TableCell>
|
||||
<div className="font-medium">{r.project.title}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{formatEnumLabel(r.category)}
|
||||
{r.project.country && (
|
||||
<>
|
||||
{' · '}
|
||||
{r.project.country}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={badge.variant} className="text-xs">
|
||||
{badge.label}
|
||||
</Badge>
|
||||
{r.promotedFromWaitlistEntryId && (
|
||||
<Badge variant="outline" className="ml-1 text-xs">
|
||||
Waitlist
|
||||
</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
<div>{formatDeadline(new Date(r.deadline))}</div>
|
||||
{r.status === 'PENDING' && (
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{relativeFromNow(new Date(r.deadline))}
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">
|
||||
{r.attendeeCount}
|
||||
</TableCell>
|
||||
<TableCell className="text-muted-foreground max-w-[20rem] truncate text-xs">
|
||||
{r.status === 'DECLINED' && r.declineReason
|
||||
? `Reason: ${r.declineReason}`
|
||||
: r.status === 'CONFIRMED' && r.confirmedAt
|
||||
? `Confirmed ${formatDeadline(new Date(r.confirmedAt))}`
|
||||
: r.status === 'EXPIRED' && r.expiredAt
|
||||
? `Expired ${formatDeadline(new Date(r.expiredAt))}`
|
||||
: '—'}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
{isPending ? (
|
||||
<div className="flex justify-end gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="default"
|
||||
onClick={() =>
|
||||
setDialogState({
|
||||
open: true,
|
||||
mode: 'confirm',
|
||||
confirmationId: r.id,
|
||||
})
|
||||
}
|
||||
>
|
||||
Confirm
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() =>
|
||||
setDialogState({
|
||||
open: true,
|
||||
mode: 'decline',
|
||||
confirmationId: r.id,
|
||||
})
|
||||
}
|
||||
>
|
||||
Decline
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<span className="text-muted-foreground text-xs">—</span>
|
||||
)}
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<AdminAttendanceDialog
|
||||
open={dialogState.open}
|
||||
mode={dialogState.mode}
|
||||
confirmationId={dialogState.confirmationId}
|
||||
programId={programId}
|
||||
onOpenChange={(next) =>
|
||||
setDialogState((prev) => ({ ...prev, open: next }))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
175
src/components/admin/logistics/hotels-tab.tsx
Normal file
175
src/components/admin/logistics/hotels-tab.tsx
Normal file
@@ -0,0 +1,175 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { ExternalLink, Hotel as HotelIcon, Loader2, Save } from 'lucide-react'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
export function HotelsTab({ programId }: Props) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: hotel, isLoading } = trpc.logistics.getHotel.useQuery({ programId })
|
||||
|
||||
const [name, setName] = useState('')
|
||||
const [address, setAddress] = useState('')
|
||||
const [link, setLink] = useState('')
|
||||
const [notes, setNotes] = useState('')
|
||||
|
||||
// Sync form state from server data on first load / after save.
|
||||
useEffect(() => {
|
||||
if (hotel) {
|
||||
setName(hotel.name)
|
||||
setAddress(hotel.address ?? '')
|
||||
setLink(hotel.link ?? '')
|
||||
setNotes(hotel.notes ?? '')
|
||||
}
|
||||
}, [hotel])
|
||||
|
||||
const upsertMutation = trpc.logistics.upsertHotel.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Hotel saved')
|
||||
utils.logistics.getHotel.invalidate({ programId })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const handleSave = () => {
|
||||
if (!name.trim()) {
|
||||
toast.error('Hotel name is required')
|
||||
return
|
||||
}
|
||||
upsertMutation.mutate({
|
||||
programId,
|
||||
name: name.trim(),
|
||||
address: address.trim() || undefined,
|
||||
link: link.trim() || '',
|
||||
notes: notes.trim() || undefined,
|
||||
})
|
||||
}
|
||||
|
||||
if (isLoading) return <Skeleton className="h-96 w-full" />
|
||||
|
||||
const dirty =
|
||||
name !== (hotel?.name ?? '') ||
|
||||
address !== (hotel?.address ?? '') ||
|
||||
link !== (hotel?.link ?? '') ||
|
||||
notes !== (hotel?.notes ?? '')
|
||||
|
||||
return (
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<div className="md:col-span-2">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex items-center gap-2">
|
||||
<HotelIcon className="text-muted-foreground h-4 w-4" />
|
||||
<CardTitle className="text-base">Hotel for this edition</CardTitle>
|
||||
</div>
|
||||
<CardDescription>
|
||||
One hotel per edition. Used in confirmation emails and finalist communications.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="hotel-name">Name *</Label>
|
||||
<Input
|
||||
id="hotel-name"
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="Hôtel de Paris"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="hotel-address">Address</Label>
|
||||
<Textarea
|
||||
id="hotel-address"
|
||||
value={address}
|
||||
onChange={(e) => setAddress(e.target.value)}
|
||||
placeholder="Place du Casino, 98000 Monaco"
|
||||
rows={2}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="hotel-link">Hotel website / booking link</Label>
|
||||
<Input
|
||||
id="hotel-link"
|
||||
type="url"
|
||||
value={link}
|
||||
onChange={(e) => setLink(e.target.value)}
|
||||
placeholder="https://hoteldeparismontecarlo.com"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="hotel-notes">Internal notes</Label>
|
||||
<Textarea
|
||||
id="hotel-notes"
|
||||
value={notes}
|
||||
onChange={(e) => setNotes(e.target.value)}
|
||||
placeholder="Check-in time, special arrangements, etc."
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-end">
|
||||
<Button
|
||||
onClick={handleSave}
|
||||
disabled={!dirty || upsertMutation.isPending}
|
||||
>
|
||||
{upsertMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<Save className="mr-2 h-4 w-4" />
|
||||
)}
|
||||
Save
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="md:col-span-1">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Email preview</CardTitle>
|
||||
<CardDescription>What teams will see in confirmation emails.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{!name.trim() ? (
|
||||
<p className="text-muted-foreground text-sm">Save a hotel to see the preview.</p>
|
||||
) : (
|
||||
<div className="bg-muted/30 rounded-md border p-4 text-sm">
|
||||
<div className="text-muted-foreground mb-1 text-xs uppercase tracking-wide">
|
||||
Your accommodation
|
||||
</div>
|
||||
<div className="font-semibold">{name}</div>
|
||||
{address.trim() && (
|
||||
<div className="text-muted-foreground mt-1 whitespace-pre-line text-xs">
|
||||
{address}
|
||||
</div>
|
||||
)}
|
||||
{link.trim() && (
|
||||
<a
|
||||
href={link}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-primary mt-2 inline-flex items-center gap-1 text-xs hover:underline"
|
||||
>
|
||||
Visit hotel website <ExternalLink className="h-3 w-3" />
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
160
src/components/admin/logistics/lunch-dishes.tsx
Normal file
160
src/components/admin/logistics/lunch-dishes.tsx
Normal file
@@ -0,0 +1,160 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Plus, Pencil, Trash2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const DIETARY_TAGS = ['VEGETARIAN', 'VEGAN', 'GLUTEN_FREE', 'PESCATARIAN'] as const
|
||||
type DietaryTag = (typeof DIETARY_TAGS)[number]
|
||||
|
||||
function formatTag(tag: string): string {
|
||||
return tag.replace('_', ' ').toLowerCase()
|
||||
}
|
||||
|
||||
export function LunchDishes({
|
||||
programId,
|
||||
lunchEventId,
|
||||
}: {
|
||||
programId: string
|
||||
lunchEventId: string
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: dishes } = trpc.lunch.listDishes.useQuery({ lunchEventId })
|
||||
|
||||
const invalidateAll = () => {
|
||||
utils.lunch.listDishes.invalidate({ lunchEventId })
|
||||
utils.lunch.getManifest.invalidate({ programId })
|
||||
}
|
||||
|
||||
const create = trpc.lunch.createDish.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const update = trpc.lunch.updateDish.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const del = trpc.lunch.deleteDish.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const [newName, setNewName] = useState('')
|
||||
const [newTags, setNewTags] = useState<DietaryTag[]>([])
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Dishes</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
{dishes && dishes.length === 0 && (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
Add at least one dish to open picks.
|
||||
</p>
|
||||
)}
|
||||
<ul className="space-y-2">
|
||||
{dishes?.map((d) => (
|
||||
<li
|
||||
key={d.id}
|
||||
className="flex items-center gap-3 rounded-md border p-3"
|
||||
>
|
||||
<span className="font-medium">{d.name}</span>
|
||||
<div className="flex gap-1">
|
||||
{d.dietaryTags.map((t) => (
|
||||
<Badge key={t} variant="outline">
|
||||
{formatTag(t)}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
<div className="ml-auto flex gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
const name = prompt('Edit dish name', d.name)
|
||||
if (name && name !== d.name) {
|
||||
update.mutate({ dishId: d.id, name })
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
if (
|
||||
confirm(
|
||||
`Delete "${d.name}"? Existing picks will go back to "not picked".`,
|
||||
)
|
||||
) {
|
||||
del.mutate({ dishId: d.id })
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
<div className="flex flex-wrap items-center gap-2 border-t pt-4">
|
||||
<Input
|
||||
placeholder="New dish name"
|
||||
value={newName}
|
||||
onChange={(e) => setNewName(e.target.value)}
|
||||
className="max-w-xs"
|
||||
/>
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{DIETARY_TAGS.map((t) => (
|
||||
<Button
|
||||
key={t}
|
||||
size="sm"
|
||||
variant={newTags.includes(t) ? 'default' : 'outline'}
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setNewTags(
|
||||
newTags.includes(t)
|
||||
? newTags.filter((x) => x !== t)
|
||||
: [...newTags, t],
|
||||
)
|
||||
}
|
||||
>
|
||||
{formatTag(t)}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
<Button
|
||||
disabled={!newName.trim() || create.isPending}
|
||||
onClick={() => {
|
||||
if (!newName.trim()) return
|
||||
create.mutate(
|
||||
{
|
||||
lunchEventId,
|
||||
name: newName.trim(),
|
||||
dietaryTags: newTags,
|
||||
sortOrder: dishes?.length ?? 0,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
setNewName('')
|
||||
setNewTags([])
|
||||
},
|
||||
},
|
||||
)
|
||||
}}
|
||||
>
|
||||
<Plus className="mr-1 h-4 w-4" /> Add
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
246
src/components/admin/logistics/lunch-event-config.tsx
Normal file
246
src/components/admin/logistics/lunch-event-config.tsx
Normal file
@@ -0,0 +1,246 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import type { LunchEvent } from '@prisma/client'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { X } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
function toLocalDateTimeInputValue(d: Date | null | undefined): string {
|
||||
if (!d) return ''
|
||||
// datetime-local expects "YYYY-MM-DDTHH:mm" in local time.
|
||||
const pad = (n: number) => String(n).padStart(2, '0')
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(
|
||||
d.getHours(),
|
||||
)}:${pad(d.getMinutes())}`
|
||||
}
|
||||
|
||||
export function LunchEventConfig({
|
||||
programId,
|
||||
event,
|
||||
}: {
|
||||
programId: string
|
||||
event: LunchEvent
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const update = trpc.lunch.updateEvent.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.lunch.getEvent.invalidate({ programId })
|
||||
utils.lunch.getEventForMember.invalidate({ programId })
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const [extraInput, setExtraInput] = useState('')
|
||||
|
||||
const eventAt = event.eventAt ? new Date(event.eventAt) : null
|
||||
const endAt = event.endAt ? new Date(event.endAt) : null
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Event configuration</CardTitle>
|
||||
<CardDescription>Per-edition lunch settings.</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
{/* enabled */}
|
||||
<div className="flex items-center justify-between gap-4 rounded-md border p-4">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="lunch-enabled">Enable lunch event</Label>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
When off, attendees see no banner or picker; admins still see this tab.
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="lunch-enabled"
|
||||
checked={event.enabled}
|
||||
onCheckedChange={(v) => update.mutate({ programId, enabled: v })}
|
||||
disabled={update.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* eventAt */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="event-at">Event start</Label>
|
||||
<Input
|
||||
id="event-at"
|
||||
type="datetime-local"
|
||||
defaultValue={toLocalDateTimeInputValue(eventAt)}
|
||||
onBlur={(e) => {
|
||||
const v = e.target.value
|
||||
update.mutate({ programId, eventAt: v ? new Date(v) : null })
|
||||
}}
|
||||
disabled={update.isPending}
|
||||
className="max-w-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* endAt */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="end-at">Event end (optional)</Label>
|
||||
<Input
|
||||
id="end-at"
|
||||
type="datetime-local"
|
||||
defaultValue={toLocalDateTimeInputValue(endAt)}
|
||||
onBlur={(e) => {
|
||||
const v = e.target.value
|
||||
update.mutate({ programId, endAt: v ? new Date(v) : null })
|
||||
}}
|
||||
disabled={update.isPending}
|
||||
className="max-w-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* venue */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="venue">Venue (optional)</Label>
|
||||
<Input
|
||||
id="venue"
|
||||
defaultValue={event.venue ?? ''}
|
||||
placeholder="e.g. Hôtel Hermitage, Salle Belle Époque"
|
||||
onBlur={(e) =>
|
||||
update.mutate({ programId, venue: e.target.value || null })
|
||||
}
|
||||
disabled={update.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* notes */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="notes">Notes for attendees (optional)</Label>
|
||||
<Textarea
|
||||
id="notes"
|
||||
defaultValue={event.notes ?? ''}
|
||||
placeholder="Wine pairings included. Vegetarian options at table 4."
|
||||
onBlur={(e) =>
|
||||
update.mutate({ programId, notes: e.target.value || null })
|
||||
}
|
||||
disabled={update.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* changeCutoffHours */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="cutoff">Change cutoff (hours before event)</Label>
|
||||
<Input
|
||||
id="cutoff"
|
||||
type="number"
|
||||
min={0}
|
||||
max={720}
|
||||
defaultValue={event.changeCutoffHours}
|
||||
onBlur={(e) => {
|
||||
const n = Number(e.target.value)
|
||||
if (Number.isFinite(n) && n !== event.changeCutoffHours) {
|
||||
update.mutate({ programId, changeCutoffHours: n })
|
||||
}
|
||||
}}
|
||||
disabled={update.isPending}
|
||||
className="max-w-[12rem]"
|
||||
/>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
After this many hours before the event, attendees and team leads can
|
||||
no longer change their picks. Admins always can.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* reminderHoursBeforeDeadline */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="reminder">Reminder (hours before deadline)</Label>
|
||||
<Input
|
||||
id="reminder"
|
||||
type="number"
|
||||
min={0}
|
||||
max={720}
|
||||
defaultValue={event.reminderHoursBeforeDeadline ?? ''}
|
||||
placeholder="Leave blank for no reminder"
|
||||
onBlur={(e) => {
|
||||
const v = e.target.value
|
||||
update.mutate({
|
||||
programId,
|
||||
reminderHoursBeforeDeadline: v === '' ? null : Number(v),
|
||||
})
|
||||
}}
|
||||
disabled={update.isPending}
|
||||
className="max-w-[12rem]"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* cronEnabled */}
|
||||
<div className="flex items-center justify-between gap-4 rounded-md border p-4">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="cron-enabled">Auto-send recap at deadline</Label>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
When on, the platform automatically emails the manifest when the
|
||||
change deadline passes.
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="cron-enabled"
|
||||
checked={event.cronEnabled}
|
||||
onCheckedChange={(v) => update.mutate({ programId, cronEnabled: v })}
|
||||
disabled={update.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* extraRecipients */}
|
||||
<div className="space-y-1.5">
|
||||
<Label>Extra recap recipients (optional)</Label>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
All edition admins receive the recap automatically. Add additional
|
||||
email addresses here (e.g. caterer, event manager).
|
||||
</p>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{event.extraRecipients.map((email) => (
|
||||
<Badge key={email} variant="secondary" className="gap-1">
|
||||
{email}
|
||||
<button
|
||||
type="button"
|
||||
className="ml-1"
|
||||
onClick={() =>
|
||||
update.mutate({
|
||||
programId,
|
||||
extraRecipients: event.extraRecipients.filter(
|
||||
(e) => e !== email,
|
||||
),
|
||||
})
|
||||
}
|
||||
aria-label={`Remove ${email}`}
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</button>
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
<Input
|
||||
placeholder="email@example.com — press Enter to add"
|
||||
value={extraInput}
|
||||
onChange={(e) => setExtraInput(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && extraInput.trim()) {
|
||||
e.preventDefault()
|
||||
const next = [
|
||||
...event.extraRecipients,
|
||||
extraInput.trim(),
|
||||
]
|
||||
update.mutate({ programId, extraRecipients: next })
|
||||
setExtraInput('')
|
||||
}
|
||||
}}
|
||||
className="max-w-sm"
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
341
src/components/admin/logistics/lunch-externals.tsx
Normal file
341
src/components/admin/logistics/lunch-externals.tsx
Normal file
@@ -0,0 +1,341 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useImperativeHandle, forwardRef } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Plus, Pencil, Trash2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const ALLERGENS = [
|
||||
'GLUTEN',
|
||||
'CRUSTACEANS',
|
||||
'EGGS',
|
||||
'FISH',
|
||||
'PEANUTS',
|
||||
'SOYBEANS',
|
||||
'MILK',
|
||||
'TREE_NUTS',
|
||||
'CELERY',
|
||||
'MUSTARD',
|
||||
'SESAME',
|
||||
'SULPHITES',
|
||||
'LUPIN',
|
||||
'MOLLUSCS',
|
||||
] as const
|
||||
type Allergen = (typeof ALLERGENS)[number]
|
||||
|
||||
const STANDALONE = '__standalone__'
|
||||
const NO_DISH = '__no_dish__'
|
||||
|
||||
type Editing = { mode: 'new' } | { mode: 'edit'; id: string } | null
|
||||
|
||||
export type LunchExternalsHandle = {
|
||||
openEditDialog: (id: string) => void
|
||||
}
|
||||
|
||||
export const LunchExternals = forwardRef<
|
||||
LunchExternalsHandle,
|
||||
{ programId: string; lunchEventId: string }
|
||||
>(function LunchExternals({ programId, lunchEventId }, ref) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: externals } = trpc.lunch.listExternals.useQuery({ lunchEventId })
|
||||
const { data: dishes } = trpc.lunch.listDishes.useQuery({ lunchEventId })
|
||||
const { data: projects } = trpc.program.listFinalistProjects.useQuery({
|
||||
programId,
|
||||
})
|
||||
|
||||
const [editing, setEditing] = useState<Editing>(null)
|
||||
|
||||
useImperativeHandle(
|
||||
ref,
|
||||
() => ({
|
||||
openEditDialog: (id: string) => setEditing({ mode: 'edit', id }),
|
||||
}),
|
||||
[],
|
||||
)
|
||||
|
||||
const invalidateAll = () => {
|
||||
utils.lunch.listExternals.invalidate({ lunchEventId })
|
||||
utils.lunch.getManifest.invalidate({ programId })
|
||||
}
|
||||
|
||||
const create = trpc.lunch.createExternal.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const update = trpc.lunch.updateExternal.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
const del = trpc.lunch.deleteExternal.useMutation({
|
||||
onSuccess: invalidateAll,
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const editingRow =
|
||||
editing?.mode === 'edit'
|
||||
? (externals?.find((e) => e.id === editing.id) ?? null)
|
||||
: null
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between">
|
||||
<span>External attendees</span>
|
||||
<Button size="sm" onClick={() => setEditing({ mode: 'new' })}>
|
||||
<Plus className="mr-1 h-4 w-4" /> Add external
|
||||
</Button>
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{externals?.length === 0 && (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
No external attendees yet. Add jurors, dignitaries, or per-team plus-ones.
|
||||
</p>
|
||||
)}
|
||||
{externals && externals.length > 0 && (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<tbody>
|
||||
{externals.map((e) => (
|
||||
<tr key={e.id} className="border-b last:border-b-0">
|
||||
<td className="py-2 font-medium">{e.name}</td>
|
||||
<td className="text-muted-foreground">
|
||||
{e.project?.title ?? 'Standalone'}
|
||||
</td>
|
||||
<td className="text-muted-foreground">{e.roleNote ?? ''}</td>
|
||||
<td className="text-right">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => setEditing({ mode: 'edit', id: e.id })}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
if (confirm(`Delete external attendee "${e.name}"?`)) {
|
||||
del.mutate({ externalId: e.id })
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
|
||||
{editing && (
|
||||
<ExternalDialog
|
||||
mode={editing.mode}
|
||||
initial={editingRow}
|
||||
dishes={dishes ?? []}
|
||||
projects={projects ?? []}
|
||||
submitting={create.isPending || update.isPending}
|
||||
onClose={() => setEditing(null)}
|
||||
onSubmit={(values) => {
|
||||
if (editing.mode === 'new') {
|
||||
create.mutate(
|
||||
{ lunchEventId, ...values },
|
||||
{ onSuccess: () => setEditing(null) },
|
||||
)
|
||||
} else {
|
||||
update.mutate(
|
||||
{ externalId: editing.id, ...values },
|
||||
{ onSuccess: () => setEditing(null) },
|
||||
)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Card>
|
||||
)
|
||||
})
|
||||
|
||||
function ExternalDialog({
|
||||
mode,
|
||||
initial,
|
||||
dishes,
|
||||
projects,
|
||||
submitting,
|
||||
onClose,
|
||||
onSubmit,
|
||||
}: {
|
||||
mode: 'new' | 'edit'
|
||||
initial: {
|
||||
name: string
|
||||
email: string | null
|
||||
projectId: string | null
|
||||
roleNote: string | null
|
||||
dishId: string | null
|
||||
allergens: string[]
|
||||
allergenOther: string | null
|
||||
} | null
|
||||
dishes: Array<{ id: string; name: string }>
|
||||
projects: Array<{ id: string; title: string }>
|
||||
submitting: boolean
|
||||
onClose: () => void
|
||||
onSubmit: (values: {
|
||||
name: string
|
||||
email?: string
|
||||
projectId?: string | null
|
||||
roleNote?: string
|
||||
dishId?: string | null
|
||||
allergens: Allergen[]
|
||||
allergenOther?: string | null
|
||||
}) => void
|
||||
}) {
|
||||
const [name, setName] = useState(initial?.name ?? '')
|
||||
const [email, setEmail] = useState(initial?.email ?? '')
|
||||
const [projectId, setProjectId] = useState(initial?.projectId ?? '')
|
||||
const [roleNote, setRoleNote] = useState(initial?.roleNote ?? '')
|
||||
const [dishId, setDishId] = useState(initial?.dishId ?? '')
|
||||
const [allergens, setAllergens] = useState<Allergen[]>(
|
||||
(initial?.allergens as Allergen[]) ?? [],
|
||||
)
|
||||
const [allergenOther, setAllergenOther] = useState(initial?.allergenOther ?? '')
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={(o) => { if (!o) onClose() }}>
|
||||
<DialogContent className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{mode === 'new' ? 'Add external attendee' : 'Edit external attendee'}
|
||||
</DialogTitle>
|
||||
</DialogHeader>
|
||||
<div className="space-y-4">
|
||||
<div>
|
||||
<Label>Name *</Label>
|
||||
<Input value={name} onChange={(e) => setName(e.target.value)} />
|
||||
</div>
|
||||
<div>
|
||||
<Label>Email (optional)</Label>
|
||||
<Input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Project (optional)</Label>
|
||||
<Select
|
||||
value={projectId === '' ? STANDALONE : projectId}
|
||||
onValueChange={(v) => setProjectId(v === STANDALONE ? '' : v)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={STANDALONE}>Standalone</SelectItem>
|
||||
{projects.map((p) => (
|
||||
<SelectItem key={p.id} value={p.id}>
|
||||
{p.title}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Role / note (optional)</Label>
|
||||
<Input
|
||||
value={roleNote}
|
||||
onChange={(e) => setRoleNote(e.target.value)}
|
||||
placeholder="e.g. Foundation rep, Speaker, Sponsor"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Dish</Label>
|
||||
<Select
|
||||
value={dishId === '' ? NO_DISH : dishId}
|
||||
onValueChange={(v) => setDishId(v === NO_DISH ? '' : v)}
|
||||
>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Not picked" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_DISH}>Not picked</SelectItem>
|
||||
{dishes.map((d) => (
|
||||
<SelectItem key={d.id} value={d.id}>
|
||||
{d.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Allergens</Label>
|
||||
<div className="grid grid-cols-2 gap-2 sm:grid-cols-3">
|
||||
{ALLERGENS.map((a) => (
|
||||
<label key={a} className="flex items-center gap-2 text-sm">
|
||||
<Checkbox
|
||||
checked={allergens.includes(a)}
|
||||
onCheckedChange={(v) =>
|
||||
setAllergens(
|
||||
v ? [...allergens, a] : allergens.filter((x) => x !== a),
|
||||
)
|
||||
}
|
||||
/>
|
||||
{a.replace('_', ' ').toLowerCase()}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<Label>Other allergens / notes (optional)</Label>
|
||||
<Textarea
|
||||
value={allergenOther}
|
||||
onChange={(e) => setAllergenOther(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
disabled={!name.trim() || submitting}
|
||||
onClick={() =>
|
||||
onSubmit({
|
||||
name: name.trim(),
|
||||
email: email.trim() || undefined,
|
||||
projectId: projectId || null,
|
||||
roleNote: roleNote.trim() || undefined,
|
||||
dishId: dishId || null,
|
||||
allergens,
|
||||
allergenOther: allergenOther.trim() || null,
|
||||
})
|
||||
}
|
||||
>
|
||||
Save
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
254
src/components/admin/logistics/lunch-manifest.tsx
Normal file
254
src/components/admin/logistics/lunch-manifest.tsx
Normal file
@@ -0,0 +1,254 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useMemo } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
SheetDescription,
|
||||
} from '@/components/ui/sheet'
|
||||
import { LunchPickForm } from '@/components/applicant/lunch-pick-form'
|
||||
import { Pencil, Download } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
function formatAllergens(allergens: string[], other: string | null): string {
|
||||
return [...allergens.map((a) => a.replace('_', ' ').toLowerCase()), other]
|
||||
.filter(Boolean)
|
||||
.join(', ')
|
||||
}
|
||||
|
||||
function DownloadCsvButton({ programId }: { programId: string }) {
|
||||
const utils = trpc.useUtils()
|
||||
const [pending, setPending] = useState(false)
|
||||
return (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={pending}
|
||||
onClick={async () => {
|
||||
setPending(true)
|
||||
try {
|
||||
const csv = await utils.client.lunch.exportManifestCsv.query({ programId })
|
||||
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8' })
|
||||
const url = URL.createObjectURL(blob)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = 'lunch-manifest.csv'
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(url)
|
||||
} catch (e) {
|
||||
toast.error((e as Error).message)
|
||||
} finally {
|
||||
setPending(false)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Download className="mr-1 h-4 w-4" /> Download CSV
|
||||
</Button>
|
||||
)
|
||||
}
|
||||
|
||||
export function LunchManifest({
|
||||
programId,
|
||||
onEditExternal,
|
||||
}: {
|
||||
programId: string
|
||||
onEditExternal?: (externalId: string) => void
|
||||
}) {
|
||||
const { data } = trpc.lunch.getManifest.useQuery({ programId })
|
||||
const [search, setSearch] = useState('')
|
||||
const [missingOnly, setMissingOnly] = useState(false)
|
||||
const [editingMemberId, setEditingMemberId] = useState<string | null>(null)
|
||||
const editingMember = data?.members.find(
|
||||
(m) => m.attendingMemberId === editingMemberId,
|
||||
)
|
||||
|
||||
type Row =
|
||||
| (NonNullable<typeof data>['members'][number] & { sortKey: string })
|
||||
| (NonNullable<typeof data>['externals'][number] & { sortKey: string })
|
||||
|
||||
const rows: Row[] = useMemo(() => {
|
||||
if (!data) return []
|
||||
const all: Row[] = [
|
||||
...data.members.map((m) => ({
|
||||
...m,
|
||||
sortKey: `0-${m.project?.name ?? ''}-${m.name}`,
|
||||
})),
|
||||
...data.externals.map((e) => ({
|
||||
...e,
|
||||
sortKey: `1-${e.project?.name ?? ''}-${e.name}`,
|
||||
})),
|
||||
]
|
||||
return all
|
||||
.filter(
|
||||
(r) =>
|
||||
!search ||
|
||||
(r.project?.name ?? '').toLowerCase().includes(search.toLowerCase()) ||
|
||||
r.name.toLowerCase().includes(search.toLowerCase()),
|
||||
)
|
||||
.filter((r) => !missingOnly || !r.dish)
|
||||
.sort((a, b) => a.sortKey.localeCompare(b.sortKey))
|
||||
}, [data, search, missingOnly])
|
||||
|
||||
if (!data) return null
|
||||
|
||||
// Aggregate dietary + allergen counts client-side for the summary chip
|
||||
const dietaryCounts: Record<string, number> = {}
|
||||
const allergenCounts: Record<string, number> = {}
|
||||
const allRows = [...data.members, ...data.externals]
|
||||
for (const r of allRows) {
|
||||
if (r.dish) {
|
||||
for (const t of r.dish.dietaryTags) {
|
||||
dietaryCounts[t] = (dietaryCounts[t] ?? 0) + 1
|
||||
}
|
||||
}
|
||||
for (const a of r.allergens) {
|
||||
allergenCounts[a] = (allergenCounts[a] ?? 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex flex-wrap items-center gap-2">
|
||||
<span>Manifest</span>
|
||||
<Badge variant="outline">
|
||||
{data.summary.picked}/{data.summary.total} picked
|
||||
{data.summary.missing > 0 ? ` · ${data.summary.missing} missing` : ''}
|
||||
</Badge>
|
||||
{Object.entries(dietaryCounts).map(([tag, n]) => (
|
||||
<Badge key={tag} variant="secondary">
|
||||
{n} {tag.replace('_', ' ').toLowerCase()}
|
||||
</Badge>
|
||||
))}
|
||||
{Object.entries(allergenCounts).map(([a, n]) => (
|
||||
<Badge key={a} variant="destructive">
|
||||
{n} {a.replace('_', ' ').toLowerCase()}
|
||||
</Badge>
|
||||
))}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex flex-wrap items-center gap-4">
|
||||
<Input
|
||||
placeholder="Filter by team or name"
|
||||
value={search}
|
||||
onChange={(e) => setSearch(e.target.value)}
|
||||
className="max-w-xs"
|
||||
/>
|
||||
<div className="flex items-center gap-2">
|
||||
<Switch
|
||||
id="missing-only"
|
||||
checked={missingOnly}
|
||||
onCheckedChange={setMissingOnly}
|
||||
/>
|
||||
<Label htmlFor="missing-only">Missing picks only</Label>
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<DownloadCsvButton programId={programId} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="text-muted-foreground border-b text-left">
|
||||
<tr>
|
||||
<th className="py-2 font-medium">Team</th>
|
||||
<th className="font-medium">Attendee</th>
|
||||
<th className="font-medium">Type</th>
|
||||
<th className="font-medium">Dish</th>
|
||||
<th className="font-medium">Allergens</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((r) => {
|
||||
const id =
|
||||
r.kind === 'MEMBER' ? r.attendingMemberId : r.externalId
|
||||
return (
|
||||
<tr key={id} className="border-b">
|
||||
<td className="py-2">{r.project?.name ?? '—'}</td>
|
||||
<td>{r.name}</td>
|
||||
<td>
|
||||
<Badge variant="outline">
|
||||
{r.kind === 'MEMBER' ? 'Member' : 'External'}
|
||||
</Badge>
|
||||
</td>
|
||||
<td>
|
||||
{r.dish ? (
|
||||
r.dish.name
|
||||
) : (
|
||||
<span className="text-muted-foreground">not picked</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="text-muted-foreground">
|
||||
{formatAllergens(r.allergens, r.allergenOther)}
|
||||
</td>
|
||||
<td className="text-right">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={() => {
|
||||
if (r.kind === 'EXTERNAL') {
|
||||
onEditExternal?.(r.externalId)
|
||||
} else {
|
||||
setEditingMemberId(r.attendingMemberId)
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
</td>
|
||||
</tr>
|
||||
)
|
||||
})}
|
||||
{rows.length === 0 && (
|
||||
<tr>
|
||||
<td colSpan={6} className="text-muted-foreground py-6 text-center">
|
||||
No rows match the current filter.
|
||||
</td>
|
||||
</tr>
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</CardContent>
|
||||
|
||||
<Sheet
|
||||
open={!!editingMemberId}
|
||||
onOpenChange={(o) => { if (!o) setEditingMemberId(null) }}
|
||||
>
|
||||
<SheetContent side="right" className="w-full sm:max-w-md">
|
||||
<SheetHeader>
|
||||
<SheetTitle>Edit lunch pick</SheetTitle>
|
||||
{editingMember && (
|
||||
<SheetDescription>
|
||||
{editingMember.name} · {editingMember.project?.name}
|
||||
</SheetDescription>
|
||||
)}
|
||||
</SheetHeader>
|
||||
{editingMemberId && data?.event && (
|
||||
<div className="mt-6">
|
||||
<LunchPickForm
|
||||
attendingMemberId={editingMemberId}
|
||||
programId={programId}
|
||||
lunchEventId={data.event.id}
|
||||
canEdit
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
147
src/components/admin/logistics/lunch-recap-actions.tsx
Normal file
147
src/components/admin/logistics/lunch-recap-actions.tsx
Normal file
@@ -0,0 +1,147 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogFooter,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Send, Eye } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
export function LunchRecapActions({
|
||||
programId,
|
||||
recapSentAt,
|
||||
extraRecipientCount,
|
||||
}: {
|
||||
programId: string
|
||||
recapSentAt: Date | null
|
||||
extraRecipientCount: number
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const [previewOpen, setPreviewOpen] = useState(false)
|
||||
|
||||
const send = trpc.lunch.sendRecap.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.lunch.getEvent.invalidate({ programId })
|
||||
toast.success('Recap sent')
|
||||
},
|
||||
onError: (e) => {
|
||||
if (e.data?.code === 'PRECONDITION_FAILED') {
|
||||
if (
|
||||
confirm(
|
||||
"You've already sent a recap. Send updated version to all recipients?",
|
||||
)
|
||||
) {
|
||||
send.mutate({ programId, forceUpdate: true })
|
||||
}
|
||||
} else {
|
||||
toast.error(e.message)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
const { data: preview, isLoading: loadingPreview } =
|
||||
trpc.lunch.getRecapPreview.useQuery(
|
||||
{ programId },
|
||||
{ enabled: previewOpen },
|
||||
)
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Recap</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button variant="outline" onClick={() => setPreviewOpen(true)}>
|
||||
<Eye className="mr-2 h-4 w-4" /> Preview recap
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => send.mutate({ programId })}
|
||||
disabled={send.isPending}
|
||||
>
|
||||
<Send className="mr-2 h-4 w-4" /> Send recap now
|
||||
</Button>
|
||||
</div>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
{recapSentAt
|
||||
? `Last sent: ${new Date(recapSentAt).toLocaleString()}. Recipients: edition admins${extraRecipientCount > 0 ? ` + ${extraRecipientCount} extra` : ''}.`
|
||||
: 'Recap has not been sent yet.'}
|
||||
</p>
|
||||
</CardContent>
|
||||
|
||||
<Dialog open={previewOpen} onOpenChange={setPreviewOpen}>
|
||||
<DialogContent className="max-w-3xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Recap preview</DialogTitle>
|
||||
</DialogHeader>
|
||||
{loadingPreview && (
|
||||
<p className="text-muted-foreground text-sm">Loading…</p>
|
||||
)}
|
||||
{preview && (
|
||||
<div className="space-y-4 text-sm">
|
||||
<p>
|
||||
<strong>
|
||||
{preview.summary.picked}/{preview.summary.total}
|
||||
</strong>{' '}
|
||||
picked
|
||||
{preview.summary.missing > 0
|
||||
? ` · ${preview.summary.missing} missing`
|
||||
: ''}
|
||||
</p>
|
||||
{Object.keys(preview.dishCounts).length > 0 && (
|
||||
<div>
|
||||
<h4 className="font-medium">Dishes</h4>
|
||||
<ul className="ml-4 list-disc">
|
||||
{Object.entries(preview.dishCounts).map(([n, c]) => (
|
||||
<li key={n}>
|
||||
{c}× {n}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
{Object.keys(preview.dietaryCounts).length > 0 && (
|
||||
<div>
|
||||
<h4 className="font-medium">Dietary tags</h4>
|
||||
<ul className="ml-4 list-disc">
|
||||
{Object.entries(preview.dietaryCounts).map(([n, c]) => (
|
||||
<li key={n}>
|
||||
{c}× {n.replace('_', ' ').toLowerCase()}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<h4 className="font-medium">Allergens</h4>
|
||||
{Object.keys(preview.allergenCounts).length === 0 ? (
|
||||
<p className="text-muted-foreground">None reported.</p>
|
||||
) : (
|
||||
<ul className="ml-4 list-disc">
|
||||
{Object.entries(preview.allergenCounts).map(([n, c]) => (
|
||||
<li key={n}>
|
||||
{c}× {n.replace('_', ' ').toLowerCase()}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setPreviewOpen(false)}>
|
||||
Close
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
42
src/components/admin/logistics/lunch-tab.tsx
Normal file
42
src/components/admin/logistics/lunch-tab.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
'use client'
|
||||
|
||||
import { useRef } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { LunchEventConfig } from './lunch-event-config'
|
||||
import { LunchDishes } from './lunch-dishes'
|
||||
import { LunchManifest } from './lunch-manifest'
|
||||
import { LunchExternals, type LunchExternalsHandle } from './lunch-externals'
|
||||
import { LunchRecapActions } from './lunch-recap-actions'
|
||||
|
||||
export function LunchTab({ programId }: { programId: string }) {
|
||||
const { data: event, isLoading } = trpc.lunch.getEvent.useQuery({ programId })
|
||||
const externalsRef = useRef<LunchExternalsHandle>(null)
|
||||
if (isLoading || !event) {
|
||||
return <Skeleton className="h-48 w-full" />
|
||||
}
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<LunchEventConfig programId={programId} event={event} />
|
||||
{event.enabled && (
|
||||
<>
|
||||
<LunchDishes programId={programId} lunchEventId={event.id} />
|
||||
<LunchManifest
|
||||
programId={programId}
|
||||
onEditExternal={(id) => externalsRef.current?.openEditDialog(id)}
|
||||
/>
|
||||
<LunchExternals
|
||||
ref={externalsRef}
|
||||
programId={programId}
|
||||
lunchEventId={event.id}
|
||||
/>
|
||||
<LunchRecapActions
|
||||
programId={programId}
|
||||
recapSentAt={event.recapSentAt}
|
||||
extraRecipientCount={event.extraRecipients.length}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
426
src/components/admin/logistics/travel-tab.tsx
Normal file
426
src/components/admin/logistics/travel-tab.tsx
Normal file
@@ -0,0 +1,426 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetDescription,
|
||||
SheetFooter,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
} from '@/components/ui/sheet'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { Loader2, Plane } from 'lucide-react'
|
||||
import type { FlightDetailStatus } from '@prisma/client'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
type AttendeeRow = {
|
||||
id: string
|
||||
needsVisa: boolean
|
||||
user: { id: string; name: string | null; email: string; country: string | null }
|
||||
confirmation: {
|
||||
project: {
|
||||
id: string
|
||||
title: string
|
||||
country: string | null
|
||||
competitionCategory: string | null
|
||||
}
|
||||
}
|
||||
flightDetail: {
|
||||
id: string
|
||||
arrivalAt: Date | null
|
||||
arrivalFlightNumber: string | null
|
||||
arrivalAirport: string | null
|
||||
departureAt: Date | null
|
||||
departureFlightNumber: string | null
|
||||
departureAirport: string | null
|
||||
status: FlightDetailStatus
|
||||
adminNotes: string | null
|
||||
} | null
|
||||
}
|
||||
|
||||
type StatusFilter = 'all' | 'PENDING' | 'CONFIRMED' | 'unfilled'
|
||||
|
||||
function formatDateTime(d: Date | null): string {
|
||||
if (!d) return '—'
|
||||
return new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
}).format(new Date(d))
|
||||
}
|
||||
|
||||
function isoLocalForInput(d: Date | null): string {
|
||||
if (!d) return ''
|
||||
// Format as 'YYYY-MM-DDTHH:mm' for datetime-local input
|
||||
const local = new Date(d.getTime() - new Date().getTimezoneOffset() * 60000)
|
||||
return local.toISOString().slice(0, 16)
|
||||
}
|
||||
|
||||
function FlightEditorSheet({
|
||||
attendee,
|
||||
programId,
|
||||
open,
|
||||
onClose,
|
||||
}: {
|
||||
attendee: AttendeeRow | null
|
||||
programId: string
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const [arrivalAt, setArrivalAt] = useState('')
|
||||
const [arrivalFlightNumber, setArrivalFlightNumber] = useState('')
|
||||
const [arrivalAirport, setArrivalAirport] = useState('')
|
||||
const [departureAt, setDepartureAt] = useState('')
|
||||
const [departureFlightNumber, setDepartureFlightNumber] = useState('')
|
||||
const [departureAirport, setDepartureAirport] = useState('')
|
||||
const [adminNotes, setAdminNotes] = useState('')
|
||||
|
||||
useEffect(() => {
|
||||
if (!attendee) return
|
||||
const fd = attendee.flightDetail
|
||||
setArrivalAt(isoLocalForInput(fd?.arrivalAt ?? null))
|
||||
setArrivalFlightNumber(fd?.arrivalFlightNumber ?? '')
|
||||
setArrivalAirport(fd?.arrivalAirport ?? '')
|
||||
setDepartureAt(isoLocalForInput(fd?.departureAt ?? null))
|
||||
setDepartureFlightNumber(fd?.departureFlightNumber ?? '')
|
||||
setDepartureAirport(fd?.departureAirport ?? '')
|
||||
setAdminNotes(fd?.adminNotes ?? '')
|
||||
}, [attendee])
|
||||
|
||||
const upsertMutation = trpc.logistics.upsertFlightDetail.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Flight details saved')
|
||||
utils.logistics.listFlightDetails.invalidate({ programId })
|
||||
onClose()
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
if (!attendee) return null
|
||||
|
||||
const handleSave = () => {
|
||||
upsertMutation.mutate({
|
||||
attendingMemberId: attendee.id,
|
||||
arrivalAt: arrivalAt ? new Date(arrivalAt) : null,
|
||||
arrivalFlightNumber: arrivalFlightNumber.trim() || null,
|
||||
arrivalAirport: arrivalAirport.trim().toUpperCase() || null,
|
||||
departureAt: departureAt ? new Date(departureAt) : null,
|
||||
departureFlightNumber: departureFlightNumber.trim() || null,
|
||||
departureAirport: departureAirport.trim().toUpperCase() || null,
|
||||
adminNotes: adminNotes.trim() || null,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={(o) => !o && onClose()}>
|
||||
<SheetContent className="sm:max-w-md overflow-y-auto">
|
||||
<SheetHeader>
|
||||
<SheetTitle>{attendee.user.name ?? attendee.user.email}</SheetTitle>
|
||||
<SheetDescription>
|
||||
{attendee.confirmation.project.title}
|
||||
</SheetDescription>
|
||||
</SheetHeader>
|
||||
|
||||
<div className="space-y-5 py-6">
|
||||
<div className="space-y-3 rounded-md border p-3">
|
||||
<div className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Arrival
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="arr-at">Date & time</Label>
|
||||
<Input
|
||||
id="arr-at"
|
||||
type="datetime-local"
|
||||
value={arrivalAt}
|
||||
onChange={(e) => setArrivalAt(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="arr-flight">Flight number</Label>
|
||||
<Input
|
||||
id="arr-flight"
|
||||
value={arrivalFlightNumber}
|
||||
onChange={(e) => setArrivalFlightNumber(e.target.value)}
|
||||
placeholder="AF7400"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="arr-airport">Airport (IATA)</Label>
|
||||
<Input
|
||||
id="arr-airport"
|
||||
value={arrivalAirport}
|
||||
onChange={(e) => setArrivalAirport(e.target.value)}
|
||||
placeholder="NCE"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-3 rounded-md border p-3">
|
||||
<div className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Departure
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="dep-at">Date & time</Label>
|
||||
<Input
|
||||
id="dep-at"
|
||||
type="datetime-local"
|
||||
value={departureAt}
|
||||
onChange={(e) => setDepartureAt(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="dep-flight">Flight number</Label>
|
||||
<Input
|
||||
id="dep-flight"
|
||||
value={departureFlightNumber}
|
||||
onChange={(e) => setDepartureFlightNumber(e.target.value)}
|
||||
placeholder="AF7405"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="dep-airport">Airport (IATA)</Label>
|
||||
<Input
|
||||
id="dep-airport"
|
||||
value={departureAirport}
|
||||
onChange={(e) => setDepartureAirport(e.target.value)}
|
||||
placeholder="NCE"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="notes">Admin notes</Label>
|
||||
<Textarea
|
||||
id="notes"
|
||||
value={adminNotes}
|
||||
onChange={(e) => setAdminNotes(e.target.value)}
|
||||
placeholder="e.g. paid by program, awaiting receipt"
|
||||
rows={3}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<SheetFooter>
|
||||
<Button variant="outline" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} disabled={upsertMutation.isPending}>
|
||||
{upsertMutation.isPending ? (
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
) : null}
|
||||
Save
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
|
||||
export function TravelTab({ programId }: Props) {
|
||||
const utils = trpc.useUtils()
|
||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all')
|
||||
const [editing, setEditing] = useState<AttendeeRow | null>(null)
|
||||
|
||||
const { data, isLoading } = trpc.logistics.listFlightDetails.useQuery(
|
||||
{ programId },
|
||||
{ refetchInterval: 60_000 },
|
||||
)
|
||||
|
||||
const setStatusMutation = trpc.logistics.setFlightStatus.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Status updated')
|
||||
utils.logistics.listFlightDetails.invalidate({ programId })
|
||||
},
|
||||
onError: (err) => toast.error(err.message),
|
||||
})
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!data) return []
|
||||
if (statusFilter === 'all') return data
|
||||
if (statusFilter === 'unfilled') return data.filter((r) => !r.flightDetail)
|
||||
return data.filter((r) => r.flightDetail?.status === statusFilter)
|
||||
}, [data, statusFilter])
|
||||
|
||||
const totals = useMemo(() => {
|
||||
const c = { all: 0, PENDING: 0, CONFIRMED: 0, unfilled: 0 }
|
||||
for (const r of data ?? []) {
|
||||
c.all++
|
||||
if (!r.flightDetail) c.unfilled++
|
||||
else c[r.flightDetail.status]++
|
||||
}
|
||||
return c
|
||||
}, [data])
|
||||
|
||||
const StatusPill = ({
|
||||
value,
|
||||
label,
|
||||
count,
|
||||
}: {
|
||||
value: StatusFilter
|
||||
label: string
|
||||
count: number
|
||||
}) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStatusFilter(value)}
|
||||
className={`rounded-md border px-2.5 py-1 text-xs font-medium transition-colors ${
|
||||
statusFilter === value
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'bg-background hover:bg-muted'
|
||||
}`}
|
||||
>
|
||||
{label} <span className="tabular-nums opacity-80">({count})</span>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Card>
|
||||
<CardHeader className="space-y-3">
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Plane className="text-muted-foreground h-4 w-4" />
|
||||
<CardTitle className="text-base">Travel for confirmed finalists</CardTitle>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
<StatusPill value="all" label="All" count={totals.all} />
|
||||
<StatusPill value="unfilled" label="Unfilled" count={totals.unfilled} />
|
||||
<StatusPill value="PENDING" label="Pending" count={totals.PENDING} />
|
||||
<StatusPill value="CONFIRMED" label="Confirmed" count={totals.CONFIRMED} />
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<p className="text-muted-foreground py-12 text-center text-sm">
|
||||
{data && data.length === 0
|
||||
? 'No confirmed finalist attendees yet.'
|
||||
: 'No attendees match this filter.'}
|
||||
</p>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Attendee</TableHead>
|
||||
<TableHead>Arrival</TableHead>
|
||||
<TableHead>Departure</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filtered.map((r) => {
|
||||
const fd = r.flightDetail
|
||||
return (
|
||||
<TableRow key={r.id}>
|
||||
<TableCell>
|
||||
<div className="font-medium">
|
||||
{r.user.name ?? r.user.email}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{r.confirmation.project.title}
|
||||
{r.needsVisa ? ' · needs visa' : ''}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
<div>{formatDateTime(fd?.arrivalAt ?? null)}</div>
|
||||
{(fd?.arrivalFlightNumber || fd?.arrivalAirport) && (
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{fd.arrivalFlightNumber ?? '—'}
|
||||
{fd.arrivalAirport ? ` · ${fd.arrivalAirport}` : ''}
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
<div>{formatDateTime(fd?.departureAt ?? null)}</div>
|
||||
{(fd?.departureFlightNumber || fd?.departureAirport) && (
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{fd.departureFlightNumber ?? '—'}
|
||||
{fd.departureAirport ? ` · ${fd.departureAirport}` : ''}
|
||||
</div>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{fd ? (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setStatusMutation.mutate({
|
||||
flightDetailId: fd.id,
|
||||
status: fd.status === 'PENDING' ? 'CONFIRMED' : 'PENDING',
|
||||
})
|
||||
}
|
||||
className="cursor-pointer"
|
||||
title="Click to toggle"
|
||||
>
|
||||
<Badge
|
||||
variant={fd.status === 'CONFIRMED' ? 'default' : 'secondary'}
|
||||
className="text-xs"
|
||||
>
|
||||
{fd.status === 'CONFIRMED' ? 'Confirmed' : 'Pending'}
|
||||
</Badge>
|
||||
</button>
|
||||
) : (
|
||||
<Badge variant="outline" className="text-xs">
|
||||
No info
|
||||
</Badge>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => setEditing(r as AttendeeRow)}
|
||||
>
|
||||
Edit
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
<FlightEditorSheet
|
||||
attendee={editing}
|
||||
programId={programId}
|
||||
open={!!editing}
|
||||
onClose={() => setEditing(null)}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
217
src/components/admin/logistics/visa-edit-dialog.tsx
Normal file
217
src/components/admin/logistics/visa-edit-dialog.tsx
Normal file
@@ -0,0 +1,217 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import type { VisaStatus } from '@prisma/client'
|
||||
|
||||
const STATUS_OPTIONS: { value: VisaStatus; label: string }[] = [
|
||||
{ value: 'NOT_NEEDED', label: 'Not needed' },
|
||||
{ value: 'REQUESTED', label: 'Requested' },
|
||||
{ value: 'INVITATION_SENT', label: 'Invitation sent' },
|
||||
{ value: 'APPOINTMENT_BOOKED', label: 'Appointment booked' },
|
||||
{ value: 'GRANTED', label: 'Granted' },
|
||||
{ value: 'DENIED', label: 'Denied' },
|
||||
]
|
||||
|
||||
function toDateInputValue(d: Date | null | undefined): string {
|
||||
if (!d) return ''
|
||||
const dt = new Date(d)
|
||||
if (Number.isNaN(dt.getTime())) return ''
|
||||
// YYYY-MM-DD for <input type="date">
|
||||
return dt.toISOString().slice(0, 10)
|
||||
}
|
||||
|
||||
function fromDateInputValue(s: string): Date | null {
|
||||
if (!s) return null
|
||||
const dt = new Date(s)
|
||||
return Number.isNaN(dt.getTime()) ? null : dt
|
||||
}
|
||||
|
||||
export type VisaEditTarget = {
|
||||
id: string
|
||||
status: VisaStatus
|
||||
nationality: string | null
|
||||
invitationSentAt: Date | null
|
||||
appointmentAt: Date | null
|
||||
decisionAt: Date | null
|
||||
notes: string | null
|
||||
attendeeName: string
|
||||
projectTitle: string
|
||||
}
|
||||
|
||||
export function VisaEditDialog({
|
||||
open,
|
||||
target,
|
||||
programId,
|
||||
onOpenChange,
|
||||
}: {
|
||||
open: boolean
|
||||
target: VisaEditTarget | null
|
||||
programId: string
|
||||
onOpenChange: (next: boolean) => void
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const [status, setStatus] = useState<VisaStatus>('REQUESTED')
|
||||
const [nationality, setNationality] = useState('')
|
||||
const [invitationSent, setInvitationSent] = useState('')
|
||||
const [appointment, setAppointment] = useState('')
|
||||
const [decision, setDecision] = useState('')
|
||||
const [notes, setNotes] = useState('')
|
||||
|
||||
useEffect(() => {
|
||||
if (target && open) {
|
||||
setStatus(target.status)
|
||||
setNationality(target.nationality ?? '')
|
||||
setInvitationSent(toDateInputValue(target.invitationSentAt))
|
||||
setAppointment(toDateInputValue(target.appointmentAt))
|
||||
setDecision(toDateInputValue(target.decisionAt))
|
||||
setNotes(target.notes ?? '')
|
||||
}
|
||||
}, [target, open])
|
||||
|
||||
const mutation = trpc.logistics.updateVisaApplication.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Visa application updated')
|
||||
utils.logistics.listVisaApplications.invalidate({ programId })
|
||||
onOpenChange(false)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const handleSave = () => {
|
||||
if (!target) return
|
||||
mutation.mutate({
|
||||
id: target.id,
|
||||
status,
|
||||
nationality: nationality.trim() || null,
|
||||
invitationSentAt: fromDateInputValue(invitationSent),
|
||||
appointmentAt: fromDateInputValue(appointment),
|
||||
decisionAt: fromDateInputValue(decision),
|
||||
notes: notes.trim() || null,
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!mutation.isPending) onOpenChange(next)
|
||||
}}
|
||||
>
|
||||
<DialogContent className="sm:max-w-md">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Update visa application</DialogTitle>
|
||||
<DialogDescription>
|
||||
{target
|
||||
? `${target.attendeeName} · ${target.projectTitle}`
|
||||
: 'Loading…'}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-status">Status</Label>
|
||||
<Select value={status} onValueChange={(v) => setStatus(v as VisaStatus)}>
|
||||
<SelectTrigger id="visa-status">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{STATUS_OPTIONS.map((o) => (
|
||||
<SelectItem key={o.value} value={o.value}>
|
||||
{o.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-nationality">Nationality</Label>
|
||||
<Input
|
||||
id="visa-nationality"
|
||||
value={nationality}
|
||||
onChange={(e) => setNationality(e.target.value)}
|
||||
placeholder="Self-declared, optional"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 gap-3 sm:grid-cols-3">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-invitation">Invitation sent</Label>
|
||||
<Input
|
||||
id="visa-invitation"
|
||||
type="date"
|
||||
value={invitationSent}
|
||||
onChange={(e) => setInvitationSent(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-appointment">Appointment</Label>
|
||||
<Input
|
||||
id="visa-appointment"
|
||||
type="date"
|
||||
value={appointment}
|
||||
onChange={(e) => setAppointment(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-decision">Decision</Label>
|
||||
<Input
|
||||
id="visa-decision"
|
||||
type="date"
|
||||
value={decision}
|
||||
onChange={(e) => setDecision(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="visa-notes">Notes</Label>
|
||||
<Textarea
|
||||
id="visa-notes"
|
||||
value={notes}
|
||||
onChange={(e) => setNotes(e.target.value)}
|
||||
rows={3}
|
||||
placeholder="Free-text notes — embassy, contact, follow-ups, etc. No documents."
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={mutation.isPending}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} disabled={!target || mutation.isPending}>
|
||||
{mutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Save
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
249
src/components/admin/logistics/visas-tab.tsx
Normal file
249
src/components/admin/logistics/visas-tab.tsx
Normal file
@@ -0,0 +1,249 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from '@/components/ui/table'
|
||||
import { Settings as SettingsIcon, ShieldOff } from 'lucide-react'
|
||||
import { VisaEditDialog, type VisaEditTarget } from './visa-edit-dialog'
|
||||
import type { VisaStatus } from '@prisma/client'
|
||||
|
||||
interface Props {
|
||||
programId: string
|
||||
}
|
||||
|
||||
const STATUS_BADGE: Record<
|
||||
VisaStatus,
|
||||
{ label: string; variant: 'default' | 'secondary' | 'destructive' | 'outline' }
|
||||
> = {
|
||||
NOT_NEEDED: { label: 'Not needed', variant: 'outline' },
|
||||
REQUESTED: { label: 'Requested', variant: 'secondary' },
|
||||
INVITATION_SENT: { label: 'Invitation sent', variant: 'secondary' },
|
||||
APPOINTMENT_BOOKED: { label: 'Appointment booked', variant: 'default' },
|
||||
GRANTED: { label: 'Granted', variant: 'default' },
|
||||
DENIED: { label: 'Denied', variant: 'destructive' },
|
||||
}
|
||||
|
||||
type StatusFilter = 'all' | VisaStatus
|
||||
|
||||
function formatDateOnly(d: Date | null | undefined): string {
|
||||
if (!d) return '—'
|
||||
return new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' }).format(new Date(d))
|
||||
}
|
||||
|
||||
function nextDate(row: {
|
||||
invitationSentAt: Date | null
|
||||
appointmentAt: Date | null
|
||||
decisionAt: Date | null
|
||||
status: VisaStatus
|
||||
}): { label: string; date: Date | null } {
|
||||
if (row.status === 'GRANTED' || row.status === 'DENIED') {
|
||||
return { label: 'Decision', date: row.decisionAt }
|
||||
}
|
||||
if (row.appointmentAt) return { label: 'Appointment', date: row.appointmentAt }
|
||||
if (row.invitationSentAt) return { label: 'Invitation sent', date: row.invitationSentAt }
|
||||
return { label: '—', date: null }
|
||||
}
|
||||
|
||||
export function VisasTab({ programId }: Props) {
|
||||
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all')
|
||||
const [editTarget, setEditTarget] = useState<VisaEditTarget | null>(null)
|
||||
const [editOpen, setEditOpen] = useState(false)
|
||||
|
||||
const { data, isLoading } = trpc.logistics.listVisaApplications.useQuery({ programId })
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
if (!data) return []
|
||||
return statusFilter === 'all'
|
||||
? data
|
||||
: data.filter((r) => r.status === statusFilter)
|
||||
}, [data, statusFilter])
|
||||
|
||||
const totals = useMemo(() => {
|
||||
const counts: Record<VisaStatus, number> = {
|
||||
NOT_NEEDED: 0,
|
||||
REQUESTED: 0,
|
||||
INVITATION_SENT: 0,
|
||||
APPOINTMENT_BOOKED: 0,
|
||||
GRANTED: 0,
|
||||
DENIED: 0,
|
||||
}
|
||||
for (const r of data ?? []) counts[r.status]++
|
||||
return counts
|
||||
}, [data])
|
||||
|
||||
const StatusPill = ({
|
||||
value,
|
||||
label,
|
||||
count,
|
||||
}: {
|
||||
value: StatusFilter
|
||||
label: string
|
||||
count: number
|
||||
}) => (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setStatusFilter(value)}
|
||||
className={`rounded-md border px-2.5 py-1 text-xs font-medium transition-colors ${
|
||||
statusFilter === value
|
||||
? 'bg-primary text-primary-foreground border-primary'
|
||||
: 'bg-background hover:bg-muted'
|
||||
}`}
|
||||
>
|
||||
{label} <span className="tabular-nums opacity-80">({count})</span>
|
||||
</button>
|
||||
)
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Card>
|
||||
<CardHeader className="space-y-3">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<CardTitle className="text-base">Visa applications</CardTitle>
|
||||
<p className="text-muted-foreground mt-1 max-w-2xl text-xs">
|
||||
Process metadata only — invitation letters, passport copies, and visa decisions
|
||||
continue to flow over email and are never stored on this platform.
|
||||
</p>
|
||||
</div>
|
||||
<Button variant="outline" size="sm" asChild>
|
||||
<Link href="/admin/settings?tab=edition">
|
||||
<SettingsIcon className="mr-2 h-3.5 w-3.5" />
|
||||
Edition settings
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
<StatusPill value="all" label="All" count={(data ?? []).length} />
|
||||
<StatusPill value="REQUESTED" label="Requested" count={totals.REQUESTED} />
|
||||
<StatusPill
|
||||
value="INVITATION_SENT"
|
||||
label="Invitation sent"
|
||||
count={totals.INVITATION_SENT}
|
||||
/>
|
||||
<StatusPill
|
||||
value="APPOINTMENT_BOOKED"
|
||||
label="Appointment booked"
|
||||
count={totals.APPOINTMENT_BOOKED}
|
||||
/>
|
||||
<StatusPill value="GRANTED" label="Granted" count={totals.GRANTED} />
|
||||
<StatusPill value="DENIED" label="Denied" count={totals.DENIED} />
|
||||
<StatusPill value="NOT_NEEDED" label="Not needed" count={totals.NOT_NEEDED} />
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-14 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : filtered.length === 0 ? (
|
||||
<div className="text-muted-foreground py-12 text-center text-sm">
|
||||
<ShieldOff className="mx-auto mb-2 h-6 w-6 opacity-60" />
|
||||
{statusFilter === 'all'
|
||||
? 'No visa applications yet. They are auto-created when a team confirms with needsVisa=true.'
|
||||
: 'No applications match this filter.'}
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-hidden rounded-md border">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead>Project</TableHead>
|
||||
<TableHead>Member</TableHead>
|
||||
<TableHead>Nationality</TableHead>
|
||||
<TableHead>Status</TableHead>
|
||||
<TableHead>Next date</TableHead>
|
||||
<TableHead>Notes</TableHead>
|
||||
<TableHead className="text-right">Actions</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{filtered.map((r) => {
|
||||
const badge = STATUS_BADGE[r.status]
|
||||
const next = nextDate(r)
|
||||
return (
|
||||
<TableRow key={r.id}>
|
||||
<TableCell className="font-medium">{r.project.title}</TableCell>
|
||||
<TableCell>
|
||||
<div className="text-sm">
|
||||
{r.attendee.user.name ?? r.attendee.user.email}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{r.attendee.user.email}
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
{r.nationality ?? <span className="text-muted-foreground">—</span>}
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant={badge.variant} className="text-xs">
|
||||
{badge.label}
|
||||
</Badge>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm">
|
||||
{next.date ? (
|
||||
<>
|
||||
<div>{formatDateOnly(next.date)}</div>
|
||||
<div className="text-muted-foreground text-xs">{next.label}</div>
|
||||
</>
|
||||
) : (
|
||||
<span className="text-muted-foreground">—</span>
|
||||
)}
|
||||
</TableCell>
|
||||
<TableCell className="text-muted-foreground max-w-[18rem] truncate text-xs">
|
||||
{r.notes ?? '—'}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setEditTarget({
|
||||
id: r.id,
|
||||
status: r.status,
|
||||
nationality: r.nationality,
|
||||
invitationSentAt: r.invitationSentAt,
|
||||
appointmentAt: r.appointmentAt,
|
||||
decisionAt: r.decisionAt,
|
||||
notes: r.notes,
|
||||
attendeeName: r.attendee.user.name ?? r.attendee.user.email,
|
||||
projectTitle: r.project.title,
|
||||
})
|
||||
setEditOpen(true)
|
||||
}}
|
||||
>
|
||||
Edit
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<VisaEditDialog
|
||||
open={editOpen}
|
||||
target={editTarget}
|
||||
programId={programId}
|
||||
onOpenChange={setEditOpen}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -191,6 +191,20 @@ export function MembersContent() {
|
||||
},
|
||||
})
|
||||
|
||||
const bulkUpdateRoles = trpc.user.bulkUpdateRoles.useMutation({
|
||||
onSuccess: (r) => {
|
||||
const parts: string[] = []
|
||||
if (r.updated > 0) parts.push(`Updated ${r.updated} user${r.updated === 1 ? '' : 's'}`)
|
||||
if (r.alreadyHadRole > 0) parts.push(`${r.alreadyHadRole} already had role`)
|
||||
toast.success(parts.join(' · ') || 'No changes')
|
||||
setSelectedIds(new Set())
|
||||
utils.user.list.invalidate()
|
||||
},
|
||||
onError: (error) => {
|
||||
toast.error(error.message || 'Failed to update roles')
|
||||
},
|
||||
})
|
||||
|
||||
const selectableUsers = useMemo(
|
||||
() => data?.users ?? [],
|
||||
[data?.users]
|
||||
@@ -321,9 +335,29 @@ export function MembersContent() {
|
||||
<Card>
|
||||
<CardContent className="py-3 flex flex-wrap items-center justify-between gap-2">
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Selection persists across pages and filters.
|
||||
{selectedIds.size > 0
|
||||
? `${selectedIds.size} selected. Selection persists across pages and filters.`
|
||||
: 'Selection persists across pages and filters.'}
|
||||
</p>
|
||||
<div className="flex items-center gap-2">
|
||||
{selectedIds.size > 0 && (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() =>
|
||||
bulkUpdateRoles.mutate({
|
||||
userIds: Array.from(selectedIds),
|
||||
addRole: 'MENTOR',
|
||||
})
|
||||
}
|
||||
disabled={bulkUpdateRoles.isPending}
|
||||
>
|
||||
{bulkUpdateRoles.isPending ? (
|
||||
<Loader2 className="h-4 w-4 mr-2 animate-spin" />
|
||||
) : null}
|
||||
Add MENTOR role
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
|
||||
257
src/components/admin/mentor/mentor-detail-sheet.tsx
Normal file
257
src/components/admin/mentor/mentor-detail-sheet.tsx
Normal file
@@ -0,0 +1,257 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetDescription,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
} from '@/components/ui/sheet'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Separator } from '@/components/ui/separator'
|
||||
import {
|
||||
Mail,
|
||||
MapPin,
|
||||
GraduationCap,
|
||||
CheckCircle2,
|
||||
XCircle,
|
||||
MessageSquare,
|
||||
FileText,
|
||||
Target,
|
||||
Calendar,
|
||||
} from 'lucide-react'
|
||||
import { formatEnumLabel } from '@/lib/utils'
|
||||
|
||||
function formatDateOnly(d: Date | string | null | undefined): string {
|
||||
if (!d) return '—'
|
||||
return new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' }).format(new Date(d))
|
||||
}
|
||||
|
||||
function formatRelativePast(d: Date | string | null): string {
|
||||
if (!d) return '—'
|
||||
const dt = typeof d === 'string' ? new Date(d) : d
|
||||
const ms = Date.now() - dt.getTime()
|
||||
const days = Math.floor(ms / 86_400_000)
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
if (days >= 1) return `${days}d ago`
|
||||
if (hours >= 1) return `${hours}h ago`
|
||||
const minutes = Math.floor(ms / 60_000)
|
||||
return `${Math.max(0, minutes)}m ago`
|
||||
}
|
||||
|
||||
export function MentorDetailSheet({
|
||||
mentorId,
|
||||
open,
|
||||
onOpenChange,
|
||||
}: {
|
||||
mentorId: string | null
|
||||
open: boolean
|
||||
onOpenChange: (next: boolean) => void
|
||||
}) {
|
||||
const { data, isLoading } = trpc.mentor.getMentorDetail.useQuery(
|
||||
{ mentorId: mentorId ?? '' },
|
||||
{ enabled: open && !!mentorId },
|
||||
)
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||
<SheetContent className="w-full overflow-y-auto sm:max-w-xl">
|
||||
<SheetHeader>
|
||||
<SheetTitle>
|
||||
{isLoading || !data ? (
|
||||
<Skeleton className="h-6 w-48" />
|
||||
) : (
|
||||
data.mentor.name ?? 'Unnamed mentor'
|
||||
)}
|
||||
</SheetTitle>
|
||||
<SheetDescription>
|
||||
{isLoading || !data ? (
|
||||
<Skeleton className="h-4 w-64" />
|
||||
) : (
|
||||
<span className="flex flex-wrap items-center gap-2 text-sm">
|
||||
<span className="inline-flex items-center gap-1">
|
||||
<Mail className="h-3 w-3" /> {data.mentor.email}
|
||||
</span>
|
||||
{data.mentor.country && (
|
||||
<span className="inline-flex items-center gap-1">
|
||||
<MapPin className="h-3 w-3" /> {data.mentor.country}
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
</SheetDescription>
|
||||
</SheetHeader>
|
||||
|
||||
{isLoading || !data ? (
|
||||
<div className="mt-6 space-y-4">
|
||||
<Skeleton className="h-20 w-full" />
|
||||
<Skeleton className="h-20 w-full" />
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-6 space-y-6">
|
||||
{/* Profile summary */}
|
||||
<section className="space-y-3">
|
||||
<h3 className="text-muted-foreground text-xs font-semibold uppercase tracking-wide">
|
||||
Profile
|
||||
</h3>
|
||||
<div className="space-y-2 rounded-md border p-4">
|
||||
<div className="flex items-start gap-2 text-sm">
|
||||
<GraduationCap className="text-muted-foreground mt-0.5 h-4 w-4 shrink-0" />
|
||||
<div>
|
||||
<div className="text-muted-foreground text-xs">Expertise</div>
|
||||
{data.mentor.expertiseTags.length > 0 ? (
|
||||
<div className="mt-1 flex flex-wrap gap-1">
|
||||
{data.mentor.expertiseTags.map((t) => (
|
||||
<Badge key={t} variant="secondary" className="text-xs">
|
||||
{t}
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="text-muted-foreground text-sm italic">
|
||||
None declared
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<Calendar className="text-muted-foreground h-4 w-4 shrink-0" />
|
||||
<span className="text-muted-foreground text-xs">Joined</span>
|
||||
<span>{formatDateOnly(data.mentor.createdAt)}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground text-xs">Max assignments</span>
|
||||
<span className="tabular-nums">
|
||||
{data.mentor.maxAssignments ?? '∞'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Assignments */}
|
||||
<section className="space-y-3">
|
||||
<div className="flex items-center justify-between">
|
||||
<h3 className="text-muted-foreground text-xs font-semibold uppercase tracking-wide">
|
||||
Teams ({data.assignments.length})
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
{data.assignments.length === 0 ? (
|
||||
<div className="text-muted-foreground rounded-md border py-8 text-center text-sm">
|
||||
This mentor has no assignments yet.
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.assignments.map((a) => {
|
||||
const isCompleted = a.completionStatus === 'completed'
|
||||
const isDropped = !!a.droppedAt
|
||||
return (
|
||||
<div
|
||||
key={a.id}
|
||||
className={`rounded-md border p-4 ${isDropped ? 'opacity-70' : ''}`}
|
||||
>
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0 flex-1">
|
||||
<Link
|
||||
href={`/admin/projects/${a.project.id}`}
|
||||
className="text-sm font-medium hover:underline"
|
||||
>
|
||||
{a.project.title}
|
||||
</Link>
|
||||
<div className="text-muted-foreground mt-0.5 flex flex-wrap items-center gap-2 text-xs">
|
||||
{a.project.competitionCategory && (
|
||||
<span>
|
||||
{formatEnumLabel(a.project.competitionCategory)}
|
||||
</span>
|
||||
)}
|
||||
{a.project.country && (
|
||||
<>
|
||||
<span>·</span>
|
||||
<span>{a.project.country}</span>
|
||||
</>
|
||||
)}
|
||||
<span>·</span>
|
||||
<span>Assigned {formatDateOnly(a.assignedAt)}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex shrink-0 flex-col items-end gap-1">
|
||||
{isCompleted ? (
|
||||
<Badge variant="default" className="gap-1">
|
||||
<CheckCircle2 className="h-3 w-3" /> Completed
|
||||
</Badge>
|
||||
) : isDropped ? (
|
||||
<Badge variant="destructive" className="gap-1">
|
||||
<XCircle className="h-3 w-3" /> Dropped
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="secondary">Active</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isDropped && a.droppedReason && (
|
||||
<div className="text-muted-foreground bg-muted/50 mt-3 rounded-md p-2 text-xs">
|
||||
<strong>Drop reason</strong>
|
||||
{a.droppedBy ? ` (by ${a.droppedBy})` : ''}: {a.droppedReason}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Separator className="my-3" />
|
||||
|
||||
{/* Activity counts */}
|
||||
<div className="grid grid-cols-3 gap-2 text-xs">
|
||||
<div className="flex items-start gap-2 rounded-md border p-2">
|
||||
<MessageSquare className="text-muted-foreground mt-0.5 h-3.5 w-3.5 shrink-0" />
|
||||
<div>
|
||||
<div className="font-semibold tabular-nums">
|
||||
{a.messageCount}
|
||||
</div>
|
||||
<div className="text-muted-foreground">
|
||||
{a.lastMessageAt
|
||||
? formatRelativePast(a.lastMessageAt)
|
||||
: 'no messages'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-md border p-2">
|
||||
<FileText className="text-muted-foreground mt-0.5 h-3.5 w-3.5 shrink-0" />
|
||||
<div>
|
||||
<div className="font-semibold tabular-nums">
|
||||
{a.fileCount}
|
||||
</div>
|
||||
<div className="text-muted-foreground">
|
||||
{a.lastFileAt
|
||||
? formatRelativePast(a.lastFileAt)
|
||||
: 'no files'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-start gap-2 rounded-md border p-2">
|
||||
<Target className="text-muted-foreground mt-0.5 h-3.5 w-3.5 shrink-0" />
|
||||
<div>
|
||||
<div className="font-semibold tabular-nums">
|
||||
{a.milestoneCompletionCount}
|
||||
</div>
|
||||
<div className="text-muted-foreground">
|
||||
{a.lastMilestoneAt
|
||||
? formatRelativePast(a.lastMilestoneAt)
|
||||
: 'no milestones'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</section>
|
||||
</div>
|
||||
)}
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
)
|
||||
}
|
||||
177
src/components/admin/project-email-dialog.tsx
Normal file
177
src/components/admin/project-email-dialog.tsx
Normal file
@@ -0,0 +1,177 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Mail, Send, Eye } from 'lucide-react'
|
||||
|
||||
interface Props {
|
||||
open: boolean
|
||||
onClose: () => void
|
||||
projectId: string
|
||||
projectTitle: string
|
||||
}
|
||||
|
||||
function useDebounced<T>(value: T, delayMs: number): T {
|
||||
const [debounced, setDebounced] = useState(value)
|
||||
useEffect(() => {
|
||||
const t = setTimeout(() => setDebounced(value), delayMs)
|
||||
return () => clearTimeout(t)
|
||||
}, [value, delayMs])
|
||||
return debounced
|
||||
}
|
||||
|
||||
export function ProjectEmailDialog({ open, onClose, projectId, projectTitle }: Props) {
|
||||
const initialBody = useMemo(() => `Hello ${projectTitle} team,\n\n`, [projectTitle])
|
||||
const [subject, setSubject] = useState('')
|
||||
const [body, setBody] = useState(initialBody)
|
||||
const [showPreview, setShowPreview] = useState(false)
|
||||
|
||||
// Reset state whenever the dialog opens for a new project
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setSubject('')
|
||||
setBody(initialBody)
|
||||
setShowPreview(false)
|
||||
}
|
||||
}, [open, initialBody])
|
||||
|
||||
const debouncedSubject = useDebounced(subject, 300)
|
||||
const debouncedBody = useDebounced(body, 300)
|
||||
|
||||
const recipientPreview = trpc.message.previewRecipients.useQuery(
|
||||
{ recipientType: 'PROJECT_TEAM', recipientFilter: { projectId } },
|
||||
{ enabled: open }
|
||||
)
|
||||
|
||||
const emailPreview = trpc.message.previewEmail.useQuery(
|
||||
{ subject: debouncedSubject, body: debouncedBody },
|
||||
{ enabled: showPreview && debouncedSubject.length > 0 && debouncedBody.length > 0 }
|
||||
)
|
||||
|
||||
const sendTestMutation = trpc.message.sendTest.useMutation({
|
||||
onSuccess: ({ to }) => toast.success(`Test email sent to ${to}`),
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const sendMutation = trpc.message.send.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success(`Email sent to ${recipientPreview.data?.totalApplicants ?? 0} team members`)
|
||||
onClose()
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const recipientCount = recipientPreview.data?.totalApplicants ?? 0
|
||||
const canSend = subject.length > 0 && body.length > 0 && recipientCount > 0
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(v) => !v && onClose()}>
|
||||
<DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<Mail className="h-5 w-5" />
|
||||
Email Team — {projectTitle}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
Compose a custom email to all members of this project's team.
|
||||
{recipientPreview.isLoading
|
||||
? ' Loading recipients…'
|
||||
: ` Will be sent to ${recipientCount} team member${recipientCount === 1 ? '' : 's'}.`}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email-subject">Subject</Label>
|
||||
<Input
|
||||
id="email-subject"
|
||||
value={subject}
|
||||
onChange={(e) => setSubject(e.target.value)}
|
||||
placeholder="Subject of your email"
|
||||
maxLength={500}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="email-body">Body</Label>
|
||||
<Textarea
|
||||
id="email-body"
|
||||
value={body}
|
||||
onChange={(e) => setBody(e.target.value)}
|
||||
rows={10}
|
||||
className="font-mono text-sm"
|
||||
placeholder={initialBody}
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
The greeting is pre-filled — edit freely. The full email is wrapped in the standard
|
||||
MOPC styled template when sent. Click "Show preview" to see exactly what recipients will see.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setShowPreview((s) => !s)}
|
||||
disabled={subject.length === 0 || body.length === 0}
|
||||
>
|
||||
<Eye className="mr-2 h-4 w-4" />
|
||||
{showPreview ? 'Hide preview' : 'Show preview'}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => sendTestMutation.mutate({ subject, body })}
|
||||
disabled={!canSend || sendTestMutation.isPending}
|
||||
>
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
{sendTestMutation.isPending ? 'Sending…' : 'Send test to me'}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{showPreview && emailPreview.data && (
|
||||
<div className="border rounded-md overflow-hidden">
|
||||
<div className="bg-muted text-xs px-3 py-2 border-b">Email preview</div>
|
||||
<iframe
|
||||
title="Email preview"
|
||||
srcDoc={emailPreview.data.html}
|
||||
className="w-full h-96 bg-white"
|
||||
sandbox=""
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="ghost" onClick={onClose}>Cancel</Button>
|
||||
<Button
|
||||
onClick={() =>
|
||||
sendMutation.mutate({
|
||||
recipientType: 'PROJECT_TEAM',
|
||||
recipientFilter: { projectId },
|
||||
subject,
|
||||
body,
|
||||
deliveryChannels: ['EMAIL'],
|
||||
linkType: 'NONE',
|
||||
})
|
||||
}
|
||||
disabled={!canSend || sendMutation.isPending}
|
||||
>
|
||||
<Send className="mr-2 h-4 w-4" />
|
||||
{sendMutation.isPending ? 'Sending…' : `Send to ${recipientCount}`}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
217
src/components/admin/round/mentoring-round-overview.tsx
Normal file
217
src/components/admin/round/mentoring-round-overview.tsx
Normal file
@@ -0,0 +1,217 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import {
|
||||
ArrowRight,
|
||||
Clock,
|
||||
FileText,
|
||||
MessageCircle,
|
||||
Target,
|
||||
UserCheck,
|
||||
Users,
|
||||
} from 'lucide-react'
|
||||
|
||||
interface Props {
|
||||
roundId: string
|
||||
}
|
||||
|
||||
function formatRelativeFuture(date: Date | null): { label: string; tone: 'normal' | 'amber' | 'red' } {
|
||||
if (!date) return { label: '—', tone: 'normal' }
|
||||
const ms = date.getTime() - Date.now()
|
||||
if (ms <= 0) return { label: 'Closed', tone: 'red' }
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
const days = Math.floor(hours / 24)
|
||||
const tone: 'normal' | 'amber' | 'red' =
|
||||
hours <= 12 ? 'red' : hours <= 48 ? 'amber' : 'normal'
|
||||
const label = days > 0 ? `Closes in ${days}d` : `Closes in ${hours}h`
|
||||
return { label, tone }
|
||||
}
|
||||
|
||||
function formatRelativePast(date: Date | null): string {
|
||||
if (!date) return '—'
|
||||
const ms = Date.now() - date.getTime()
|
||||
const minutes = Math.floor(ms / 60_000)
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
const days = Math.floor(ms / 86_400_000)
|
||||
if (days > 0) return `${days}d ago`
|
||||
if (hours > 0) return `${hours}h ago`
|
||||
return `${Math.max(0, minutes)}m ago`
|
||||
}
|
||||
|
||||
export function MentoringRoundOverview({ roundId }: Props) {
|
||||
const { data: stats, isLoading: statsLoading } = trpc.mentor.getRoundStats.useQuery(
|
||||
{ roundId },
|
||||
{ refetchInterval: 30_000 },
|
||||
)
|
||||
const { data: pool, isLoading: poolLoading } = trpc.mentor.getMentorPool.useQuery({})
|
||||
|
||||
if (statsLoading || poolLoading) {
|
||||
return (
|
||||
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
{[1, 2, 3, 4].map((i) => (
|
||||
<Skeleton key={i} className="h-32 w-full rounded-md" />
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
if (!stats || !pool) return null
|
||||
|
||||
const requestedPct = stats.totalProjects
|
||||
? Math.round((stats.requestedCount / stats.totalProjects) * 100)
|
||||
: 0
|
||||
const assignedPct = stats.totalProjects
|
||||
? Math.round((stats.assignedCount / stats.totalProjects) * 100)
|
||||
: 0
|
||||
|
||||
const window = formatRelativeFuture(
|
||||
stats.requestWindow.deadline ? new Date(stats.requestWindow.deadline) : null,
|
||||
)
|
||||
|
||||
const avgLoad =
|
||||
pool.poolSize > 0 ? (pool.totalCurrentAssignments / pool.poolSize).toFixed(1) : '—'
|
||||
const lastActivity = stats.workspaceActivity.lastActivityAt
|
||||
? new Date(stats.workspaceActivity.lastActivityAt)
|
||||
: null
|
||||
|
||||
return (
|
||||
<div className="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Requested mentoring
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="text-3xl font-bold tabular-nums">
|
||||
{stats.requestedCount}
|
||||
<span className="text-muted-foreground ml-2 text-sm font-normal">
|
||||
/ {stats.totalProjects}
|
||||
</span>
|
||||
</div>
|
||||
<p className="text-muted-foreground mt-1 text-xs">{requestedPct}% of round</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Mentor assigned
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-baseline gap-2">
|
||||
<div className="text-3xl font-bold tabular-nums">{stats.assignedCount}</div>
|
||||
<UserCheck className="text-muted-foreground h-5 w-5" />
|
||||
</div>
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
{assignedPct}% of round{' '}
|
||||
{stats.awaitingAssignment > 0 && (
|
||||
<span className="text-amber-700 dark:text-amber-400">
|
||||
· {stats.awaitingAssignment} awaiting
|
||||
</span>
|
||||
)}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Request window
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-baseline gap-2">
|
||||
<Clock className="text-muted-foreground h-5 w-5" />
|
||||
<Badge
|
||||
variant={
|
||||
window.tone === 'red'
|
||||
? 'destructive'
|
||||
: window.tone === 'amber'
|
||||
? 'secondary'
|
||||
: 'outline'
|
||||
}
|
||||
className="text-sm"
|
||||
>
|
||||
{window.label}
|
||||
</Badge>
|
||||
</div>
|
||||
<p className="text-muted-foreground mt-2 text-xs">
|
||||
{stats.requestWindow.deadline
|
||||
? `Closes ${new Date(stats.requestWindow.deadline).toLocaleDateString()} · ${stats.requestWindow.deadlineDays}-day window`
|
||||
: 'No window deadline set'}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader className="flex flex-row items-center justify-between pb-2">
|
||||
<CardTitle className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
|
||||
Mentor pool
|
||||
</CardTitle>
|
||||
<Link
|
||||
href="/admin/mentors"
|
||||
className="text-muted-foreground hover:text-foreground inline-flex items-center text-xs"
|
||||
>
|
||||
View all
|
||||
<ArrowRight className="ml-0.5 h-3 w-3" />
|
||||
</Link>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-baseline gap-2">
|
||||
<div className="text-3xl font-bold tabular-nums">{pool.poolSize}</div>
|
||||
<Users className="text-muted-foreground h-5 w-5" />
|
||||
</div>
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
Avg load <span className="text-foreground font-medium">{avgLoad}</span> ·{' '}
|
||||
{pool.totalCurrentAssignments} active
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card className="md:col-span-2 xl:col-span-4">
|
||||
<CardHeader className="pb-2">
|
||||
<CardTitle className="text-sm">Workspace activity</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="grid grid-cols-2 gap-x-6 gap-y-3 text-sm md:grid-cols-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<MessageCircle className="text-muted-foreground h-4 w-4" />
|
||||
<div>
|
||||
<div className="font-bold tabular-nums">{stats.workspaceActivity.messageCount}</div>
|
||||
<div className="text-muted-foreground text-xs">messages</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<FileText className="text-muted-foreground h-4 w-4" />
|
||||
<div>
|
||||
<div className="font-bold tabular-nums">{stats.workspaceActivity.fileCount}</div>
|
||||
<div className="text-muted-foreground text-xs">files</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Target className="text-muted-foreground h-4 w-4" />
|
||||
<div>
|
||||
<div className="font-bold tabular-nums">
|
||||
{stats.workspaceActivity.milestoneCount}
|
||||
</div>
|
||||
<div className="text-muted-foreground text-xs">milestones</div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<Clock className="text-muted-foreground h-4 w-4" />
|
||||
<div>
|
||||
<div className="font-medium">{formatRelativePast(lastActivity)}</div>
|
||||
<div className="text-muted-foreground text-xs">last activity</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -39,6 +39,7 @@ import { Textarea } from '@/components/ui/textarea'
|
||||
import { Slider } from '@/components/ui/slider'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { ScoreExplainerDialog } from '@/components/shared/score-explainer-dialog'
|
||||
import { csvCell } from '@/lib/csv'
|
||||
import {
|
||||
Collapsible,
|
||||
CollapsibleContent,
|
||||
@@ -652,16 +653,9 @@ export function RankingDashboard({ competitionId: _competitionId, roundId }: Ran
|
||||
}
|
||||
const headers = result.columns
|
||||
const csvRows = [
|
||||
headers.join(','),
|
||||
headers.map((h: string) => csvCell(h)).join(','),
|
||||
...result.data.map((row: Record<string, unknown>) =>
|
||||
headers.map((h: string) => {
|
||||
const val = row[h]
|
||||
if (val == null) return ''
|
||||
const str = String(val)
|
||||
return str.includes(',') || str.includes('"') || str.includes('\n')
|
||||
? `"${str.replace(/"/g, '""')}"`
|
||||
: str
|
||||
}).join(','),
|
||||
headers.map((h: string) => csvCell(row[h])).join(','),
|
||||
),
|
||||
]
|
||||
const blob = new Blob([csvRows.join('\n')], { type: 'text/csv;charset=utf-8;' })
|
||||
|
||||
@@ -40,6 +40,11 @@ export function MentoringConfig({ config, onChange }: MentoringConfigProps) {
|
||||
<SelectItem value="admin_selected">Admin Selected</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<ul className="text-xs text-muted-foreground space-y-1 mt-2">
|
||||
<li><strong>All Advancing Projects</strong> — every project that enters this round is paired with a mentor.</li>
|
||||
<li><strong>Requested Only</strong> — only projects that explicitly request mentoring participate (default).</li>
|
||||
<li><strong>Admin Selected</strong> — admin manually picks which projects get a mentor.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
@@ -56,6 +61,46 @@ export function MentoringConfig({ config, onChange }: MentoringConfigProps) {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Mentoring Request Window</CardTitle>
|
||||
<CardDescription>How long teams have to request a mentor, and what happens to non-requesters</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="mentoringRequestDeadlineDays">Request deadline (days from round opening)</Label>
|
||||
<p className="text-xs text-muted-foreground">After this many days, teams can no longer submit a mentoring request. Default: 14.</p>
|
||||
<Input
|
||||
id="mentoringRequestDeadlineDays"
|
||||
type="number"
|
||||
min={1}
|
||||
max={90}
|
||||
className="w-32"
|
||||
value={(config.mentoringRequestDeadlineDays as number) ?? 14}
|
||||
onChange={(e) => {
|
||||
const v = parseInt(e.target.value, 10)
|
||||
if (!Number.isNaN(v) && v >= 1 && v <= 90) update('mentoringRequestDeadlineDays', v)
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<Label htmlFor="passThroughIfNoRequest">Auto-pass non-requesters</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
When ON, projects that don't request mentoring auto-PASS to the next round (default).
|
||||
When OFF, all projects are held in PENDING until the admin decides — useful when mentoring is mandatory.
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="passThroughIfNoRequest"
|
||||
checked={(config.passThroughIfNoRequest as boolean | undefined) ?? true}
|
||||
onCheckedChange={(v) => update('passThroughIfNoRequest', v)}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">Communication & Files</CardTitle>
|
||||
|
||||
226
src/components/admin/settings/edition-settings-tab.tsx
Normal file
226
src/components/admin/settings/edition-settings-tab.tsx
Normal file
@@ -0,0 +1,226 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { useEdition } from '@/contexts/edition-context'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Loader2, ScrollText, Stamp, Users } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
function NumberField({
|
||||
id,
|
||||
label,
|
||||
hint,
|
||||
value,
|
||||
onCommit,
|
||||
disabled,
|
||||
min,
|
||||
max,
|
||||
}: {
|
||||
id: string
|
||||
label: string
|
||||
hint?: string
|
||||
value: number | null
|
||||
onCommit: (next: number) => void
|
||||
disabled?: boolean
|
||||
min?: number
|
||||
max?: number
|
||||
}) {
|
||||
const [draft, setDraft] = useState<string>(value != null ? String(value) : '')
|
||||
useEffect(() => {
|
||||
setDraft(value != null ? String(value) : '')
|
||||
}, [value])
|
||||
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor={id}>{label}</Label>
|
||||
<Input
|
||||
id={id}
|
||||
type="number"
|
||||
min={min}
|
||||
max={max}
|
||||
value={draft}
|
||||
disabled={disabled}
|
||||
onChange={(e) => setDraft(e.target.value)}
|
||||
onBlur={() => {
|
||||
const parsed = Number(draft)
|
||||
if (!Number.isFinite(parsed) || parsed === value) return
|
||||
if (min !== undefined && parsed < min) return
|
||||
if (max !== undefined && parsed > max) return
|
||||
onCommit(parsed)
|
||||
}}
|
||||
className="max-w-[12rem]"
|
||||
/>
|
||||
{hint && <p className="text-muted-foreground text-xs">{hint}</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export function EditionSettingsTab() {
|
||||
const { currentEdition } = useEdition()
|
||||
const programId = currentEdition?.id ?? null
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
const { data, isLoading } = trpc.program.getEditionSettings.useQuery(
|
||||
{ programId: programId ?? '' },
|
||||
{ enabled: !!programId },
|
||||
)
|
||||
const update = trpc.program.updateEditionSettings.useMutation({
|
||||
onSuccess: () => {
|
||||
if (programId) utils.program.getEditionSettings.invalidate({ programId })
|
||||
toast.success('Edition settings updated')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
if (!programId) {
|
||||
return (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
Select an edition from the sidebar dropdown to manage settings.
|
||||
</p>
|
||||
)
|
||||
}
|
||||
if (isLoading || !data) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<Skeleton className="h-48 w-full" />
|
||||
<Skeleton className="h-32 w-full" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const noLiveFinalRound = data.liveFinalRoundId == null
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Grand-finale logistics */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2.5 text-base">
|
||||
<div className="rounded-lg bg-emerald-500/10 p-1.5">
|
||||
<Users className="h-4 w-4 text-emerald-500" />
|
||||
</div>
|
||||
Grand-finale logistics
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Per-edition limits and deadlines that drive finalist confirmation, attendee
|
||||
editing, and visa visibility.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-6">
|
||||
<NumberField
|
||||
id="default-attendee-cap"
|
||||
label="Default attendee cap"
|
||||
hint="Maximum number of team members allowed at the grand finale per finalist team."
|
||||
min={1}
|
||||
max={20}
|
||||
value={data.defaultAttendeeCap}
|
||||
disabled={update.isPending}
|
||||
onCommit={(next) =>
|
||||
update.mutate({ programId, defaultAttendeeCap: next })
|
||||
}
|
||||
/>
|
||||
<NumberField
|
||||
id="confirmation-window-hours"
|
||||
label="Confirmation window (hours)"
|
||||
hint={
|
||||
noLiveFinalRound
|
||||
? 'Will be editable once the grand-finale (LIVE_FINAL) round is created.'
|
||||
: 'How long teams have to click the confirm/decline link after we send it.'
|
||||
}
|
||||
min={1}
|
||||
max={720}
|
||||
value={data.confirmationWindowHours}
|
||||
disabled={update.isPending || noLiveFinalRound}
|
||||
onCommit={(next) =>
|
||||
update.mutate({ programId, confirmationWindowHours: next })
|
||||
}
|
||||
/>
|
||||
<NumberField
|
||||
id="attendee-edit-cutoff-hours"
|
||||
label="Attendee edit cutoff (hours before grand finale)"
|
||||
hint={
|
||||
noLiveFinalRound
|
||||
? 'Will be editable once the grand-finale (LIVE_FINAL) round is created.'
|
||||
: 'After this many hours before the grand finale opens, the team lead can no longer change attendees.'
|
||||
}
|
||||
min={0}
|
||||
max={720}
|
||||
value={data.attendeeEditCutoffHours}
|
||||
disabled={update.isPending || noLiveFinalRound}
|
||||
onCommit={(next) =>
|
||||
update.mutate({ programId, attendeeEditCutoffHours: next })
|
||||
}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Visa */}
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2.5 text-base">
|
||||
<div className="rounded-lg bg-sky-500/10 p-1.5">
|
||||
<Stamp className="h-4 w-4 text-sky-500" />
|
||||
</div>
|
||||
Visa
|
||||
</CardTitle>
|
||||
<CardDescription>
|
||||
Visa documents are exchanged over email and never stored on the platform —
|
||||
we track only process metadata. Choose whether teams see their own status.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<div className="flex items-center justify-between gap-4 rounded-md border p-4">
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="visa-visibility-edition">Visible to teams</Label>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
When on, attendees with needsVisa=true see their status on the
|
||||
applicant dashboard. When off, only admins see the workflow.
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="visa-visibility-edition"
|
||||
checked={data.visaStatusVisibleToMembers}
|
||||
disabled={update.isPending}
|
||||
onCheckedChange={(v) =>
|
||||
update.mutate({ programId, visaStatusVisibleToMembers: v })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* Coming soon */}
|
||||
<Card className="border-dashed">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-muted-foreground text-base">Coming soon</CardTitle>
|
||||
<CardDescription>
|
||||
Editable email templates land in an upcoming update and will surface here.
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="text-muted-foreground space-y-2 text-sm">
|
||||
<div className="flex items-center gap-2">
|
||||
<ScrollText className="h-4 w-4" /> Email templates — editable subject + body
|
||||
for confirmation, decline-cascade, mentor onboarding, etc.
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{update.isPending && (
|
||||
<div className="text-muted-foreground inline-flex items-center gap-2 text-xs">
|
||||
<Loader2 className="h-3 w-3 animate-spin" /> Saving…
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
210
src/components/applicant/attending-members-card.tsx
Normal file
210
src/components/applicant/attending-members-card.tsx
Normal file
@@ -0,0 +1,210 @@
|
||||
'use client'
|
||||
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { PlaneTakeoff, ShieldCheck, AlertTriangle } from 'lucide-react'
|
||||
import { EditAttendeesDialog } from './edit-attendees-dialog'
|
||||
import { LunchPickForm } from './lunch-pick-form'
|
||||
import type { VisaStatus } from '@prisma/client'
|
||||
import { useSession } from 'next-auth/react'
|
||||
|
||||
const VISA_BADGE: Record<
|
||||
VisaStatus,
|
||||
{ label: string; variant: 'default' | 'secondary' | 'destructive' | 'outline' }
|
||||
> = {
|
||||
NOT_NEEDED: { label: 'Visa not needed', variant: 'outline' },
|
||||
REQUESTED: { label: 'Visa requested', variant: 'secondary' },
|
||||
INVITATION_SENT: { label: 'Invitation sent', variant: 'secondary' },
|
||||
APPOINTMENT_BOOKED: { label: 'Appointment booked', variant: 'default' },
|
||||
GRANTED: { label: 'Visa granted', variant: 'default' },
|
||||
DENIED: { label: 'Visa denied', variant: 'destructive' },
|
||||
}
|
||||
|
||||
function formatDateOnly(d: Date | string): string {
|
||||
return new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' }).format(new Date(d))
|
||||
}
|
||||
|
||||
function nextVisaDate(v: {
|
||||
invitationSentAt: Date | string | null
|
||||
appointmentAt: Date | string | null
|
||||
decisionAt: Date | string | null
|
||||
status: VisaStatus
|
||||
}): { label: string; date: Date | string } | null {
|
||||
if (v.status === 'GRANTED' || v.status === 'DENIED') {
|
||||
if (v.decisionAt) return { label: 'Decision', date: v.decisionAt }
|
||||
return null
|
||||
}
|
||||
if (v.appointmentAt) return { label: 'Appointment', date: v.appointmentAt }
|
||||
if (v.invitationSentAt) return { label: 'Invitation sent', date: v.invitationSentAt }
|
||||
return null
|
||||
}
|
||||
|
||||
export function AttendingMembersCard() {
|
||||
const { data: session } = useSession()
|
||||
const { data, isLoading } = trpc.applicant.getMyFinalistConfirmation.useQuery()
|
||||
const { data: myVisas } = trpc.applicant.getMyVisaApplications.useQuery()
|
||||
const programId = data?.project.programId
|
||||
const { data: lunchEvent } = trpc.lunch.getEventForMember.useQuery(
|
||||
{ programId: programId ?? '' },
|
||||
{ enabled: !!programId },
|
||||
)
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<Skeleton className="h-5 w-48" />
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<Skeleton className="h-24 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
if (!data || data.confirmation.status !== 'CONFIRMED') return null
|
||||
|
||||
const cutoffAt = data.cutoffAt ? new Date(data.cutoffAt) : null
|
||||
const userById = new Map(data.project.teamMembers.map((tm) => [tm.userId, tm.user]))
|
||||
const attendees = data.confirmation.attendingMembers
|
||||
const visaByUser = new Map(
|
||||
(myVisas ?? []).map((v) => [v.userId, v] as const),
|
||||
)
|
||||
|
||||
const editDisabled = !data.editableNow
|
||||
const editDisabledReason = !data.editableNow
|
||||
? 'Attendee changes are closed for this edition.'
|
||||
: undefined
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader className="flex-row items-start justify-between gap-4 space-y-0">
|
||||
<div>
|
||||
<CardTitle className="flex items-center gap-2.5 text-lg">
|
||||
<div className="rounded-lg bg-sky-500/10 p-1.5">
|
||||
<PlaneTakeoff className="h-4 w-4 text-sky-500" />
|
||||
</div>
|
||||
Grand Finale Attendees
|
||||
</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
Team members confirmed to travel to Monaco
|
||||
{cutoffAt && data.editableNow && (
|
||||
<>
|
||||
{' '}
|
||||
· editable until{' '}
|
||||
<strong>
|
||||
{new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
}).format(cutoffAt)}
|
||||
</strong>
|
||||
</>
|
||||
)}
|
||||
{cutoffAt && !data.editableNow && (
|
||||
<span className="text-muted-foreground inline-flex items-center gap-1">
|
||||
{' '}
|
||||
· <AlertTriangle className="h-3 w-3" /> editing closed
|
||||
</span>
|
||||
)}
|
||||
</CardDescription>
|
||||
</div>
|
||||
{data.isLead && (
|
||||
<EditAttendeesDialog
|
||||
confirmationId={data.confirmation.id}
|
||||
cap={data.project.program.defaultAttendeeCap}
|
||||
teamMembers={data.project.teamMembers}
|
||||
attendingMembers={attendees}
|
||||
cutoffAt={cutoffAt}
|
||||
disabled={editDisabled}
|
||||
disabledReason={editDisabledReason}
|
||||
/>
|
||||
)}
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{attendees.length === 0 ? (
|
||||
<p className="text-muted-foreground text-sm">No attendees selected yet.</p>
|
||||
) : (
|
||||
<ul className="space-y-2">
|
||||
{attendees.map((a) => {
|
||||
const user = userById.get(a.userId)
|
||||
if (!user) return null
|
||||
const visa = visaByUser.get(a.userId)
|
||||
const visaBadge = visa ? VISA_BADGE[visa.status] : null
|
||||
const next = visa ? nextVisaDate(visa) : null
|
||||
const sessionUserId = session?.user?.id
|
||||
const sessionRole = session?.user?.role
|
||||
const isAdmin =
|
||||
sessionRole === 'SUPER_ADMIN' || sessionRole === 'PROGRAM_ADMIN'
|
||||
const isSelf = sessionUserId === a.userId
|
||||
const isLeadActing = data.isLead && !isSelf
|
||||
const lunchDeadline = lunchEvent?.changeDeadline
|
||||
? new Date(lunchEvent.changeDeadline)
|
||||
: null
|
||||
const lunchPastDeadline =
|
||||
!!lunchDeadline && new Date() > lunchDeadline
|
||||
const canEditLunch =
|
||||
!!lunchEvent &&
|
||||
((isSelf && !lunchPastDeadline) ||
|
||||
(data.isLead && !lunchPastDeadline) ||
|
||||
isAdmin)
|
||||
return (
|
||||
<li
|
||||
key={a.userId}
|
||||
className="space-y-3 rounded-md border px-3 py-2"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<div className="text-sm font-medium">{user.name ?? user.email}</div>
|
||||
<div className="text-muted-foreground text-xs">{user.email}</div>
|
||||
</div>
|
||||
<div className="flex flex-col items-end gap-1">
|
||||
{visa && visaBadge ? (
|
||||
<>
|
||||
<Badge variant={visaBadge.variant} className="gap-1">
|
||||
<ShieldCheck className="h-3 w-3" />
|
||||
{visaBadge.label}
|
||||
</Badge>
|
||||
{next && (
|
||||
<span className="text-muted-foreground text-xs">
|
||||
{next.label}: {formatDateOnly(next.date)}
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
a.needsVisa && (
|
||||
<Badge variant="outline" className="gap-1">
|
||||
<ShieldCheck className="h-3 w-3" />
|
||||
Visa support
|
||||
</Badge>
|
||||
)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{lunchEvent && programId && (
|
||||
<LunchPickForm
|
||||
attendingMemberId={a.id}
|
||||
programId={programId}
|
||||
lunchEventId={lunchEvent.id}
|
||||
canEdit={canEditLunch}
|
||||
editingOnBehalfOf={
|
||||
isLeadActing ? (user.name ?? user.email) : null
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
182
src/components/applicant/edit-attendees-dialog.tsx
Normal file
182
src/components/applicant/edit-attendees-dialog.tsx
Normal file
@@ -0,0 +1,182 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Loader2, Pencil } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
type TeamMember = {
|
||||
userId: string
|
||||
role: string
|
||||
user: { id: string; name: string | null; email: string }
|
||||
}
|
||||
|
||||
type AttendingMember = { userId: string; needsVisa: boolean }
|
||||
|
||||
export function EditAttendeesDialog({
|
||||
confirmationId,
|
||||
cap,
|
||||
teamMembers,
|
||||
attendingMembers,
|
||||
cutoffAt,
|
||||
disabled,
|
||||
disabledReason,
|
||||
}: {
|
||||
confirmationId: string
|
||||
cap: number
|
||||
teamMembers: TeamMember[]
|
||||
attendingMembers: AttendingMember[]
|
||||
cutoffAt: Date | null
|
||||
disabled?: boolean
|
||||
disabledReason?: string
|
||||
}) {
|
||||
const [open, setOpen] = useState(false)
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set())
|
||||
const [visa, setVisa] = useState<Record<string, boolean>>({})
|
||||
|
||||
const utils = trpc.useUtils()
|
||||
const edit = trpc.finalist.editAttendees.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('Attendees updated')
|
||||
utils.applicant.getMyFinalistConfirmation.invalidate()
|
||||
setOpen(false)
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
// Reset form to current roster when dialog opens
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setSelected(new Set(attendingMembers.map((m) => m.userId)))
|
||||
setVisa(
|
||||
Object.fromEntries(attendingMembers.map((m) => [m.userId, m.needsVisa])),
|
||||
)
|
||||
}
|
||||
}, [open, attendingMembers])
|
||||
|
||||
const toggle = (userId: string, checked: boolean) => {
|
||||
setSelected((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (checked) next.add(userId)
|
||||
else next.delete(userId)
|
||||
return next
|
||||
})
|
||||
}
|
||||
|
||||
const overCap = selected.size > cap
|
||||
const noneSelected = selected.size === 0
|
||||
|
||||
const handleSubmit = () => {
|
||||
const ids = Array.from(selected)
|
||||
edit.mutate({
|
||||
confirmationId,
|
||||
attendingUserIds: ids,
|
||||
visaFlags: Object.fromEntries(ids.map((id) => [id, !!visa[id]])),
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!edit.isPending) setOpen(next)
|
||||
}}
|
||||
>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="sm" disabled={disabled} title={disabledReason}>
|
||||
<Pencil className="mr-2 h-3.5 w-3.5" />
|
||||
Edit attendees
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-lg">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Edit attendees</DialogTitle>
|
||||
<DialogDescription>
|
||||
Update who from your team will travel to the grand finale. You can select up to{' '}
|
||||
<strong>{cap}</strong> team members. Mark anyone who needs visa support so we can prepare
|
||||
documents in time.
|
||||
{cutoffAt && (
|
||||
<>
|
||||
{' '}
|
||||
Editable until{' '}
|
||||
<strong>
|
||||
{new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: 'short',
|
||||
}).format(cutoffAt)}
|
||||
</strong>
|
||||
.
|
||||
</>
|
||||
)}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<ul className="space-y-3 max-h-[50vh] overflow-y-auto pr-1">
|
||||
{teamMembers.map((tm) => {
|
||||
const checked = selected.has(tm.userId)
|
||||
return (
|
||||
<li key={tm.userId} className="flex items-start justify-between gap-4">
|
||||
<label className="flex flex-1 items-start gap-3 cursor-pointer">
|
||||
<Checkbox
|
||||
checked={checked}
|
||||
onCheckedChange={(c) => toggle(tm.userId, c === true)}
|
||||
className="mt-0.5"
|
||||
/>
|
||||
<div>
|
||||
<div className="font-medium">{tm.user.name ?? tm.user.email}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{tm.user.email}
|
||||
{tm.role && tm.role !== 'MEMBER' ? ` · ${tm.role.toLowerCase()}` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</label>
|
||||
{checked && (
|
||||
<label className="flex items-center gap-2 text-sm">
|
||||
<span className="text-muted-foreground">Needs visa?</span>
|
||||
<Switch
|
||||
checked={!!visa[tm.userId]}
|
||||
onCheckedChange={(c) =>
|
||||
setVisa((prev) => ({ ...prev, [tm.userId]: c }))
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
|
||||
{overCap && (
|
||||
<p className="text-destructive text-sm">
|
||||
Please select no more than {cap} member{cap === 1 ? '' : 's'}.
|
||||
</p>
|
||||
)}
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setOpen(false)} disabled={edit.isPending}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
disabled={overCap || noneSelected || edit.isPending}
|
||||
>
|
||||
{edit.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Save attendees
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
25
src/components/applicant/external-attendees-strip.tsx
Normal file
25
src/components/applicant/external-attendees-strip.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
'use client'
|
||||
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { UsersRound } from 'lucide-react'
|
||||
|
||||
export function ExternalAttendeesStrip({ projectId }: { projectId: string }) {
|
||||
const { data } = trpc.lunch.getProjectExternals.useQuery({ projectId })
|
||||
if (!data || data.length === 0) return null
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="flex flex-wrap items-center gap-2 py-3">
|
||||
<UsersRound className="text-muted-foreground h-4 w-4" />
|
||||
<span className="text-sm font-medium">External attendees joining your team:</span>
|
||||
{data.map((e) => (
|
||||
<Badge key={e.id} variant="outline">
|
||||
{e.name}
|
||||
{e.roleNote ? ` (${e.roleNote})` : ''}
|
||||
</Badge>
|
||||
))}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
54
src/components/applicant/lunch-banner.tsx
Normal file
54
src/components/applicant/lunch-banner.tsx
Normal file
@@ -0,0 +1,54 @@
|
||||
'use client'
|
||||
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { Calendar, MapPin, Salad, Clock } from 'lucide-react'
|
||||
|
||||
export function LunchBanner({ programId }: { programId: string }) {
|
||||
const { data: event } = trpc.lunch.getEventForMember.useQuery({ programId })
|
||||
if (!event) return null
|
||||
const fmt = new Intl.DateTimeFormat(undefined, {
|
||||
timeZone: 'Europe/Monaco',
|
||||
dateStyle: 'long',
|
||||
timeStyle: 'short',
|
||||
})
|
||||
const eventAt = event.eventAt ? new Date(event.eventAt) : null
|
||||
const deadline = event.changeDeadline ? new Date(event.changeDeadline) : null
|
||||
const deadlinePassed = deadline ? new Date() > deadline : false
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="flex flex-wrap items-center gap-4 py-3 text-sm">
|
||||
<Salad className="h-4 w-4 text-emerald-500" />
|
||||
<span className="font-medium">Lunch event</span>
|
||||
{eventAt && (
|
||||
<span className="flex items-center gap-1.5">
|
||||
<Calendar className="h-4 w-4" /> {fmt.format(eventAt)}{' '}
|
||||
<span className="text-muted-foreground text-xs">(Monaco time)</span>
|
||||
</span>
|
||||
)}
|
||||
{event.venue && (
|
||||
<span className="flex items-center gap-1.5">
|
||||
<MapPin className="h-4 w-4" /> {event.venue}
|
||||
</span>
|
||||
)}
|
||||
{deadline && (
|
||||
<span
|
||||
className={`text-muted-foreground ml-auto flex items-center gap-1.5 ${deadlinePassed ? 'text-destructive' : ''}`}
|
||||
>
|
||||
<Clock className="h-4 w-4" />
|
||||
{deadlinePassed ? 'Picks closed' : 'Picks close'}: {fmt.format(deadline)}
|
||||
</span>
|
||||
)}
|
||||
{event.notes && (
|
||||
<details className="basis-full">
|
||||
<summary className="text-muted-foreground cursor-pointer text-xs">
|
||||
Notes from organizers
|
||||
</summary>
|
||||
<p className="mt-1 text-sm whitespace-pre-wrap">{event.notes}</p>
|
||||
</details>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
204
src/components/applicant/lunch-pick-form.tsx
Normal file
204
src/components/applicant/lunch-pick-form.tsx
Normal file
@@ -0,0 +1,204 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectLabel,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/components/ui/select'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Salad, Lock, CheckCircle2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const ALLERGENS = [
|
||||
'GLUTEN',
|
||||
'CRUSTACEANS',
|
||||
'EGGS',
|
||||
'FISH',
|
||||
'PEANUTS',
|
||||
'SOYBEANS',
|
||||
'MILK',
|
||||
'TREE_NUTS',
|
||||
'CELERY',
|
||||
'MUSTARD',
|
||||
'SESAME',
|
||||
'SULPHITES',
|
||||
'LUPIN',
|
||||
'MOLLUSCS',
|
||||
] as const
|
||||
type Allergen = (typeof ALLERGENS)[number]
|
||||
|
||||
const NO_DISH = '__no_dish__'
|
||||
|
||||
function formatTag(t: string): string {
|
||||
return t.replace('_', ' ').toLowerCase()
|
||||
}
|
||||
|
||||
export function LunchPickForm({
|
||||
attendingMemberId,
|
||||
programId,
|
||||
lunchEventId,
|
||||
canEdit,
|
||||
editingOnBehalfOf,
|
||||
}: {
|
||||
attendingMemberId: string
|
||||
programId: string
|
||||
lunchEventId: string
|
||||
canEdit: boolean
|
||||
editingOnBehalfOf?: string | null
|
||||
}) {
|
||||
const utils = trpc.useUtils()
|
||||
const { data: dishes } = trpc.lunch.listDishes.useQuery({ lunchEventId })
|
||||
const { data: row, refetch } = trpc.lunch.getMemberPick.useQuery({
|
||||
attendingMemberId,
|
||||
})
|
||||
const pick = row?.pick ?? null
|
||||
|
||||
const [dishId, setDishId] = useState<string>('')
|
||||
const [allergens, setAllergens] = useState<Allergen[]>([])
|
||||
const [allergenOther, setAllergenOther] = useState<string>('')
|
||||
const [hydrated, setHydrated] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!hydrated && pick) {
|
||||
setDishId(pick.dishId ?? '')
|
||||
setAllergens((pick.allergens as Allergen[]) ?? [])
|
||||
setAllergenOther(pick.allergenOther ?? '')
|
||||
setHydrated(true)
|
||||
}
|
||||
}, [pick, hydrated])
|
||||
|
||||
const upsert = trpc.lunch.upsertPick.useMutation({
|
||||
onSuccess: () => {
|
||||
refetch()
|
||||
utils.lunch.getManifest.invalidate({ programId })
|
||||
toast.success('Lunch pick saved')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
function commit(next: {
|
||||
dishId?: string
|
||||
allergens?: Allergen[]
|
||||
allergenOther?: string
|
||||
}) {
|
||||
if (!canEdit) return
|
||||
upsert.mutate({
|
||||
attendingMemberId,
|
||||
dishId: (next.dishId ?? dishId) || null,
|
||||
allergens: next.allergens ?? allergens,
|
||||
allergenOther: (next.allergenOther ?? allergenOther) || null,
|
||||
})
|
||||
}
|
||||
|
||||
if (!dishes) return null
|
||||
|
||||
const grouped: Record<string, typeof dishes> = {}
|
||||
for (const d of dishes) {
|
||||
const key = d.dietaryTags.length > 0 ? d.dietaryTags[0] : 'OTHER'
|
||||
if (!grouped[key]) grouped[key] = []
|
||||
grouped[key].push(d)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-3 rounded-md border-l-2 border-emerald-500/30 bg-emerald-500/5 p-3 text-sm">
|
||||
<div className="flex items-center gap-2">
|
||||
<Salad className="h-4 w-4 text-emerald-500" />
|
||||
<span className="font-medium">Lunch</span>
|
||||
{pick?.pickedAt && (
|
||||
<Badge variant="outline" className="gap-1">
|
||||
<CheckCircle2 className="h-3 w-3" /> picked
|
||||
</Badge>
|
||||
)}
|
||||
{!canEdit && (
|
||||
<Badge variant="secondary" className="gap-1">
|
||||
<Lock className="h-3 w-3" /> read-only
|
||||
</Badge>
|
||||
)}
|
||||
{editingOnBehalfOf && (
|
||||
<span className="text-muted-foreground text-xs">
|
||||
Editing on behalf of {editingOnBehalfOf}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label className="text-xs">Dish</Label>
|
||||
<Select
|
||||
value={dishId === '' ? NO_DISH : dishId}
|
||||
onValueChange={(v) => {
|
||||
const next = v === NO_DISH ? '' : v
|
||||
setDishId(next)
|
||||
commit({ dishId: next })
|
||||
}}
|
||||
disabled={!canEdit}
|
||||
>
|
||||
<SelectTrigger className="max-w-sm">
|
||||
<SelectValue placeholder="Pick a dish" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NO_DISH}>Not picked</SelectItem>
|
||||
{Object.entries(grouped).map(([group, items]) => (
|
||||
<SelectGroup key={group}>
|
||||
<SelectLabel>
|
||||
{group === 'OTHER' ? 'All options' : formatTag(group)}
|
||||
</SelectLabel>
|
||||
{items.map((d) => (
|
||||
<SelectItem key={d.id} value={d.id}>
|
||||
{d.name}
|
||||
{d.dietaryTags.length > 0 && (
|
||||
<span className="text-muted-foreground ml-2 text-xs">
|
||||
{d.dietaryTags.map(formatTag).join(', ')}
|
||||
</span>
|
||||
)}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label className="text-xs">Allergens</Label>
|
||||
<div className="grid grid-cols-2 gap-2 sm:grid-cols-3">
|
||||
{ALLERGENS.map((a) => (
|
||||
<label key={a} className="flex items-center gap-2">
|
||||
<Checkbox
|
||||
checked={allergens.includes(a)}
|
||||
disabled={!canEdit}
|
||||
onCheckedChange={(v) => {
|
||||
const next = v
|
||||
? [...allergens, a]
|
||||
: allergens.filter((x) => x !== a)
|
||||
setAllergens(next)
|
||||
commit({ allergens: next })
|
||||
}}
|
||||
/>
|
||||
<span className="text-xs">{formatTag(a)}</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Label className="text-xs">Other allergens / notes</Label>
|
||||
<Textarea
|
||||
value={allergenOther}
|
||||
disabled={!canEdit}
|
||||
onChange={(e) => setAllergenOther(e.target.value)}
|
||||
onBlur={() => commit({ allergenOther })}
|
||||
rows={2}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
60
src/components/applicant/mentor-conversation-card.tsx
Normal file
60
src/components/applicant/mentor-conversation-card.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { MessageCircle } from 'lucide-react'
|
||||
|
||||
interface Props {
|
||||
projectId: string
|
||||
}
|
||||
|
||||
export function MentorConversationCard({ projectId }: Props) {
|
||||
const { data, isLoading } = trpc.applicant.getMentorConversationPreview.useQuery(
|
||||
{ projectId, limit: 3 },
|
||||
{ refetchInterval: 30_000 },
|
||||
)
|
||||
|
||||
if (isLoading) return <Skeleton className="h-44 w-full rounded-md" />
|
||||
if (!data || !data.mentor) return null // No mentor assigned — render nothing
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center justify-between gap-2 text-base">
|
||||
<span className="flex items-center gap-2">
|
||||
<MessageCircle className="h-4 w-4" /> Conversation with{' '}
|
||||
{data.mentor.name ?? data.mentor.email}
|
||||
</span>
|
||||
{data.unreadCount > 0 && <Badge variant="default">{data.unreadCount} new</Badge>}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{data.messages.length === 0 ? (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
Say hi to your mentor — they're here to help you sharpen your project.
|
||||
</p>
|
||||
) : (
|
||||
<ul className="space-y-2">
|
||||
{data.messages.map((m) => (
|
||||
<li key={m.id} className="bg-muted/20 rounded-md border p-2.5">
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{m.sender.name ?? m.sender.email}
|
||||
</div>
|
||||
<div className="mt-0.5 line-clamp-2 text-sm">{m.message}</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<div className="mt-3 flex justify-end">
|
||||
<Button asChild size="sm">
|
||||
<Link href="/applicant/mentor">Open chat</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
@@ -30,8 +30,10 @@ import {
|
||||
LogOut,
|
||||
ChevronRight,
|
||||
BookOpen,
|
||||
GraduationCap,
|
||||
Handshake,
|
||||
History,
|
||||
Plane,
|
||||
Trophy,
|
||||
User,
|
||||
MessageSquare,
|
||||
@@ -50,6 +52,7 @@ import { UserAvatar } from '@/components/shared/user-avatar'
|
||||
import { NotificationBell } from '@/components/shared/notification-bell'
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { useRoleSwitcher } from './role-switcher'
|
||||
|
||||
interface AdminSidebarProps {
|
||||
user: {
|
||||
@@ -84,6 +87,16 @@ const navigation: NavItem[] = [
|
||||
href: '/admin/juries',
|
||||
icon: Scale,
|
||||
},
|
||||
{
|
||||
name: 'Mentors',
|
||||
href: '/admin/mentors',
|
||||
icon: GraduationCap,
|
||||
},
|
||||
{
|
||||
name: 'Logistics',
|
||||
href: '/admin/logistics',
|
||||
icon: Plane,
|
||||
},
|
||||
{
|
||||
name: 'Awards',
|
||||
href: '/admin/awards',
|
||||
@@ -157,14 +170,6 @@ const roleLabels: Record<string, string> = {
|
||||
AWARD_MASTER: 'Award Master',
|
||||
}
|
||||
|
||||
// Role switcher config — maps roles to their dashboard views
|
||||
const ROLE_SWITCH_OPTIONS: Record<string, { label: string; path: string; icon: typeof LayoutDashboard }> = {
|
||||
JURY_MEMBER: { label: 'Jury View', path: '/jury', icon: Scale },
|
||||
MENTOR: { label: 'Mentor View', path: '/mentor', icon: Handshake },
|
||||
OBSERVER: { label: 'Observer View', path: '/observer', icon: Eye },
|
||||
AWARD_MASTER: { label: 'Award Master', path: '/award-master', icon: Trophy },
|
||||
}
|
||||
|
||||
export function AdminSidebar({ user }: AdminSidebarProps) {
|
||||
const pathname = usePathname()
|
||||
const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false)
|
||||
@@ -186,11 +191,10 @@ export function AdminSidebar({ user }: AdminSidebarProps) {
|
||||
const isSuperAdmin = user.role === 'SUPER_ADMIN'
|
||||
const roleLabel = roleLabels[user.role || ''] || 'User'
|
||||
|
||||
// Roles the user can switch to (non-admin roles they hold)
|
||||
const userRoles = (session?.user?.roles as UserRole[] | undefined) ?? []
|
||||
const switchableRoles = Object.entries(ROLE_SWITCH_OPTIONS).filter(
|
||||
([role]) => userRoles.includes(role as UserRole)
|
||||
)
|
||||
// Roles the user can switch to — shared logic. Admin sidebar dropdown
|
||||
// no longer renders these; the RoleSwitcherPill in the layout's top-bar
|
||||
// handles role switching for admins, matching every other dashboard.
|
||||
const { switchableRoles } = useRoleSwitcher('/admin')
|
||||
|
||||
// Build dynamic admin nav with current edition's apply page
|
||||
const dynamicAdminNav = adminNavigation.map((item) => {
|
||||
@@ -374,46 +378,9 @@ export function AdminSidebar({ user }: AdminSidebarProps) {
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
|
||||
{switchableRoles.length > 0 && (
|
||||
<>
|
||||
<DropdownMenuSeparator className="my-1" />
|
||||
{switchableRoles.length <= 2 ? (
|
||||
// Flat list for 1-2 roles
|
||||
switchableRoles.map(([, opt]) => (
|
||||
<DropdownMenuItem key={opt.path} asChild>
|
||||
<Link
|
||||
href={opt.path as Route}
|
||||
className="flex cursor-pointer items-center gap-2.5 rounded-md px-2 py-2"
|
||||
>
|
||||
<opt.icon className="h-4 w-4 text-muted-foreground" />
|
||||
<span>{opt.label}</span>
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
))
|
||||
) : (
|
||||
// Submenu for 3+ roles
|
||||
<DropdownMenuSub>
|
||||
<DropdownMenuSubTrigger className="flex items-center gap-2.5 rounded-md px-2 py-2">
|
||||
<ArrowRightLeft className="h-4 w-4 text-muted-foreground" />
|
||||
<span>Switch View</span>
|
||||
</DropdownMenuSubTrigger>
|
||||
<DropdownMenuSubContent className="min-w-[160px]">
|
||||
{switchableRoles.map(([, opt]) => (
|
||||
<DropdownMenuItem key={opt.path} asChild>
|
||||
<Link
|
||||
href={opt.path as Route}
|
||||
className="flex cursor-pointer items-center gap-2.5 rounded-md px-2 py-2"
|
||||
>
|
||||
<opt.icon className="h-4 w-4 text-muted-foreground" />
|
||||
<span>{opt.label}</span>
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuSubContent>
|
||||
</DropdownMenuSub>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{/* Role switcher items moved to the layout's top-bar
|
||||
RoleSwitcherPill — single source of truth across all
|
||||
dashboards. */}
|
||||
|
||||
<DropdownMenuSeparator className="my-1" />
|
||||
|
||||
|
||||
@@ -19,14 +19,14 @@ import {
|
||||
import type { Route } from 'next'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import {
|
||||
LogOut, Menu, Moon, Settings, Sun, User, X, Trophy,
|
||||
LayoutDashboard, Scale, Handshake, Eye, ArrowRightLeft,
|
||||
LogOut, Menu, Moon, Settings, Sun, User, X,
|
||||
ArrowRightLeft,
|
||||
ExternalLink as ExternalLinkIcon, HelpCircle, Mail,
|
||||
} from 'lucide-react'
|
||||
import type { UserRole } from '@prisma/client'
|
||||
import { useTheme } from 'next-themes'
|
||||
import { Logo } from '@/components/shared/logo'
|
||||
import { NotificationBell } from '@/components/shared/notification-bell'
|
||||
import { useRoleSwitcher, RoleSwitcherPill } from './role-switcher'
|
||||
|
||||
export type NavItem = {
|
||||
name: string
|
||||
@@ -54,16 +54,6 @@ type RoleNavProps = {
|
||||
helpEmail?: string
|
||||
}
|
||||
|
||||
// Role switcher config — maps roles to their dashboard views
|
||||
const ROLE_SWITCH_OPTIONS: Record<string, { label: string; path: string; icon: typeof LayoutDashboard }> = {
|
||||
SUPER_ADMIN: { label: 'Admin View', path: '/admin', icon: LayoutDashboard },
|
||||
PROGRAM_ADMIN: { label: 'Admin View', path: '/admin', icon: LayoutDashboard },
|
||||
JURY_MEMBER: { label: 'Jury View', path: '/jury', icon: Scale },
|
||||
MENTOR: { label: 'Mentor View', path: '/mentor', icon: Handshake },
|
||||
OBSERVER: { label: 'Observer View', path: '/observer', icon: Eye },
|
||||
AWARD_MASTER: { label: 'Award Master', path: '/award-master', icon: Trophy },
|
||||
}
|
||||
|
||||
function isNavItemActive(pathname: string, href: string, basePath: string): boolean {
|
||||
return pathname === href || (href !== basePath && pathname.startsWith(href))
|
||||
}
|
||||
@@ -111,12 +101,8 @@ export function RoleNav({ navigation, roleName, user, basePath, statusBadge, edi
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isAuthenticated])
|
||||
|
||||
// Roles the user can switch to (excluding current view)
|
||||
const userRoles = (session?.user?.roles as UserRole[] | undefined) ?? []
|
||||
const switchableRoles = Object.entries(ROLE_SWITCH_OPTIONS)
|
||||
.filter(([role, opt]) => userRoles.includes(role as UserRole) && opt.path !== basePath)
|
||||
// Deduplicate admin paths (SUPER_ADMIN and PROGRAM_ADMIN both go to /admin)
|
||||
.filter((entry, i, arr) => arr.findIndex(([, o]) => o.path === entry[1].path) === i)
|
||||
// Roles the user can switch to (excluding current view) — shared logic
|
||||
const { switchableRoles } = useRoleSwitcher(basePath)
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-40 border-b bg-card">
|
||||
@@ -200,6 +186,7 @@ export function RoleNav({ navigation, roleName, user, basePath, statusBadge, edi
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
<RoleSwitcherPill currentBasePath={basePath} />
|
||||
<NotificationBell />
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
|
||||
100
src/components/layouts/role-switcher.tsx
Normal file
100
src/components/layouts/role-switcher.tsx
Normal file
@@ -0,0 +1,100 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo } from 'react'
|
||||
import Link from 'next/link'
|
||||
import { useSession } from 'next-auth/react'
|
||||
import {
|
||||
Check,
|
||||
ChevronDown,
|
||||
Eye,
|
||||
Handshake,
|
||||
LayoutDashboard,
|
||||
Scale,
|
||||
Trophy,
|
||||
type LucideIcon,
|
||||
} from 'lucide-react'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import type { Route } from 'next'
|
||||
import type { UserRole } from '@prisma/client'
|
||||
|
||||
export type RoleSwitchOption = { label: string; path: string; icon: LucideIcon }
|
||||
|
||||
export const ROLE_SWITCH_OPTIONS: Record<string, RoleSwitchOption> = {
|
||||
SUPER_ADMIN: { label: 'Admin View', path: '/admin', icon: LayoutDashboard },
|
||||
PROGRAM_ADMIN: { label: 'Admin View', path: '/admin', icon: LayoutDashboard },
|
||||
JURY_MEMBER: { label: 'Jury View', path: '/jury', icon: Scale },
|
||||
MENTOR: { label: 'Mentor View', path: '/mentor', icon: Handshake },
|
||||
OBSERVER: { label: 'Observer View', path: '/observer', icon: Eye },
|
||||
AWARD_MASTER: { label: 'Award Master', path: '/award-master', icon: Trophy },
|
||||
}
|
||||
|
||||
export function useRoleSwitcher(currentBasePath: string): {
|
||||
currentOption: RoleSwitchOption | null
|
||||
switchableRoles: Array<[string, RoleSwitchOption]>
|
||||
isImpersonating: boolean
|
||||
} {
|
||||
const { data: session } = useSession()
|
||||
const userRoles = (session?.user?.roles as UserRole[] | undefined) ?? []
|
||||
const isImpersonating = !!session?.user?.impersonating
|
||||
|
||||
const { currentOption, switchableRoles } = useMemo(() => {
|
||||
const accessible = Object.entries(ROLE_SWITCH_OPTIONS)
|
||||
.filter(([role]) => userRoles.includes(role as UserRole))
|
||||
.filter((entry, i, arr) => arr.findIndex(([, o]) => o.path === entry[1].path) === i)
|
||||
const current = accessible.find(([, opt]) => opt.path === currentBasePath)?.[1] ?? null
|
||||
const others = accessible.filter(([, opt]) => opt.path !== currentBasePath)
|
||||
return { currentOption: current, switchableRoles: others }
|
||||
}, [userRoles, currentBasePath])
|
||||
|
||||
return { currentOption, switchableRoles, isImpersonating }
|
||||
}
|
||||
|
||||
/**
|
||||
* Top-right view-switcher pill. Trigger names the current view; the dropdown
|
||||
* lists alternative views. Hidden for single-view users and during impersonation.
|
||||
*/
|
||||
export function RoleSwitcherPill({ currentBasePath }: { currentBasePath: string }) {
|
||||
const { currentOption, switchableRoles, isImpersonating } = useRoleSwitcher(currentBasePath)
|
||||
if (switchableRoles.length === 0 || isImpersonating || !currentOption) return null
|
||||
|
||||
const CurrentIcon = currentOption.icon
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="sm" className="gap-1.5" aria-label="Switch view">
|
||||
<CurrentIcon className="h-3.5 w-3.5" />
|
||||
<span className="hidden sm:inline">{currentOption.label}</span>
|
||||
<ChevronDown className="h-3.5 w-3.5 opacity-60" />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="min-w-52">
|
||||
<DropdownMenuLabel className="text-muted-foreground text-xs font-normal">
|
||||
Switch to another view
|
||||
</DropdownMenuLabel>
|
||||
<DropdownMenuItem disabled className="opacity-100">
|
||||
<CurrentIcon className="mr-2 h-4 w-4" />
|
||||
<span className="flex-1">{currentOption.label}</span>
|
||||
<Check className="text-muted-foreground h-3.5 w-3.5" />
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
{switchableRoles.map(([role, opt]) => (
|
||||
<DropdownMenuItem key={role} asChild>
|
||||
<Link href={opt.path as Route} className="flex cursor-pointer items-center">
|
||||
<opt.icon className="mr-2 h-4 w-4" />
|
||||
{opt.label}
|
||||
</Link>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
111
src/components/mentor/drop-assignment-dialog.tsx
Normal file
111
src/components/mentor/drop-assignment-dialog.tsx
Normal file
@@ -0,0 +1,111 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Label } from '@/components/ui/label'
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
AlertDialogCancel,
|
||||
AlertDialogContent,
|
||||
AlertDialogDescription,
|
||||
AlertDialogFooter,
|
||||
AlertDialogHeader,
|
||||
AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { Loader2, UserMinus } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const MIN_REASON = 10
|
||||
const MAX_REASON = 1000
|
||||
|
||||
export function DropAssignmentDialog({
|
||||
assignmentId,
|
||||
projectTitle,
|
||||
}: {
|
||||
assignmentId: string
|
||||
projectTitle: string
|
||||
}) {
|
||||
const router = useRouter()
|
||||
const [open, setOpen] = useState(false)
|
||||
const [reason, setReason] = useState('')
|
||||
|
||||
const drop = trpc.mentor.dropAssignment.useMutation({
|
||||
onSuccess: () => {
|
||||
toast.success('You have dropped this assignment.')
|
||||
setOpen(false)
|
||||
router.push('/mentor')
|
||||
router.refresh()
|
||||
},
|
||||
onError: (e) => {
|
||||
toast.error(e.message)
|
||||
},
|
||||
})
|
||||
|
||||
const trimmed = reason.trim()
|
||||
const tooShort = trimmed.length < MIN_REASON
|
||||
const tooLong = trimmed.length > MAX_REASON
|
||||
|
||||
return (
|
||||
<AlertDialog
|
||||
open={open}
|
||||
onOpenChange={(next) => {
|
||||
if (!drop.isPending) setOpen(next)
|
||||
if (!next) setReason('')
|
||||
}}
|
||||
>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="outline" size="sm" className="text-destructive hover:text-destructive">
|
||||
<UserMinus className="mr-2 h-4 w-4" />
|
||||
Drop this team
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Drop {projectTitle}?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
The team will be unassigned from you and an admin will be notified so they can find a
|
||||
replacement mentor. Please share why so we can adjust future assignments.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="drop-reason">Reason</Label>
|
||||
<Textarea
|
||||
id="drop-reason"
|
||||
value={reason}
|
||||
onChange={(e) => setReason(e.target.value)}
|
||||
placeholder="e.g. schedule conflict, expertise mismatch, conflict of interest..."
|
||||
rows={4}
|
||||
disabled={drop.isPending}
|
||||
className="resize-none"
|
||||
/>
|
||||
<p
|
||||
className={`text-xs ${tooShort || tooLong ? 'text-destructive' : 'text-muted-foreground'}`}
|
||||
>
|
||||
{trimmed.length}/{MAX_REASON} characters · minimum {MIN_REASON}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel disabled={drop.isPending}>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction
|
||||
disabled={tooShort || tooLong || drop.isPending}
|
||||
onClick={(e) => {
|
||||
e.preventDefault()
|
||||
drop.mutate({ assignmentId, reason: trimmed })
|
||||
}}
|
||||
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
||||
>
|
||||
{drop.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
||||
Drop assignment
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
)
|
||||
}
|
||||
@@ -32,8 +32,11 @@ export function FilePromotionPanel({ mentorAssignmentId }: FilePromotionPanelPro
|
||||
const [selectedSlot, setSelectedSlot] = useState<string>('')
|
||||
const utils = trpc.useUtils()
|
||||
|
||||
// Mock workspace files - in real implementation, would fetch from workspaceGetFiles
|
||||
const workspaceFiles: any[] = [] // Placeholder
|
||||
const { data: workspaceFiles = [], isLoading: filesLoading } =
|
||||
trpc.mentor.workspaceGetFiles.useQuery(
|
||||
{ mentorAssignmentId },
|
||||
{ enabled: !!mentorAssignmentId },
|
||||
)
|
||||
|
||||
const promoteMutation = trpc.mentor.workspacePromoteFile.useMutation({
|
||||
onSuccess: () => {
|
||||
@@ -56,7 +59,7 @@ export function FilePromotionPanel({ mentorAssignmentId }: FilePromotionPanelPro
|
||||
})
|
||||
}
|
||||
|
||||
const isLoading = false // Placeholder
|
||||
const isLoading = filesLoading
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
|
||||
66
src/components/mentor/recent-messages-card.tsx
Normal file
66
src/components/mentor/recent-messages-card.tsx
Normal file
@@ -0,0 +1,66 @@
|
||||
'use client'
|
||||
|
||||
import Link from 'next/link'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { MessageCircle } from 'lucide-react'
|
||||
|
||||
function formatRelativePast(date: Date | string | null): string {
|
||||
if (!date) return ''
|
||||
const d = typeof date === 'string' ? new Date(date) : date
|
||||
const ms = Date.now() - d.getTime()
|
||||
const minutes = Math.floor(ms / 60_000)
|
||||
const hours = Math.floor(ms / 3_600_000)
|
||||
const days = Math.floor(ms / 86_400_000)
|
||||
if (days > 0) return `${days}d ago`
|
||||
if (hours > 0) return `${hours}h ago`
|
||||
return `${Math.max(0, minutes)}m ago`
|
||||
}
|
||||
|
||||
export function RecentMessagesCard() {
|
||||
const { data, isLoading } = trpc.mentor.getRecentMessages.useQuery({ limit: 5 })
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="flex items-center gap-2 text-base">
|
||||
<MessageCircle className="h-4 w-4" /> Recent Messages
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<div className="space-y-2">
|
||||
{[1, 2, 3].map((i) => (
|
||||
<Skeleton key={i} className="h-12 w-full" />
|
||||
))}
|
||||
</div>
|
||||
) : !data || data.unread.length === 0 ? (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
No new messages. Your mentees will appear here when they reach out.
|
||||
</p>
|
||||
) : (
|
||||
<ul className="space-y-3">
|
||||
{data.unread.map((m) => (
|
||||
<li key={m.id}>
|
||||
<Link
|
||||
href={`/mentor/workspace/${m.project.id}`}
|
||||
className="hover:bg-muted/40 block rounded-md border p-3"
|
||||
>
|
||||
<div className="flex items-baseline justify-between gap-2">
|
||||
<div className="text-sm font-medium">{m.sender.name ?? m.sender.email}</div>
|
||||
<div className="text-muted-foreground text-xs">
|
||||
{formatRelativePast(m.createdAt as unknown as Date)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-muted-foreground mt-0.5 text-xs">{m.project.title}</div>
|
||||
<div className="mt-1 line-clamp-2 text-sm">{m.message}</div>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
193
src/components/mentor/workspace-files-panel.tsx
Normal file
193
src/components/mentor/workspace-files-panel.tsx
Normal file
@@ -0,0 +1,193 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useRef } from 'react'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { toast } from 'sonner'
|
||||
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import {
|
||||
AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
|
||||
AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle,
|
||||
AlertDialogTrigger,
|
||||
} from '@/components/ui/alert-dialog'
|
||||
import { FileText, Upload, Download, Trash2, MessageSquare } from 'lucide-react'
|
||||
import { formatDistanceToNow } from 'date-fns'
|
||||
|
||||
interface Props {
|
||||
mentorAssignmentId: string
|
||||
/** Set true on the applicant side to label uploads as "Team upload" — purely cosmetic. */
|
||||
asApplicant?: boolean
|
||||
}
|
||||
|
||||
function formatSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 B'
|
||||
const k = 1024
|
||||
const sizes = ['B', 'KB', 'MB', 'GB']
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
||||
}
|
||||
|
||||
export function WorkspaceFilesPanel({ mentorAssignmentId, asApplicant }: Props) {
|
||||
const utils = trpc.useUtils()
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
const [uploading, setUploading] = useState(false)
|
||||
const [description, setDescription] = useState('')
|
||||
|
||||
const { data: files, isLoading } = trpc.mentor.workspaceGetFiles.useQuery(
|
||||
{ mentorAssignmentId },
|
||||
{ enabled: !!mentorAssignmentId }
|
||||
)
|
||||
|
||||
const presign = trpc.mentor.workspaceGetUploadUrl.useMutation()
|
||||
const recordUpload = trpc.mentor.workspaceUploadFile.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.workspaceGetFiles.invalidate({ mentorAssignmentId })
|
||||
setDescription('')
|
||||
toast.success('File uploaded')
|
||||
},
|
||||
})
|
||||
const downloadMutation = trpc.mentor.workspaceGetFileDownloadUrl.useMutation()
|
||||
const deleteMutation = trpc.mentor.workspaceDeleteFile.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.mentor.workspaceGetFiles.invalidate({ mentorAssignmentId })
|
||||
toast.success('File deleted')
|
||||
},
|
||||
onError: (e) => toast.error(e.message),
|
||||
})
|
||||
|
||||
const handleFileSelected = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0]
|
||||
if (!file) return
|
||||
e.target.value = ''
|
||||
setUploading(true)
|
||||
try {
|
||||
const { uploadUrl, uploadToken } = await presign.mutateAsync({
|
||||
mentorAssignmentId,
|
||||
fileName: file.name,
|
||||
mimeType: file.type || 'application/octet-stream',
|
||||
size: file.size,
|
||||
})
|
||||
const putRes = await fetch(uploadUrl, {
|
||||
method: 'PUT',
|
||||
body: file,
|
||||
headers: { 'Content-Type': file.type || 'application/octet-stream' },
|
||||
})
|
||||
if (!putRes.ok) throw new Error(`Upload failed: HTTP ${putRes.status}`)
|
||||
await recordUpload.mutateAsync({ uploadToken, description: description || undefined })
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : 'Upload failed')
|
||||
} finally {
|
||||
setUploading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleDownload = async (mentorFileId: string) => {
|
||||
try {
|
||||
const { url } = await downloadMutation.mutateAsync({ mentorFileId })
|
||||
window.open(url, '_blank')
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : 'Download failed')
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader><CardTitle>Workspace Files</CardTitle></CardHeader>
|
||||
<CardContent className="space-y-2">
|
||||
<Skeleton className="h-12 w-full" />
|
||||
<Skeleton className="h-12 w-full" />
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Workspace Files</CardTitle>
|
||||
<CardDescription>
|
||||
{asApplicant
|
||||
? 'Files shared with your mentor in this workspace.'
|
||||
: 'Files you and the team have shared in this workspace.'}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="flex flex-col sm:flex-row gap-2">
|
||||
<Input
|
||||
value={description}
|
||||
onChange={(e) => setDescription(e.target.value)}
|
||||
placeholder="Optional description for the next upload"
|
||||
className="flex-1"
|
||||
/>
|
||||
<Button
|
||||
disabled={uploading}
|
||||
onClick={() => inputRef.current?.click()}
|
||||
className="shrink-0"
|
||||
>
|
||||
<Upload className="mr-2 h-4 w-4" />
|
||||
{uploading ? 'Uploading…' : 'Upload file'}
|
||||
</Button>
|
||||
<input ref={inputRef} type="file" hidden onChange={handleFileSelected} />
|
||||
</div>
|
||||
|
||||
{files && files.length === 0 && (
|
||||
<div className="text-center py-8 text-sm text-muted-foreground">
|
||||
<FileText className="h-10 w-10 mx-auto mb-2 opacity-40" />
|
||||
No files in this workspace yet.
|
||||
</div>
|
||||
)}
|
||||
|
||||
<ul className="divide-y">
|
||||
{(files ?? []).map((f) => (
|
||||
<li key={f.id} className="flex items-center gap-3 py-3">
|
||||
<FileText className="h-5 w-5 text-muted-foreground shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="font-medium truncate">{f.fileName}</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{f.uploadedBy.name ?? f.uploadedBy.email} · {formatSize(f.size)} ·{' '}
|
||||
{formatDistanceToNow(new Date(f.createdAt), { addSuffix: true })}
|
||||
{f._count.comments > 0 && (
|
||||
<span className="ml-2 inline-flex items-center gap-1">
|
||||
<MessageSquare className="h-3 w-3" />
|
||||
{f._count.comments}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{f.description && (
|
||||
<div className="text-xs text-muted-foreground mt-1">{f.description}</div>
|
||||
)}
|
||||
</div>
|
||||
<Button variant="ghost" size="icon" onClick={() => handleDownload(f.id)}>
|
||||
<Download className="h-4 w-4" />
|
||||
</Button>
|
||||
<AlertDialog>
|
||||
<AlertDialogTrigger asChild>
|
||||
<Button variant="ghost" size="icon" className="text-destructive hover:text-destructive">
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</AlertDialogTrigger>
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>Delete this file?</AlertDialogTitle>
|
||||
<AlertDialogDescription>
|
||||
This removes the file from MinIO and the workspace. Comments on the file are deleted with it.
|
||||
</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
||||
<AlertDialogAction onClick={() => deleteMutation.mutate({ mentorFileId: f.id })}>
|
||||
Delete
|
||||
</AlertDialogAction>
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
@@ -25,7 +25,9 @@ import {
|
||||
ShieldAlert,
|
||||
Webhook,
|
||||
MessageCircle,
|
||||
Trophy,
|
||||
} from 'lucide-react'
|
||||
import { EditionSettingsTab } from '@/components/admin/settings/edition-settings-tab'
|
||||
import Link from 'next/link'
|
||||
import { AnimatedCard } from '@/components/shared/animated-container'
|
||||
import { AISettingsForm } from './ai-settings-form'
|
||||
@@ -179,6 +181,10 @@ export function SettingsContent({ initialSettings, isSuperAdmin = true }: Settin
|
||||
<SettingsIcon className="h-4 w-4" />
|
||||
Defaults
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="edition" className="gap-2 shrink-0">
|
||||
<Trophy className="h-4 w-4" />
|
||||
Edition
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="branding" className="gap-2 shrink-0">
|
||||
<Palette className="h-4 w-4" />
|
||||
Branding
|
||||
@@ -252,6 +258,10 @@ export function SettingsContent({ initialSettings, isSuperAdmin = true }: Settin
|
||||
<SettingsIcon className="h-4 w-4" />
|
||||
Defaults
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="edition" className="justify-start gap-2 w-full px-3 py-2 h-auto data-[state=active]:bg-muted">
|
||||
<Trophy className="h-4 w-4" />
|
||||
Edition
|
||||
</TabsTrigger>
|
||||
<TabsTrigger value="branding" className="justify-start gap-2 w-full px-3 py-2 h-auto data-[state=active]:bg-muted">
|
||||
<Palette className="h-4 w-4" />
|
||||
Branding
|
||||
@@ -464,6 +474,12 @@ export function SettingsContent({ initialSettings, isSuperAdmin = true }: Settin
|
||||
</TabsContent>
|
||||
)}
|
||||
|
||||
<TabsContent value="edition" className="space-y-6">
|
||||
<AnimatedCard>
|
||||
<EditionSettingsTab />
|
||||
</AnimatedCard>
|
||||
</TabsContent>
|
||||
|
||||
<TabsContent value="defaults" className="space-y-6">
|
||||
<AnimatedCard>
|
||||
<Card>
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Download, Loader2 } from 'lucide-react'
|
||||
import { csvCell } from '@/lib/csv'
|
||||
|
||||
/**
|
||||
* Converts a camelCase or snake_case column name to Title Case.
|
||||
@@ -105,30 +106,14 @@ export function CsvExportDialog({
|
||||
|
||||
const columnsArray = exportData.columns.filter((col) => selectedColumns.has(col))
|
||||
|
||||
// Build CSV header with formatted names
|
||||
const csvHeader = columnsArray.map((col) => {
|
||||
const formatted = formatColumnName(col)
|
||||
// Escape quotes in header
|
||||
if (formatted.includes(',') || formatted.includes('"')) {
|
||||
return `"${formatted.replace(/"/g, '""')}"`
|
||||
}
|
||||
return formatted
|
||||
})
|
||||
// Build CSV header with formatted names. Use csvCell so formula-character
|
||||
// headers (unlikely in practice) are still defanged.
|
||||
const csvHeader = columnsArray.map((col) => csvCell(formatColumnName(col)))
|
||||
|
||||
const csvContent = [
|
||||
csvHeader.join(','),
|
||||
...exportData.data.map((row) =>
|
||||
columnsArray
|
||||
.map((col) => {
|
||||
const value = row[col]
|
||||
if (value === null || value === undefined) return ''
|
||||
const str = String(value)
|
||||
if (str.includes(',') || str.includes('"') || str.includes('\n')) {
|
||||
return `"${str.replace(/"/g, '""')}"`
|
||||
}
|
||||
return str
|
||||
})
|
||||
.join(',')
|
||||
columnsArray.map((col) => csvCell(row[col])).join(',')
|
||||
),
|
||||
].join('\n')
|
||||
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
'use client'
|
||||
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { trpc } from '@/lib/trpc/client'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ArrowLeft, Loader2 } from 'lucide-react'
|
||||
import { toast } from 'sonner'
|
||||
import type { UserRole } from '@prisma/client'
|
||||
|
||||
export function ImpersonationBanner() {
|
||||
const { data: session, update } = useSession()
|
||||
const router = useRouter()
|
||||
const endImpersonation = trpc.user.endImpersonation.useMutation()
|
||||
|
||||
if (!session?.user?.impersonating) return null
|
||||
@@ -18,23 +17,29 @@ export function ImpersonationBanner() {
|
||||
try {
|
||||
await endImpersonation.mutateAsync()
|
||||
await update({ endImpersonation: true })
|
||||
// Full page navigation to ensure updated JWT cookie is sent
|
||||
window.location.href = '/admin/members'
|
||||
} catch (error) {
|
||||
toast.error(error instanceof Error ? error.message : 'Failed to end impersonation')
|
||||
}
|
||||
}
|
||||
|
||||
// Show every role the impersonated user holds (multi-role support).
|
||||
const roles = (session.user.roles as UserRole[] | undefined) ?? [session.user.role as UserRole]
|
||||
const rolesLabel = roles.map((r) => r.replace(/_/g, ' ')).join(', ')
|
||||
|
||||
return (
|
||||
<div className="fixed top-0 left-0 right-0 z-50 flex items-center justify-center gap-3 bg-red-600 px-4 py-1.5 text-sm text-white shadow-md">
|
||||
// pointer-events-none on the wrapper so the banner doesn't intercept clicks
|
||||
// on anything underneath (e.g., the user-menu dropdown). The button inside
|
||||
// re-enables pointer-events on itself only.
|
||||
<div className="pointer-events-none fixed top-0 left-0 right-0 z-50 flex items-center justify-center gap-3 bg-red-600 px-4 py-1.5 text-sm text-white shadow-md">
|
||||
<span>
|
||||
Impersonating <strong>{session.user.name || session.user.email}</strong>{' '}
|
||||
({session.user.role.replace('_', ' ')})
|
||||
({rolesLabel})
|
||||
</span>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="secondary"
|
||||
className="h-6 px-3 text-xs"
|
||||
className="pointer-events-auto h-6 px-3 text-xs"
|
||||
onClick={handleReturn}
|
||||
disabled={endImpersonation.isPending}
|
||||
>
|
||||
|
||||
40
src/lib/csv.ts
Normal file
40
src/lib/csv.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* CSV cell escaping that prevents formula injection.
|
||||
*
|
||||
* A CSV cell whose first character is `=`, `+`, `-`, `@`, `\t`, or `\r` is
|
||||
* interpreted as a formula by Excel and LibreOffice when the file is opened.
|
||||
* Even with the "enable content" prompt for DDE, `=HYPERLINK(...)` and
|
||||
* `=WEBSERVICE(...)` execute on cell focus and can exfiltrate row data to an
|
||||
* attacker-controlled URL. Because user-controlled fields (project titles,
|
||||
* names, free-text feedback, User-Agent strings persisted to audit logs) end
|
||||
* up in our exports, we must defang any leading formula character.
|
||||
*
|
||||
* The defense: prefix the cell with a single quote `'` so spreadsheet apps
|
||||
* treat the value as text. Then apply standard CSV quoting (wrap in `"` and
|
||||
* double-up internal quotes) when the cell contains commas, quotes, or
|
||||
* newlines.
|
||||
*/
|
||||
export function csvCell(value: unknown): string {
|
||||
if (value === null || value === undefined) return ''
|
||||
let s = String(value)
|
||||
|
||||
if (s.length > 0) {
|
||||
const first = s.charCodeAt(0)
|
||||
// = + - @ \t \r
|
||||
if (
|
||||
first === 0x3d ||
|
||||
first === 0x2b ||
|
||||
first === 0x2d ||
|
||||
first === 0x40 ||
|
||||
first === 0x09 ||
|
||||
first === 0x0d
|
||||
) {
|
||||
s = `'${s}`
|
||||
}
|
||||
}
|
||||
|
||||
if (s.includes(',') || s.includes('"') || s.includes('\n')) {
|
||||
return `"${s.replace(/"/g, '""')}"`
|
||||
}
|
||||
return s
|
||||
}
|
||||
384
src/lib/email.ts
384
src/lib/email.ts
@@ -564,6 +564,79 @@ Together for a healthier ocean.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate award juror notification template — used when an admin assigns a
|
||||
* juror to a special award and when sending follow-up reminders. Tells the
|
||||
* juror what the award is, how many projects are eligible, and links them
|
||||
* straight to the voting page.
|
||||
*/
|
||||
function getAwardJurorNotificationTemplate(
|
||||
name: string,
|
||||
awardName: string,
|
||||
url: string,
|
||||
options?: {
|
||||
eligibleCount?: number
|
||||
votingEndAt?: Date | null
|
||||
customMessage?: string
|
||||
isReminder?: boolean
|
||||
},
|
||||
): EmailTemplate {
|
||||
const greeting = name ? `Hello ${name},` : 'Hello,'
|
||||
const eligibleCount = options?.eligibleCount
|
||||
const votingEndAt = options?.votingEndAt
|
||||
const customMessage = options?.customMessage?.trim()
|
||||
const isReminder = options?.isReminder ?? false
|
||||
|
||||
const lead = isReminder
|
||||
? `This is a reminder that you've been assigned as a juror for <strong style="color: ${BRAND.darkBlue};">${escapeHtml(awardName)}</strong>.`
|
||||
: `You've been assigned as a juror for <strong style="color: ${BRAND.darkBlue};">${escapeHtml(awardName)}</strong>.`
|
||||
|
||||
const projectsLine = typeof eligibleCount === 'number' && eligibleCount > 0
|
||||
? paragraph(`There ${eligibleCount === 1 ? 'is' : 'are'} <strong>${eligibleCount}</strong> eligible project${eligibleCount === 1 ? '' : 's'} for you to review.`)
|
||||
: ''
|
||||
|
||||
const deadlineLine = votingEndAt
|
||||
? paragraph(`<strong>Voting closes:</strong> ${escapeHtml(votingEndAt.toLocaleString('en-GB', { dateStyle: 'long', timeStyle: 'short' }))}`)
|
||||
: ''
|
||||
|
||||
const customMessageHtml = customMessage
|
||||
? `<div style="color: ${BRAND.textDark}; font-size: 15px; line-height: 1.7; margin: 20px 0; padding: 20px; background-color: ${BRAND.lightGray}; border-radius: 8px;">${escapeHtml(customMessage).replace(/\n/g, '<br>')}</div>`
|
||||
: ''
|
||||
|
||||
const content = `
|
||||
${sectionTitle(greeting)}
|
||||
${paragraph(lead)}
|
||||
${projectsLine}
|
||||
${deadlineLine}
|
||||
${customMessageHtml}
|
||||
${ctaButton(url, 'Review & Vote')}
|
||||
<p style="color: ${BRAND.textMuted}; margin: 20px 0 0 0; font-size: 13px; text-align: center;">
|
||||
Sign in with your existing MOPC credentials to access the voting page.
|
||||
</p>
|
||||
`
|
||||
|
||||
return {
|
||||
subject: isReminder
|
||||
? `Reminder: vote for the ${awardName}`
|
||||
: `You've been assigned as a juror for ${awardName}`,
|
||||
html: getEmailWrapper(content),
|
||||
text: `
|
||||
${greeting}
|
||||
|
||||
${isReminder ? 'This is a reminder that you' : 'You'}'ve been assigned as a juror for ${awardName}.
|
||||
${typeof eligibleCount === 'number' && eligibleCount > 0 ? `\nThere ${eligibleCount === 1 ? 'is' : 'are'} ${eligibleCount} eligible project${eligibleCount === 1 ? '' : 's'} for you to review.` : ''}${votingEndAt ? `\nVoting closes: ${votingEndAt.toLocaleString('en-GB', { dateStyle: 'long', timeStyle: 'short' })}` : ''}
|
||||
${customMessage ? `\n${customMessage}\n` : ''}
|
||||
Review & vote: ${url}
|
||||
|
||||
Sign in with your existing MOPC credentials to access the voting page.
|
||||
|
||||
---
|
||||
Monaco Ocean Protection Challenge
|
||||
Together for a healthier ocean.
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate jury invitation email template
|
||||
*/
|
||||
@@ -2308,6 +2381,29 @@ export async function sendInvitationEmail(
|
||||
await sendEmail({ to: email, subject: template.subject, text: template.text, html: template.html })
|
||||
}
|
||||
|
||||
/**
|
||||
* Send award juror notification — used both for the initial assignment
|
||||
* notification and for admin-triggered reminders.
|
||||
*/
|
||||
export async function sendAwardJurorNotificationEmail(opts: {
|
||||
email: string
|
||||
name: string | null
|
||||
awardName: string
|
||||
url: string
|
||||
eligibleCount?: number
|
||||
votingEndAt?: Date | null
|
||||
customMessage?: string
|
||||
isReminder?: boolean
|
||||
}): Promise<void> {
|
||||
const template = getAwardJurorNotificationTemplate(opts.name || '', opts.awardName, opts.url, {
|
||||
eligibleCount: opts.eligibleCount,
|
||||
votingEndAt: opts.votingEndAt,
|
||||
customMessage: opts.customMessage,
|
||||
isReminder: opts.isReminder,
|
||||
})
|
||||
await sendEmail({ to: opts.email, subject: template.subject, text: template.text, html: template.html })
|
||||
}
|
||||
|
||||
/**
|
||||
* Send jury invitation email (round-specific)
|
||||
*/
|
||||
@@ -2494,3 +2590,291 @@ export async function sendNotificationEmail(
|
||||
const template = getNotificationEmailTemplate(name, subject, body, ensureAbsoluteUrl(linkUrl))
|
||||
await sendEmail({ to: email, subject: template.subject, text: template.text, html: template.html })
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Mentor onboarding (one-shot, on first MENTOR role grant)
|
||||
// =============================================================================
|
||||
|
||||
function getMentorOnboardingTemplate(name: string, baseUrl: string): EmailTemplate {
|
||||
const mentorUrl = `${baseUrl.replace(/\/$/, '')}/mentor`
|
||||
const subject = 'Welcome to MOPC mentoring'
|
||||
const text = [
|
||||
`Hi ${name || 'there'},`,
|
||||
'',
|
||||
'You have been added as a mentor for the Monaco Ocean Protection Challenge.',
|
||||
'',
|
||||
'As a mentor, you will:',
|
||||
' • Be matched with one or more shortlisted projects',
|
||||
' • Communicate with project teams in a private workspace',
|
||||
' • Share files, comments, and milestone feedback',
|
||||
' • Help projects sharpen their submissions before the live final',
|
||||
'',
|
||||
`Your mentor dashboard: ${mentorUrl}`,
|
||||
'',
|
||||
'If you also have other roles on the platform (e.g. juror), look for the',
|
||||
'"Switch View" pill in the top-right of any page to move between dashboards.',
|
||||
'',
|
||||
'The MOPC team',
|
||||
].join('\n')
|
||||
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body style="margin:0;padding:0;background:#f6f8fa;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;color:#0f172a;">
|
||||
<div style="max-width:560px;margin:32px auto;background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 1px 3px rgba(0,0,0,0.06);">
|
||||
<div style="background:#053d57;padding:24px 28px;color:#fefefe;">
|
||||
<h1 style="margin:0;font-size:20px;font-weight:600;">Welcome to MOPC mentoring</h1>
|
||||
</div>
|
||||
<div style="padding:24px 28px;line-height:1.5;font-size:14px;">
|
||||
<p style="margin-top:0;">Hi ${name || 'there'},</p>
|
||||
<p>You have been added as a mentor for the Monaco Ocean Protection Challenge.</p>
|
||||
<p>As a mentor, you will:</p>
|
||||
<ul style="padding-left:20px;">
|
||||
<li>Be matched with one or more shortlisted projects</li>
|
||||
<li>Communicate with project teams in a private workspace</li>
|
||||
<li>Share files, comments, and milestone feedback</li>
|
||||
<li>Help projects sharpen their submissions before the live final</li>
|
||||
</ul>
|
||||
<p style="margin-top:24px;">
|
||||
<a href="${mentorUrl}" style="display:inline-block;padding:10px 20px;background:#de0f1e;color:#fff;text-decoration:none;border-radius:6px;font-weight:600;">Open Mentor Dashboard</a>
|
||||
</p>
|
||||
<p style="margin-top:24px;color:#64748b;font-size:12px;">
|
||||
If you also have other roles on the platform, use the "Switch View" pill in the top-right of any page to move between dashboards.
|
||||
</p>
|
||||
</div>
|
||||
<div style="padding:16px 28px;background:#f1f5f9;color:#64748b;font-size:12px;text-align:center;">
|
||||
Monaco Ocean Protection Challenge
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`.trim()
|
||||
|
||||
return { subject, text, html }
|
||||
}
|
||||
|
||||
/**
|
||||
* Send mentor onboarding email. Idempotency is enforced at the call site
|
||||
* (see user.bulkUpdateRoles / user.updateRoles) by checking
|
||||
* User.mentorOnboardingSentAt.
|
||||
*/
|
||||
export async function sendMentorOnboardingEmail(email: string, name: string | null): Promise<void> {
|
||||
const baseUrl = process.env.NEXTAUTH_URL || 'https://monaco-opc.com'
|
||||
const template = getMentorOnboardingTemplate(name || '', baseUrl)
|
||||
await sendEmail({ to: email, subject: template.subject, text: template.text, html: template.html })
|
||||
}
|
||||
|
||||
function getFinalistConfirmationTemplate(
|
||||
name: string,
|
||||
projectTitle: string,
|
||||
deadlineIso: string,
|
||||
confirmUrl: string,
|
||||
): EmailTemplate {
|
||||
const subject = `Grand Finale: confirm your attendance for "${projectTitle}"`
|
||||
const greeting = name ? `Hi ${name},` : 'Hi,'
|
||||
const text = [
|
||||
greeting,
|
||||
'',
|
||||
`Congratulations — your project "${projectTitle}" has been selected as a finalist`,
|
||||
'for the Monaco Ocean Protection Challenge grand finale.',
|
||||
'',
|
||||
`Please confirm your team's attendance by ${deadlineIso}.`,
|
||||
'On the confirmation page you will:',
|
||||
' • Choose which team members will attend',
|
||||
' • Indicate who needs visa support',
|
||||
'',
|
||||
`Confirm here: ${confirmUrl}`,
|
||||
'',
|
||||
'If your team cannot attend, please use the same link to decline so',
|
||||
'we can offer the slot to a waitlisted team in time.',
|
||||
'',
|
||||
'The MOPC team',
|
||||
].join('\n')
|
||||
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body style="margin:0;padding:0;background:#f6f8fa;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Helvetica,Arial,sans-serif;color:#0f172a;">
|
||||
<div style="max-width:560px;margin:32px auto;background:#fff;border-radius:8px;overflow:hidden;box-shadow:0 1px 3px rgba(0,0,0,0.06);">
|
||||
<div style="background:#053d57;padding:24px 28px;color:#fefefe;">
|
||||
<h1 style="margin:0;font-size:20px;font-weight:600;">You're a Grand Finale finalist</h1>
|
||||
</div>
|
||||
<div style="padding:24px 28px;line-height:1.5;font-size:14px;">
|
||||
<p style="margin-top:0;">${greeting}</p>
|
||||
<p>Congratulations — your project <strong>${escapeHtml(projectTitle)}</strong> has been selected as a finalist for the Monaco Ocean Protection Challenge grand finale.</p>
|
||||
<p style="margin-top:20px;padding:12px 16px;background:#fef3c7;border-left:3px solid #d97706;border-radius:4px;">
|
||||
<strong>Confirm by ${escapeHtml(deadlineIso)}.</strong>
|
||||
</p>
|
||||
<p>On the confirmation page you'll choose which team members will attend and indicate who needs visa support.</p>
|
||||
<p style="margin-top:24px;">
|
||||
<a href="${confirmUrl}" style="display:inline-block;padding:10px 20px;background:#de0f1e;color:#fff;text-decoration:none;border-radius:6px;font-weight:600;">Confirm Attendance</a>
|
||||
</p>
|
||||
<p style="margin-top:24px;color:#64748b;font-size:12px;">
|
||||
If your team cannot attend, please use the same link to decline so we can offer the slot to a waitlisted team in time.
|
||||
</p>
|
||||
</div>
|
||||
<div style="padding:16px 28px;background:#f1f5f9;color:#64748b;font-size:12px;text-align:center;">
|
||||
Monaco Ocean Protection Challenge
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`.trim()
|
||||
|
||||
return { subject, text, html }
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a finalist confirmation email. Failures are intentionally not awaited
|
||||
* inside any DB transaction — the calling tRPC mutation logs failures but
|
||||
* does not roll back the confirmation row creation.
|
||||
*/
|
||||
export async function sendFinalistConfirmationEmail(
|
||||
email: string,
|
||||
name: string | null,
|
||||
projectTitle: string,
|
||||
deadline: Date,
|
||||
confirmUrl: string,
|
||||
): Promise<void> {
|
||||
const template = getFinalistConfirmationTemplate(name || '', projectTitle, deadline.toISOString(), confirmUrl)
|
||||
await sendEmail({ to: email, subject: template.subject, text: template.text, html: template.html })
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// LUNCH (PR 6)
|
||||
// =============================================================================
|
||||
|
||||
type LunchRecapPayload = {
|
||||
event: { eventAt: Date | null; venue: string | null } | null
|
||||
members: Array<{
|
||||
name: string
|
||||
project: { name: string } | null
|
||||
dish: { name: string } | null
|
||||
allergens: string[]
|
||||
allergenOther: string | null
|
||||
}>
|
||||
externals: Array<{
|
||||
name: string
|
||||
project: { name: string } | null
|
||||
dish: { name: string } | null
|
||||
allergens: string[]
|
||||
allergenOther: string | null
|
||||
roleNote?: string | null
|
||||
}>
|
||||
dishCounts: Record<string, number>
|
||||
dietaryCounts: Record<string, number>
|
||||
allergenCounts: Record<string, number>
|
||||
summary: { total: number; picked: number; missing: number }
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a lunch reminder to one attendee whose pick is still missing.
|
||||
* Failures are caught at the cron layer; this function may throw on
|
||||
* individual failures so the caller can decide.
|
||||
*/
|
||||
export async function sendLunchReminderEmail(opts: {
|
||||
to: string
|
||||
memberName: string
|
||||
eventAt: Date
|
||||
venue: string | null
|
||||
changeDeadline: Date
|
||||
pickUrl: string
|
||||
}): Promise<void> {
|
||||
const fmt = new Intl.DateTimeFormat('en-GB', {
|
||||
timeZone: 'Europe/Monaco', dateStyle: 'long', timeStyle: 'short',
|
||||
})
|
||||
const subject = `Pick your lunch dish — deadline ${fmt.format(opts.changeDeadline)} (Monaco)`
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html><body style="font-family:system-ui,-apple-system,sans-serif;background:#f8fafc;padding:24px;">
|
||||
<div style="max-width:560px;margin:0 auto;background:white;border-radius:12px;padding:32px;">
|
||||
<h2 style="margin:0 0 16px;color:#0f172a;">Pick your lunch dish</h2>
|
||||
<p>Hi ${escapeHtml(opts.memberName)},</p>
|
||||
<p>You haven't picked your lunch dish for the Monaco Ocean Protection Challenge grand finale yet.</p>
|
||||
<p>
|
||||
<strong>Event:</strong> ${fmt.format(opts.eventAt)} (Europe/Monaco)<br/>
|
||||
${opts.venue ? `<strong>Venue:</strong> ${escapeHtml(opts.venue)}<br/>` : ''}
|
||||
<strong>Deadline to pick:</strong> ${fmt.format(opts.changeDeadline)}
|
||||
</p>
|
||||
<p style="margin-top:24px;">
|
||||
<a href="${opts.pickUrl}" style="display:inline-block;background:#053d57;color:white;padding:12px 24px;border-radius:8px;text-decoration:none;font-weight:600;">Open the picker</a>
|
||||
</p>
|
||||
<p style="margin-top:24px;color:#64748b;font-size:12px;">
|
||||
If you have any questions, reply to this email and we'll help.
|
||||
</p>
|
||||
</div>
|
||||
</body></html>`.trim()
|
||||
const text = `Pick your lunch dish.
|
||||
Event: ${opts.eventAt.toISOString()}
|
||||
${opts.venue ? `Venue: ${opts.venue}\n` : ''}Deadline: ${opts.changeDeadline.toISOString()}
|
||||
${opts.pickUrl}`
|
||||
await sendEmail({ to: opts.to, subject, text, html })
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the lunch recap manifest to admins + extra recipients.
|
||||
* Caller passes the assembled recap payload from `buildRecapPayload`.
|
||||
*/
|
||||
export async function sendLunchRecapEmail(
|
||||
recipients: string[],
|
||||
payload: LunchRecapPayload,
|
||||
): Promise<void> {
|
||||
if (recipients.length === 0) return
|
||||
const fmt = new Intl.DateTimeFormat('en-GB', {
|
||||
timeZone: 'Europe/Monaco', dateStyle: 'long', timeStyle: 'short',
|
||||
})
|
||||
const subject = `Lunch manifest — ${payload.event?.eventAt ? fmt.format(payload.event.eventAt) : 'TBD'}`
|
||||
const dishLines = Object.entries(payload.dishCounts)
|
||||
.sort(([, a], [, b]) => b - a)
|
||||
.map(([name, n]) => `<li>${n}× ${escapeHtml(name)}</li>`).join('')
|
||||
const dietaryLines = Object.entries(payload.dietaryCounts)
|
||||
.map(([name, n]) => `<li>${n}× ${name.replace('_', ' ').toLowerCase()}</li>`).join('')
|
||||
const allergenLines = Object.entries(payload.allergenCounts)
|
||||
.sort(([, a], [, b]) => b - a)
|
||||
.map(([name, n]) => `<li>${n}× ${name.replace('_', ' ').toLowerCase()}</li>`).join('')
|
||||
const formatAllergens = (allergens: string[], other: string | null) =>
|
||||
[...allergens.map(a => a.replace('_', ' ').toLowerCase()), other].filter(Boolean).join(', ')
|
||||
const memberRows = payload.members.map((r) => `
|
||||
<tr>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${escapeHtml(r.project?.name ?? '')}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${escapeHtml(r.name)}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${r.dish ? escapeHtml(r.dish.name) : '<em style="color:#94a3b8;">not picked</em>'}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${escapeHtml(formatAllergens(r.allergens, r.allergenOther))}</td>
|
||||
</tr>`).join('')
|
||||
const externalRows = payload.externals.map((r) => `
|
||||
<tr>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">External${r.project?.name ? ` (with ${escapeHtml(r.project.name)})` : ''}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${escapeHtml(r.name)}${r.roleNote ? ` — <em>${escapeHtml(r.roleNote)}</em>` : ''}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${r.dish ? escapeHtml(r.dish.name) : '<em style="color:#94a3b8;">not picked</em>'}</td>
|
||||
<td style="padding:6px 10px;border:1px solid #e2e8f0;">${escapeHtml(formatAllergens(r.allergens, r.allergenOther))}</td>
|
||||
</tr>`).join('')
|
||||
const html = `
|
||||
<!DOCTYPE html>
|
||||
<html><body style="font-family:system-ui,-apple-system,sans-serif;background:#f8fafc;padding:24px;">
|
||||
<div style="max-width:760px;margin:0 auto;background:white;border-radius:12px;padding:32px;">
|
||||
<h2 style="margin:0 0 8px;color:#0f172a;">Lunch manifest</h2>
|
||||
${payload.event?.eventAt ? `<p style="color:#475569;">${fmt.format(payload.event.eventAt)}${payload.event.venue ? ` · ${escapeHtml(payload.event.venue)}` : ''}</p>` : ''}
|
||||
<p><strong>${payload.summary.picked}/${payload.summary.total} picked</strong>${payload.summary.missing ? ` (${payload.summary.missing} missing)` : ''}.</p>
|
||||
<h3 style="margin-top:24px;">Dishes</h3>
|
||||
<ul>${dishLines || '<li>None picked yet</li>'}</ul>
|
||||
${dietaryLines ? `<h3>Dietary needs</h3><ul>${dietaryLines}</ul>` : ''}
|
||||
<h3>Allergens</h3>
|
||||
<ul>${allergenLines || '<li>None reported</li>'}</ul>
|
||||
<h3 style="margin-top:24px;">Per-attendee</h3>
|
||||
<table style="border-collapse:collapse;width:100%;font-size:14px;">
|
||||
<thead><tr>
|
||||
<th style="padding:6px 10px;background:#f1f5f9;border:1px solid #e2e8f0;text-align:left;">Team</th>
|
||||
<th style="padding:6px 10px;background:#f1f5f9;border:1px solid #e2e8f0;text-align:left;">Name</th>
|
||||
<th style="padding:6px 10px;background:#f1f5f9;border:1px solid #e2e8f0;text-align:left;">Dish</th>
|
||||
<th style="padding:6px 10px;background:#f1f5f9;border:1px solid #e2e8f0;text-align:left;">Allergies</th>
|
||||
</tr></thead>
|
||||
<tbody>${memberRows}${externalRows}</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body></html>`.trim()
|
||||
const text = `${payload.summary.picked}/${payload.summary.total} picked. See HTML version for the full manifest.`
|
||||
for (const to of recipients) {
|
||||
await sendEmail({ to, subject, text, html })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
45
src/lib/finalist-token.ts
Normal file
45
src/lib/finalist-token.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { createHmac, timingSafeEqual } from 'crypto'
|
||||
|
||||
export type FinalistTokenPayload = {
|
||||
confirmationId: string
|
||||
/** Unix seconds. Token is rejected after this. */
|
||||
exp: number
|
||||
}
|
||||
|
||||
function getSecret(): string {
|
||||
const s = process.env.NEXTAUTH_SECRET
|
||||
if (!s) throw new Error('NEXTAUTH_SECRET is not set; cannot sign finalist tokens')
|
||||
return s
|
||||
}
|
||||
|
||||
function hmac(payloadB64: string): string {
|
||||
return createHmac('sha256', getSecret()).update(payloadB64).digest('hex')
|
||||
}
|
||||
|
||||
export function signFinalistToken(payload: FinalistTokenPayload): string {
|
||||
const payloadB64 = Buffer.from(JSON.stringify(payload)).toString('base64url')
|
||||
const sig = hmac(payloadB64)
|
||||
return `${payloadB64}.${sig}`
|
||||
}
|
||||
|
||||
export function verifyFinalistToken(token: string): FinalistTokenPayload {
|
||||
const parts = token.split('.')
|
||||
if (parts.length !== 2) throw new Error('Invalid finalist token: malformed')
|
||||
const [payloadB64, sig] = parts
|
||||
const expected = hmac(payloadB64)
|
||||
const a = Buffer.from(sig, 'hex')
|
||||
const b = Buffer.from(expected, 'hex')
|
||||
if (a.length !== b.length || !timingSafeEqual(a, b)) {
|
||||
throw new Error('Invalid finalist token: signature mismatch')
|
||||
}
|
||||
let payload: FinalistTokenPayload
|
||||
try {
|
||||
payload = JSON.parse(Buffer.from(payloadB64, 'base64url').toString('utf-8'))
|
||||
} catch {
|
||||
throw new Error('Invalid finalist token: payload not parseable')
|
||||
}
|
||||
if (typeof payload.exp !== 'number' || payload.exp < Math.floor(Date.now() / 1000)) {
|
||||
throw new Error('Invalid finalist token: expired')
|
||||
}
|
||||
return payload
|
||||
}
|
||||
51
src/lib/mentor-upload-token.ts
Normal file
51
src/lib/mentor-upload-token.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { createHmac, timingSafeEqual } from 'crypto'
|
||||
|
||||
export type MentorUploadPayload = {
|
||||
mentorAssignmentId: string
|
||||
uploaderUserId: string
|
||||
fileName: string
|
||||
mimeType: string
|
||||
size: number
|
||||
bucket: string
|
||||
objectKey: string
|
||||
/** Unix seconds. Token is rejected after this. */
|
||||
exp: number
|
||||
}
|
||||
|
||||
function getSecret(): string {
|
||||
const s = process.env.NEXTAUTH_SECRET
|
||||
if (!s) throw new Error('NEXTAUTH_SECRET is not set; cannot sign mentor upload tokens')
|
||||
return s
|
||||
}
|
||||
|
||||
function hmac(payloadB64: string): string {
|
||||
return createHmac('sha256', getSecret()).update(payloadB64).digest('hex')
|
||||
}
|
||||
|
||||
export function signMentorUploadToken(payload: MentorUploadPayload): string {
|
||||
const payloadB64 = Buffer.from(JSON.stringify(payload)).toString('base64url')
|
||||
const sig = hmac(payloadB64)
|
||||
return `${payloadB64}.${sig}`
|
||||
}
|
||||
|
||||
export function verifyMentorUploadToken(token: string): MentorUploadPayload {
|
||||
const parts = token.split('.')
|
||||
if (parts.length !== 2) throw new Error('Invalid mentor upload token: malformed')
|
||||
const [payloadB64, sig] = parts
|
||||
const expected = hmac(payloadB64)
|
||||
const a = Buffer.from(sig, 'hex')
|
||||
const b = Buffer.from(expected, 'hex')
|
||||
if (a.length !== b.length || !timingSafeEqual(a, b)) {
|
||||
throw new Error('Invalid mentor upload token: signature mismatch')
|
||||
}
|
||||
let payload: MentorUploadPayload
|
||||
try {
|
||||
payload = JSON.parse(Buffer.from(payloadB64, 'base64url').toString('utf-8'))
|
||||
} catch {
|
||||
throw new Error('Invalid mentor upload token: payload not parseable')
|
||||
}
|
||||
if (typeof payload.exp !== 'number' || payload.exp < Math.floor(Date.now() / 1000)) {
|
||||
throw new Error('Invalid mentor upload token: expired')
|
||||
}
|
||||
return payload
|
||||
}
|
||||
@@ -116,7 +116,7 @@ export async function deleteObject(
|
||||
* Sanitize a name for use as a MinIO path segment.
|
||||
* Removes special characters, replaces spaces with underscores, limits length.
|
||||
*/
|
||||
function sanitizePath(name: string): string {
|
||||
export function sanitizePath(name: string): string {
|
||||
return (
|
||||
name
|
||||
.trim()
|
||||
@@ -149,3 +149,31 @@ export function generateObjectKey(
|
||||
return `${sanitizedProject}/${sanitizedRound}/${timestamp}-${sanitizedFile}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique object key for a mentor-workspace file.
|
||||
*
|
||||
* Structure: {ProjectName}/mentorship/{timestamp}-{fileName}
|
||||
*
|
||||
* Mirrors generateObjectKey but pins the round-name slot to "mentorship"
|
||||
* so all mentor workspace files for a project live under one folder.
|
||||
*/
|
||||
export function generateMentorObjectKey(
|
||||
projectTitle: string,
|
||||
fileName: string,
|
||||
): string {
|
||||
return generateObjectKey(projectTitle, fileName, 'mentorship')
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify a client-supplied object key actually belongs to a project's
|
||||
* sanitized prefix. Used to prevent cross-tenant binding where an attacker
|
||||
* passes another team's `bucket+objectKey` into a metadata-save procedure.
|
||||
*/
|
||||
export function objectKeyBelongsToProject(
|
||||
objectKey: string,
|
||||
projectTitle: string,
|
||||
): boolean {
|
||||
const sanitized = sanitizePath(projectTitle)
|
||||
return objectKey.startsWith(`${sanitized}/`)
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ export function formatFileSize(bytes: number): string {
|
||||
|
||||
export function formatEnumLabel(value: string): string {
|
||||
return value
|
||||
.toLowerCase()
|
||||
.replace(/_/g, ' ')
|
||||
.replace(/\b\w/g, (c) => c.toUpperCase())
|
||||
}
|
||||
|
||||
@@ -49,6 +49,10 @@ import { roundEngineRouter } from './roundEngine'
|
||||
import { roundAssignmentRouter } from './roundAssignment'
|
||||
import { deliberationRouter } from './deliberation'
|
||||
import { resultLockRouter } from './resultLock'
|
||||
// Grand-finale logistics
|
||||
import { finalistRouter } from './finalist'
|
||||
import { logisticsRouter } from './logistics'
|
||||
import { lunchRouter } from './lunch'
|
||||
|
||||
/**
|
||||
* Root tRPC router that combines all domain routers
|
||||
@@ -104,6 +108,10 @@ export const appRouter = router({
|
||||
roundAssignment: roundAssignmentRouter,
|
||||
deliberation: deliberationRouter,
|
||||
resultLock: resultLockRouter,
|
||||
// Grand-finale logistics
|
||||
finalist: finalistRouter,
|
||||
logistics: logisticsRouter,
|
||||
lunch: lunchRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
|
||||
@@ -2,7 +2,7 @@ import crypto from 'crypto'
|
||||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, publicProcedure, protectedProcedure } from '../trpc'
|
||||
import { getPresignedUrl, generateObjectKey, BUCKET_NAME } from '@/lib/minio'
|
||||
import { getPresignedUrl, generateObjectKey, BUCKET_NAME, objectKeyBelongsToProject } from '@/lib/minio'
|
||||
import { generateLogoKey, createStorageProvider, type StorageProviderType } from '@/lib/storage'
|
||||
import { getImageUploadUrl, confirmImageUpload, getImageUrl, deleteImage, type ImageUploadConfig } from '@/server/utils/image-upload'
|
||||
import { sendStyledNotificationEmail, sendTeamMemberInviteEmail } from '@/lib/email'
|
||||
@@ -408,6 +408,24 @@ export const applicantRouter = router({
|
||||
})
|
||||
}
|
||||
|
||||
// Verify the client-supplied objectKey actually belongs to this project's
|
||||
// sanitized prefix. Without this, a user could pass another team's
|
||||
// bucket+objectKey here, attach it to their own project row, and then
|
||||
// download the foreign object via file.getDownloadUrl (which authorizes
|
||||
// by bucket+objectKey -> projectId).
|
||||
if (input.bucket !== BUCKET_NAME) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Invalid file location',
|
||||
})
|
||||
}
|
||||
if (!objectKeyBelongsToProject(input.objectKey, project.title)) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Invalid file location for this project',
|
||||
})
|
||||
}
|
||||
|
||||
// Block rejected projects
|
||||
if (await isProjectRejected(ctx.prisma, input.projectId)) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN', message: 'Your project has been rejected. File changes are no longer permitted.' })
|
||||
@@ -2659,4 +2677,172 @@ export const applicantRouter = router({
|
||||
|
||||
return { success: true, roundId: activePrs.roundId, roundName: activePrs.round.name }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Last N messages + unread count for the applicant's project mentor workspace.
|
||||
* Drives the 'Conversation with [Mentor]' card on /applicant.
|
||||
*/
|
||||
getMentorConversationPreview: protectedProcedure
|
||||
.input(z.object({ projectId: z.string(), limit: z.number().min(1).max(10).default(3) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const teamMembership = await ctx.prisma.teamMember.findFirst({
|
||||
where: { projectId: input.projectId, userId: ctx.user.id },
|
||||
select: { id: true },
|
||||
})
|
||||
if (!teamMembership) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Not a team member of this project',
|
||||
})
|
||||
}
|
||||
|
||||
const assignment = await ctx.prisma.mentorAssignment.findUnique({
|
||||
where: { projectId: input.projectId },
|
||||
include: { mentor: { select: { id: true, name: true, email: true } } },
|
||||
})
|
||||
|
||||
const [messages, unreadCount] = await Promise.all([
|
||||
ctx.prisma.mentorMessage.findMany({
|
||||
where: { projectId: input.projectId },
|
||||
include: { sender: { select: { id: true, name: true, email: true } } },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: input.limit,
|
||||
}),
|
||||
ctx.prisma.mentorMessage.count({
|
||||
where: {
|
||||
projectId: input.projectId,
|
||||
senderId: { not: ctx.user.id },
|
||||
isRead: false,
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
return {
|
||||
mentor: assignment?.mentor ?? null,
|
||||
messages: messages.reverse(),
|
||||
unreadCount,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Returns the caller's project's finalist confirmation (if any) plus the
|
||||
* data needed by the team-lead's "Edit attendees" dialog: the team roster,
|
||||
* the current AttendingMember rows, the program cap, and the editable
|
||||
* cutoff derived from the LIVE_FINAL round window.
|
||||
*
|
||||
* Returns null when the caller is not on a team with a confirmation.
|
||||
*/
|
||||
getMyFinalistConfirmation: protectedProcedure.query(async ({ ctx }) => {
|
||||
const project = await ctx.prisma.project.findFirst({
|
||||
where: { teamMembers: { some: { userId: ctx.user.id } } },
|
||||
include: {
|
||||
program: { select: { id: true, defaultAttendeeCap: true } },
|
||||
teamMembers: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true, email: true } },
|
||||
},
|
||||
orderBy: { joinedAt: 'asc' },
|
||||
},
|
||||
finalistConfirmation: {
|
||||
include: {
|
||||
attendingMembers: {
|
||||
select: { id: true, userId: true, needsVisa: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
})
|
||||
|
||||
if (!project || !project.finalistConfirmation) return null
|
||||
|
||||
const callerMember = project.teamMembers.find((tm) => tm.userId === ctx.user.id)
|
||||
const isLead = callerMember?.role === 'LEAD'
|
||||
|
||||
let cutoffAt: Date | null = null
|
||||
let editableNow = true
|
||||
const round = await ctx.prisma.round.findFirst({
|
||||
where: { competition: { programId: project.program.id }, roundType: 'LIVE_FINAL' },
|
||||
orderBy: { sortOrder: 'desc' },
|
||||
select: { windowOpenAt: true, configJson: true },
|
||||
})
|
||||
if (round?.windowOpenAt) {
|
||||
const cfg = (round.configJson ?? {}) as { attendeeEditCutoffHours?: number }
|
||||
const cutoffHours = cfg.attendeeEditCutoffHours ?? 48
|
||||
cutoffAt = new Date(round.windowOpenAt.getTime() - cutoffHours * 3_600_000)
|
||||
editableNow = Date.now() <= cutoffAt.getTime()
|
||||
}
|
||||
|
||||
return {
|
||||
project: {
|
||||
id: project.id,
|
||||
title: project.title,
|
||||
programId: project.program.id,
|
||||
teamMembers: project.teamMembers.map((tm) => ({
|
||||
userId: tm.userId,
|
||||
role: tm.role,
|
||||
user: tm.user,
|
||||
})),
|
||||
program: { defaultAttendeeCap: project.program.defaultAttendeeCap },
|
||||
},
|
||||
confirmation: {
|
||||
id: project.finalistConfirmation.id,
|
||||
status: project.finalistConfirmation.status,
|
||||
attendingMembers: project.finalistConfirmation.attendingMembers,
|
||||
},
|
||||
isLead,
|
||||
cutoffAt,
|
||||
editableNow,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Returns the caller's visa application rows (one per AttendingMember
|
||||
* the caller owns) when their program has visaStatusVisibleToMembers=true.
|
||||
* Returns null when the toggle is off so the UI can hide the section
|
||||
* entirely. Returns an empty array when the toggle is on but the caller
|
||||
* has no needsVisa attendees yet.
|
||||
*/
|
||||
getMyVisaApplications: protectedProcedure.query(async ({ ctx }) => {
|
||||
const attendees = await ctx.prisma.attendingMember.findMany({
|
||||
where: { userId: ctx.user.id },
|
||||
include: {
|
||||
confirmation: {
|
||||
select: {
|
||||
id: true,
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
programId: true,
|
||||
program: { select: { visaStatusVisibleToMembers: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
visaApplication: true,
|
||||
},
|
||||
})
|
||||
|
||||
const visibleAttendees = attendees.filter(
|
||||
(a) => a.confirmation.project.program.visaStatusVisibleToMembers,
|
||||
)
|
||||
if (attendees.length > 0 && visibleAttendees.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return visibleAttendees
|
||||
.filter((a) => a.visaApplication !== null)
|
||||
.map((a) => ({
|
||||
id: a.visaApplication!.id,
|
||||
attendingMemberId: a.id,
|
||||
userId: a.userId,
|
||||
status: a.visaApplication!.status,
|
||||
nationality: a.visaApplication!.nationality,
|
||||
invitationSentAt: a.visaApplication!.invitationSentAt,
|
||||
appointmentAt: a.visaApplication!.appointmentAt,
|
||||
decisionAt: a.visaApplication!.decisionAt,
|
||||
notes: a.visaApplication!.notes,
|
||||
projectId: a.confirmation.project.id,
|
||||
}))
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -670,11 +670,24 @@ export const evaluationRouter = router({
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get COI status for an assignment
|
||||
* Get COI status for an assignment.
|
||||
* Caller must own the assignment (or be admin). The COI description can
|
||||
* contain confidential personal/financial relationships.
|
||||
*/
|
||||
getCOIStatus: protectedProcedure
|
||||
.input(z.object({ assignmentId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const assignment = await ctx.prisma.assignment.findUnique({
|
||||
where: { id: input.assignmentId },
|
||||
select: { userId: true },
|
||||
})
|
||||
if (!assignment) {
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Assignment not found' })
|
||||
}
|
||||
const isAdmin = userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')
|
||||
if (!isAdmin && assignment.userId !== ctx.user.id) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
return ctx.prisma.conflictOfInterest.findUnique({
|
||||
where: { assignmentId: input.assignmentId },
|
||||
})
|
||||
@@ -1085,7 +1098,8 @@ export const evaluationRouter = router({
|
||||
}),
|
||||
|
||||
/**
|
||||
* Get or create a discussion for a project evaluation
|
||||
* Get or create a discussion for a project evaluation.
|
||||
* Caller must have an Assignment for this project+round (or be admin).
|
||||
*/
|
||||
getDiscussion: juryProcedure
|
||||
.input(
|
||||
@@ -1095,6 +1109,24 @@ export const evaluationRouter = router({
|
||||
})
|
||||
)
|
||||
.query(async ({ ctx, input }) => {
|
||||
// Authorization: admins always allowed; jurors must have an Assignment.
|
||||
if (!userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')) {
|
||||
const assignment = await ctx.prisma.assignment.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
if (!assignment) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this project',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Get or create discussion
|
||||
let discussion = await ctx.prisma.evaluationDiscussion.findUnique({
|
||||
where: {
|
||||
@@ -1171,7 +1203,8 @@ export const evaluationRouter = router({
|
||||
}),
|
||||
|
||||
/**
|
||||
* Add a comment to a project evaluation discussion
|
||||
* Add a comment to a project evaluation discussion.
|
||||
* Caller must have an Assignment for this project+round (or be admin).
|
||||
*/
|
||||
addComment: juryProcedure
|
||||
.input(
|
||||
@@ -1182,6 +1215,24 @@ export const evaluationRouter = router({
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Authorization: admins always allowed; jurors must have an Assignment.
|
||||
if (!userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')) {
|
||||
const assignment = await ctx.prisma.assignment.findFirst({
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
projectId: input.projectId,
|
||||
roundId: input.roundId,
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
if (!assignment) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this project',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Check max comment length from round settings
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, protectedProcedure, adminProcedure } from '../trpc'
|
||||
import { getPresignedUrl, generateObjectKey, deleteObject, BUCKET_NAME } from '@/lib/minio'
|
||||
import { getPresignedUrl, generateObjectKey, deleteObject, BUCKET_NAME, objectKeyBelongsToProject } from '@/lib/minio'
|
||||
import { logAudit } from '../utils/audit'
|
||||
import { checkRequirementsAndTransition } from '../services/round-engine'
|
||||
|
||||
@@ -517,32 +517,25 @@ export const fileRouter = router({
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const isAdminOrObserver = ['SUPER_ADMIN', 'PROGRAM_ADMIN', 'OBSERVER', 'AWARD_MASTER'].includes(ctx.user.role)
|
||||
// Replace is a write operation on the team's submission. Only admins
|
||||
// and the team itself (submitter or team member) may replace files —
|
||||
// jurors and mentors are read-only on project files even though they
|
||||
// can see them. Observers/Award masters are also read-only.
|
||||
const isAdmin = ctx.user.role === 'SUPER_ADMIN' || ctx.user.role === 'PROGRAM_ADMIN'
|
||||
|
||||
if (!isAdminOrObserver) {
|
||||
// Check user has access to the project (assigned or team member)
|
||||
const [assignment, mentorAssignment, teamMembership] = await Promise.all([
|
||||
ctx.prisma.assignment.findFirst({
|
||||
where: { userId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.mentorAssignment.findFirst({
|
||||
where: { mentorId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
}),
|
||||
ctx.prisma.project.findFirst({
|
||||
where: {
|
||||
id: input.projectId,
|
||||
OR: [
|
||||
{ submittedByUserId: ctx.user.id },
|
||||
{ teamMembers: { some: { userId: ctx.user.id } } },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
}),
|
||||
])
|
||||
if (!isAdmin) {
|
||||
const teamMembership = await ctx.prisma.project.findFirst({
|
||||
where: {
|
||||
id: input.projectId,
|
||||
OR: [
|
||||
{ submittedByUserId: ctx.user.id },
|
||||
{ teamMembers: { some: { userId: ctx.user.id } } },
|
||||
],
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (!assignment && !mentorAssignment && !teamMembership) {
|
||||
if (!teamMembership) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You do not have access to replace files for this project',
|
||||
@@ -563,6 +556,28 @@ export const fileRouter = router({
|
||||
})
|
||||
}
|
||||
|
||||
// Verify the client-supplied objectKey actually belongs to this project's
|
||||
// sanitized prefix. Without this check, a team member (or admin) could
|
||||
// pass another project's bucket+objectKey here and bind it as a "new
|
||||
// version" — then anyone with project access could read the foreign
|
||||
// object via file.getDownloadUrl, which authorizes by bucket+objectKey.
|
||||
const project = await ctx.prisma.project.findUniqueOrThrow({
|
||||
where: { id: input.projectId },
|
||||
select: { title: true },
|
||||
})
|
||||
if (input.bucket !== BUCKET_NAME) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Invalid file location',
|
||||
})
|
||||
}
|
||||
if (!objectKeyBelongsToProject(input.objectKey, project.title)) {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
message: 'Invalid file location for this project',
|
||||
})
|
||||
}
|
||||
|
||||
// Create new file and update old file in a transaction
|
||||
const result = await ctx.prisma.$transaction(async (tx) => {
|
||||
const newFile = await tx.projectFile.create({
|
||||
@@ -703,11 +718,18 @@ export const fileRouter = router({
|
||||
.query(async ({ ctx, input }) => {
|
||||
const isAdminOrObserver = ['SUPER_ADMIN', 'PROGRAM_ADMIN', 'OBSERVER', 'AWARD_MASTER'].includes(ctx.user.role)
|
||||
|
||||
// For non-admin/observer callers, mirror the per-round scope used by
|
||||
// file.getDownloadUrl: a juror assigned to round N may only pull URLs
|
||||
// for files in rounds with sortOrder <= N. Without this, a juror
|
||||
// assigned to EVALUATION can bulk-download LIVE_FINAL/DELIBERATION
|
||||
// confidential files via this endpoint.
|
||||
let priorRoundIds: string[] | null = null
|
||||
|
||||
if (!isAdminOrObserver) {
|
||||
const [assignment, mentorAssignment, teamMembership] = await Promise.all([
|
||||
ctx.prisma.assignment.findFirst({
|
||||
where: { userId: ctx.user.id, projectId: input.projectId },
|
||||
select: { id: true },
|
||||
select: { id: true, roundId: true },
|
||||
}),
|
||||
ctx.prisma.mentorAssignment.findFirst({
|
||||
where: { mentorId: ctx.user.id, projectId: input.projectId },
|
||||
@@ -731,6 +753,26 @@ export const fileRouter = router({
|
||||
message: 'You do not have access to this project\'s files',
|
||||
})
|
||||
}
|
||||
|
||||
// Apply per-round filter only when the caller's access is jury-only.
|
||||
// Mentors / team members / award jurors get unrestricted file access
|
||||
// on the project (matches getDownloadUrl semantics).
|
||||
if (assignment && !mentorAssignment && !teamMembership) {
|
||||
const assignedRound = await ctx.prisma.round.findUnique({
|
||||
where: { id: assignment.roundId },
|
||||
select: { competitionId: true, sortOrder: true },
|
||||
})
|
||||
if (assignedRound) {
|
||||
const priorOrCurrent = await ctx.prisma.round.findMany({
|
||||
where: {
|
||||
competitionId: assignedRound.competitionId,
|
||||
sortOrder: { lte: assignedRound.sortOrder },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
priorRoundIds = priorOrCurrent.map((r) => r.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get files
|
||||
@@ -738,6 +780,17 @@ export const fileRouter = router({
|
||||
if (input.fileIds && input.fileIds.length > 0) {
|
||||
where.id = { in: input.fileIds }
|
||||
}
|
||||
if (priorRoundIds !== null) {
|
||||
// Allow a file when:
|
||||
// - it has a requirement whose round is in the allowed set, OR
|
||||
// - it has no requirement and its roundId is in the allowed set, OR
|
||||
// - it has neither requirement nor roundId (legacy general files)
|
||||
where.OR = [
|
||||
{ requirement: { roundId: { in: priorRoundIds } } },
|
||||
{ requirementId: null, roundId: { in: priorRoundIds } },
|
||||
{ requirementId: null, roundId: null },
|
||||
]
|
||||
}
|
||||
|
||||
const files = await ctx.prisma.projectFile.findMany({
|
||||
where,
|
||||
|
||||
1115
src/server/routers/finalist.ts
Normal file
1115
src/server/routers/finalist.ts
Normal file
File diff suppressed because it is too large
Load Diff
@@ -432,6 +432,11 @@ export const liveVotingRouter = router({
|
||||
|
||||
/**
|
||||
* Submit a vote (supports both simple and criteria modes)
|
||||
*
|
||||
* SECURITY: This endpoint records JURY votes only (isAudienceVote=false).
|
||||
* Audience voters use `castAudienceVote` (publicProcedure with separate
|
||||
* AudienceVoter records). Caller must be either an admin or a member of
|
||||
* the round's jury group; otherwise FORBIDDEN.
|
||||
*/
|
||||
vote: protectedProcedure
|
||||
.input(
|
||||
@@ -448,8 +453,42 @@ export const liveVotingRouter = router({
|
||||
// Verify session is in progress
|
||||
const session = await ctx.prisma.liveVotingSession.findUniqueOrThrow({
|
||||
where: { id: input.sessionId },
|
||||
include: {
|
||||
round: {
|
||||
select: { id: true, juryGroupId: true },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// Authorization: admins always allowed; otherwise the caller must be a
|
||||
// JuryGroupMember of the round's jury group. JURY_MEMBER role alone is
|
||||
// not enough — we verify the actual membership row.
|
||||
const isAdmin =
|
||||
ctx.user.role === 'SUPER_ADMIN' || ctx.user.role === 'PROGRAM_ADMIN'
|
||||
if (!isAdmin) {
|
||||
const juryGroupId = session.round?.juryGroupId
|
||||
if (!juryGroupId) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'No jury group is associated with this voting session',
|
||||
})
|
||||
}
|
||||
const membership = await ctx.prisma.juryGroupMember.findUnique({
|
||||
where: {
|
||||
juryGroupId_userId: {
|
||||
juryGroupId,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
},
|
||||
})
|
||||
if (!membership) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not a member of this jury group',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (session.status !== 'IN_PROGRESS') {
|
||||
throw new TRPCError({
|
||||
code: 'BAD_REQUEST',
|
||||
@@ -512,7 +551,8 @@ export const liveVotingRouter = router({
|
||||
criterionScoresJson = input.criterionScores
|
||||
}
|
||||
|
||||
// Upsert vote (allow vote change during window)
|
||||
// Upsert vote (allow vote change during window). Explicitly recorded
|
||||
// as a jury vote — audience votes go through castAudienceVote.
|
||||
const vote = await ctx.prisma.liveVote.upsert({
|
||||
where: {
|
||||
sessionId_projectId_userId: {
|
||||
@@ -526,10 +566,12 @@ export const liveVotingRouter = router({
|
||||
projectId: input.projectId,
|
||||
userId: ctx.user.id,
|
||||
score: finalScore,
|
||||
isAudienceVote: false,
|
||||
criterionScoresJson: criterionScoresJson ?? undefined,
|
||||
},
|
||||
update: {
|
||||
score: finalScore,
|
||||
isAudienceVote: false,
|
||||
criterionScoresJson: criterionScoresJson ?? undefined,
|
||||
votedAt: new Date(),
|
||||
},
|
||||
|
||||
351
src/server/routers/logistics.ts
Normal file
351
src/server/routers/logistics.ts
Normal file
@@ -0,0 +1,351 @@
|
||||
import { z } from 'zod'
|
||||
import { FlightDetailStatus, VisaStatus } from '@prisma/client'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, adminProcedure } from '../trpc'
|
||||
import { logAudit } from '../utils/audit'
|
||||
|
||||
export const logisticsRouter = router({
|
||||
/** Read the hotel for a program (1:1). Null if not yet set. */
|
||||
getHotel: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.hotel.findUnique({ where: { programId: input.programId } })
|
||||
}),
|
||||
|
||||
/** Create or update the program's hotel. Empty link strings are stored as null. */
|
||||
upsertHotel: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string(),
|
||||
name: z.string().min(1).max(200),
|
||||
address: z.string().max(500).optional(),
|
||||
link: z
|
||||
.string()
|
||||
.url()
|
||||
.or(z.literal(''))
|
||||
.optional(),
|
||||
notes: z.string().max(2000).optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const link = input.link && input.link.trim().length > 0 ? input.link : null
|
||||
const hotel = await ctx.prisma.hotel.upsert({
|
||||
where: { programId: input.programId },
|
||||
create: {
|
||||
programId: input.programId,
|
||||
name: input.name,
|
||||
address: input.address ?? null,
|
||||
link,
|
||||
notes: input.notes ?? null,
|
||||
},
|
||||
update: {
|
||||
name: input.name,
|
||||
address: input.address ?? null,
|
||||
link,
|
||||
notes: input.notes ?? null,
|
||||
},
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'HOTEL_UPSERT',
|
||||
entityType: 'Hotel',
|
||||
entityId: hotel.id,
|
||||
detailsJson: { programId: input.programId, name: input.name },
|
||||
})
|
||||
return hotel
|
||||
}),
|
||||
|
||||
/**
|
||||
* Read-only listing of every FinalistConfirmation in a program, with the
|
||||
* joined project + attendee count + decline reason. Sorted by status
|
||||
* priority (PENDING first) then deadline ascending so the most urgent
|
||||
* decisions surface at the top of the table.
|
||||
*/
|
||||
listConfirmations: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const rows = await ctx.prisma.finalistConfirmation.findMany({
|
||||
where: { project: { programId: input.programId } },
|
||||
include: {
|
||||
project: {
|
||||
select: { id: true, title: true, competitionCategory: true, country: true },
|
||||
},
|
||||
_count: { select: { attendingMembers: true } },
|
||||
},
|
||||
})
|
||||
const STATUS_PRIORITY: Record<string, number> = {
|
||||
PENDING: 0,
|
||||
CONFIRMED: 1,
|
||||
DECLINED: 2,
|
||||
EXPIRED: 3,
|
||||
SUPERSEDED: 4,
|
||||
}
|
||||
return rows
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
status: r.status,
|
||||
deadline: r.deadline,
|
||||
confirmedAt: r.confirmedAt,
|
||||
declinedAt: r.declinedAt,
|
||||
declineReason: r.declineReason,
|
||||
expiredAt: r.expiredAt,
|
||||
category: r.category,
|
||||
promotedFromWaitlistEntryId: r.promotedFromWaitlistEntryId,
|
||||
project: r.project,
|
||||
attendeeCount: r._count.attendingMembers,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
const sa = STATUS_PRIORITY[a.status] ?? 9
|
||||
const sb = STATUS_PRIORITY[b.status] ?? 9
|
||||
if (sa !== sb) return sa - sb
|
||||
return a.deadline.getTime() - b.deadline.getTime()
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* List all attending members for CONFIRMED finalists in a program, with
|
||||
* their (optional) flight details. One row per attendee — even those
|
||||
* without a FlightDetail row yet, so the UI can render empty editors.
|
||||
*/
|
||||
listFlightDetails: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return ctx.prisma.attendingMember.findMany({
|
||||
where: {
|
||||
confirmation: {
|
||||
status: 'CONFIRMED',
|
||||
project: { programId: input.programId },
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
needsVisa: true,
|
||||
user: { select: { id: true, name: true, email: true, country: true } },
|
||||
confirmation: {
|
||||
select: {
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
country: true,
|
||||
competitionCategory: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
flightDetail: true,
|
||||
},
|
||||
orderBy: [{ user: { name: 'asc' } }],
|
||||
})
|
||||
}),
|
||||
|
||||
/** Create or update a flight detail row for an attending member. */
|
||||
upsertFlightDetail: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
attendingMemberId: z.string(),
|
||||
arrivalAt: z.date().nullable().optional(),
|
||||
arrivalFlightNumber: z.string().max(20).nullable().optional(),
|
||||
arrivalAirport: z.string().max(10).nullable().optional(),
|
||||
departureAt: z.date().nullable().optional(),
|
||||
departureFlightNumber: z.string().max(20).nullable().optional(),
|
||||
departureAirport: z.string().max(10).nullable().optional(),
|
||||
adminNotes: z.string().max(1000).nullable().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { attendingMemberId, ...rest } = input
|
||||
// Strip out undefineds so an upsert update doesn't blow away unset fields.
|
||||
const data: Record<string, unknown> = {}
|
||||
for (const [k, v] of Object.entries(rest)) {
|
||||
if (v !== undefined) data[k] = v
|
||||
}
|
||||
const detail = await ctx.prisma.flightDetail.upsert({
|
||||
where: { attendingMemberId },
|
||||
create: { attendingMemberId, ...(data as object) },
|
||||
update: data,
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'FLIGHT_DETAIL_UPSERT',
|
||||
entityType: 'FlightDetail',
|
||||
entityId: detail.id,
|
||||
detailsJson: { attendingMemberId },
|
||||
})
|
||||
return detail
|
||||
}),
|
||||
|
||||
/** Toggle PENDING ↔ CONFIRMED on a flight detail. */
|
||||
setFlightStatus: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
flightDetailId: z.string(),
|
||||
status: z.nativeEnum(FlightDetailStatus),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const detail = await ctx.prisma.flightDetail.update({
|
||||
where: { id: input.flightDetailId },
|
||||
data: { status: input.status },
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'FLIGHT_STATUS_SET',
|
||||
entityType: 'FlightDetail',
|
||||
entityId: detail.id,
|
||||
detailsJson: { status: input.status },
|
||||
})
|
||||
return detail
|
||||
}),
|
||||
|
||||
/**
|
||||
* List all VisaApplication rows for a program, joined with the project +
|
||||
* attendee + project so the admin Visas tab can render a flat table.
|
||||
* Sorted by status priority (REQUESTED first → resolved last) so the most
|
||||
* urgent in-flight applications surface at the top.
|
||||
*/
|
||||
listVisaApplications: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const rows = await ctx.prisma.visaApplication.findMany({
|
||||
where: {
|
||||
attendingMember: {
|
||||
confirmation: { project: { programId: input.programId } },
|
||||
},
|
||||
},
|
||||
include: {
|
||||
attendingMember: {
|
||||
include: {
|
||||
user: { select: { id: true, name: true, email: true } },
|
||||
confirmation: {
|
||||
select: {
|
||||
id: true,
|
||||
project: { select: { id: true, title: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const STATUS_PRIORITY: Record<VisaStatus, number> = {
|
||||
REQUESTED: 0,
|
||||
INVITATION_SENT: 1,
|
||||
APPOINTMENT_BOOKED: 2,
|
||||
GRANTED: 3,
|
||||
DENIED: 4,
|
||||
NOT_NEEDED: 5,
|
||||
}
|
||||
return rows
|
||||
.map((r) => ({
|
||||
id: r.id,
|
||||
status: r.status,
|
||||
nationality: r.nationality,
|
||||
invitationSentAt: r.invitationSentAt,
|
||||
appointmentAt: r.appointmentAt,
|
||||
decisionAt: r.decisionAt,
|
||||
notes: r.notes,
|
||||
updatedAt: r.updatedAt,
|
||||
attendee: {
|
||||
id: r.attendingMember.id,
|
||||
user: r.attendingMember.user,
|
||||
},
|
||||
project: r.attendingMember.confirmation.project,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
const sa = STATUS_PRIORITY[a.status] ?? 9
|
||||
const sb = STATUS_PRIORITY[b.status] ?? 9
|
||||
if (sa !== sb) return sa - sb
|
||||
return a.project.title.localeCompare(b.project.title)
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Update a VisaApplication's status, dates, nationality, and notes. Empty
|
||||
* date fields clear the value. Audit-logged as VISA_UPDATE.
|
||||
*/
|
||||
updateVisaApplication: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
status: z.nativeEnum(VisaStatus).optional(),
|
||||
nationality: z.string().max(100).optional().nullable(),
|
||||
invitationSentAt: z.date().optional().nullable(),
|
||||
appointmentAt: z.date().optional().nullable(),
|
||||
decisionAt: z.date().optional().nullable(),
|
||||
notes: z.string().max(2000).optional().nullable(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const existing = await ctx.prisma.visaApplication.findUnique({
|
||||
where: { id: input.id },
|
||||
})
|
||||
if (!existing) {
|
||||
throw new TRPCError({ code: 'NOT_FOUND', message: 'Visa application not found' })
|
||||
}
|
||||
const data: Record<string, unknown> = {}
|
||||
if (input.status !== undefined) data.status = input.status
|
||||
if (input.nationality !== undefined) data.nationality = input.nationality
|
||||
if (input.invitationSentAt !== undefined) data.invitationSentAt = input.invitationSentAt
|
||||
if (input.appointmentAt !== undefined) data.appointmentAt = input.appointmentAt
|
||||
if (input.decisionAt !== undefined) data.decisionAt = input.decisionAt
|
||||
if (input.notes !== undefined) data.notes = input.notes
|
||||
const updated = await ctx.prisma.visaApplication.update({
|
||||
where: { id: input.id },
|
||||
data,
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'VISA_UPDATE',
|
||||
entityType: 'VisaApplication',
|
||||
entityId: updated.id,
|
||||
detailsJson: {
|
||||
previous: {
|
||||
status: existing.status,
|
||||
nationality: existing.nationality,
|
||||
invitationSentAt: existing.invitationSentAt,
|
||||
appointmentAt: existing.appointmentAt,
|
||||
decisionAt: existing.decisionAt,
|
||||
},
|
||||
next: data,
|
||||
},
|
||||
})
|
||||
return updated
|
||||
}),
|
||||
|
||||
/** Read Program.visaStatusVisibleToMembers — drives the admin Visas tab toggle. */
|
||||
getVisaVisibility: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const program = await ctx.prisma.program.findUniqueOrThrow({
|
||||
where: { id: input.programId },
|
||||
select: { visaStatusVisibleToMembers: true },
|
||||
})
|
||||
return { visible: program.visaStatusVisibleToMembers }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Flip Program.visaStatusVisibleToMembers. Controls whether the team can
|
||||
* see their own visa status on the applicant dashboard.
|
||||
*/
|
||||
setVisaVisibility: adminProcedure
|
||||
.input(z.object({ programId: z.string(), visible: z.boolean() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const program = await ctx.prisma.program.update({
|
||||
where: { id: input.programId },
|
||||
data: { visaStatusVisibleToMembers: input.visible },
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'VISA_VISIBILITY_SET',
|
||||
entityType: 'Program',
|
||||
entityId: program.id,
|
||||
detailsJson: { visible: input.visible },
|
||||
})
|
||||
return { visible: program.visaStatusVisibleToMembers }
|
||||
}),
|
||||
})
|
||||
617
src/server/routers/lunch.ts
Normal file
617
src/server/routers/lunch.ts
Normal file
@@ -0,0 +1,617 @@
|
||||
import { z } from 'zod'
|
||||
import { TRPCError } from '@trpc/server'
|
||||
import { router, adminProcedure, protectedProcedure } from '../trpc'
|
||||
import { logAudit } from '../utils/audit'
|
||||
import { buildManifest, buildRecapPayload } from '../services/lunch-recap'
|
||||
import { sendLunchRecapEmail } from '@/lib/email'
|
||||
import { csvCell } from '@/lib/csv'
|
||||
|
||||
// ─── Shared zod schemas ──────────────────────────────────────────────────────
|
||||
|
||||
const dietaryTags = z.array(
|
||||
z.enum(['VEGETARIAN', 'VEGAN', 'GLUTEN_FREE', 'PESCATARIAN']),
|
||||
)
|
||||
|
||||
const allergens = z.array(
|
||||
z.enum([
|
||||
'GLUTEN', 'CRUSTACEANS', 'EGGS', 'FISH', 'PEANUTS', 'SOYBEANS', 'MILK',
|
||||
'TREE_NUTS', 'CELERY', 'MUSTARD', 'SESAME', 'SULPHITES', 'LUPIN', 'MOLLUSCS',
|
||||
]),
|
||||
)
|
||||
|
||||
// ─── Router ──────────────────────────────────────────────────────────────────
|
||||
|
||||
export const lunchRouter = router({
|
||||
/**
|
||||
* Get-or-create the LunchEvent for a program. Lazy creation mirrors
|
||||
* the hotel pattern: callers don't have to know whether the row
|
||||
* already exists.
|
||||
*/
|
||||
getEvent: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const existing = await ctx.prisma.lunchEvent.findUnique({
|
||||
where: { programId: input.programId },
|
||||
})
|
||||
if (existing) return existing
|
||||
return ctx.prisma.lunchEvent.create({ data: { programId: input.programId } })
|
||||
}),
|
||||
|
||||
// ─── Dish CRUD ────────────────────────────────────────────────────────────
|
||||
|
||||
listDishes: adminProcedure
|
||||
.input(z.object({ lunchEventId: z.string() }))
|
||||
.query(({ ctx, input }) =>
|
||||
ctx.prisma.dish.findMany({
|
||||
where: { lunchEventId: input.lunchEventId },
|
||||
orderBy: [{ sortOrder: 'asc' }, { createdAt: 'asc' }],
|
||||
}),
|
||||
),
|
||||
|
||||
createDish: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
lunchEventId: z.string(),
|
||||
name: z.string().min(1).max(200),
|
||||
dietaryTags,
|
||||
sortOrder: z.number().int().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const dish = await ctx.prisma.dish.create({
|
||||
data: {
|
||||
lunchEventId: input.lunchEventId,
|
||||
name: input.name,
|
||||
dietaryTags: input.dietaryTags,
|
||||
sortOrder: input.sortOrder ?? 0,
|
||||
},
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_DISH_CREATED',
|
||||
entityType: 'Dish',
|
||||
entityId: dish.id,
|
||||
detailsJson: { name: dish.name, dietaryTags: dish.dietaryTags },
|
||||
})
|
||||
return dish
|
||||
}),
|
||||
|
||||
updateDish: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
dishId: z.string(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
dietaryTags: dietaryTags.optional(),
|
||||
sortOrder: z.number().int().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { dishId, ...patch } = input
|
||||
const dish = await ctx.prisma.dish.update({ where: { id: dishId }, data: patch })
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_DISH_UPDATED',
|
||||
entityType: 'Dish',
|
||||
entityId: dish.id,
|
||||
detailsJson: patch as Record<string, unknown>,
|
||||
})
|
||||
return dish
|
||||
}),
|
||||
|
||||
deleteDish: adminProcedure
|
||||
.input(z.object({ dishId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const dish = await ctx.prisma.dish.delete({ where: { id: input.dishId } })
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_DISH_DELETED',
|
||||
entityType: 'Dish',
|
||||
entityId: dish.id,
|
||||
detailsJson: { name: dish.name },
|
||||
})
|
||||
return { ok: true as const }
|
||||
}),
|
||||
|
||||
reorderDishes: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
ordered: z.array(
|
||||
z.object({ dishId: z.string(), sortOrder: z.number().int() }),
|
||||
),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
await ctx.prisma.$transaction(
|
||||
input.ordered.map(({ dishId, sortOrder }) =>
|
||||
ctx.prisma.dish.update({ where: { id: dishId }, data: { sortOrder } }),
|
||||
),
|
||||
)
|
||||
return { ok: true as const }
|
||||
}),
|
||||
|
||||
// ─── External attendees CRUD ─────────────────────────────────────────────
|
||||
|
||||
listExternals: adminProcedure
|
||||
.input(z.object({ lunchEventId: z.string() }))
|
||||
.query(({ ctx, input }) =>
|
||||
ctx.prisma.externalAttendee.findMany({
|
||||
where: { lunchEventId: input.lunchEventId },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
include: { project: { select: { id: true, title: true } } },
|
||||
}),
|
||||
),
|
||||
|
||||
createExternal: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
lunchEventId: z.string(),
|
||||
name: z.string().min(1).max(200),
|
||||
email: z.string().email().optional(),
|
||||
projectId: z.string().nullable().optional(),
|
||||
roleNote: z.string().max(500).optional(),
|
||||
dishId: z.string().nullable().optional(),
|
||||
allergens: allergens.optional(),
|
||||
allergenOther: z.string().max(500).nullable().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const ext = await ctx.prisma.externalAttendee.create({
|
||||
data: {
|
||||
lunchEventId: input.lunchEventId,
|
||||
name: input.name,
|
||||
email: input.email,
|
||||
projectId: input.projectId ?? null,
|
||||
roleNote: input.roleNote,
|
||||
dishId: input.dishId ?? null,
|
||||
allergens: input.allergens ?? [],
|
||||
allergenOther: input.allergenOther,
|
||||
},
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_EXTERNAL_CREATED',
|
||||
entityType: 'ExternalAttendee',
|
||||
entityId: ext.id,
|
||||
detailsJson: { name: ext.name, projectId: ext.projectId },
|
||||
})
|
||||
return ext
|
||||
}),
|
||||
|
||||
updateExternal: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
externalId: z.string(),
|
||||
name: z.string().min(1).max(200).optional(),
|
||||
email: z.string().email().nullable().optional(),
|
||||
projectId: z.string().nullable().optional(),
|
||||
roleNote: z.string().max(500).nullable().optional(),
|
||||
dishId: z.string().nullable().optional(),
|
||||
allergens: allergens.optional(),
|
||||
allergenOther: z.string().max(500).nullable().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { externalId, ...patch } = input
|
||||
const ext = await ctx.prisma.externalAttendee.update({
|
||||
where: { id: externalId },
|
||||
data: patch,
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_EXTERNAL_UPDATED',
|
||||
entityType: 'ExternalAttendee',
|
||||
entityId: ext.id,
|
||||
detailsJson: patch as Record<string, unknown>,
|
||||
})
|
||||
return ext
|
||||
}),
|
||||
|
||||
deleteExternal: adminProcedure
|
||||
.input(z.object({ externalId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const ext = await ctx.prisma.externalAttendee.delete({
|
||||
where: { id: input.externalId },
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_EXTERNAL_DELETED',
|
||||
entityType: 'ExternalAttendee',
|
||||
entityId: ext.id,
|
||||
detailsJson: { name: ext.name },
|
||||
})
|
||||
return { ok: true as const }
|
||||
}),
|
||||
|
||||
// ─── Single-row pick read (used by per-row picker UI) ────────────────────
|
||||
|
||||
/**
|
||||
* Read the current MemberLunchPick for an AttendingMember plus the dishes
|
||||
* for the parent event. Permission: any user with a TeamMember row on the
|
||||
* project, OR the AttendingMember.userId itself, OR admin.
|
||||
*/
|
||||
getMemberPick: protectedProcedure
|
||||
.input(z.object({ attendingMemberId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const am = await ctx.prisma.attendingMember.findUnique({
|
||||
where: { id: input.attendingMemberId },
|
||||
include: {
|
||||
confirmation: {
|
||||
select: {
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
programId: true,
|
||||
teamMembers: { select: { userId: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
lunchPick: true,
|
||||
},
|
||||
})
|
||||
if (!am) throw new TRPCError({ code: 'NOT_FOUND' })
|
||||
const userId = ctx.user.id
|
||||
const role = ctx.user.role
|
||||
const isAdmin = role === 'SUPER_ADMIN' || role === 'PROGRAM_ADMIN'
|
||||
const isOnTeam = am.confirmation.project.teamMembers.some(
|
||||
(tm) => tm.userId === userId,
|
||||
)
|
||||
if (!isAdmin && !isOnTeam && am.userId !== userId) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
return { pick: am.lunchPick }
|
||||
}),
|
||||
|
||||
// ─── Manifest + CSV export ───────────────────────────────────────────────
|
||||
|
||||
getManifest: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(({ ctx, input }) => buildManifest(ctx.prisma, input.programId)),
|
||||
|
||||
exportManifestCsv: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const m = await buildManifest(ctx.prisma, input.programId)
|
||||
const lines = [
|
||||
'Type,Team,Name,Email,Dish,Allergens,Allergen notes',
|
||||
...m.members.map((row) =>
|
||||
[
|
||||
'Member',
|
||||
csvCell(row.project?.name),
|
||||
csvCell(row.name),
|
||||
csvCell(row.email),
|
||||
csvCell(row.dish?.name),
|
||||
csvCell(row.allergens.join(';')),
|
||||
csvCell(row.allergenOther),
|
||||
].join(','),
|
||||
),
|
||||
...m.externals.map((row) =>
|
||||
[
|
||||
'External',
|
||||
csvCell(row.project?.name),
|
||||
csvCell(row.name),
|
||||
csvCell(row.email),
|
||||
csvCell(row.dish?.name),
|
||||
csvCell(row.allergens.join(';')),
|
||||
csvCell(row.allergenOther),
|
||||
].join(','),
|
||||
),
|
||||
]
|
||||
return lines.join('\n')
|
||||
}),
|
||||
|
||||
// ─── Recap preview + send ────────────────────────────────────────────────
|
||||
|
||||
getRecapPreview: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(({ ctx, input }) => buildRecapPayload(ctx.prisma, input.programId)),
|
||||
|
||||
sendRecap: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string(),
|
||||
forceUpdate: z.boolean().optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const event = await ctx.prisma.lunchEvent.findUnique({
|
||||
where: { programId: input.programId },
|
||||
})
|
||||
if (!event) throw new TRPCError({ code: 'NOT_FOUND', message: 'Lunch event not found' })
|
||||
if (event.recapSentAt && !input.forceUpdate) {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Recap already sent. Pass forceUpdate=true to resend.',
|
||||
})
|
||||
}
|
||||
const payload = await buildRecapPayload(ctx.prisma, input.programId)
|
||||
const adminUsers = await ctx.prisma.user.findMany({
|
||||
where: {
|
||||
role: { in: ['SUPER_ADMIN', 'PROGRAM_ADMIN'] },
|
||||
email: { not: '' },
|
||||
},
|
||||
select: { email: true },
|
||||
})
|
||||
const recipients = [
|
||||
...adminUsers.map((u) => u.email).filter(Boolean),
|
||||
...event.extraRecipients,
|
||||
]
|
||||
try {
|
||||
await sendLunchRecapEmail(recipients, payload)
|
||||
} catch (e) {
|
||||
console.error('[lunch.sendRecap] email send failed', e)
|
||||
// Continue — we still stamp recapSentAt and audit so admins see what happened.
|
||||
}
|
||||
const updated = await ctx.prisma.lunchEvent.update({
|
||||
where: { programId: input.programId },
|
||||
data: { recapSentAt: new Date() },
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_RECAP_SENT',
|
||||
entityType: 'LunchEvent',
|
||||
entityId: event.id,
|
||||
detailsJson: {
|
||||
recipientCount: recipients.length,
|
||||
forceUpdate: !!input.forceUpdate,
|
||||
source: 'manual',
|
||||
},
|
||||
})
|
||||
return updated
|
||||
}),
|
||||
|
||||
// ─── Member reads ────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Public-ish event view for the applicant dashboard banner.
|
||||
* Returns null when the lunch event is disabled (banner hidden).
|
||||
*/
|
||||
getEventForMember: protectedProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const event = await ctx.prisma.lunchEvent.findUnique({
|
||||
where: { programId: input.programId },
|
||||
select: {
|
||||
id: true,
|
||||
enabled: true,
|
||||
eventAt: true,
|
||||
endAt: true,
|
||||
venue: true,
|
||||
notes: true,
|
||||
changeCutoffHours: true,
|
||||
},
|
||||
})
|
||||
if (!event || !event.enabled) return null
|
||||
const changeDeadline = event.eventAt
|
||||
? new Date(event.eventAt.getTime() - event.changeCutoffHours * 3_600_000)
|
||||
: null
|
||||
return { ...event, changeDeadline }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Read-only list of project-attached externals for a project. Visible to
|
||||
* any team member of the project (so they know who's joining their lunch).
|
||||
*/
|
||||
getProjectExternals: protectedProcedure
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const userId = ctx.user.id
|
||||
const role = ctx.user.role
|
||||
const isAdmin = role === 'SUPER_ADMIN' || role === 'PROGRAM_ADMIN'
|
||||
if (!isAdmin) {
|
||||
const tm = await ctx.prisma.teamMember.findFirst({
|
||||
where: { projectId: input.projectId, userId },
|
||||
})
|
||||
if (!tm) throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
return ctx.prisma.externalAttendee.findMany({
|
||||
where: { projectId: input.projectId },
|
||||
include: { dish: true },
|
||||
orderBy: { createdAt: 'asc' },
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* All picks for the caller's team. Within-team transparency: every team
|
||||
* member sees their teammates' picks (lunch picks aren't sensitive).
|
||||
* Cross-team and admins go through the manifest endpoint instead, which
|
||||
* has more detail.
|
||||
*/
|
||||
getTeamPicks: protectedProcedure
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const userId = ctx.user.id
|
||||
const role = ctx.user.role
|
||||
const isAdmin = role === 'SUPER_ADMIN' || role === 'PROGRAM_ADMIN'
|
||||
if (!isAdmin) {
|
||||
const tm = await ctx.prisma.teamMember.findFirst({
|
||||
where: { projectId: input.projectId, userId },
|
||||
})
|
||||
if (!tm) throw new TRPCError({ code: 'FORBIDDEN' })
|
||||
}
|
||||
const ams = await ctx.prisma.attendingMember.findMany({
|
||||
where: { confirmation: { projectId: input.projectId } },
|
||||
include: {
|
||||
user: { select: { id: true, name: true, email: true } },
|
||||
lunchPick: { include: { dish: true } },
|
||||
},
|
||||
})
|
||||
return ams.map((am) => ({
|
||||
attendingMemberId: am.id,
|
||||
userId: am.user.id,
|
||||
memberName: am.user.name ?? am.user.email,
|
||||
dish: am.lunchPick?.dish ?? null,
|
||||
allergens: am.lunchPick?.allergens ?? [],
|
||||
allergenOther: am.lunchPick?.allergenOther ?? null,
|
||||
hasPicked: !!am.lunchPick?.pickedAt,
|
||||
}))
|
||||
}),
|
||||
|
||||
// ─── Mixed-permission picker ─────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Upsert a MemberLunchPick. Permission:
|
||||
* - admin (SUPER_ADMIN / PROGRAM_ADMIN): always allowed, no deadline cap
|
||||
* - team lead of the parent project: allowed before deadline
|
||||
* - the AttendingMember.userId themselves: allowed before deadline
|
||||
* - everyone else: FORBIDDEN
|
||||
* Audit-logged with the actor role (SELF / TEAM_LEAD / ADMIN).
|
||||
*/
|
||||
upsertPick: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
attendingMemberId: z.string(),
|
||||
dishId: z.string().nullable(),
|
||||
allergens,
|
||||
allergenOther: z.string().max(500).nullable(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const am = await ctx.prisma.attendingMember.findUnique({
|
||||
where: { id: input.attendingMemberId },
|
||||
include: {
|
||||
confirmation: {
|
||||
select: {
|
||||
project: {
|
||||
select: {
|
||||
id: true,
|
||||
programId: true,
|
||||
teamMembers: { select: { userId: true, role: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
lunchPick: true,
|
||||
},
|
||||
})
|
||||
if (!am) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Attending member not found',
|
||||
})
|
||||
}
|
||||
|
||||
const userId = ctx.user.id
|
||||
const userRole = ctx.user.role
|
||||
const isAdmin =
|
||||
userRole === 'SUPER_ADMIN' || userRole === 'PROGRAM_ADMIN'
|
||||
const isSelf = am.userId === userId
|
||||
const isLead = am.confirmation.project.teamMembers.some(
|
||||
(tm) => tm.userId === userId && tm.role === 'LEAD',
|
||||
)
|
||||
if (!isAdmin && !isSelf && !isLead) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Not allowed to edit this pick',
|
||||
})
|
||||
}
|
||||
|
||||
// Cutoff check (admins skip)
|
||||
if (!isAdmin) {
|
||||
const event = await ctx.prisma.lunchEvent.findUnique({
|
||||
where: { programId: am.confirmation.project.programId },
|
||||
select: { eventAt: true, changeCutoffHours: true },
|
||||
})
|
||||
if (event?.eventAt) {
|
||||
const deadline = new Date(
|
||||
event.eventAt.getTime() - event.changeCutoffHours * 3_600_000,
|
||||
)
|
||||
if (new Date() > deadline) {
|
||||
throw new TRPCError({
|
||||
code: 'PRECONDITION_FAILED',
|
||||
message: 'Past lunch change deadline. Contact an admin.',
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const actorRole: 'SELF' | 'TEAM_LEAD' | 'ADMIN' = isAdmin
|
||||
? 'ADMIN'
|
||||
: isLead && !isSelf
|
||||
? 'TEAM_LEAD'
|
||||
: 'SELF'
|
||||
|
||||
const pick = await ctx.prisma.memberLunchPick.upsert({
|
||||
where: { attendingMemberId: input.attendingMemberId },
|
||||
create: {
|
||||
attendingMemberId: input.attendingMemberId,
|
||||
dishId: input.dishId,
|
||||
allergens: input.allergens,
|
||||
allergenOther: input.allergenOther,
|
||||
pickedAt: input.dishId ? new Date() : null,
|
||||
},
|
||||
update: {
|
||||
dishId: input.dishId,
|
||||
allergens: input.allergens,
|
||||
allergenOther: input.allergenOther,
|
||||
pickedAt: input.dishId ? new Date() : null,
|
||||
},
|
||||
})
|
||||
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId,
|
||||
action: 'LUNCH_PICK_UPDATED',
|
||||
entityType: 'MemberLunchPick',
|
||||
entityId: pick.id,
|
||||
detailsJson: {
|
||||
actorRole,
|
||||
dishId: input.dishId,
|
||||
allergenCount: input.allergens.length,
|
||||
},
|
||||
})
|
||||
return pick
|
||||
}),
|
||||
|
||||
/** Patch any subset of LunchEvent config fields. Audit-logged. */
|
||||
updateEvent: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string(),
|
||||
enabled: z.boolean().optional(),
|
||||
eventAt: z.date().nullable().optional(),
|
||||
endAt: z.date().nullable().optional(),
|
||||
venue: z.string().nullable().optional(),
|
||||
notes: z.string().nullable().optional(),
|
||||
changeCutoffHours: z.number().int().min(0).max(720).optional(),
|
||||
reminderHoursBeforeDeadline: z
|
||||
.number()
|
||||
.int()
|
||||
.min(0)
|
||||
.max(720)
|
||||
.nullable()
|
||||
.optional(),
|
||||
cronEnabled: z.boolean().optional(),
|
||||
extraRecipients: z.array(z.string().email()).optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const { programId, ...patch } = input
|
||||
// Lazy-create before patching so updateEvent doubles as "create + update"
|
||||
await ctx.prisma.lunchEvent.upsert({
|
||||
where: { programId },
|
||||
create: { programId },
|
||||
update: {},
|
||||
})
|
||||
const updated = await ctx.prisma.lunchEvent.update({
|
||||
where: { programId },
|
||||
data: patch,
|
||||
})
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'LUNCH_EVENT_UPDATED',
|
||||
entityType: 'LunchEvent',
|
||||
entityId: updated.id,
|
||||
detailsJson: patch as Record<string, unknown>,
|
||||
})
|
||||
return updated
|
||||
}),
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,7 +14,7 @@ export const messageRouter = router({
|
||||
send: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'ALL']),
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'PROJECT_TEAM', 'ALL']),
|
||||
recipientFilter: z.any().optional(),
|
||||
roundId: z.string().optional(),
|
||||
roundIds: z.array(z.string()).optional(),
|
||||
@@ -482,7 +482,7 @@ export const messageRouter = router({
|
||||
*/
|
||||
previewRecipients: adminProcedure
|
||||
.input(z.object({
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'ALL']),
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'PROJECT_TEAM', 'ALL']),
|
||||
recipientFilter: z.any().optional(),
|
||||
roundId: z.string().optional(),
|
||||
roundIds: z.array(z.string()).optional(),
|
||||
@@ -556,7 +556,7 @@ export const messageRouter = router({
|
||||
*/
|
||||
listRecipientDetails: adminProcedure
|
||||
.input(z.object({
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'ALL']),
|
||||
recipientType: z.enum(['USER', 'ROLE', 'ROUND_JURY', 'ROUND_APPLICANTS', 'PROGRAM_TEAM', 'PROJECT_TEAM', 'ALL']),
|
||||
recipientFilter: z.any().optional(),
|
||||
roundId: z.string().optional(),
|
||||
roundIds: z.array(z.string()).optional(),
|
||||
@@ -839,6 +839,25 @@ async function resolveRecipients(
|
||||
return [...ids]
|
||||
}
|
||||
|
||||
case 'PROJECT_TEAM': {
|
||||
const projectId = filter?.projectId as string
|
||||
if (!projectId) return []
|
||||
const [teamMembers, project] = await Promise.all([
|
||||
prisma.teamMember.findMany({
|
||||
where: { projectId },
|
||||
select: { userId: true },
|
||||
}),
|
||||
prisma.project.findUnique({
|
||||
where: { id: projectId },
|
||||
select: { submittedByUserId: true },
|
||||
}),
|
||||
])
|
||||
const ids = new Set<string>()
|
||||
for (const tm of teamMembers) ids.add(tm.userId)
|
||||
if (project?.submittedByUserId) ids.add(project.submittedByUserId)
|
||||
return [...ids]
|
||||
}
|
||||
|
||||
case 'ALL': {
|
||||
const users = await prisma.user.findMany({
|
||||
where: { status: 'ACTIVE' },
|
||||
|
||||
@@ -281,4 +281,131 @@ export const programRouter = router({
|
||||
|
||||
return { success: true }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Returns the merged edition-settings view for the admin Settings page:
|
||||
* Program fields (defaultAttendeeCap, visaStatusVisibleToMembers) plus the
|
||||
* LIVE_FINAL round's configJson values (attendeeEditCutoffHours and
|
||||
* confirmationWindowHours, with sensible defaults). Round-derived values
|
||||
* are null when the LIVE_FINAL round doesn't exist yet.
|
||||
*/
|
||||
getEditionSettings: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const program = await ctx.prisma.program.findUniqueOrThrow({
|
||||
where: { id: input.programId },
|
||||
select: {
|
||||
id: true,
|
||||
defaultAttendeeCap: true,
|
||||
visaStatusVisibleToMembers: true,
|
||||
},
|
||||
})
|
||||
const round = await ctx.prisma.round.findFirst({
|
||||
where: {
|
||||
competition: { programId: input.programId },
|
||||
roundType: 'LIVE_FINAL',
|
||||
},
|
||||
orderBy: { sortOrder: 'desc' },
|
||||
select: { id: true, configJson: true },
|
||||
})
|
||||
const cfg = (round?.configJson ?? {}) as Record<string, unknown>
|
||||
return {
|
||||
programId: program.id,
|
||||
defaultAttendeeCap: program.defaultAttendeeCap,
|
||||
visaStatusVisibleToMembers: program.visaStatusVisibleToMembers,
|
||||
liveFinalRoundId: round?.id ?? null,
|
||||
attendeeEditCutoffHours: round
|
||||
? ((cfg.attendeeEditCutoffHours as number | undefined) ?? 48)
|
||||
: null,
|
||||
confirmationWindowHours: round
|
||||
? ((cfg.confirmationWindowHours as number | undefined) ?? 24)
|
||||
: null,
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Partial update for edition settings. Writes Program fields directly and
|
||||
* merges round-config keys (attendeeEditCutoffHours, confirmationWindowHours)
|
||||
* into the LIVE_FINAL round's configJson, preserving any unrelated keys
|
||||
* already in the JSON blob.
|
||||
*/
|
||||
updateEditionSettings: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
programId: z.string(),
|
||||
defaultAttendeeCap: z.number().int().min(1).max(20).optional(),
|
||||
visaStatusVisibleToMembers: z.boolean().optional(),
|
||||
attendeeEditCutoffHours: z.number().int().min(0).max(720).optional(),
|
||||
confirmationWindowHours: z.number().int().min(1).max(720).optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const programData: Record<string, unknown> = {}
|
||||
if (input.defaultAttendeeCap !== undefined) {
|
||||
programData.defaultAttendeeCap = input.defaultAttendeeCap
|
||||
}
|
||||
if (input.visaStatusVisibleToMembers !== undefined) {
|
||||
programData.visaStatusVisibleToMembers = input.visaStatusVisibleToMembers
|
||||
}
|
||||
|
||||
if (Object.keys(programData).length > 0) {
|
||||
await ctx.prisma.program.update({
|
||||
where: { id: input.programId },
|
||||
data: programData,
|
||||
})
|
||||
}
|
||||
|
||||
const roundConfigKeys = ['attendeeEditCutoffHours', 'confirmationWindowHours'] as const
|
||||
const roundUpdates: Record<string, number> = {}
|
||||
for (const k of roundConfigKeys) {
|
||||
const v = input[k]
|
||||
if (v !== undefined) roundUpdates[k] = v
|
||||
}
|
||||
if (Object.keys(roundUpdates).length > 0) {
|
||||
const round = await ctx.prisma.round.findFirst({
|
||||
where: {
|
||||
competition: { programId: input.programId },
|
||||
roundType: 'LIVE_FINAL',
|
||||
},
|
||||
orderBy: { sortOrder: 'desc' },
|
||||
select: { id: true, configJson: true },
|
||||
})
|
||||
if (round) {
|
||||
const existing = (round.configJson ?? {}) as Record<string, unknown>
|
||||
const merged = { ...existing, ...roundUpdates }
|
||||
await ctx.prisma.round.update({
|
||||
where: { id: round.id },
|
||||
data: { configJson: merged as Prisma.InputJsonValue },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: 'PROGRAM_EDITION_SETTINGS_UPDATE',
|
||||
entityType: 'Program',
|
||||
entityId: input.programId,
|
||||
detailsJson: { ...input },
|
||||
})
|
||||
|
||||
return { ok: true }
|
||||
}),
|
||||
|
||||
/**
|
||||
* List CONFIRMED finalist projects for a program — used by the lunch
|
||||
* externals dialog to attach an external attendee to a team.
|
||||
*/
|
||||
listFinalistProjects: adminProcedure
|
||||
.input(z.object({ programId: z.string() }))
|
||||
.query(({ ctx, input }) =>
|
||||
ctx.prisma.project.findMany({
|
||||
where: {
|
||||
programId: input.programId,
|
||||
finalistConfirmation: { status: 'CONFIRMED' },
|
||||
},
|
||||
select: { id: true, title: true },
|
||||
orderBy: { title: 'asc' },
|
||||
}),
|
||||
),
|
||||
})
|
||||
|
||||
@@ -177,14 +177,38 @@ export const projectRouter = router({
|
||||
]
|
||||
}
|
||||
|
||||
// Jury members can only see assigned projects (but not if they also have admin roles)
|
||||
if (
|
||||
userHasRole(ctx.user, 'JURY_MEMBER') &&
|
||||
!userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')
|
||||
) {
|
||||
where.assignments = {
|
||||
...((where.assignments as Record<string, unknown>) || {}),
|
||||
some: { userId: ctx.user.id },
|
||||
// Per-role visibility filters. Admin / Observer / Award master see all
|
||||
// (these roles are designed for cross-program oversight). Other roles
|
||||
// are scoped to projects they have a relationship with.
|
||||
const isAdmin = userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')
|
||||
const isObserverLevel = userHasRole(ctx.user, 'OBSERVER', 'AWARD_MASTER')
|
||||
if (!isAdmin && !isObserverLevel) {
|
||||
const orClauses: Array<Record<string, unknown>> = []
|
||||
if (userHasRole(ctx.user, 'JURY_MEMBER')) {
|
||||
orClauses.push({ assignments: { some: { userId: ctx.user.id } } })
|
||||
}
|
||||
if (userHasRole(ctx.user, 'MENTOR')) {
|
||||
orClauses.push({ mentorAssignment: { mentorId: ctx.user.id } })
|
||||
}
|
||||
if (userHasRole(ctx.user, 'APPLICANT')) {
|
||||
orClauses.push({ teamMembers: { some: { userId: ctx.user.id } } })
|
||||
orClauses.push({ submittedByUserId: ctx.user.id })
|
||||
}
|
||||
if (orClauses.length === 0) {
|
||||
// No relationship-based access (e.g. AUDIENCE) — return nothing.
|
||||
where.id = { in: [] }
|
||||
} else if (orClauses.length === 1) {
|
||||
Object.assign(where, orClauses[0])
|
||||
} else {
|
||||
// Multiple roles — combine with the existing search OR if any.
|
||||
// Compose with existing `where.OR` (from `search`) by AND-ing.
|
||||
const existingOr = where.OR as Array<Record<string, unknown>> | undefined
|
||||
if (existingOr) {
|
||||
where.AND = [{ OR: existingOr }, { OR: orClauses }]
|
||||
delete where.OR
|
||||
} else {
|
||||
where.OR = orClauses
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -510,19 +534,42 @@ export const projectRouter = router({
|
||||
// ProjectTag table may not exist yet
|
||||
}
|
||||
|
||||
// Check access for jury members (but not if they also have admin roles)
|
||||
if (userHasRole(ctx.user, 'JURY_MEMBER') && !userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')) {
|
||||
const assignment = await ctx.prisma.assignment.findFirst({
|
||||
where: {
|
||||
projectId: input.id,
|
||||
userId: ctx.user.id,
|
||||
},
|
||||
})
|
||||
|
||||
if (!assignment) {
|
||||
// Per-role access check. Admin / Observer / Award master can read any
|
||||
// project. Jury / Mentor / Applicant must have a relationship to it.
|
||||
const isAdmin = userHasRole(ctx.user, 'SUPER_ADMIN', 'PROGRAM_ADMIN')
|
||||
const isObserverLevel = userHasRole(ctx.user, 'OBSERVER', 'AWARD_MASTER')
|
||||
if (!isAdmin && !isObserverLevel) {
|
||||
const checks: Array<Promise<unknown>> = []
|
||||
if (userHasRole(ctx.user, 'JURY_MEMBER')) {
|
||||
checks.push(
|
||||
ctx.prisma.assignment.findFirst({
|
||||
where: { projectId: input.id, userId: ctx.user.id },
|
||||
select: { id: true },
|
||||
}),
|
||||
)
|
||||
}
|
||||
if (userHasRole(ctx.user, 'MENTOR')) {
|
||||
checks.push(
|
||||
ctx.prisma.mentorAssignment.findFirst({
|
||||
where: { projectId: input.id, mentorId: ctx.user.id },
|
||||
select: { id: true },
|
||||
}),
|
||||
)
|
||||
}
|
||||
if (userHasRole(ctx.user, 'APPLICANT')) {
|
||||
checks.push(
|
||||
ctx.prisma.teamMember.findFirst({
|
||||
where: { projectId: input.id, userId: ctx.user.id },
|
||||
select: { id: true },
|
||||
}),
|
||||
)
|
||||
}
|
||||
const results = await Promise.all(checks)
|
||||
const hasAccess = results.some((r) => r !== null && r !== undefined)
|
||||
if (!hasAccess) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You are not assigned to this project',
|
||||
message: 'You do not have access to this project',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,6 +215,35 @@ export const roundRouter = router({
|
||||
)
|
||||
}),
|
||||
|
||||
/**
|
||||
* Count projects in a MENTORING round eligible for mentor auto-fill
|
||||
* (i.e., no mentorAssignment, scoped by configJson.eligibility).
|
||||
* Used by the Auto-fill remaining toolbar on the round Projects tab.
|
||||
*/
|
||||
getProjectsNeedingMentor: adminProcedure
|
||||
.input(z.object({ roundId: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const round = await ctx.prisma.round.findUniqueOrThrow({
|
||||
where: { id: input.roundId },
|
||||
select: { roundType: true, configJson: true },
|
||||
})
|
||||
if (round.roundType !== 'MENTORING') return { count: 0 }
|
||||
const config = (round.configJson ?? {}) as Record<string, unknown>
|
||||
const eligibility = (config.eligibility as string) ?? 'requested_only'
|
||||
if (eligibility === 'admin_selected') return { count: 0 }
|
||||
|
||||
const count = await ctx.prisma.projectRoundState.count({
|
||||
where: {
|
||||
roundId: input.roundId,
|
||||
project: {
|
||||
mentorAssignment: null,
|
||||
...(eligibility === 'requested_only' ? { wantsMentorship: true } : {}),
|
||||
},
|
||||
},
|
||||
})
|
||||
return { count }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Delete a round
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,11 @@ import { getUserAvatarUrl } from '../utils/avatar-url'
|
||||
import { logAudit } from '../utils/audit'
|
||||
import { processEligibilityJob } from '../services/award-eligibility-job'
|
||||
import { resolveAwardWinner } from '../services/award-winner-resolver'
|
||||
import { getAwardSelectionNotificationTemplate, sendJuryInvitationEmail } from '@/lib/email'
|
||||
import {
|
||||
getAwardSelectionNotificationTemplate,
|
||||
sendJuryInvitationEmail,
|
||||
sendAwardJurorNotificationEmail,
|
||||
} from '@/lib/email'
|
||||
import { generateInviteToken, getInviteExpiryMs } from '@/server/utils/invite'
|
||||
import { attachProjectLogoUrls } from '../utils/project-logo-url'
|
||||
import { sendBatchNotifications } from '../services/notification-sender'
|
||||
@@ -31,6 +35,58 @@ async function ensureUserExists(db: PrismaClient, userId: string): Promise<strin
|
||||
return user.id
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the "you've been assigned to vote on this award" email to a set of
|
||||
* jurors. Used by addJuror / bulkInviteJurors (auto-send on assignment) and
|
||||
* by the explicit notifyJurors admin reminder. Errors per recipient are
|
||||
* caught so a single SMTP failure doesn't break the bulk operation.
|
||||
*/
|
||||
async function sendAwardJurorEmails(
|
||||
db: PrismaClient,
|
||||
awardId: string,
|
||||
userIds: string[],
|
||||
options: { customMessage?: string; isReminder?: boolean } = {},
|
||||
): Promise<{ sent: number; failed: number }> {
|
||||
if (userIds.length === 0) return { sent: 0, failed: 0 }
|
||||
|
||||
const award = await db.specialAward.findUniqueOrThrow({
|
||||
where: { id: awardId },
|
||||
select: { id: true, name: true, votingEndAt: true },
|
||||
})
|
||||
const eligibleCount = await db.awardEligibility.count({
|
||||
where: { awardId, eligible: true },
|
||||
})
|
||||
const users = await db.user.findMany({
|
||||
where: { id: { in: userIds } },
|
||||
select: { id: true, email: true, name: true },
|
||||
})
|
||||
|
||||
const baseUrl = process.env.NEXTAUTH_URL || ''
|
||||
const url = `${baseUrl}/jury/awards/${awardId}`
|
||||
|
||||
let sent = 0
|
||||
let failed = 0
|
||||
for (const u of users) {
|
||||
try {
|
||||
await sendAwardJurorNotificationEmail({
|
||||
email: u.email,
|
||||
name: u.name,
|
||||
awardName: award.name,
|
||||
url,
|
||||
eligibleCount,
|
||||
votingEndAt: award.votingEndAt,
|
||||
customMessage: options.customMessage,
|
||||
isReminder: options.isReminder,
|
||||
})
|
||||
sent++
|
||||
} catch {
|
||||
// Email failure shouldn't break the calling operation.
|
||||
failed++
|
||||
}
|
||||
}
|
||||
return { sent, failed }
|
||||
}
|
||||
|
||||
export const specialAwardRouter = router({
|
||||
// ─── Admin Queries ──────────────────────────────────────────────────────
|
||||
|
||||
@@ -521,15 +577,22 @@ export const specialAwardRouter = router({
|
||||
z.object({
|
||||
awardId: z.string(),
|
||||
userId: z.string(),
|
||||
sendEmail: z.boolean().default(true),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
return ctx.prisma.awardJuror.create({
|
||||
const created = await ctx.prisma.awardJuror.create({
|
||||
data: {
|
||||
awardId: input.awardId,
|
||||
userId: input.userId,
|
||||
},
|
||||
})
|
||||
|
||||
if (input.sendEmail) {
|
||||
await sendAwardJurorEmails(ctx.prisma, input.awardId, [input.userId])
|
||||
}
|
||||
|
||||
return created
|
||||
}),
|
||||
|
||||
/**
|
||||
@@ -561,20 +624,35 @@ export const specialAwardRouter = router({
|
||||
z.object({
|
||||
awardId: z.string(),
|
||||
userIds: z.array(z.string()),
|
||||
sendEmail: z.boolean().default(true),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const data = input.userIds.map((userId) => ({
|
||||
const existingRows = await ctx.prisma.awardJuror.findMany({
|
||||
where: { awardId: input.awardId, userId: { in: input.userIds } },
|
||||
select: { userId: true },
|
||||
})
|
||||
const existing = new Set(existingRows.map((r) => r.userId))
|
||||
const newlyAddedIds = input.userIds.filter((id) => !existing.has(id))
|
||||
|
||||
const data = newlyAddedIds.map((userId) => ({
|
||||
awardId: input.awardId,
|
||||
userId,
|
||||
}))
|
||||
|
||||
await ctx.prisma.awardJuror.createMany({
|
||||
data,
|
||||
skipDuplicates: true,
|
||||
})
|
||||
if (data.length > 0) {
|
||||
await ctx.prisma.awardJuror.createMany({
|
||||
data,
|
||||
skipDuplicates: true,
|
||||
})
|
||||
}
|
||||
|
||||
return { added: input.userIds.length }
|
||||
let emailStats = { sent: 0, failed: 0 }
|
||||
if (input.sendEmail && newlyAddedIds.length > 0) {
|
||||
emailStats = await sendAwardJurorEmails(ctx.prisma, input.awardId, newlyAddedIds)
|
||||
}
|
||||
|
||||
return { added: newlyAddedIds.length, ...emailStats }
|
||||
}),
|
||||
|
||||
/**
|
||||
@@ -641,6 +719,13 @@ export const specialAwardRouter = router({
|
||||
results.push({ email: invitee.email, status: 'existing' })
|
||||
}
|
||||
|
||||
const priorAttachment = await ctx.prisma.awardJuror.findUnique({
|
||||
where: {
|
||||
awardId_userId: { awardId: input.awardId, userId: user.id },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
await ctx.prisma.awardJuror.upsert({
|
||||
where: {
|
||||
awardId_userId: { awardId: input.awardId, userId: user.id },
|
||||
@@ -648,6 +733,16 @@ export const specialAwardRouter = router({
|
||||
update: {},
|
||||
create: { awardId: input.awardId, userId: user.id },
|
||||
})
|
||||
|
||||
// For existing-user invitees the new-account invite email above
|
||||
// never fired (no `created` branch). Send the juror-assignment
|
||||
// notification so they know they were added — but only if this
|
||||
// call actually attached them (skip duplicate "Bulk Invite" clicks
|
||||
// to avoid spam).
|
||||
const lastResult = results[results.length - 1]
|
||||
if (lastResult?.status === 'existing' && !priorAttachment) {
|
||||
await sendAwardJurorEmails(ctx.prisma, input.awardId, [user.id])
|
||||
}
|
||||
} catch (err) {
|
||||
results.push({
|
||||
email: invitee.email,
|
||||
@@ -679,6 +774,51 @@ export const specialAwardRouter = router({
|
||||
}
|
||||
}),
|
||||
|
||||
/**
|
||||
* Send a reminder email to currently-assigned jurors. Pass `userIds` to
|
||||
* target a subset, omit to email every juror on the award. The email links
|
||||
* the juror straight to the voting page.
|
||||
*/
|
||||
notifyJurors: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
awardId: z.string(),
|
||||
userIds: z.array(z.string()).optional(),
|
||||
customMessage: z.string().max(1000).optional(),
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const jurors = await ctx.prisma.awardJuror.findMany({
|
||||
where: {
|
||||
awardId: input.awardId,
|
||||
...(input.userIds && input.userIds.length > 0
|
||||
? { userId: { in: input.userIds } }
|
||||
: {}),
|
||||
},
|
||||
select: { userId: true },
|
||||
})
|
||||
|
||||
const targetIds = jurors.map((j) => j.userId)
|
||||
const stats = await sendAwardJurorEmails(ctx.prisma, input.awardId, targetIds, {
|
||||
customMessage: input.customMessage,
|
||||
isReminder: true,
|
||||
})
|
||||
|
||||
await logAudit({
|
||||
userId: ctx.user.id,
|
||||
action: 'NOTIFY',
|
||||
entityType: 'AwardJuror',
|
||||
entityId: input.awardId,
|
||||
detailsJson: {
|
||||
action: 'JUROR_REMINDER',
|
||||
targetUserIds: targetIds,
|
||||
...stats,
|
||||
},
|
||||
})
|
||||
|
||||
return { targeted: targetIds.length, ...stats }
|
||||
}),
|
||||
|
||||
// ─── Jury Queries ───────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@ import { TRPCError } from '@trpc/server'
|
||||
import type { Prisma } from '@prisma/client'
|
||||
import { UserRole } from '@prisma/client'
|
||||
import { router, protectedProcedure, adminProcedure, superAdminProcedure, publicProcedure } from '../trpc'
|
||||
import { sendInvitationEmail, sendJuryInvitationEmail, sendMagicLinkEmail, sendPasswordResetEmail } from '@/lib/email'
|
||||
import { sendInvitationEmail, sendJuryInvitationEmail, sendMagicLinkEmail, sendMentorOnboardingEmail, sendPasswordResetEmail } from '@/lib/email'
|
||||
import { hashPassword, validatePassword } from '@/lib/password'
|
||||
import { attachAvatarUrls, getUserAvatarUrl } from '@/server/utils/avatar-url'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
@@ -96,7 +96,6 @@ export const userRouter = router({
|
||||
updateProfile: protectedProcedure
|
||||
.input(
|
||||
z.object({
|
||||
email: z.string().email().optional(),
|
||||
name: z.string().min(1).max(255).optional(),
|
||||
bio: z.string().max(1000).optional(),
|
||||
phoneNumber: z.string().max(20).optional().nullable(),
|
||||
@@ -111,35 +110,19 @@ export const userRouter = router({
|
||||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Email is intentionally NOT in the input schema. Allowing self-service
|
||||
// email changes turns any short-lived session compromise into permanent
|
||||
// account takeover via password reset on the new address. Email changes
|
||||
// require an admin-driven flow (or a future verified-change procedure).
|
||||
const {
|
||||
bio,
|
||||
expertiseTags,
|
||||
availabilityJson,
|
||||
preferredWorkload,
|
||||
digestFrequency,
|
||||
email,
|
||||
...directFields
|
||||
} = input
|
||||
|
||||
const normalizedEmail = email?.toLowerCase().trim()
|
||||
|
||||
if (normalizedEmail !== undefined) {
|
||||
const existing = await ctx.prisma.user.findFirst({
|
||||
where: {
|
||||
email: normalizedEmail,
|
||||
NOT: { id: ctx.user.id },
|
||||
},
|
||||
select: { id: true },
|
||||
})
|
||||
|
||||
if (existing) {
|
||||
throw new TRPCError({
|
||||
code: 'CONFLICT',
|
||||
message: 'Another account already uses this email address',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// If bio is provided, merge it into metadataJson
|
||||
let metadataJson: Prisma.InputJsonValue | undefined
|
||||
if (bio !== undefined) {
|
||||
@@ -155,7 +138,6 @@ export const userRouter = router({
|
||||
where: { id: ctx.user.id },
|
||||
data: {
|
||||
...directFields,
|
||||
...(normalizedEmail !== undefined && { email: normalizedEmail }),
|
||||
...(metadataJson !== undefined && { metadataJson }),
|
||||
...(expertiseTags !== undefined && { expertiseTags }),
|
||||
...(digestFrequency !== undefined && { digestFrequency }),
|
||||
@@ -564,15 +546,25 @@ export const userRouter = router({
|
||||
where: { id },
|
||||
})
|
||||
|
||||
if (targetUser.role === 'SUPER_ADMIN' && ctx.user.role !== 'SUPER_ADMIN') {
|
||||
const callerIsSuperAdmin = ctx.user.role === 'SUPER_ADMIN'
|
||||
const targetHasSuperAdmin =
|
||||
targetUser.role === 'SUPER_ADMIN' || targetUser.roles.includes('SUPER_ADMIN')
|
||||
const targetHasProgramAdmin =
|
||||
targetUser.role === 'PROGRAM_ADMIN' || targetUser.roles.includes('PROGRAM_ADMIN')
|
||||
|
||||
if (targetHasSuperAdmin && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Cannot modify super admin',
|
||||
})
|
||||
}
|
||||
|
||||
// Prevent non-super-admins from changing admin roles
|
||||
if (data.role && targetUser.role === 'PROGRAM_ADMIN' && ctx.user.role !== 'SUPER_ADMIN') {
|
||||
// Prevent non-super-admins from changing admin roles (singular OR array)
|
||||
if (
|
||||
(data.role || data.roles) &&
|
||||
targetHasProgramAdmin &&
|
||||
!callerIsSuperAdmin
|
||||
) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can change admin roles',
|
||||
@@ -580,13 +572,26 @@ export const userRouter = router({
|
||||
}
|
||||
|
||||
// Prevent non-super-admins from assigning super admin or admin role
|
||||
if (data.role === 'SUPER_ADMIN' && ctx.user.role !== 'SUPER_ADMIN') {
|
||||
// — check both the singular `role` field AND the `roles[]` array.
|
||||
if (data.role === 'SUPER_ADMIN' && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can assign super admin role',
|
||||
})
|
||||
}
|
||||
if (data.role === 'PROGRAM_ADMIN' && ctx.user.role !== 'SUPER_ADMIN') {
|
||||
if (data.role === 'PROGRAM_ADMIN' && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can assign admin role',
|
||||
})
|
||||
}
|
||||
if (data.roles?.includes('SUPER_ADMIN') && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can assign super admin role',
|
||||
})
|
||||
}
|
||||
if (data.roles?.includes('PROGRAM_ADMIN') && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can assign admin role',
|
||||
@@ -1396,7 +1401,18 @@ export const userRouter = router({
|
||||
*/
|
||||
getOnboardingContext: protectedProcedure.query(async ({ ctx }) => {
|
||||
const memberships = await ctx.prisma.juryGroupMember.findMany({
|
||||
where: { userId: ctx.user.id },
|
||||
where: {
|
||||
userId: ctx.user.id,
|
||||
juryGroup: {
|
||||
rounds: {
|
||||
some: {
|
||||
roundType: {
|
||||
in: ['INTAKE', 'FILTERING', 'EVALUATION', 'SUBMISSION', 'MENTORING'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
include: {
|
||||
juryGroup: {
|
||||
select: {
|
||||
@@ -1793,10 +1809,30 @@ export const userRouter = router({
|
||||
roles: z.array(z.nativeEnum(UserRole)).min(1),
|
||||
}))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
// Guard: only SUPER_ADMIN can grant SUPER_ADMIN
|
||||
if (input.roles.includes('SUPER_ADMIN') && ctx.user.role !== 'SUPER_ADMIN') {
|
||||
const callerIsSuperAdmin = ctx.user.role === 'SUPER_ADMIN'
|
||||
|
||||
// Guard: only SUPER_ADMIN can grant SUPER_ADMIN or PROGRAM_ADMIN
|
||||
if (input.roles.includes('SUPER_ADMIN') && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN', message: 'Only super admins can grant super admin role' })
|
||||
}
|
||||
if (input.roles.includes('PROGRAM_ADMIN') && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN', message: 'Only super admins can grant admin role' })
|
||||
}
|
||||
|
||||
// Guard: only SUPER_ADMIN can modify a user who currently holds SUPER_ADMIN
|
||||
// or PROGRAM_ADMIN — otherwise a PROGRAM_ADMIN could demote peers/super-admins.
|
||||
const target = await ctx.prisma.user.findUniqueOrThrow({
|
||||
where: { id: input.userId },
|
||||
select: { role: true, roles: true },
|
||||
})
|
||||
const targetHasAdmin =
|
||||
target.role === 'SUPER_ADMIN' ||
|
||||
target.role === 'PROGRAM_ADMIN' ||
|
||||
target.roles.includes('SUPER_ADMIN') ||
|
||||
target.roles.includes('PROGRAM_ADMIN')
|
||||
if (targetHasAdmin && !callerIsSuperAdmin) {
|
||||
throw new TRPCError({ code: 'FORBIDDEN', message: 'Only super admins can change admin roles' })
|
||||
}
|
||||
|
||||
// Set primary role to highest privilege role
|
||||
const rolePriority: UserRole[] = ['SUPER_ADMIN', 'PROGRAM_ADMIN', 'JURY_MEMBER', 'MENTOR', 'OBSERVER', 'AWARD_MASTER', 'APPLICANT', 'AUDIENCE']
|
||||
@@ -1808,6 +1844,172 @@ export const userRouter = router({
|
||||
})
|
||||
}),
|
||||
|
||||
/**
|
||||
* Bulk add/remove a single role across multiple users. Fires the mentor
|
||||
* onboarding email exactly once per user when MENTOR is freshly added,
|
||||
* idempotent via User.mentorOnboardingSentAt.
|
||||
*/
|
||||
bulkUpdateRoles: adminProcedure
|
||||
.input(
|
||||
z.object({
|
||||
userIds: z.array(z.string()).min(1).max(200),
|
||||
addRole: z.nativeEnum(UserRole).optional(),
|
||||
removeRole: z.nativeEnum(UserRole).optional(),
|
||||
}).refine((d) => d.addRole || d.removeRole, {
|
||||
message: 'Provide addRole or removeRole',
|
||||
}),
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const callerIsSuperAdmin = ctx.user.role === 'SUPER_ADMIN'
|
||||
|
||||
// Self-demote guards
|
||||
if (input.removeRole === 'SUPER_ADMIN' && input.userIds.includes(ctx.user.id)) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You cannot remove SUPER_ADMIN from self',
|
||||
})
|
||||
}
|
||||
if (input.removeRole === 'PROGRAM_ADMIN' && input.userIds.includes(ctx.user.id)) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'You cannot remove PROGRAM_ADMIN from self',
|
||||
})
|
||||
}
|
||||
|
||||
// Privilege guards: only SUPER_ADMIN may add/remove SUPER_ADMIN or PROGRAM_ADMIN.
|
||||
// Without the symmetric remove-side guard a PROGRAM_ADMIN could strip
|
||||
// SUPER_ADMIN from peers; without the add-side PROGRAM_ADMIN guard a
|
||||
// PROGRAM_ADMIN could grant peer-admin laterally.
|
||||
if (
|
||||
(input.addRole === 'SUPER_ADMIN' || input.removeRole === 'SUPER_ADMIN') &&
|
||||
!callerIsSuperAdmin
|
||||
) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can change super admin role',
|
||||
})
|
||||
}
|
||||
if (
|
||||
(input.addRole === 'PROGRAM_ADMIN' || input.removeRole === 'PROGRAM_ADMIN') &&
|
||||
!callerIsSuperAdmin
|
||||
) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can change admin role',
|
||||
})
|
||||
}
|
||||
|
||||
const targets = await ctx.prisma.user.findMany({
|
||||
where: { id: { in: input.userIds } },
|
||||
select: { id: true, name: true, email: true, role: true, roles: true, mentorOnboardingSentAt: true },
|
||||
})
|
||||
|
||||
// Block modifying any target who currently holds an admin tier role
|
||||
// unless the caller is SUPER_ADMIN. This prevents a PROGRAM_ADMIN from
|
||||
// using a non-admin add/remove (e.g. addRole: MENTOR) to mutate the
|
||||
// record of a SUPER_ADMIN target — even though Prisma would only touch
|
||||
// `roles[]`, the audit trail and downstream logic shouldn't allow
|
||||
// peer admins to mutate higher-tier accounts at all.
|
||||
if (!callerIsSuperAdmin) {
|
||||
const adminTarget = targets.find(
|
||||
(t) =>
|
||||
t.role === 'SUPER_ADMIN' ||
|
||||
t.role === 'PROGRAM_ADMIN' ||
|
||||
t.roles.includes('SUPER_ADMIN') ||
|
||||
t.roles.includes('PROGRAM_ADMIN'),
|
||||
)
|
||||
if (adminTarget) {
|
||||
throw new TRPCError({
|
||||
code: 'FORBIDDEN',
|
||||
message: 'Only super admins can modify admin accounts',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let updated = 0
|
||||
let alreadyHadRole = 0
|
||||
const newlyMentor: typeof targets = []
|
||||
|
||||
const rolePriority: UserRole[] = [
|
||||
'SUPER_ADMIN',
|
||||
'PROGRAM_ADMIN',
|
||||
'JURY_MEMBER',
|
||||
'MENTOR',
|
||||
'OBSERVER',
|
||||
'AWARD_MASTER',
|
||||
'APPLICANT',
|
||||
'AUDIENCE',
|
||||
]
|
||||
|
||||
for (const u of targets) {
|
||||
const current = new Set(u.roles)
|
||||
const next = new Set(current)
|
||||
|
||||
if (input.addRole) {
|
||||
if (current.has(input.addRole)) {
|
||||
alreadyHadRole++
|
||||
continue
|
||||
}
|
||||
next.add(input.addRole)
|
||||
}
|
||||
if (input.removeRole) {
|
||||
if (!current.has(input.removeRole)) {
|
||||
alreadyHadRole++
|
||||
continue
|
||||
}
|
||||
next.delete(input.removeRole)
|
||||
}
|
||||
if (next.size === 0) next.add('APPLICANT' as UserRole) // safety: never empty
|
||||
|
||||
const nextArr = Array.from(next)
|
||||
const primary = rolePriority.find((r) => nextArr.includes(r)) ?? nextArr[0]
|
||||
|
||||
const isFreshMentor =
|
||||
input.addRole === 'MENTOR' && !current.has('MENTOR') && !u.mentorOnboardingSentAt
|
||||
|
||||
await ctx.prisma.user.update({
|
||||
where: { id: u.id },
|
||||
data: {
|
||||
roles: nextArr,
|
||||
role: primary,
|
||||
...(isFreshMentor ? { mentorOnboardingSentAt: new Date() } : {}),
|
||||
},
|
||||
})
|
||||
|
||||
await logAudit({
|
||||
prisma: ctx.prisma,
|
||||
userId: ctx.user.id,
|
||||
action: input.addRole ? 'USER_ROLE_ADD' : 'USER_ROLE_REMOVE',
|
||||
entityType: 'User',
|
||||
entityId: u.id,
|
||||
detailsJson: {
|
||||
addRole: input.addRole,
|
||||
removeRole: input.removeRole,
|
||||
before: u.roles,
|
||||
after: nextArr,
|
||||
},
|
||||
ipAddress: ctx.ip,
|
||||
userAgent: ctx.userAgent,
|
||||
})
|
||||
|
||||
if (isFreshMentor) newlyMentor.push(u)
|
||||
updated++
|
||||
}
|
||||
|
||||
// Send onboarding emails outside the per-user mutation loop. Errors
|
||||
// caught and logged — we don't roll back the role grant if email
|
||||
// delivery fails.
|
||||
for (const u of newlyMentor) {
|
||||
try {
|
||||
await sendMentorOnboardingEmail(u.email, u.name)
|
||||
} catch (err) {
|
||||
console.error('[bulkUpdateRoles] mentor-onboarding email failed for', u.email, err)
|
||||
}
|
||||
}
|
||||
|
||||
return { updated, alreadyHadRole, total: targets.length }
|
||||
}),
|
||||
|
||||
/**
|
||||
* List applicant users with project info for admin bulk-invite page.
|
||||
*/
|
||||
@@ -2028,4 +2230,109 @@ export const userRouter = router({
|
||||
|
||||
return { ended: true }
|
||||
}),
|
||||
|
||||
/**
|
||||
* Context-aware default dashboard. Returns the highest-priority role for
|
||||
* which the user has actionable work right now, or the highest-priority
|
||||
* role they hold (static fallback) if nothing is actionable.
|
||||
*
|
||||
* Priority order: SUPER_ADMIN > PROGRAM_ADMIN > AWARD_MASTER > JURY_MEMBER >
|
||||
* MENTOR > APPLICANT > OBSERVER > AUDIENCE.
|
||||
*
|
||||
* Used by src/app/page.tsx to route users at login.
|
||||
*/
|
||||
getDefaultDashboard: protectedProcedure.query(async ({ ctx }) => {
|
||||
const user = await ctx.prisma.user.findUniqueOrThrow({
|
||||
where: { id: ctx.user.id },
|
||||
select: { id: true, roles: true, role: true },
|
||||
})
|
||||
|
||||
const userRoles = new Set<UserRole>([...(user.roles ?? []), user.role])
|
||||
|
||||
type Entry = { role: UserRole; path: string; predicate: () => Promise<boolean> | boolean }
|
||||
const PRIORITY: Entry[] = [
|
||||
{ role: 'SUPER_ADMIN', path: '/admin', predicate: () => true },
|
||||
{ role: 'PROGRAM_ADMIN', path: '/admin', predicate: () => true },
|
||||
{
|
||||
role: 'AWARD_MASTER',
|
||||
path: '/award-master',
|
||||
predicate: async () => {
|
||||
const cnt = await ctx.prisma.awardJuror.count({ where: { userId: user.id } })
|
||||
return cnt > 0
|
||||
},
|
||||
},
|
||||
{
|
||||
role: 'JURY_MEMBER',
|
||||
path: '/jury',
|
||||
predicate: async () => {
|
||||
const cnt = await ctx.prisma.assignment.count({
|
||||
where: {
|
||||
userId: user.id,
|
||||
isCompleted: false,
|
||||
round: { status: 'ROUND_ACTIVE' },
|
||||
},
|
||||
})
|
||||
return cnt > 0
|
||||
},
|
||||
},
|
||||
{
|
||||
role: 'MENTOR',
|
||||
path: '/mentor',
|
||||
predicate: async () => {
|
||||
const cnt = await ctx.prisma.mentorAssignment.count({
|
||||
where: {
|
||||
mentorId: user.id,
|
||||
workspaceEnabled: true,
|
||||
project: {
|
||||
projectRoundStates: {
|
||||
some: { round: { status: 'ROUND_ACTIVE' } },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return cnt > 0
|
||||
},
|
||||
},
|
||||
{
|
||||
role: 'APPLICANT',
|
||||
path: '/applicant',
|
||||
predicate: async () => {
|
||||
const cnt = await ctx.prisma.teamMember.count({
|
||||
where: {
|
||||
userId: user.id,
|
||||
project: {
|
||||
projectRoundStates: {
|
||||
some: {
|
||||
round: { status: 'ROUND_ACTIVE' },
|
||||
state: { in: ['PENDING', 'IN_PROGRESS'] },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
return cnt > 0
|
||||
},
|
||||
},
|
||||
{ role: 'OBSERVER', path: '/observer', predicate: () => false },
|
||||
{ role: 'AUDIENCE', path: '/applicant', predicate: () => false },
|
||||
]
|
||||
|
||||
// Walk priority. Return first role the user holds whose predicate is true.
|
||||
for (const entry of PRIORITY) {
|
||||
if (!userRoles.has(entry.role)) continue
|
||||
const has = await entry.predicate()
|
||||
if (has) {
|
||||
return { role: entry.role, path: entry.path, reason: 'has-active-work' as const }
|
||||
}
|
||||
}
|
||||
|
||||
// Static fallback: highest-priority role they hold.
|
||||
for (const entry of PRIORITY) {
|
||||
if (userRoles.has(entry.role)) {
|
||||
return { role: entry.role, path: entry.path, reason: 'static-fallback' as const }
|
||||
}
|
||||
}
|
||||
|
||||
return { role: 'APPLICANT' as UserRole, path: '/applicant', reason: 'static-fallback' as const }
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -14,6 +14,12 @@ import { getOpenAI, getConfiguredModel, buildCompletionParams } from '@/lib/open
|
||||
import { logAIUsage, extractTokenUsage } from '@/server/utils/ai-usage'
|
||||
import { classifyAIError, logAIError } from './ai-errors'
|
||||
import { extractMultipleFileContents } from './file-content-extractor'
|
||||
import {
|
||||
toProjectWithRelations,
|
||||
anonymizeProjectsForAI,
|
||||
validateAnonymizedProjects,
|
||||
sanitizeText,
|
||||
} from './anonymization'
|
||||
import type { PrismaClient, CompetitionCategory } from '@prisma/client'
|
||||
|
||||
// ─── Types ──────────────────────────────────────────────────────────────────
|
||||
@@ -166,8 +172,15 @@ async function generateCategoryShortlist(
|
||||
}
|
||||
}
|
||||
|
||||
// Aggregate scores per project
|
||||
const projectSummaries = projects.map((project: any) => {
|
||||
// Aggregate per-project stats and free-text feedback. Sanitize feedback
|
||||
// before it enters the prompt — sanitizeText strips email/phone/url/ssn
|
||||
// patterns embedded in juror free-text. Without this, juror feedback like
|
||||
// "Contact applicant Jane at jane@example.com" leaks PII to OpenAI.
|
||||
const aggregatesByProjectId = new Map<
|
||||
string,
|
||||
{ avgScore: number; evaluationCount: number; feedbackSamples: string[] }
|
||||
>()
|
||||
for (const project of projects as any[]) {
|
||||
const evaluations = project.assignments
|
||||
.map((a: any) => a.evaluation)
|
||||
.filter(Boolean)
|
||||
@@ -178,40 +191,86 @@ async function generateCategoryShortlist(
|
||||
? scores.reduce((sum: number, s: number) => sum + s, 0) / scores.length
|
||||
: 0
|
||||
|
||||
const feedbacks = evaluations
|
||||
const feedbackSamples = evaluations
|
||||
.map((e: any) => e.feedbackGeneral || e.feedbackText)
|
||||
.filter(Boolean)
|
||||
.filter((t: unknown): t is string => typeof t === 'string' && t.length > 0)
|
||||
.slice(0, 3)
|
||||
.map((t: string) => sanitizeText(t).slice(0, 1000))
|
||||
|
||||
return {
|
||||
id: project.id,
|
||||
description: project.description,
|
||||
category: project.competitionCategory,
|
||||
tags: project.projectTags.map((pt: any) => pt.tag.name),
|
||||
aggregatesByProjectId.set(project.id, {
|
||||
avgScore,
|
||||
evaluationCount: evaluations.length,
|
||||
feedbackSamples: feedbacks.slice(0, 3),
|
||||
files: (project.files || []).map((f: any) => ({
|
||||
file_type: f.fileType ?? 'OTHER',
|
||||
page_count: f.pageCount ?? null,
|
||||
size_kb: Math.round((f.size ?? 0) / 1024),
|
||||
round_name: f.roundId ? (roundNames.get(f.roundId) || null) : null,
|
||||
is_current_round: f.roundId === roundId,
|
||||
...(fileContents?.get(f.id) ? { text_content: fileContents.get(f.id) } : {}),
|
||||
feedbackSamples,
|
||||
})
|
||||
}
|
||||
|
||||
// Route every project through the canonical anonymization pipeline so
|
||||
// description/title/institution are PII-stripped, free-text is truncated,
|
||||
// and file text_content is sanitized (handled in anonymizeProjectForAI).
|
||||
const projectsWithRelations = (projects as any[]).map((p) =>
|
||||
toProjectWithRelations({
|
||||
id: p.id,
|
||||
title: p.title,
|
||||
description: p.description,
|
||||
competitionCategory: p.competitionCategory,
|
||||
oceanIssue: p.oceanIssue ?? null,
|
||||
country: p.country ?? null,
|
||||
geographicZone: p.geographicZone ?? null,
|
||||
institution: p.institution ?? null,
|
||||
tags: (p.projectTags ?? []).map((pt: any) => pt.tag.name),
|
||||
foundedAt: p.foundedAt ?? null,
|
||||
wantsMentorship: p.wantsMentorship ?? false,
|
||||
submissionSource: p.submissionSource ?? 'MANUAL',
|
||||
submittedAt: p.submittedAt ?? null,
|
||||
_count: { teamMembers: p.teamMembers?.length ?? 0, files: p.files?.length ?? 0 },
|
||||
files: (p.files ?? []).map((f: any) => ({
|
||||
fileType: f.fileType ?? null,
|
||||
size: f.size,
|
||||
pageCount: f.pageCount,
|
||||
roundName: f.roundId ? roundNames.get(f.roundId) : undefined,
|
||||
isCurrentRound: f.roundId === roundId,
|
||||
textContent: fileContents?.get(f.id),
|
||||
})),
|
||||
}),
|
||||
)
|
||||
|
||||
const { anonymized: anonymizedBase, mappings } = anonymizeProjectsForAI(
|
||||
projectsWithRelations,
|
||||
'FILTERING',
|
||||
)
|
||||
|
||||
if (!validateAnonymizedProjects(anonymizedBase)) {
|
||||
console.error('[AI Shortlist] Anonymization validation failed')
|
||||
return {
|
||||
recommendations: [],
|
||||
tokensUsed: 0,
|
||||
errors: ['GDPR compliance check failed: PII detected in anonymized data'],
|
||||
}
|
||||
}
|
||||
|
||||
// Merge anonymized base with per-project aggregates, keyed by mapping order.
|
||||
// Use the same anonymousId scheme the AI prompt expects.
|
||||
const anonymized = anonymizedBase.map((p, index) => {
|
||||
const realId = mappings[index].realId
|
||||
const agg = aggregatesByProjectId.get(realId) ?? {
|
||||
avgScore: 0,
|
||||
evaluationCount: 0,
|
||||
feedbackSamples: [],
|
||||
}
|
||||
return {
|
||||
anonymousId: `PROJECT_${String(index + 1).padStart(3, '0')}`,
|
||||
...p,
|
||||
project_id: undefined,
|
||||
avgScore: agg.avgScore,
|
||||
evaluationCount: agg.evaluationCount,
|
||||
feedbackSamples: agg.feedbackSamples,
|
||||
}
|
||||
})
|
||||
|
||||
// Anonymize for AI
|
||||
const anonymized = projectSummaries.map((p: any, index: number) => ({
|
||||
anonymousId: `PROJECT_${String(index + 1).padStart(3, '0')}`,
|
||||
...p,
|
||||
id: undefined,
|
||||
}))
|
||||
|
||||
// Build idMap for de-anonymization
|
||||
const idMap = new Map<string, string>()
|
||||
projectSummaries.forEach((p: any, index: number) => {
|
||||
idMap.set(`PROJECT_${String(index + 1).padStart(3, '0')}`, p.id)
|
||||
mappings.forEach((m, index) => {
|
||||
idMap.set(`PROJECT_${String(index + 1).padStart(3, '0')}`, m.realId)
|
||||
})
|
||||
|
||||
// Call AI
|
||||
|
||||
@@ -353,7 +353,9 @@ export function anonymizeProjectForAI(
|
||||
...(f.langConfidence != null ? { lang_confidence: f.langConfidence } : {}),
|
||||
...(f.roundName ? { round_name: f.roundName } : {}),
|
||||
...(f.isCurrentRound !== undefined ? { is_current_round: f.isCurrentRound } : {}),
|
||||
...(f.textContent ? { text_content: f.textContent } : {}),
|
||||
// Strip PII patterns (email/phone/url/ssn) from extracted file text
|
||||
// before it leaves the trust boundary to OpenAI.
|
||||
...(f.textContent ? { text_content: sanitizeText(f.textContent) } : {}),
|
||||
})) ?? [],
|
||||
wants_mentorship: project.wantsMentorship ?? false,
|
||||
submission_source: project.submissionSource,
|
||||
|
||||
153
src/server/services/finalist-confirmation.ts
Normal file
153
src/server/services/finalist-confirmation.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import type { CompetitionCategory, PrismaClient } from '@prisma/client'
|
||||
import { signFinalistToken } from '@/lib/finalist-token'
|
||||
import { sendFinalistConfirmationEmail } from '@/lib/email'
|
||||
import { logAudit } from '@/server/utils/audit'
|
||||
|
||||
type AnyPrisma = Pick<PrismaClient, 'finalistConfirmation' | 'waitlistEntry' | 'project'>
|
||||
|
||||
/**
|
||||
* Create a PENDING FinalistConfirmation row with a signed token. Caller is
|
||||
* responsible for sending the notification email separately.
|
||||
*/
|
||||
export async function createPendingConfirmation(
|
||||
prisma: Pick<PrismaClient, 'finalistConfirmation'>,
|
||||
args: {
|
||||
projectId: string
|
||||
category: CompetitionCategory
|
||||
windowHours: number
|
||||
promotedFromWaitlistEntryId?: string
|
||||
},
|
||||
): Promise<{ id: string; token: string; deadline: Date }> {
|
||||
const deadline = new Date(Date.now() + args.windowHours * 3_600_000)
|
||||
// Generate the row ID up front so we can sign it into the token before
|
||||
// writing the row (token is unique-indexed; embedding the ID gives the
|
||||
// public verify path a stable lookup key).
|
||||
const id = `cmfc_${Math.random().toString(36).slice(2, 10)}_${Date.now().toString(36)}`
|
||||
const token = signFinalistToken({
|
||||
confirmationId: id,
|
||||
exp: Math.floor(deadline.getTime() / 1000),
|
||||
})
|
||||
await prisma.finalistConfirmation.create({
|
||||
data: {
|
||||
id,
|
||||
projectId: args.projectId,
|
||||
category: args.category,
|
||||
status: 'PENDING',
|
||||
deadline,
|
||||
token,
|
||||
promotedFromWaitlistEntryId: args.promotedFromWaitlistEntryId ?? null,
|
||||
},
|
||||
})
|
||||
return { id, token, deadline }
|
||||
}
|
||||
|
||||
/**
|
||||
* Promote the lowest-ranked WAITING waitlist entry in the given category to
|
||||
* PROMOTED, create a fresh PENDING confirmation for the project, and send
|
||||
* the notification email. No-op if no WAITING entry exists.
|
||||
*/
|
||||
export async function promoteNextWaitlistEntry(
|
||||
prisma: AnyPrisma,
|
||||
args: { programId: string; category: CompetitionCategory; windowHours: number },
|
||||
): Promise<{ promoted: boolean; entryId?: string; confirmationId?: string }> {
|
||||
const entry = await prisma.waitlistEntry.findFirst({
|
||||
where: {
|
||||
programId: args.programId,
|
||||
category: args.category,
|
||||
status: 'WAITING',
|
||||
},
|
||||
orderBy: { rank: 'asc' },
|
||||
})
|
||||
if (!entry) return { promoted: false }
|
||||
|
||||
await prisma.waitlistEntry.update({
|
||||
where: { id: entry.id },
|
||||
data: { status: 'PROMOTED' },
|
||||
})
|
||||
|
||||
const { id: confirmationId, token, deadline } = await createPendingConfirmation(prisma, {
|
||||
projectId: entry.projectId,
|
||||
category: args.category,
|
||||
windowHours: args.windowHours,
|
||||
promotedFromWaitlistEntryId: entry.id,
|
||||
})
|
||||
|
||||
// Send email — log and continue on failure.
|
||||
const project = await prisma.project.findUnique({
|
||||
where: { id: entry.projectId },
|
||||
select: {
|
||||
title: true,
|
||||
teamMembers: {
|
||||
where: { role: 'LEAD' },
|
||||
take: 1,
|
||||
select: { user: { select: { email: true, name: true } } },
|
||||
},
|
||||
},
|
||||
})
|
||||
const lead = project?.teamMembers[0]?.user
|
||||
if (lead?.email && project) {
|
||||
const baseUrl = (process.env.NEXTAUTH_URL ?? 'http://localhost:3000').replace(/\/$/, '')
|
||||
const confirmUrl = `${baseUrl}/finalist/confirm/${token}`
|
||||
try {
|
||||
await sendFinalistConfirmationEmail(
|
||||
lead.email,
|
||||
lead.name ?? null,
|
||||
project.title,
|
||||
deadline,
|
||||
confirmUrl,
|
||||
)
|
||||
} catch (err) {
|
||||
console.error(
|
||||
`[promoteNextWaitlistEntry] failed to send email for project ${entry.projectId}:`,
|
||||
err,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return { promoted: true, entryId: entry.id, confirmationId }
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron entrypoint: find every PENDING confirmation past its deadline, mark
|
||||
* each EXPIRED, and promote the next waitlist entry per affected category.
|
||||
*/
|
||||
export async function expirePendingPastDeadline(
|
||||
prisma: PrismaClient,
|
||||
): Promise<{ expired: number; promoted: number }> {
|
||||
const expired = await prisma.finalistConfirmation.findMany({
|
||||
where: { status: 'PENDING', deadline: { lt: new Date() } },
|
||||
include: { project: { select: { programId: true } } },
|
||||
})
|
||||
let promoted = 0
|
||||
for (const c of expired) {
|
||||
await prisma.finalistConfirmation.update({
|
||||
where: { id: c.id },
|
||||
data: { status: 'EXPIRED', expiredAt: new Date() },
|
||||
})
|
||||
await logAudit({
|
||||
prisma,
|
||||
action: 'FINALIST_EXPIRED',
|
||||
entityType: 'FinalistConfirmation',
|
||||
entityId: c.id,
|
||||
detailsJson: { projectId: c.projectId, category: c.category },
|
||||
})
|
||||
// Resolve windowHours for this program's grand-finale round
|
||||
const round = await prisma.round.findFirst({
|
||||
where: {
|
||||
competition: { programId: c.project.programId },
|
||||
roundType: 'LIVE_FINAL',
|
||||
},
|
||||
orderBy: { sortOrder: 'desc' },
|
||||
select: { configJson: true },
|
||||
})
|
||||
const cfg = (round?.configJson ?? {}) as { confirmationWindowHours?: number }
|
||||
const windowHours = cfg.confirmationWindowHours ?? 24
|
||||
const result = await promoteNextWaitlistEntry(prisma, {
|
||||
programId: c.project.programId,
|
||||
category: c.category,
|
||||
windowHours,
|
||||
})
|
||||
if (result.promoted) promoted++
|
||||
}
|
||||
return { expired: expired.length, promoted }
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user