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
16 changes: 12 additions & 4 deletions components/forms/create-edit-prop-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ export const propFormSchema = z
message: "Props associated with a competition must be public.",
path: ["user_id"],
})
.refine((data) => !(data.user_id === null && data.category_id === null), {
message: "Public props must have a category",
path: ["category_id"],
});
.refine(
(data) =>
!(
data.user_id === null &&
data.category_id === null &&
data.competition_id === null
),
{
message: "Public props must have a category",
path: ["category_id"],
},
);

export type PropFormValues = z.infer<typeof propFormSchema>;

Expand Down
67 changes: 56 additions & 11 deletions lib/db_actions/props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,22 @@
expect(result.success).toBe(true);
});

it("should allow competition props without category", async () => {
it("should allow private competition props without category", async () => {
vi.mocked(getUser.getUserFromCookies).mockResolvedValue(mockUser as any);

const mockTrx = {
selectFrom: vi.fn().mockReturnThis(),
select: vi.fn().mockReturnThis(),
where: vi.fn().mockReturnThis(),
executeTakeFirst: vi.fn().mockResolvedValue({ is_private: false }),
selectFrom: vi.fn().mockImplementation(() => {
return {
select: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
executeTakeFirst: vi.fn().mockResolvedValue({ role: "admin" }),
}),
executeTakeFirst: vi.fn().mockResolvedValue({ is_private: true }),
}),
}),
};
}),
insertInto: vi.fn().mockReturnThis(),
values: vi.fn().mockReturnThis(),
execute: vi.fn().mockResolvedValue(undefined),
Expand All @@ -221,16 +229,45 @@

const result = await createProp({
prop: {
text: "This is a competition proposition",
text: "This is a private competition proposition",
category_id: null,
competition_id: 1, // Competition prop
competition_id: 1,
user_id: null,
},
});

expect(result.success).toBe(true);
});

it("should require category for public competition props", async () => {
vi.mocked(getUser.getUserFromCookies).mockResolvedValue(mockUser as any);

const mockTrx = {
selectFrom: vi.fn().mockReturnThis(),
select: vi.fn().mockReturnThis(),
where: vi.fn().mockReturnThis(),
executeTakeFirst: vi.fn().mockResolvedValue({ is_private: false }),
};

vi.mocked(dbHelpers.withRLSAction).mockImplementation(async (userId, fn) => {
return fn(mockTrx as any);
});

const result = await createProp({
prop: {
text: "This is a public competition proposition",
category_id: null,
competition_id: 1,
user_id: null,
},
});

expect(result.success).toBe(false);
if (!result.success) {
expect(result.code).toBe("VALIDATION_ERROR");
}
});

describe("date validation", () => {
it("should reject forecast deadline in the past", async () => {
vi.mocked(getUser.getUserFromCookies).mockResolvedValue(mockUser as any);
Expand Down Expand Up @@ -315,10 +352,18 @@
const resolutionDate = new Date(Date.now() + 86400000 * 7); // 7 days from now

const mockTrx = {
selectFrom: vi.fn().mockReturnThis(),
select: vi.fn().mockReturnThis(),
where: vi.fn().mockReturnThis(),
executeTakeFirst: vi.fn().mockResolvedValue({ is_private: false }),
selectFrom: vi.fn().mockImplementation(() => {
return {
select: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
where: vi.fn().mockReturnValue({
executeTakeFirst: vi.fn().mockResolvedValue({ role: "admin" }),
}),
executeTakeFirst: vi.fn().mockResolvedValue({ is_private: true }),
}),
}),
};
}),
insertInto: vi.fn().mockReturnThis(),
values: vi.fn().mockReturnThis(),
execute: vi.fn().mockResolvedValue(undefined),
Expand Down Expand Up @@ -350,7 +395,7 @@
const forecastDate = new Date(Date.now() + 86400000 * 3);
const resolutionDate = new Date(Date.now() + 86400000 * 7);

let selectCallCount = 0;

Check warning on line 398 in lib/db_actions/props.test.ts

View workflow job for this annotation

GitHub Actions / PR Checks

'selectCallCount' is assigned a value but never used
const mockTrx = {
selectFrom: vi.fn().mockImplementation(() => {
selectCallCount++;
Expand Down
12 changes: 10 additions & 2 deletions lib/db_actions/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ export async function createProp({
];
}

// Category is only required for public props without a competition
// Personal props (user_id set) and competition props don't require a category
// Category is required for non-personal, non-competition props
if (prop.category_id == null && prop.user_id === null && prop.competition_id === null) {
validationErrors.category_id = ["Category is required"];
}
Expand Down Expand Up @@ -445,6 +444,15 @@ export async function createProp({
return error("Competition not found", ERROR_CODES.NOT_FOUND);
}

// Public competition props must have a category
if (!competition.is_private && prop.category_id == null) {
return validationError(
"Please fix the validation errors",
{ category_id: ["Category is required for public competition props"] },
ERROR_CODES.VALIDATION_ERROR,
);
}

// For private competitions, only admins can create props
if (competition.is_private) {
const membership = await trx
Expand Down
33 changes: 33 additions & 0 deletions migrations/1773712944723_relax-prop-category-constraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { Kysely } from "kysely";
import { sql } from "kysely";

export async function up(db: Kysely<any>): Promise<void> {
// Drop the old constraint that required user_id OR category_id.
await db.schema
.alterTable("props")
.dropConstraint("at_least_one_of_user_id_and_category_id")
.execute();
// Add a relaxed constraint: competition props are allowed to have no category.
// Application-level validation still enforces category for public competition props.
await db.schema
.alterTable("props")
.addCheckConstraint(
"at_least_one_of_user_id_category_id_competition_id",
sql<boolean>`user_id IS NOT NULL OR category_id IS NOT NULL OR competition_id IS NOT NULL`,
)
.execute();
}

export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable("props")
.dropConstraint("at_least_one_of_user_id_category_id_competition_id")
.execute();
await db.schema
.alterTable("props")
.addCheckConstraint(
"at_least_one_of_user_id_and_category_id",
sql<boolean>`user_id IS NOT NULL OR category_id IS NOT NULL`,
)
.execute();
}
Loading