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

@@ -12,6 +12,7 @@ import {
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET_ATTR,
} from '../../providers/sqlite3/queries/attributename.js';
import { isUniqueConstraintError } from '../../providers/provider.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
@@ -32,6 +33,8 @@ export default function create(provider) {
* @param {number} [input.sort] - Optional sort order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, attributeId: number, value: string, sort: number}>}
*
* @throws {Error} If the value is missing, the attribute is unknown, or the value already exists for the attribute.
*/
return async (googleAuthId, attributeId, input = {}, tx) => {
const attrId = Number(attributeId);
@@ -57,11 +60,19 @@ export default function create(provider) {
sort = next.rows[0].sort;
}
const result = await runner.query(createSql, {
attributeId: attrId,
value,
sort,
});
let result;
try {
result = await runner.query(createSql, {
attributeId: attrId,
value,
sort,
});
} catch (err) {
if (isUniqueConstraintError(err)) {
throw new Error('Attribute value already exists');
}
throw err;
}
const id = result.rows[0]?.id ?? result.insertId;
return {