Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app/(platform)/(ops)/admin/users/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { hasPermission, hasRole, Permission, permissionSummary, Role, readablePe
import { statusFor } from "@/lib/phase"
import db from "@/lib/prisma8"
import { getProgramSettings, isPhaseDecided } from "@/lib/program"
import { toManageDisputes } from "@/lib/project-directory"
import { getUserDetail } from "@/lib/queries/admin"
import { firstValues, type SearchParamValue } from "@/lib/search-params"
import { parseDbTimestamp } from "@/lib/time"
Expand Down Expand Up @@ -361,6 +362,7 @@ export default async function AdminUserPage({ params, searchParams }: Props) {
buildStatus: project.buildStatus,
designFunding: fundingByProject.get(project.id) ?? null,
designStatus: project.designStatus,
disputes: toManageDisputes(project.disputes),
extension: project.submissionExtensionUntil?.toISOString() ?? null,
grantSent: project.hcbGrantTopUps.length > 0,
// A grant is decided with the design, so one on an undecided project is an artefact of an import rather than money owed.
Expand Down
29 changes: 27 additions & 2 deletions src/app/components/admin/ProjectManagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ import { ActionButton } from "@/app/components/forms/ActionButton"
import { BomSyncForm } from "@/app/components/forms/BomSyncForm"
import { GrantForm } from "@/app/components/forms/GrantForm"
import { JournalHoursEditor, type JournalHoursEntry } from "@/app/components/forms/JournalHoursEditor"
import { DisputeThread } from "@/components/disputes/DisputeThread"
import { CREDIT_NAME_PLURAL } from "@/lib/config/program"
import type { Theme } from "@/lib/enums"
import { Phase, type Theme } from "@/lib/enums"
import type { PhaseDispute } from "@/lib/queries/project-page"

/** A maker's dispute on one of a project's hand-ins, with the phase it was raised in. */
export interface ProjectManageDispute {
dispute: PhaseDispute
phase: Phase
}

/** What a project's manage panel acts on. Both the projects list and a user's themed projects hand this to the same panel. */
export interface ProjectManageData {
buildStatus: string
designStatus: string
/** Every dispute on the project, oldest first. Only the projects list fills this, so the panel leaves the section out when it is absent. */
disputes?: ProjectManageDispute[]
/** ISO string, so the extension form edits the same instant the server holds. */
extension: string | null
grantSent: boolean
Expand Down Expand Up @@ -45,7 +55,8 @@ function canHandIn(status: string): boolean {
/** Which sections a project's panel would draw. A manage button is only offered when this is true. */
export function hasProjectActions(data: ProjectManageData, flags: ProjectManageFlags): boolean {
return (
flags.canExtend
(data.disputes?.length ?? 0) > 0
|| flags.canExtend
|| flags.canMoveTheme
|| (flags.canSendGrant && grantAvailable(data))
|| flags.canSyncBom
Expand Down Expand Up @@ -84,6 +95,20 @@ export function ProjectManageSections({

return (
<>
{data.disputes && data.disputes.length > 0 ? (
<ManageSection title="Disputes">
<div className="hl-stack">
{data.disputes.map(({ dispute, phase }) => (
<div className="hl-stack hl-stack--tight" key={dispute.id}>
<p className="hl-hint" style={{ marginTop: 0 }}>
{phase === Phase.DESIGN ? "Design" : "Build"} dispute
</p>
<DisputeThread dispute={dispute} surface="console" viewer="reviewer" />
</div>
))}
</div>
</ManageSection>
) : null}
{grantAction ? (
<ManageSection title="Send grant">
<GrantForm alreadySent={data.grantSent} amountUsd={data.grantUsd ?? 0} projectId={data.id} />
Expand Down
10 changes: 7 additions & 3 deletions src/components/disputes/DisputeThread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export function DisputeThread({
sendBackUrl,
}: Readonly<{
dispute: PhaseDispute
replyUrl: string
/** Where a reply is posted. Without it the thread is read-only, as the admin project list shows it. */
replyUrl?: string
viewer: "maker" | "reviewer"
/** Which world's kit draws the thread. The copy and controls are the same in both. */
surface?: "platform" | "console"
Expand All @@ -79,6 +80,7 @@ export function DisputeThread({
}

function reply() {
if (!replyUrl) return
if (!body.trim()) return refuse("Write a reply first.")
void run(
"reply",
Expand Down Expand Up @@ -110,7 +112,7 @@ export function DisputeThread({
))}
</ol>

{open ? (
{open && replyUrl ? (
<div className={skin.form}>
<label className={skin.label} htmlFor={`dispute-reply-${dispute.id}`}>
{viewer === "reviewer" ? "Reply to the maker" : "Add to this dispute"}
Expand Down Expand Up @@ -163,7 +165,9 @@ export function DisputeThread({
) : null}
</div>
</div>
) : (
) : null}
{open && !replyUrl ? <p className={skin.outcome}>Still open, waiting on a reviewer.</p> : null}
{open ? null : (
<p className={skin.outcome}>
{dispute.outcome === "upheld"
? `a reviewer sent this back for a fresh review on ${fmtDate(dispute.resolvedAt ?? dispute.at)}.`
Expand Down
26 changes: 24 additions & 2 deletions src/lib/project-directory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import "server-only"
import type { ProjectRowData } from "@/app/(platform)/(ops)/admin/projects/ProjectRow"
import type { ProjectManageFlags } from "@/app/components/admin/ProjectManagePanel"
import type { ProjectManageDispute, ProjectManageFlags } from "@/app/components/admin/ProjectManagePanel"
import { themeLabel } from "@/lib/config/program"
import { formatUtcDateTime } from "@/lib/deadline"
import { displayName } from "@/lib/display-name"
import { PhaseStatus, Theme } from "@/lib/enums"
import { hasPermission, hasRole, Permission, Role } from "@/lib/permissions"
import { statusFor } from "@/lib/phase"
import { isPhaseDecided } from "@/lib/program"
import type { ProjectDirectoryRow, ProjectFilters } from "@/lib/queries/admin"
import type { ProjectDirectoryDispute, ProjectDirectoryRow, ProjectFilters } from "@/lib/queries/admin"

/** How many projects one page of the admin list holds. */
export const PROJECT_PAGE_SIZE = 50
Expand Down Expand Up @@ -63,6 +63,27 @@ export function projectAccessFor(roles: Role[]): { flags: ProjectManageFlags; in
return { flags, includePii }
}

/** A project's disputes as its manage panel draws them: plain JSON, with epoch times and reviewer names worked out. */
export function toManageDisputes(disputes: ProjectDirectoryDispute[]): ProjectManageDispute[] {
return disputes.map(item => ({
dispute: {
at: item.createdAt.getTime(),
id: item.id,
messages: item.messages.map(message => ({
at: message.createdAt.getTime(),
body: message.body,
fromReviewer: message.fromReviewer,
id: message.id,
reviewerName: message.author ? displayName(message.author) : null,
})),
note: item.note,
outcome: item.outcome,
resolvedAt: item.resolvedAt?.getTime() ?? null,
},
phase: item.phase,
}))
}

/** One project as the list draws it. Everything is worked out on the server, so the payload is plain JSON. */
export function toProjectRow(project: ProjectDirectoryRow, now: Date): ProjectRowData {
const userExtension =
Expand All @@ -72,6 +93,7 @@ export function toProjectRow(project: ProjectDirectoryRow, now: Date): ProjectRo
return {
buildStatus: project.buildStatus,
designStatus: project.designStatus,
disputes: toManageDisputes(project.disputes),
extension: project.submissionExtensionUntil?.toISOString() ?? null,
extensionLabel: project.submissionExtensionUntil ? formatUtcDateTime(project.submissionExtensionUntil) : null,
githubRepo: project.githubRepo,
Expand Down
93 changes: 92 additions & 1 deletion src/lib/queries/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ export interface ProjectFilters {
export interface ProjectDirectoryRow {
buildStatus: Models.public_ThemeProject["buildStatus"]
designStatus: Models.public_ThemeProject["designStatus"]
/** The disputes raised on this project's hand-ins, oldest hand-in first. Empty for most projects. */
disputes: ProjectDirectoryDispute[]
githubRepo: string | null
grantSent: boolean
grantUsd: number | null
Expand Down Expand Up @@ -201,6 +203,65 @@ export interface ProjectDirectoryRow {
}[]
}

/** One maker's dispute on a hand-in, with its thread, as the project list needs it. */
export interface ProjectDirectoryDispute {
createdAt: Date
id: string
messages: {
author: { displayName: string | null; name: string | null } | null
body: string
createdAt: Date
fromReviewer: boolean
id: string
}[]
note: string
outcome: "upheld" | "stood" | null
phase: Phase
resolvedAt: Date | null
}

/** A hand-in with its dispute loaded, as the admin project reads select it. */
interface DisputeSubmissionRow {
dispute: {
createdAt: string
id: string
messages: {
author: { displayName: string | null; name: string | null } | null
body: string
createdAt: string
fromReviewer: boolean
id: string
}[]
note: string
outcome: ProjectDirectoryDispute["outcome"]
resolvedAt: string | null
} | null
phase: Phase
}

/** A project's disputes, one per hand-in that has one, with the database's timestamp text parsed. */
const projectDisputesOf = (submissions: DisputeSubmissionRow[]): ProjectDirectoryDispute[] =>
submissions.flatMap(({ dispute, phase }) => {
if (!dispute) return []
return [
{
createdAt: parseDbTimestamp(dispute.createdAt),
id: dispute.id,
messages: dispute.messages.map(message => ({
author: message.author,
body: message.body,
createdAt: parseDbTimestamp(message.createdAt),
fromReviewer: message.fromReviewer,
id: message.id,
})),
note: dispute.note,
outcome: dispute.outcome,
phase,
resolvedAt: parseNullableTimestamp(dispute.resolvedAt),
},
]
})

export async function listProjects(
opts: ProjectFilters & { cursor?: string; limit: number },
flags?: { includePii?: boolean },
Expand Down Expand Up @@ -290,6 +351,19 @@ export async function listProjects(
.where({ phase: "DESIGN" })
.where(topUp => topUp.sentAt.isNotNull()),
)
.include("submissions", submissions =>
submissions
.select("id", "phase")
.include("dispute", dispute =>
dispute.select("createdAt", "id", "note", "outcome", "resolvedAt").include("messages", messages =>
messages
.select("body", "createdAt", "fromReviewer", "id")
.include("author", author => author.select("displayName", "name"))
.orderBy(message => message.createdAt.asc()),
),
)
.orderBy(submission => submission.createdAt.asc()),
)
.include("user", user => user.select("displayName", "email", "id", "name", "slackId", "submissionExtensionUntil"))
.include("workSessions", sessions =>
sessions
Expand All @@ -304,6 +378,7 @@ export async function listProjects(
return {
buildStatus: row.buildStatus,
designStatus: row.designStatus,
disputes: projectDisputesOf(row.submissions),
githubRepo: row.githubRepo,
grantSent: row.hcbGrantTopUps.length > 0,
grantUsd: row.grantUsd,
Expand Down Expand Up @@ -369,6 +444,8 @@ export interface UserDetail {
designBankedCoins: number | null
designSpendableCoins: number | null
designStatus: Models.public_ThemeProject["designStatus"]
/** The disputes raised on this project's hand-ins, oldest hand-in first. */
disputes: ProjectDirectoryDispute[]
grantUsd: number | null
hcbGrantCards: { hcbCardGrantId: string | null; id: string; status: string }[]
hcbGrantTopUps: { id: string }[]
Expand Down Expand Up @@ -477,6 +554,19 @@ export async function getUserDetail(
.where({ phase: "DESIGN" })
.where(topUp => topUp.sentAt.isNotNull()),
)
.include("submissions", submissions =>
submissions
.select("id", "phase")
.include("dispute", dispute =>
dispute.select("createdAt", "id", "note", "outcome", "resolvedAt").include("messages", messages =>
messages
.select("body", "createdAt", "fromReviewer", "id")
.include("author", author => author.select("displayName", "name"))
.orderBy(message => message.createdAt.asc()),
),
)
.orderBy(submission => submission.createdAt.asc()),
)
.include("workSessions", sessions =>
sessions
.select("effectiveDate", "hoursApproved", "hoursClaimed", "id", "phase", "title")
Expand Down Expand Up @@ -512,8 +602,9 @@ export async function getUserDetail(
slackDisplayName: row.slackDisplayName,
slackId: row.slackId,
submissionExtensionUntil: parseNullableTimestamp(row.submissionExtensionUntil),
themeProjects: row.themeProjects.map(project => ({
themeProjects: row.themeProjects.map(({ submissions, ...project }) => ({
...project,
disputes: projectDisputesOf(submissions),
submissionExtensionUntil: parseNullableTimestamp(project.submissionExtensionUntil),
})),
verificationStatus: row.verificationStatus,
Expand Down
Loading