sunrise-cms/database/getIntermentContainerTypes.js

24 lines
1.1 KiB
JavaScript

import sqlite from 'better-sqlite3';
import { sunriseDB } from '../helpers/database.helpers.js';
import { updateRecordOrderNumber } from './updateRecordOrderNumber.js';
export default function getIntermentContainerTypes(includeDeleted = false) {
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();
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;
}