cityssm/parking-ticket-system

View on GitHub
database/parkingDB/markConvictionBatchAsSent.ts

Summary

Maintainability
A
35 mins
Test Coverage
import { dateToInteger } from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'

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

export default function markConvictionBatchAsSent(
  batchId: number,
  sessionUser: PTSUser
): boolean {
  const database = sqlite(databasePath)

  const rightNow = new Date()

  const info = database
    .prepare(
      `update ParkingTicketConvictionBatches
        set sentDate = ?,
        recordUpdate_userName = ?,
        recordUpdate_timeMillis = ?
        where batchId = ?
        and recordDelete_timeMillis is null
        and lockDate is not null
        and sentDate is null`
    )
    .run(
      dateToInteger(rightNow),
      sessionUser.userName,
      rightNow.getTime(),
      batchId
    )

  database
    .prepare(
      `update ParkingTickets
        set resolvedDate = ?,
        recordUpdate_userName = ?,
        recordUpdate_timeMillis = ?
        where resolvedDate is null
        and exists (
          select 1 from ParkingTicketStatusLog s
          where ParkingTickets.ticketId = s.ticketId
          and s.recordDelete_timeMillis is null
          and s.statusField = ?)`
    )
    .run(
      dateToInteger(rightNow),
      sessionUser.userName,
      rightNow.getTime(),
      batchId.toString()
    )

  database.close()

  return info.changes > 0
}