u
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
CREATE as SQLITE_CREATE,
|
||||
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
|
||||
} from '../../providers/sqlite3/queries/attributename.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, values: Array}>}
|
||||
*
|
||||
* @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('Attribute name already exists');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
const id = result.rows[0]?.id ?? result.insertId;
|
||||
return {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
GET_BY_COMPANY_AND_ID as SQLITE_GET,
|
||||
UPDATE_BY_ID as SQLITE_UPDATE,
|
||||
} 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 update(provider) {
|
||||
* @param {boolean} [input.exclusive] - New exclusive flag.
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean, values: Array}>}
|
||||
*
|
||||
* @throws {Error} If the attribute 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))) {
|
||||
@@ -53,13 +56,20 @@ export default function update(provider) {
|
||||
const sort = input.sort !== undefined && Number.isInteger(input.sort) ? input.sort : existing.rows[0].sort;
|
||||
const exclusive = input.exclusive !== undefined ? (Boolean(input.exclusive) ? 1 : 0) : existing.rows[0].exclusive;
|
||||
|
||||
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('Attribute name already exists');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
return {
|
||||
id,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
GET_BY_ID_AND_COMPANY as SQLITE_GET,
|
||||
UPDATE_BY_ID as SQLITE_UPDATE,
|
||||
} from '../../providers/sqlite3/queries/attributevalue.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 update(provider) {
|
||||
* @param {number} [input.sort] - New sort order.
|
||||
* @param {object} [tx] - Optional transaction.
|
||||
* @returns {Promise<{id: number, attributeId: number, value: string, sort: number}>}
|
||||
*
|
||||
* @throws {Error} If the attribute value is unknown, the value is missing, or the value is already taken.
|
||||
*/
|
||||
return async (googleAuthId, id, input = {}, tx) => {
|
||||
const valId = Number(id);
|
||||
@@ -46,11 +49,18 @@ export default function update(provider) {
|
||||
}
|
||||
const sort = input.sort !== undefined && Number.isInteger(input.sort) ? input.sort : existing.rows[0].sort;
|
||||
|
||||
await runner.query(updateSql, {
|
||||
id: valId,
|
||||
value,
|
||||
sort,
|
||||
});
|
||||
try {
|
||||
await runner.query(updateSql, {
|
||||
id: valId,
|
||||
value,
|
||||
sort,
|
||||
});
|
||||
} catch (err) {
|
||||
if (isUniqueConstraintError(err)) {
|
||||
throw new Error('Attribute value already exists');
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
return {
|
||||
id: valId,
|
||||
|
||||
@@ -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) };
|
||||
|
||||
@@ -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) };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user