File size: 550 Bytes
598f0d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439093c
598f0d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { eq } from "drizzle-orm";
import { throwForbidden, throwNotFound } from "./errors";

interface OwnedRow {
  creatorUserId: string | null;
}

export function assertOwnership(row: OwnedRow | null | undefined, userId: string, resource = "Resource"): void {
  if (!row) throwNotFound(resource);
  if (row.creatorUserId !== userId) throwForbidden();
}

export function ownershipFilter<TTable extends Record<string, any>>(
  table: TTable,
  userId: string,
  creatorColumn: keyof TTable,
) {
  return eq(table[creatorColumn as string], userId);
}