Adds proposals + proposal_votes + policies + incidents + incident_updates
tables with full RLS, async voting flow, dismissal-appeal pathway,
versioned policies with provenance, and an anonymous-incident token mode.
Adds per-space configurable areas, financial visibility settings, member
self-profile + COI disclosure, and a mission statement that renders in
the app footer.
Reorganises lib/actions.ts into per-domain files (tasks, projects,
members, payments, contacts, knowledge-base, secrets, area-leads,
settings, imports, proposals, incidents, policies, areas) plus a barrel.
Adds lib/permissions.ts and lib/auth-helpers.ts so action bodies stop
duplicating the user-to-member-to-role check. Wires every action that has
a Zod schema; introduces a flexibleDateTime preprocessor so HTML date
pickers no longer hit "Invalid datetime".
Hardens RLS: drops NOT NULL on space_members.user_id for offline members,
adds a privilege-escalation trigger that blocks self-edits to role / tier
/ status / approved / has_card_access / space_id, closes four
cross-tenant holes in the governance kernel, and adds a partial unique
index so two admins cannot concurrently activate two drafts of the same
policy slug.
Ships every deployment artifact: Dockerfile, docker-compose,
.do/app.yaml, .env.example, /api/health, plus a managed-Supabase guide
(docs/DEPLOYMENT.md), an end-to-end self-hosted Droplet guide
(docs/DEPLOY_DO_SELFHOSTED.md), and a Supabase-CLI local-dev quickstart
(docs/LOCAL_DEV.md). Replaces middleware.ts with proxy.ts for Next.js 16.
Adds scripts/schema.sql as the canonical idempotent full deploy plus
fourteen numbered incremental migrations.
Adds scripts/seed.sql so a fresh local DB renders something instead of
empty pages: a demo space, five members across all roles, a v1 code of
conduct, three proposals in different states with seven votes already
cast, an open incident, a project, three tasks, a KB entry, and matching
activity-log rows.
Replaces v0-generated test scaffolding with 266 real tests across
__tests__/{actions,auth-helpers,components,governance,permissions,
security,utils,validations}.test.ts. Adds react-markdown + remark-gfm
for proposal / incident / policy body rendering. Deletes four dead v0
fix-* scripts, the unused components/app-shell.tsx, and the duplicate
app/auth/login/page.tsx.
Routes (31 total): /api/health, /api/paypal/sync, /auth/callback, /,
/login, /signup, /signup/confirm, /dashboard, /tasks, /projects,
/members, /payments, /financials, /contacts, /ops, /ops/[id], /ops/new,
/proposals, /proposals/[id], /proposals/new, /incidents, /incidents/[id],
/incidents/new, /policies, /policies/[slug], /policies/new, /profile,
/recruitment, /settings, /comms, /import.
Full pass-by-pass history is in docs/HANDOFF.md; the roadmap is in
docs/GOVERNANCE_FEATURES.md.
232 lines
7.0 KiB
TypeScript
232 lines
7.0 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import {
|
|
requireMember,
|
|
requireMemberWithRole,
|
|
logActivity,
|
|
parseInput,
|
|
} from '@/lib/auth-helpers'
|
|
import { ADMIN_ROLES } from '@/lib/permissions'
|
|
import {
|
|
createProposalSchema,
|
|
openProposalSchema,
|
|
castVoteSchema,
|
|
decideProposalSchema,
|
|
withdrawProposalSchema,
|
|
uuidSchema,
|
|
} from '@/lib/validations'
|
|
|
|
/**
|
|
* Create a proposal. Defaults to draft status. Pass `open_immediately: true`
|
|
* to skip the draft step and go straight to voting (trigger computes quorum
|
|
* and voting window from space defaults).
|
|
*/
|
|
export async function createProposal(formData: {
|
|
title: string
|
|
body?: string
|
|
proposal_type?: string
|
|
threshold?: string
|
|
policy_ref_id?: string | null
|
|
parent_incident_id?: string | null
|
|
voting_opens_at?: string | null
|
|
voting_closes_at?: string | null
|
|
open_immediately?: boolean
|
|
}) {
|
|
const v = parseInput(createProposalSchema, formData)
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const status = v.data.open_immediately ? 'open' : 'draft'
|
|
|
|
const { data, error } = await supabase
|
|
.from('proposals')
|
|
.insert({
|
|
space_id: member.space_id,
|
|
proposer_id: member.id,
|
|
proposer_name: member.display_name,
|
|
title: v.data.title,
|
|
body: v.data.body,
|
|
proposal_type: v.data.proposal_type,
|
|
threshold: v.data.threshold ?? 'simple_majority',
|
|
policy_ref_id: v.data.policy_ref_id ?? null,
|
|
parent_incident_id: v.data.parent_incident_id ?? null,
|
|
voting_opens_at: v.data.voting_opens_at ?? null,
|
|
voting_closes_at: v.data.voting_closes_at ?? null,
|
|
status,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'created', 'proposal', data.id, v.data.title)
|
|
|
|
revalidatePath('/proposals')
|
|
return { data }
|
|
}
|
|
|
|
/**
|
|
* Move a draft proposal to open. Trigger computes quorum_required and the
|
|
* voting window from the space's defaults if voting_closes_at is null.
|
|
*/
|
|
export async function openProposal(proposalId: string, voting_closes_at?: string | null) {
|
|
const v = parseInput(openProposalSchema, { proposalId, voting_closes_at })
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const updates: Record<string, unknown> = { status: 'open' }
|
|
if (v.data.voting_closes_at) updates.voting_closes_at = v.data.voting_closes_at
|
|
|
|
const { error } = await supabase
|
|
.from('proposals')
|
|
.update(updates)
|
|
.eq('id', v.data.proposalId)
|
|
.eq('space_id', member.space_id)
|
|
.eq('status', 'draft')
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'opened', 'proposal', v.data.proposalId)
|
|
|
|
revalidatePath('/proposals')
|
|
revalidatePath(`/proposals/${v.data.proposalId}`)
|
|
return { success: true as const }
|
|
}
|
|
|
|
/**
|
|
* Withdraw a proposal. Proposer can withdraw their own (any status pre-decided);
|
|
* admin/board can withdraw any. Status becomes 'withdrawn'.
|
|
*/
|
|
export async function withdrawProposal(proposalId: string) {
|
|
const v = parseInput(withdrawProposalSchema, { proposalId })
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { error } = await supabase
|
|
.from('proposals')
|
|
.update({ status: 'withdrawn' })
|
|
.eq('id', v.data.proposalId)
|
|
.eq('space_id', member.space_id)
|
|
.in('status', ['draft', 'open'])
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'withdrew', 'proposal', v.data.proposalId)
|
|
|
|
revalidatePath('/proposals')
|
|
revalidatePath(`/proposals/${v.data.proposalId}`)
|
|
return { success: true as const }
|
|
}
|
|
|
|
/**
|
|
* Cast or update a vote. Upserts on (proposal_id, member_id). The DB-level
|
|
* trigger refreshes the proposal's tally on every change. Voting window is
|
|
* enforced by RLS, not here.
|
|
*/
|
|
export async function castVote(formData: {
|
|
proposalId: string
|
|
position: string
|
|
recusal_reason?: string | null
|
|
comment?: string | null
|
|
}) {
|
|
const v = parseInput(castVoteSchema, formData)
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMember(supabase)
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { error } = await supabase
|
|
.from('proposal_votes')
|
|
.upsert(
|
|
{
|
|
proposal_id: v.data.proposalId,
|
|
member_id: member.id,
|
|
position: v.data.position,
|
|
recusal_reason: v.data.recusal_reason ?? null,
|
|
comment: v.data.comment ?? null,
|
|
voted_at: new Date().toISOString(),
|
|
},
|
|
{ onConflict: 'proposal_id,member_id' },
|
|
)
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'voted', 'proposal', v.data.proposalId, v.data.position)
|
|
|
|
revalidatePath('/proposals')
|
|
revalidatePath(`/proposals/${v.data.proposalId}`)
|
|
return { success: true as const }
|
|
}
|
|
|
|
/**
|
|
* Decide a proposal: flip status to 'decided' and freeze the outcome. The
|
|
* tally is already maintained live by the refresh_proposal_tally trigger;
|
|
* this just commits the decision and stamps decided_at.
|
|
*/
|
|
export async function decideProposal(proposalId: string) {
|
|
const v = parseInput(decideProposalSchema, { proposalId })
|
|
if (!v.ok) return { error: v.error }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ADMIN_ROLES, 'Admin or board access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { error } = await supabase
|
|
.from('proposals')
|
|
.update({ status: 'decided', decided_at: new Date().toISOString() })
|
|
.eq('id', v.data.proposalId)
|
|
.eq('space_id', member.space_id)
|
|
.eq('status', 'open')
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'decided', 'proposal', v.data.proposalId)
|
|
|
|
revalidatePath('/proposals')
|
|
revalidatePath(`/proposals/${v.data.proposalId}`)
|
|
return { success: true as const }
|
|
}
|
|
|
|
/**
|
|
* Delete a proposal. Admin/board only. Use sparingly; prefer withdraw.
|
|
*/
|
|
export async function deleteProposal(proposalId: string) {
|
|
const v = parseInput(uuidSchema, proposalId)
|
|
if (!v.ok) return { error: 'Invalid proposal ID' }
|
|
|
|
const supabase = await createClient()
|
|
const auth = await requireMemberWithRole(supabase, ADMIN_ROLES, 'Admin or board access required')
|
|
if (!auth.ok) return { error: auth.error }
|
|
const { member } = auth
|
|
|
|
const { error } = await supabase
|
|
.from('proposals')
|
|
.delete()
|
|
.eq('id', v.data)
|
|
.eq('space_id', member.space_id)
|
|
|
|
if (error) return { error: error.message }
|
|
|
|
await logActivity(supabase, member, 'deleted', 'proposal', v.data)
|
|
|
|
revalidatePath('/proposals')
|
|
return { success: true as const }
|
|
}
|