cityssm/parking-ticket-system

View on GitHub
database/parkingDB/isParkingTicketConvicted.ts

Summary

Maintainability
A
0 mins
Test Coverage
import sqlite from 'better-sqlite3'

import { parkingDB as databasePath } from '../../data/databasePaths.js'

export default function isParkingTicketConvicted(
  ticketId: number,
  connectedDatabase?: sqlite.Database
): boolean {
  const database = connectedDatabase ?? sqlite(databasePath)

  const statusIndex = database
    .prepare(
      `select statusIndex
        from ParkingTicketStatusLog
        where recordDelete_timeMillis is null
        and ticketId = ?
        and statusKey = 'convicted'`
    )
    .pluck()
    .get(ticketId) as number | undefined

  if (connectedDatabase === undefined) {
    database.close()
  }

  return statusIndex !== undefined
}