sqlite
This commit is contained in:
@@ -1,8 +1,25 @@
|
||||
import { GET_BY_ID, SET_OFF, LIST_CATEGORY_IDS } from '../../providers/mssql/queries/posterminal.js';
|
||||
import {
|
||||
GET_BY_ID as MSSQL_GET_BY_ID,
|
||||
SET_OFF as MSSQL_SET_OFF,
|
||||
LIST_CATEGORY_IDS as MSSQL_LIST_CATEGORY_IDS,
|
||||
} from '../../providers/mssql/queries/posterminal.js';
|
||||
import {
|
||||
GET_BY_ID as SQLITE_GET_BY_ID,
|
||||
SET_OFF as SQLITE_SET_OFF,
|
||||
LIST_CATEGORY_IDS as SQLITE_LIST_CATEGORY_IDS,
|
||||
} from '../../providers/sqlite3/queries/posterminal.js';
|
||||
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
||||
import makeRunner from '../runner.js';
|
||||
|
||||
export default function setOff(pool) {
|
||||
const access = resolveSelectedCompanyAccess(pool);
|
||||
export default function setOff(provider) {
|
||||
const access = resolveSelectedCompanyAccess(provider);
|
||||
const isSqlite = provider?.dialect === 'sqlite3';
|
||||
|
||||
const getByIdSql = isSqlite ? SQLITE_GET_BY_ID : MSSQL_GET_BY_ID;
|
||||
const setOffSql = isSqlite ? SQLITE_SET_OFF : MSSQL_SET_OFF;
|
||||
const listCatIdsSql = isSqlite
|
||||
? SQLITE_LIST_CATEGORY_IDS
|
||||
: MSSQL_LIST_CATEGORY_IDS;
|
||||
|
||||
/**
|
||||
* Removes a category assignment from a posterminal (turns it off).
|
||||
@@ -10,6 +27,7 @@ export default function setOff(pool) {
|
||||
* @param {number} googleAuthId - The id of the Google auth user.
|
||||
* @param {number} posterminalId - The id of the posterminal.
|
||||
* @param {number} categoryId - The id of the category to remove.
|
||||
* @param {object} [tx] - Optional transaction to run within.
|
||||
* @returns {Promise<{id: number, categoryIds: Array<number>}>} The posterminal with its remaining category ids.
|
||||
* @throws {Error} If the posterminal is not found.
|
||||
*
|
||||
@@ -17,36 +35,30 @@ export default function setOff(pool) {
|
||||
* const result = await store.posterminal.setOff(42, 5, 9);
|
||||
* // => { id, categoryIds: [...] }
|
||||
*/
|
||||
return async (googleAuthId, posterminalId, categoryId) => {
|
||||
return async (googleAuthId, posterminalId, categoryId, tx) => {
|
||||
if (!Number.isFinite(posterminalId) || !Number.isFinite(categoryId)) {
|
||||
throw new Error('Invalid posterminal or category');
|
||||
}
|
||||
|
||||
const { companyId } = await access(googleAuthId);
|
||||
const runner = makeRunner(provider, tx);
|
||||
const { companyId } = await access(googleAuthId, tx);
|
||||
|
||||
const existing = await pool
|
||||
.request()
|
||||
.input('id', posterminalId)
|
||||
.query(GET_BY_ID);
|
||||
const existing = await runner.query(getByIdSql, { id: posterminalId });
|
||||
|
||||
if (!existing.recordset[0] || existing.recordset[0].companyId !== companyId) {
|
||||
if (!existing.rows[0] || existing.rows[0].companyId !== companyId) {
|
||||
throw new Error('Posterminal not found');
|
||||
}
|
||||
|
||||
await pool
|
||||
.request()
|
||||
.input('posterminalId', posterminalId)
|
||||
.input('categoryId', categoryId)
|
||||
.query(SET_OFF);
|
||||
await runner.query(setOffSql, {
|
||||
posterminalId,
|
||||
categoryId,
|
||||
});
|
||||
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('posterminalId', posterminalId)
|
||||
.query(LIST_CATEGORY_IDS);
|
||||
const result = await runner.query(listCatIdsSql, { posterminalId });
|
||||
|
||||
return {
|
||||
id: posterminalId,
|
||||
categoryIds: result.recordset.map((r) => r.categoryId),
|
||||
categoryIds: result.rows.map((r) => r.categoryId),
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user