All files / Bandstand/src/lib cues-server.ts

0% Statements 0/29
0% Branches 0/1
0% Functions 0/1
0% Lines 0/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                                   
import {prisma} from './db'
import type {Cue} from './cues'
 
/** This person's cues on these songs, by song id. */
export async function myCues(userId: string, songIds: string[]) {
  const rows = await prisma.cue.findMany({
    where: {userId, songId: {in: songIds}},
    include: {image: {select: {width: true, height: true}}},
    orderBy: [{position: 'asc'}, {createdAt: 'asc'}],
  })
  const out = new Map<string, Cue[]>()
  for (const r of rows)
    out.set(r.songId, [...(out.get(r.songId) ?? []), toCue(r)])
  return out
}
 
export function toCue(r: {
  id: string
  anchor: string
  position: number
  kind: Cue['kind']
  text: string | null
  updatedAt: Date
  image: {width: number; height: number} | null
}): Cue {
  return {
    id: r.id,
    anchor: r.anchor,
    position: r.position,
    kind: r.kind,
    text: r.text,
    image: r.image
      ? {
          // ?v= changes with every edit, so the image caches for good
          url: `/api/cues/${r.id}/image?v=${r.updatedAt.getTime()}`,
          width: r.image.width,
          height: r.image.height,
        }
      : null,
  }
}