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,52 @@
import {
CREATE as MSSQL_CREATE,
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
} from '../../providers/mssql/queries/propertyname.js';
import {
CREATE as SQLITE_CREATE,
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
} from '../../providers/sqlite3/queries/propertyname.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function create(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
/**
* Creates a property name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - The property name input.
* @param {string} input.name - The property name.
* @param {number} [input.sort] - Optional sort order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number}>}
*/
return async (googleAuthId, input = {}, tx) => {
const name = input.name?.trim();
if (!name) {
throw new Error('Name required');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
let sort = input.sort;
if (sort == null || !Number.isInteger(sort)) {
const next = await runner.query(getSortSql, { companyId });
sort = next.rows[0].sort;
}
const result = await runner.query(createSql, {
companyId,
name,
sort,
});
const id = result.rows[0]?.id ?? result.insertId;
return { id, name, sort };
};
}

View File

@@ -0,0 +1,37 @@
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET,
DELETE_BY_COMPANY_AND_ID as MSSQL_DELETE,
} from '../../providers/mssql/queries/propertyname.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE,
} from '../../providers/sqlite3/queries/propertyname.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function deletePropertyname(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
/**
* Deletes a property name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The property name id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<void>}
*/
return async (googleAuthId, id, tx) => {
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
const existing = await runner.query(getSql, { id, companyId });
if (!existing.rows[0]) {
throw new Error('Property name not found');
}
await runner.query(deleteSql, { id, companyId });
};
}

View File

@@ -0,0 +1,26 @@
import { LIST_BY_COMPANY as MSSQL_LIST } from '../../providers/mssql/queries/propertyname.js';
import { LIST_BY_COMPANY as SQLITE_LIST } from '../../providers/sqlite3/queries/propertyname.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
/**
* Lists the property names for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, name: string, sort: number}>>}
*/
return async (googleAuthId, tx) => {
const { companyId } = await access(googleAuthId, tx);
const result = await makeRunner(provider, tx).query(sql, { companyId });
return result.rows.map((row) => ({
id: row.id,
name: row.name,
sort: row.sort,
}));
};
}

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));
};
}

View File

@@ -0,0 +1,57 @@
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET,
UPDATE_BY_ID as MSSQL_UPDATE,
} from '../../providers/mssql/queries/propertyname.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
UPDATE_BY_ID as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/propertyname.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function update(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const getSql = isSqlite ? SQLITE_GET : MSSQL_GET;
const updateSql = isSqlite ? SQLITE_UPDATE : MSSQL_UPDATE;
/**
* Updates a property name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The property name id.
* @param {object} input - Update patch.
* @param {string} [input.name] - New name.
* @param {number} [input.sort] - New sort.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number}>}
*/
return async (googleAuthId, id, input = {}, tx) => {
if (!id || !Number.isInteger(Number(id))) {
throw new Error('Invalid id');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
const existing = await runner.query(getSql, { id, companyId });
if (!existing.rows[0]) {
throw new Error('Property name not found');
}
const name = input.name !== undefined ? input.name?.trim() : existing.rows[0].name;
if (!name) {
throw new Error('Name required');
}
const sort = input.sort !== undefined && Number.isInteger(input.sort) ? input.sort : existing.rows[0].sort;
await runner.query(updateSql, {
id,
companyId,
name,
sort,
});
return { id, name, sort };
};
}