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
30 changes: 27 additions & 3 deletions app/api/users/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import { z } from "zod";
import { auth } from "@/auth";
import { createUser, getUserByEmail, getUsers } from "@/services/user";

// Validation for new-user creation. ucsdId is optional because Google OAuth
// can't give us a real PID users can fill it in later from their profile.
// Validation for new-user creation. ucsdId is required and must be unique
// (Google OAuth doesn't give us a real PID, so users enter it at signup).
const userCreateSchema = z.object({
ucsdId: z.string().min(1).optional(),
ucsdId: z
.string()
.trim()
.regex(/^[A-Za-z][0-9]{8}$/, "UCSD PID must be 1 letter + 8 digits"),
email: z
.string()
.email()
Expand All @@ -27,6 +30,11 @@ const userCreateSchema = z.object({
last: z.string().min(1),
}),
role: z.enum(["PI", "LAB_MANAGER", "RESEARCHER", "VIEWER"]),
profile: z.object({
pronouns: z.string().max(50).optional(),
phone: z.string().max(30).optional(),
description: z.string().min(1, "Description is required").max(500),
}),
});

/**
Expand Down Expand Up @@ -98,6 +106,22 @@ export async function POST(request: Request) {
});
return NextResponse.json(newUser, { status: 201 });
} catch (err) {
// Surface duplicate-key collisions (e.g. ucsdId already taken) as 409
// instead of a generic 500.
if (
typeof err === "object" &&
err !== null &&
"code" in err &&
(err as { code?: number }).code === 11000
) {
const field =
Object.keys((err as { keyPattern?: Record<string, unknown> })
.keyPattern ?? {})[0] ?? "field";
return NextResponse.json(
{ message: `That ${field} is already in use.` },
{ status: 409 },
);
}
console.error(err);
return NextResponse.json(
{ message: "Internal server error" },
Expand Down
90 changes: 90 additions & 0 deletions app/onboarding/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export default function OnboardingPage() {

const [firstName, setFirstName] = useState("");
const [lastName, setLastName] = useState("");
const [ucsdId, setUcsdId] = useState("");
const [role, setRole] = useState<Role | "">("");
const [pronouns, setPronouns] = useState("");
const [phone, setPhone] = useState("");
const [description, setDescription] = useState("");
const [submitting, setSubmitting] = useState(false);
const [success, setSuccess] = useState(false);
const [error, setError] = useState<string | null>(null);
Expand Down Expand Up @@ -95,6 +99,14 @@ export default function OnboardingPage() {
setError("First and last name are required.");
return;
}
if (!/^[A-Za-z][0-9]{8}$/.test(ucsdId.trim())) {
setError("UCSD PID must be 1 letter followed by 8 digits (e.g. A12345678).");
return;
}
if (!description.trim()) {
setError("Please add a short description about yourself.");
return;
}

setSubmitting(true);
try {
Expand All @@ -103,11 +115,17 @@ export default function OnboardingPage() {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: session.user.email,
ucsdId: ucsdId.trim().toUpperCase(),
name: {
first: firstName.trim(),
last: lastName.trim(),
},
role,
profile: {
pronouns: pronouns.trim(),
phone: phone.trim(),
description: description.trim(),
},
}),
});

Expand Down Expand Up @@ -187,6 +205,25 @@ export default function OnboardingPage() {
</div>
</div>

<div className="space-y-1.5">
<label className="block text-sm font-medium text-gray-700">
UCSD PID
</label>
<input
type="text"
value={ucsdId}
onChange={(e) =>
setUcsdId(e.target.value.toUpperCase())
}
placeholder="A12345678"
maxLength={9}
required
pattern="[A-Za-z][0-9]{8}"
title="1 letter followed by 8 digits"
className="block w-full px-3 py-2.5 rounded-md bg-[#f3f4f6] text-gray-900 focus:bg-white focus:ring-2 focus:ring-[#ea7032]/50 outline-none transition-colors sm:text-sm font-mono tracking-wider"
/>
</div>

<div className="space-y-1.5">
<label className="block text-sm font-medium text-gray-700">
Your role in the lab
Expand All @@ -208,6 +245,59 @@ export default function OnboardingPage() {
</select>
</div>

<div className="grid grid-cols-2 gap-3">
<div className="space-y-1.5">
<label className="block text-sm font-medium text-gray-700">
Pronouns{" "}
<span className="text-gray-400 font-normal">
(optional)
</span>
</label>
<input
type="text"
value={pronouns}
onChange={(e) => setPronouns(e.target.value)}
placeholder="she/her, he/him, they/them…"
maxLength={50}
className="block w-full px-3 py-2.5 rounded-md bg-[#f3f4f6] text-gray-900 focus:bg-white focus:ring-2 focus:ring-[#ea7032]/50 outline-none transition-colors sm:text-sm"
/>
</div>
<div className="space-y-1.5">
<label className="block text-sm font-medium text-gray-700">
Phone number{" "}
<span className="text-gray-400 font-normal">
(optional)
</span>
</label>
<input
type="tel"
value={phone}
onChange={(e) => setPhone(e.target.value)}
placeholder="(555) 123-4567"
maxLength={30}
className="block w-full px-3 py-2.5 rounded-md bg-[#f3f4f6] text-gray-900 focus:bg-white focus:ring-2 focus:ring-[#ea7032]/50 outline-none transition-colors sm:text-sm"
/>
</div>
</div>

<div className="space-y-1.5">
<label className="block text-sm font-medium text-gray-700">
About you
</label>
<textarea
value={description}
onChange={(e) => setDescription(e.target.value)}
placeholder="A short description — research interests, what you work on, etc."
maxLength={500}
rows={3}
required
className="block w-full px-3 py-2.5 rounded-md bg-[#f3f4f6] text-gray-900 focus:bg-white focus:ring-2 focus:ring-[#ea7032]/50 outline-none transition-colors sm:text-sm resize-none"
/>
<p className="text-[11px] text-gray-400 text-right">
{description.length}/500
</p>
</div>

{error ? (
<p className="text-sm text-red-600">{error}</p>
) : null}
Expand Down
10 changes: 7 additions & 3 deletions models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ export interface IUser extends Document {
lastReviewedAt: Date;
};
profile: {
title: string;
department: string;
phone: string;
title?: string;
department?: string;
phone?: string;
pronouns?: string;
description?: string;
}
status: Status;
createdAt: Date;
Expand Down Expand Up @@ -86,6 +88,8 @@ const userSchema = new Schema<IUser>({
title: { type: String },
department: { type: String },
phone: { type: String },
pronouns: { type: String },
description: { type: String },
},
status: {
type: String,
Expand Down
Loading