Files
TREK/server/src/db/repositories/CollabPolls.repository.ts
T
Maurice 724c88c201 style: format server and shared with prettier
A mechanical prettier --write over server/src, server/tests, server/scripts
and shared/src with the workspaces' .prettierrc; nothing but layout and
import order changes, and every file keeps the same set of imports. shared
is formatted throughout and its format baseline is empty. The server
baseline keeps the 35 files that Prettier would grow past their lint:size
limit (they need a split first, which is not a formatting change), and the
generated entity files stay as their generator writes them.
2026-10-10 21:23:37 +02:00

100 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { CollabPolls } from '../entities/CollabPolls.entity';
import type { DB } from '../kysely/db';
import { type AssertRowKeys } from './_shared/rows';
import { TrekRepository } from './_shared/trek-repository';
/** A bare `collab_polls` row — every scalar column of the entity, incl. the two `persist(false)` relation mirrors (`trip_id`, `user_id`). */
export interface CollabPollRow {
id: number;
trip_id: number;
user_id: number;
question: string;
options: string;
multiple: number | null;
closed: number | null;
deadline: string | null;
created_at: string | null;
}
const _collabPollRowKeys: AssertRowKeys<CollabPollRow, CollabPolls> = true;
/** CB23's joined projection — `p.*, u.username, u.avatar`. */
export interface CollabPollJoinRow extends CollabPollRow {
username: string;
avatar: string | null;
}
type CollabPollsKyselyDB = Pick<DB, 'collab_polls' | 'users'>;
/**
* `collab_polls` — decision polls. Kysely throughout: `trip_id`/`user_id`
* are `persist(false)` mirrors (the `TripFilesRepository` class docstring's
* trap), so a bare QueryBuilder `.select([...])` silently drops them.
*/
export class CollabPollsRepository extends TrekRepository<CollabPolls> {
/** CB23 (`getPollWithVotes`'s poll half) — `SELECT p.*, u.username, u.avatar FROM collab_polls p JOIN users u ON p.user_id = u.id WHERE p.id = ?`. `id: number`: the service resolves the route's `string | number` id via `toRowId` before calling (rule 15). */
async findWithUser(id: number): Promise<CollabPollJoinRow | undefined> {
return await this.kysely<CollabPollsKyselyDB>()
.selectFrom('collab_polls as p')
.innerJoin('users as u', 'u.id', 'p.user_id')
.selectAll('p')
.select(['u.username', 'u.avatar'])
.where('p.id', '=', id)
.executeTakeFirst();
}
/** CB25 (`listPolls`) — `SELECT id FROM collab_polls WHERE trip_id = ? ORDER BY created_at DESC`. */
async listIdsForTrip(trip_id: number): Promise<number[]> {
const rows = await this.kysely<CollabPollsKyselyDB>()
.selectFrom('collab_polls')
.select(['id'])
.where('trip_id', '=', trip_id)
.orderBy('created_at', 'desc')
.execute();
return rows.map((r) => r.id);
}
/**
* CB27/CB32/CB34 (`votePoll`/`closePoll`/`deletePoll`'s shared trip-scoping
* guard) — `SELECT * FROM collab_polls WHERE id = ? AND trip_id = ?`, same
* text at all three call sites.
*/
async findInTrip(id: number, trip_id: number): Promise<CollabPollRow | undefined> {
return await this.kysely<CollabPollsKyselyDB>()
.selectFrom('collab_polls')
.selectAll()
.where('id', '=', id)
.where('trip_id', '=', trip_id)
.executeTakeFirst();
}
/** CB26 (`createPoll`) — `INSERT INTO collab_polls (trip_id, user_id, question, options, multiple, deadline) VALUES (?×6)`. Returns the new row's id. */
async insertPoll(row: {
trip_id: number | string;
user_id: number;
question: string;
options: string;
multiple: number;
deadline: string | null;
}): Promise<number> {
return await this.insert({
trip: row.trip_id,
user: row.user_id,
question: row.question,
options: row.options,
multiple: row.multiple,
deadline: row.deadline,
});
}
/** CB33 (`closePoll`) — `UPDATE collab_polls SET closed = 1 WHERE id = ?`. */
async close(id: number): Promise<void> {
await this.nativeUpdate({ id }, { closed: 1 });
}
/** CB35 (`deletePoll`) — `DELETE FROM collab_polls WHERE id = ?`. */
async delete(id: number): Promise<void> {
await this.nativeDelete({ id });
}
}