sunrise-cms/database/getContractComments.ts

46 lines
1.4 KiB
TypeScript

import {
dateIntegerToString,
timeIntegerToPeriodString,
timeIntegerToString
} from '@cityssm/utils-datetime'
import sqlite from 'better-sqlite3'
import { sunriseDB } from '../helpers/database.helpers.js'
import type { ContractComment } from '../types/record.types.js'
export default function getContractComments(
contractId: number | string,
connectedDatabase?: sqlite.Database
): ContractComment[] {
const database = connectedDatabase ?? sqlite(sunriseDB, { readonly: true })
database.function('userFn_dateIntegerToString', dateIntegerToString)
database.function('userFn_timeIntegerToString', timeIntegerToString)
database.function(
'userFn_timeIntegerToPeriodString',
timeIntegerToPeriodString
)
const comments = database
.prepare(
`select contractCommentId,
commentDate, userFn_dateIntegerToString(commentDate) as commentDateString,
commentTime,
userFn_timeIntegerToString(commentTime) as commentTimeString,
userFn_timeIntegerToPeriodString(commentTime) as commentTimePeriodString,
comment,
recordCreate_userName, recordUpdate_userName
from ContractComments
where recordDelete_timeMillis is null
and contractId = ?
order by commentDate desc, commentTime desc, contractCommentId desc`
)
.all(contractId) as ContractComment[]
if (connectedDatabase === undefined) {
database.close()
}
return comments
}