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
6 changes: 6 additions & 0 deletions lib/db_actions/props.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import * as dbHelpers from "@/lib/db-helpers";

// Mock dependencies
vi.mock("server-only", () => ({}));

vi.mock("@/lib/pubsub/client", () => ({
publishEvent: vi.fn().mockResolvedValue("msg-mock"),
}));

vi.mock("@/lib/get-user", () => ({
getUserFromCookies: vi.fn(),
}));
Expand Down Expand Up @@ -395,7 +401,7 @@
const forecastDate = new Date(Date.now() + 86400000 * 3);
const resolutionDate = new Date(Date.now() + 86400000 * 7);

let selectCallCount = 0;

Check warning on line 404 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
51 changes: 51 additions & 0 deletions lib/db_actions/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from "@/lib/server-action-result";
import { logger } from "@/lib/logger";
import { withRLS, withRLSAction } from "@/lib/db-helpers";
import { publishEvent } from "@/lib/pubsub/client";

export async function getPropById(
propId: number,
Expand Down Expand Up @@ -488,6 +489,15 @@ export async function createProp({
revalidatePath("/props");
if (prop.competition_id) {
revalidatePath(`/competitions/${prop.competition_id}`);

// Notify competition members about the new prop (fire-and-forget)
notifyPropCreated(currentUser.id, prop.competition_id, prop.text).catch(
(err) => {
logger.error("Failed to publish prop.created event", err as Error, {
competitionId: prop.competition_id,
});
},
);
}
}

Expand Down Expand Up @@ -593,3 +603,44 @@ export async function deleteProp({
return error("Failed to delete prop", ERROR_CODES.DATABASE_ERROR);
}
}

async function notifyPropCreated(
creatorUserId: number,
competitionId: number,
propText: string,
): Promise<void> {
const comp = await withRLS(undefined, async (trx) => {
return trx
.selectFrom("competitions")
.select("name")
.where("id", "=", competitionId)
.executeTakeFirst();
});

if (!comp) return;

const members = await withRLS(undefined, async (trx) => {
return trx
.selectFrom("competition_members")
.innerJoin("users", "users.id", "competition_members.user_id")
.select(["users.name", "users.email"])
.where("competition_members.competition_id", "=", competitionId)
.where("users.id", "!=", creatorUserId)
.execute();
});

if (members.length === 0) return;

await publishEvent({
event_type: "prop.created",
source: "forecasting",
timestamp: new Date().toISOString(),
notify: members.map((m) => ({ email: m.email, name: m.name })),
notify_link: `${process.env.APP_BASE_URL}/competitions/${competitionId}`,
data: {
competition_name: comp.name,
competition_id: competitionId,
prop_text: propText,
},
});
}
Loading