sunrise-cms/database/getIntermentContainerTypes.ts

44 lines
1.2 KiB
TypeScript

import sqlite from 'better-sqlite3'
import { sunriseDB } from '../helpers/database.helpers.js'
import type { IntermentContainerType } from '../types/record.types.js'
import { updateRecordOrderNumber } from './updateRecordOrderNumber.js'
export default function getIntermentContainerTypes(
includeDeleted = false
): IntermentContainerType[] {
const database = sqlite(sunriseDB)
const containerTypes = database
.prepare(
`select intermentContainerTypeId, intermentContainerType, intermentContainerTypeKey,
isCremationType, orderNumber
from IntermentContainerTypes
${includeDeleted ? '' : ' where recordDelete_timeMillis is null '}
order by isCremationType, orderNumber, intermentContainerType, intermentContainerTypeId`
)
.all() as IntermentContainerType[]
let expectedOrderNumber = -1
for (const containerType of containerTypes) {
expectedOrderNumber += 1
if (containerType.orderNumber !== expectedOrderNumber) {
updateRecordOrderNumber(
'IntermentContainerTypes',
containerType.intermentContainerTypeId,
expectedOrderNumber,
database
)
containerType.orderNumber = expectedOrderNumber
}
}
database.close()
return containerTypes
}