This commit is contained in:
seb
2026-09-29 16:16:05 +02:00
parent 26d2f83d64
commit ae5d5cc6f8
40 changed files with 2139 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import {
LIST_IDS_BY_COMPANY as MSSQL_LIST_IDS,
UPDATE_SORT as MSSQL_UPDATE_SORT,
} from '../../providers/mssql/queries/propertyname.js';
import {
LIST_IDS_BY_COMPANY as SQLITE_LIST_IDS,
UPDATE_SORT as SQLITE_UPDATE_SORT,
} from '../../providers/sqlite3/queries/propertyname.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function order(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const listIdsSql = isSqlite ? SQLITE_LIST_IDS : MSSQL_LIST_IDS;
const updateSortSql = isSqlite ? SQLITE_UPDATE_SORT : MSSQL_UPDATE_SORT;
/**
* Reorders the property names for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - Reorder input.
* @param {Array<number>} input.propertynameIds - IDs in their new order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{propertynameIds: Array<number>}>}
*/
return async (googleAuthId, { propertynameIds } = {}, tx) => {
if (!Array.isArray(propertynameIds)) {
throw new Error('Invalid property name order');
}
const runOrder = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
const existingRows = await runner.query(listIdsSql, { companyId });
const existing = new Set(existingRows.rows.map((r) => r.id));
if (propertynameIds.length !== existing.size) {
throw new Error('Invalid property name order');
}
const seen = new Set();
for (const id of propertynameIds) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
throw new Error('Invalid property name order');
}
seen.add(id);
}
for (let sort = 0; sort < propertynameIds.length; sort++) {
await runner.query(updateSortSql, {
id: propertynameIds[sort],
companyId,
sort,
});
}
return { propertynameIds };
};
if (tx) {
return runOrder(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runOrder);
}
return runOrder(makeRunner(provider));
};
}