sunrise-cms/database/moveContractTypePrintUp.js

52 lines
2.0 KiB
JavaScript

import sqlite from 'better-sqlite3';
import { sunriseDB } from '../helpers/database.helpers.js';
import { clearCacheByTableName } from '../helpers/functions.cache.js';
export function moveContractTypePrintUp(contractTypeId, printEJS) {
const database = sqlite(sunriseDB);
const currentOrderNumber = database
.prepare('select orderNumber from ContractTypePrints where contractTypeId = ? and printEJS = ?')
.get(contractTypeId, printEJS).orderNumber;
if (currentOrderNumber <= 0) {
database.close();
return true;
}
database
.prepare(`update ContractTypePrints
set orderNumber = orderNumber + 1
where recordDelete_timeMillis is null
and contractTypeId = ?
and orderNumber = ? - 1`)
.run(contractTypeId, currentOrderNumber);
const result = database
.prepare('update ContractTypePrints set orderNumber = ? - 1 where contractTypeId = ? and printEJS = ?')
.run(currentOrderNumber, contractTypeId, printEJS);
database.close();
clearCacheByTableName('ContractTypePrints');
return result.changes > 0;
}
export function moveContractTypePrintUpToTop(contractTypeId, printEJS) {
const database = sqlite(sunriseDB);
const currentOrderNumber = database
.prepare('select orderNumber from ContractTypePrints where contractTypeId = ? and printEJS = ?')
.get(contractTypeId, printEJS).orderNumber;
if (currentOrderNumber > 0) {
database
.prepare(`update ContractTypePrints
set orderNumber = -1
where contractTypeId = ?
and printEJS = ?`)
.run(contractTypeId, printEJS);
database
.prepare(`update ContractTypePrints
set orderNumber = orderNumber + 1
where recordDelete_timeMillis is null
and contractTypeId = ?
and orderNumber < ?`)
.run(contractTypeId, currentOrderNumber);
}
database.close();
clearCacheByTableName('ContractTypePrints');
return true;
}
export default moveContractTypePrintUp;