All files / Bandstand/src/lib setlists.ts

29.66% Statements 62/209
100% Branches 20/20
33.33% Functions 1/3
29.66% Lines 62/209

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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 2411x 1x 1x   1x 1x 1x 1x   1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x               1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x                                                               1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 1x 1x 2x 3x 3x 3x 3x                                                                                                                                                                                                                                                                          
import {z} from 'zod'
import {prisma} from './db'
import {logActivity} from './songs'
 
const Clock = z
  .string()
  .trim()
  .regex(/^([01]?\d|2[0-3]):[0-5]\d$/)
 
const Minutes = z.number().int().min(1).max(600).nullable().optional()
 
/** One row of the running order: a song, a set header or a break. */
const Row = z.discriminatedUnion('kind', [
  z.object({
    kind: z.literal('SONG'),
    songId: z.string().min(1),
    note: z.string().trim().max(300).nullable().optional(),
    key: z.string().trim().max(8).nullable().optional(),
  }),
  z.object({
    kind: z.literal('SET'),
    label: z.string().trim().min(1).max(40),
    minutes: Minutes,
    startTime: Clock.nullable().optional(),
  }),
  z.object({kind: z.literal('BREAK'), minutes: Minutes}),
])
 
// Rows sent before sets existed have no kind: they're songs
const Rows = z.preprocess(
  (v) =>
    Array.isArray(v)
      ? v.map((r) =>
          r && typeof r === 'object' && !('kind' in r)
            ? {...r, kind: 'SONG'}
            : r,
        )
      : v,
  z.array(Row).max(250),
)
 
export const SetlistBody = z.object({
  name: z.string().trim().min(1).max(200),
  gigDate: z
    .string()
    .trim()
    .transform((v) => v || null)
    .nullable()
    .optional()
    .refine((v) => v == null || !Number.isNaN(Date.parse(v)), 'Invalid date'),
  venue: z.string().trim().max(200).nullable().optional(),
  // When the first set starts: "20:00"
  startTime: Clock.nullable().optional(),
  notes: z.string().max(5000).nullable().optional(),
  items: Rows.optional(),
})
 
export type SetlistInput = z.infer<typeof SetlistBody>
 
export async function getSetlist(id: string, bandId: string) {
  return prisma.setlist.findFirst({
    where: {id, bandId},
    include: {
      items: {
        orderBy: {position: 'asc'},
        include: {
          song: {
            include: {
              chartVersions: {
                orderBy: {number: 'desc'},
                take: 1,
                include: {
                  author: {
                    select: {name: true, displayName: true, email: true},
                  },
                },
              },
            },
          },
        },
      },
      updatedBy: {select: {name: true, displayName: true, email: true}},
    },
  })
}
 
/** "added X, removed Y, reordered" — for the activity log. */
export function describeSetChanges(
  before: {songId: string; title: string}[],
  after: {songId: string; title: string}[],
) {
  const b = new Set(before.map((x) => x.songId))
  const a = new Set(after.map((x) => x.songId))
  const added = after.filter((x) => !b.has(x.songId)).map((x) => x.title)
  const removed = before.filter((x) => !a.has(x.songId)).map((x) => x.title)
  const keptBefore = before.filter((x) => a.has(x.songId)).map((x) => x.songId)
  const keptAfter = after.filter((x) => b.has(x.songId)).map((x) => x.songId)
  const parts: string[] = []
  if (added.length)
    parts.push(
      added.length > 3
        ? `added ${added.length} songs`
        : `added ${added.join(', ')}`,
    )
  if (removed.length) parts.push(`removed ${removed.join(', ')}`)
  if (keptBefore.join() !== keptAfter.join()) parts.push('changed the order')
  return parts
}
 
export async function saveSetlist(
  bandId: string,
  id: string,
  userId: string,
  input: SetlistInput,
) {
  return prisma.$transaction(async (tx) => {
    const before = await tx.setlist.findFirst({
      where: {id, bandId},
      include: {
        items: {
          orderBy: {position: 'asc'},
          include: {song: {select: {title: true}}},
        },
      },
    })
    if (!before) return null
    const {items, gigDate, ...fields} = input
    await tx.setlist.update({
      where: {id},
      data: {
        ...fields,
        ...(gigDate !== undefined
          ? {gigDate: gigDate ? new Date(gigDate) : null}
          : {}),
        updatedById: userId,
      },
    })
    let changes: string[] = []
    if (items) {
      const songIds = items.flatMap((i) =>
        i.kind === 'SONG' ? [i.songId] : [],
      )
      const songs = await tx.song.findMany({
        where: {bandId, id: {in: songIds}},
        select: {id: true, title: true},
      })
      const title = new Map(songs.map((s) => [s.id, s.title]))
      // Songs from another band (or deleted since) are dropped
      const valid = items.filter(
        (i) => i.kind !== 'SONG' || title.has(i.songId),
      )
      await tx.setlistItem.deleteMany({where: {setlistId: id}})
      await tx.setlistItem.createMany({
        data: valid.map((i, position) =>
          i.kind === 'SONG'
            ? {
                setlistId: id,
                kind: i.kind,
                songId: i.songId,
                position,
                note: i.note || null,
                key: i.key || null,
              }
            : {
                setlistId: id,
                kind: i.kind,
                position,
                label: i.kind === 'SET' ? i.label : null,
                minutes: i.minutes ?? null,
                startTime: i.kind === 'SET' ? i.startTime || null : null,
              },
        ),
      })
      const songRows = (rows: {songId: string | null}[]) =>
        rows.flatMap((r) => (r.songId ? [r.songId] : []))
      changes = describeSetChanges(
        before.items.flatMap((i) =>
          i.songId && i.song ? [{songId: i.songId, title: i.song.title}] : [],
        ),
        songRows(
          valid.map((i) => ({songId: i.kind === 'SONG' ? i.songId : null})),
        ).map((songId) => ({songId, title: title.get(songId)!})),
      )
      const shape = (
        rows: {
          kind: string
          label?: string | null
          minutes?: number | null
          startTime?: string | null
          songId?: string | null
          note?: string | null
          key?: string | null
        }[],
      ) =>
        rows
          .map((r) =>
            r.kind === 'SONG'
              ? `${r.songId}|${r.note || ''}|${r.key || ''}`
              : `${r.kind}|${r.label ?? ''}|${r.minutes ?? ''}|${r.startTime ?? ''}`,
          )
          .join()
      const setShape = (
        rows: {
          kind: string
          label?: string | null
          minutes?: number | null
          startTime?: string | null
        }[],
      ) =>
        rows
          .filter((r) => r.kind !== 'SONG')
          .map(
            (r) =>
              `${r.kind}|${r.label ?? ''}|${r.minutes ?? ''}|${r.startTime ?? ''}`,
          )
          .join()
      if (setShape(before.items) !== setShape(valid))
        changes.push('changed the sets and breaks')
      else if (!changes.length && shape(before.items) !== shape(valid))
        changes.push('changed song notes or keys')
    }
    if (fields.name && fields.name !== before.name)
      changes.unshift(`renamed it from ${before.name}`)
    if (
      changes.length ||
      gigDate !== undefined ||
      fields.venue !== undefined ||
      fields.startTime !== undefined
    )
      await logActivity(tx, {
        bandId,
        userId,
        action: 'setlist.update',
        targetType: 'setlist',
        targetId: id,
        summary: `updated the setlist ${fields.name ?? before.name}${changes.length ? `: ${changes.join('; ')}` : ''}`,
      })
    return true
  })
}