This commit is contained in:
seb
2026-10-04 19:12:30 +02:00
parent 2158ac475b
commit 067ddc088c
7 changed files with 208 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ import {
CREATE as SQLITE_CREATE,
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
} from '../../providers/sqlite3/queries/propertyname.js';
import { isUniqueConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
@@ -25,6 +26,8 @@ export default function create(provider) {
* @param {boolean} [input.exclusive=false] - Whether values are mutually exclusive on an article.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean}>}
*
* @throws {Error} If the name is missing or already exists for the company.
*/
return async (googleAuthId, input = {}, tx) => {
const name = input.name?.trim();
@@ -43,12 +46,20 @@ export default function create(provider) {
const exclusive = Boolean(input.exclusive) ? 1 : 0;
const result = await runner.query(createSql, {
companyId,
name,
sort,
exclusive,
});
let result;
try {
result = await runner.query(createSql, {
companyId,
name,
sort,
exclusive,
});
} catch (err) {
if (isUniqueConstraintError(err)) {
throw new Error('Property name already exists');
}
throw err;
}
const id = result.rows[0]?.id ?? result.insertId;
return { id, name, sort, exclusive: Boolean(exclusive) };

View File

@@ -6,6 +6,7 @@ import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
UPDATE_BY_ID as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/propertyname.js';
import { isUniqueConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
@@ -26,6 +27,8 @@ export default function update(provider) {
* @param {boolean} [input.exclusive] - New exclusive flag.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean}>}
*
* @throws {Error} If the property name is unknown, the name is missing, or the name is already taken.
*/
return async (googleAuthId, id, input = {}, tx) => {
if (!id || !Number.isInteger(Number(id))) {
@@ -57,13 +60,20 @@ export default function update(provider) {
}
}
await runner.query(updateSql, {
id,
companyId,
name,
sort,
exclusive,
});
try {
await runner.query(updateSql, {
id,
companyId,
name,
sort,
exclusive,
});
} catch (err) {
if (isUniqueConstraintError(err)) {
throw new Error('Property name already exists');
}
throw err;
}
return { id, name, sort, exclusive: Boolean(exclusive) };
};