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,34 @@
import { LIST_BY_ARTICLE_ID as MSSQL_QUERY } from '../../providers/mssql/queries/articleattributevalue.js';
import { LIST_BY_ARTICLE_ID as SQLITE_QUERY } from '../../providers/sqlite3/queries/articleattributevalue.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function listByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
/**
* Lists the attribute values assigned to an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, attributeId: number, attributeName: string, exclusive: boolean, attributeValueId: number, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, tx) => {
const runner = makeRunner(provider, tx);
const { articleId } = await resolve(googleAuthId, sku, runner);
const result = await runner.query(sql, { articleId });
return result.rows.map((row) => ({
id: row.id,
articleId: row.articleId,
attributeId: row.attributeId,
attributeName: row.attributeName,
exclusive: Boolean(row.exclusive),
attributeValueId: row.attributeValueId,
value: row.value,
sort: row.sort,
}));
};
}

View File

@@ -0,0 +1,115 @@
import {
DELETE_BY_ARTICLE_ID as MSSQL_DELETE,
INSERT_ROW as MSSQL_INSERT,
GET_VALUE_DETAIL as MSSQL_GET_DETAIL,
} from '../../providers/mssql/queries/articleattributevalue.js';
import {
DELETE_BY_ARTICLE_ID as SQLITE_DELETE,
INSERT_ROW as SQLITE_INSERT,
GET_VALUE_DETAIL as SQLITE_GET_DETAIL,
} from '../../providers/sqlite3/queries/articleattributevalue.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function setByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const insertSql = isSqlite ? SQLITE_INSERT : MSSQL_INSERT;
const getDetailSql = isSqlite ? SQLITE_GET_DETAIL : MSSQL_GET_DETAIL;
/**
* Replaces all attribute values for an article by its SKU.
* Validates company ownership and enforces the `exclusive` flag on attributes.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {Array<number|{attributeValueId: number, sort?: number}>} values - Attribute value IDs or items.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, attributeId: number, attributeName: string, exclusive: boolean, attributeValueId: number, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, values = [], tx) => {
if (!Array.isArray(values)) {
throw new Error('Invalid attribute values array');
}
const runSet = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const validated = [];
const seenValues = new Set();
const exclusiveAttributesSeen = new Map(); // attributeId -> attributeName
for (let i = 0; i < values.length; i++) {
const item = values[i];
const valId = typeof item === 'object' && item !== null ? Number(item.attributeValueId) : Number(item);
if (!Number.isInteger(valId) || valId <= 0) {
throw new Error('Invalid attributeValueId');
}
if (seenValues.has(valId)) {
throw new Error('Duplicate attributeValueId');
}
seenValues.add(valId);
const detailRes = await runner.query(getDetailSql, { attributeValueId: valId });
const detail = detailRes.rows[0];
if (!detail || detail.companyId !== companyId) {
throw new Error(`Attribute value not found: ${valId}`);
}
const isExclusive = Boolean(detail.exclusive);
if (isExclusive) {
if (exclusiveAttributesSeen.has(detail.attributeId)) {
throw new Error(`Attribute "${detail.attributeName}" is exclusive and can only have one value`);
}
exclusiveAttributesSeen.set(detail.attributeId, detail.attributeName);
}
const sort = typeof item === 'object' && item !== null && item.sort != null && Number.isInteger(item.sort)
? item.sort
: i;
validated.push({
attributeId: detail.attributeId,
attributeName: detail.attributeName,
exclusive: isExclusive,
attributeValueId: valId,
value: detail.value,
sort,
});
}
await runner.query(deleteSql, { articleId });
const inserted = [];
for (const item of validated) {
const res = await runner.query(insertSql, {
articleId,
attributeValueId: item.attributeValueId,
sort: item.sort,
});
const id = res.rows[0]?.id ?? res.insertId;
inserted.push({
id,
articleId,
attributeId: item.attributeId,
attributeName: item.attributeName,
exclusive: item.exclusive,
attributeValueId: item.attributeValueId,
value: item.value,
sort: item.sort,
});
}
return inserted;
};
if (tx) {
return runSet(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runSet);
}
return runSet(makeRunner(provider));
};
}

View File

@@ -0,0 +1,28 @@
import {
DELETE_BY_ARTICLE_AND_PROPERTY_ID as MSSQL_DELETE,
} from '../../providers/mssql/queries/articleproperty.js';
import {
DELETE_BY_ARTICLE_AND_PROPERTY_ID as SQLITE_DELETE,
} from '../../providers/sqlite3/queries/articleproperty.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function deleteArticleProperty(provider) {
const resolve = resolveArticleBySku(provider);
const deleteSql = provider?.dialect === 'sqlite3' ? SQLITE_DELETE : MSSQL_DELETE;
/**
* Deletes a property from an article by SKU and propertyId.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {number} propertyId - The property id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<void>}
*/
return async (googleAuthId, sku, propertyId, tx) => {
const runner = makeRunner(provider, tx);
const { articleId } = await resolve(googleAuthId, sku, runner);
await runner.query(deleteSql, { articleId, propertyId });
};
}

View File

@@ -0,0 +1,32 @@
import { LIST_BY_ARTICLE_ID as MSSQL_QUERY } from '../../providers/mssql/queries/articleproperty.js';
import { LIST_BY_ARTICLE_ID as SQLITE_QUERY } from '../../providers/sqlite3/queries/articleproperty.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function listByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_QUERY : MSSQL_QUERY;
/**
* Lists the properties of an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, propertyId: number, name: string, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, tx) => {
const runner = makeRunner(provider, tx);
const { articleId } = await resolve(googleAuthId, sku, runner);
const result = await runner.query(sql, { articleId });
return result.rows.map((row) => ({
id: row.id,
articleId: row.articleId,
propertyId: row.propertyId,
name: row.name,
value: row.value,
sort: row.sort,
}));
};
}

View File

@@ -0,0 +1,103 @@
import {
DELETE_BY_ARTICLE_ID as MSSQL_DELETE,
INSERT_ROW as MSSQL_INSERT,
} from '../../providers/mssql/queries/articleproperty.js';
import {
DELETE_BY_ARTICLE_ID as SQLITE_DELETE,
INSERT_ROW as SQLITE_INSERT,
} from '../../providers/sqlite3/queries/articleproperty.js';
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET_PROP,
} from '../../providers/mssql/queries/propertyname.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET_PROP,
} from '../../providers/sqlite3/queries/propertyname.js';
import resolveArticleBySku from '../article/resolveArticleBySku.js';
import makeRunner from '../runner.js';
export default function setByArticleSku(provider) {
const resolve = resolveArticleBySku(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const deleteSql = isSqlite ? SQLITE_DELETE : MSSQL_DELETE;
const insertSql = isSqlite ? SQLITE_INSERT : MSSQL_INSERT;
const getPropSql = isSqlite ? SQLITE_GET_PROP : MSSQL_GET_PROP;
/**
* Replaces all properties for an article by its SKU.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {string} sku - The article SKU.
* @param {Array<{propertyId: number, value: string, sort?: number}>} properties - The properties to set.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, articleId: number, propertyId: number, name: string, value: string, sort: number}>>}
*/
return async (googleAuthId, sku, properties = [], tx) => {
if (!Array.isArray(properties)) {
throw new Error('Invalid properties array');
}
const runSet = async (runner) => {
const { articleId, companyId } = await resolve(googleAuthId, sku, runner);
const validated = [];
const seenProps = new Set();
for (let i = 0; i < properties.length; i++) {
const item = properties[i];
const propertyId = Number(item.propertyId);
if (!Number.isInteger(propertyId) || propertyId <= 0) {
throw new Error('Invalid propertyId');
}
if (seenProps.has(propertyId)) {
throw new Error('Duplicate propertyId');
}
seenProps.add(propertyId);
const propRes = await runner.query(getPropSql, { id: propertyId, companyId });
if (!propRes.rows[0]) {
throw new Error(`Property not found: ${propertyId}`);
}
const value = item.value != null ? String(item.value).trim() : '';
const sort = item.sort != null && Number.isInteger(item.sort) ? item.sort : i;
validated.push({
propertyId,
name: propRes.rows[0].name,
value,
sort,
});
}
await runner.query(deleteSql, { articleId });
const inserted = [];
for (const item of validated) {
const res = await runner.query(insertSql, {
articleId,
propertyId: item.propertyId,
value: item.value,
sort: item.sort,
});
const id = res.rows[0]?.id ?? res.insertId;
inserted.push({
id,
articleId,
propertyId: item.propertyId,
name: item.name,
value: item.value,
sort: item.sort,
});
}
return inserted;
};
if (tx) {
return runSet(makeRunner(provider, tx));
}
if (typeof provider?.transaction === 'function') {
return provider.transaction(runSet);
}
return runSet(makeRunner(provider));
};
}

View File

@@ -0,0 +1,62 @@
import {
CREATE as MSSQL_CREATE,
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
} from '../../providers/mssql/queries/attributename.js';
import {
CREATE as SQLITE_CREATE,
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
} from '../../providers/sqlite3/queries/attributename.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 an attribute name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} input - The attribute name input.
* @param {string} input.name - The attribute name.
* @param {number} [input.sort] - Optional sort order.
* @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}>}
*/
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 exclusive = Boolean(input.exclusive) ? 1 : 0;
const result = await runner.query(createSql, {
companyId,
name,
sort,
exclusive,
});
const id = result.rows[0]?.id ?? result.insertId;
return {
id,
name,
sort,
exclusive: Boolean(exclusive),
values: [],
};
};
}

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/attributename.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
DELETE_BY_COMPANY_AND_ID as SQLITE_DELETE,
} from '../../providers/sqlite3/queries/attributename.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function deleteAttributename(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 an attribute name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The attribute 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('Attribute name not found');
}
await runner.query(deleteSql, { id, companyId });
};
}

View File

@@ -0,0 +1,34 @@
import { LIST_BY_COMPANY as MSSQL_LIST } from '../../providers/mssql/queries/attributename.js';
import { LIST_BY_COMPANY as SQLITE_LIST } from '../../providers/sqlite3/queries/attributename.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function parseValues(raw) {
if (!raw) return [];
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
return Array.isArray(parsed) ? parsed : [];
}
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
/**
* Lists the attribute names for the user's selected company, including values.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, name: string, sort: number, exclusive: boolean, values: Array<{id: number, value: 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,
exclusive: Boolean(row.exclusive),
values: parseValues(row.valuesJson),
}));
};
}

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/attributename.js';
import {
LIST_IDS_BY_COMPANY as SQLITE_LIST_IDS,
UPDATE_SORT as SQLITE_UPDATE_SORT,
} from '../../providers/sqlite3/queries/attributename.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 attribute 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.attributenameIds - IDs in their new order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{attributenameIds: Array<number>}>}
*/
return async (googleAuthId, { attributenameIds } = {}, tx) => {
if (!Array.isArray(attributenameIds)) {
throw new Error('Invalid attribute 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 (attributenameIds.length !== existing.size) {
throw new Error('Invalid attribute name order');
}
const seen = new Set();
for (const id of attributenameIds) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
throw new Error('Invalid attribute name order');
}
seen.add(id);
}
for (let sort = 0; sort < attributenameIds.length; sort++) {
await runner.query(updateSortSql, {
id: attributenameIds[sort],
companyId,
sort,
});
}
return { attributenameIds };
};
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,72 @@
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET,
UPDATE_BY_ID as MSSQL_UPDATE,
} from '../../providers/mssql/queries/attributename.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET,
UPDATE_BY_ID as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/attributename.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
function parseValues(raw) {
if (!raw) return [];
const parsed = typeof raw === 'string' ? JSON.parse(raw) : raw;
return Array.isArray(parsed) ? parsed : [];
}
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 an attribute name for the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The attribute name id.
* @param {object} input - Update patch.
* @param {string} [input.name] - New name.
* @param {number} [input.sort] - New sort.
* @param {boolean} [input.exclusive] - New exclusive flag.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, name: string, sort: number, exclusive: boolean, values: Array}>}
*/
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('Attribute 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;
const exclusive = input.exclusive !== undefined ? (Boolean(input.exclusive) ? 1 : 0) : existing.rows[0].exclusive;
await runner.query(updateSql, {
id,
companyId,
name,
sort,
exclusive,
});
return {
id,
name,
sort,
exclusive: Boolean(exclusive),
values: parseValues(existing.rows[0].valuesJson),
};
};
}

View File

@@ -0,0 +1,74 @@
import {
CREATE as MSSQL_CREATE,
GET_NEXT_SORT as MSSQL_GET_NEXT_SORT,
} from '../../providers/mssql/queries/attributevalue.js';
import {
CREATE as SQLITE_CREATE,
GET_NEXT_SORT as SQLITE_GET_NEXT_SORT,
} from '../../providers/sqlite3/queries/attributevalue.js';
import {
GET_BY_COMPANY_AND_ID as MSSQL_GET_ATTR,
} from '../../providers/mssql/queries/attributename.js';
import {
GET_BY_COMPANY_AND_ID as SQLITE_GET_ATTR,
} from '../../providers/sqlite3/queries/attributename.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 getAttrSql = isSqlite ? SQLITE_GET_ATTR : MSSQL_GET_ATTR;
const getSortSql = isSqlite ? SQLITE_GET_NEXT_SORT : MSSQL_GET_NEXT_SORT;
const createSql = isSqlite ? SQLITE_CREATE : MSSQL_CREATE;
/**
* Creates an attribute value under a given attribute.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} attributeId - The attribute id.
* @param {object} input - Input value data.
* @param {string} input.value - The value text.
* @param {number} [input.sort] - Optional sort order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, attributeId: number, value: string, sort: number}>}
*/
return async (googleAuthId, attributeId, input = {}, tx) => {
const attrId = Number(attributeId);
if (!Number.isInteger(attrId) || attrId <= 0) {
throw new Error('Invalid attributeId');
}
const value = input.value?.trim();
if (!value) {
throw new Error('Value required');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
const attrRes = await runner.query(getAttrSql, { id: attrId, companyId });
if (!attrRes.rows[0]) {
throw new Error('Attribute name not found');
}
let sort = input.sort;
if (sort == null || !Number.isInteger(sort)) {
const next = await runner.query(getSortSql, { attributeId: attrId });
sort = next.rows[0].sort;
}
const result = await runner.query(createSql, {
attributeId: attrId,
value,
sort,
});
const id = result.rows[0]?.id ?? result.insertId;
return {
id,
attributeId: attrId,
value,
sort,
};
};
}

View File

@@ -0,0 +1,42 @@
import {
GET_BY_ID_AND_COMPANY as MSSQL_GET,
DELETE_BY_ID as MSSQL_DELETE,
} from '../../providers/mssql/queries/attributevalue.js';
import {
GET_BY_ID_AND_COMPANY as SQLITE_GET,
DELETE_BY_ID as SQLITE_DELETE,
} from '../../providers/sqlite3/queries/attributevalue.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function deleteAttributeValue(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 an attribute value.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The attribute value id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<void>}
*/
return async (googleAuthId, id, tx) => {
const valId = Number(id);
if (!Number.isInteger(valId) || valId <= 0) {
throw new Error('Invalid id');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
const existing = await runner.query(getSql, { id: valId, companyId });
if (!existing.rows[0]) {
throw new Error('Attribute value not found');
}
await runner.query(deleteSql, { id: valId });
};
}

View File

@@ -0,0 +1,34 @@
import { LIST_BY_ATTRIBUTE_ID as MSSQL_LIST } from '../../providers/mssql/queries/attributevalue.js';
import { LIST_BY_ATTRIBUTE_ID as SQLITE_LIST } from '../../providers/sqlite3/queries/attributevalue.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import makeRunner from '../runner.js';
export default function listByAttributeId(provider) {
const access = resolveSelectedCompanyAccess(provider);
const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST : MSSQL_LIST;
/**
* Lists the values for an attribute.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} attributeId - The attribute id.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<Array<{id: number, attributeId: number, value: string, sort: number}>>}
*/
return async (googleAuthId, attributeId, tx) => {
if (!attributeId || !Number.isInteger(Number(attributeId))) {
throw new Error('Invalid attributeId');
}
const { companyId } = await access(googleAuthId, tx);
const result = await makeRunner(provider, tx).query(sql, {
attributeId: Number(attributeId),
companyId,
});
return result.rows.map((row) => ({
id: row.id,
attributeId: row.attributeId,
value: row.value,
sort: row.sort,
}));
};
}

View File

@@ -0,0 +1,72 @@
import {
LIST_IDS_BY_ATTRIBUTE_ID as MSSQL_LIST_IDS,
UPDATE_SORT as MSSQL_UPDATE_SORT,
} from '../../providers/mssql/queries/attributevalue.js';
import {
LIST_IDS_BY_ATTRIBUTE_ID as SQLITE_LIST_IDS,
UPDATE_SORT as SQLITE_UPDATE_SORT,
} from '../../providers/sqlite3/queries/attributevalue.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 attribute values under an attribute.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} attributeId - The attribute id.
* @param {object} input - Reorder input.
* @param {Array<number>} input.attributevalueIds - Value IDs in their new order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{attributevalueIds: Array<number>}>}
*/
return async (googleAuthId, attributeId, { attributevalueIds } = {}, tx) => {
const attrId = Number(attributeId);
if (!Number.isInteger(attrId) || attrId <= 0) {
throw new Error('Invalid attributeId');
}
if (!Array.isArray(attributevalueIds)) {
throw new Error('Invalid attribute value order');
}
const runOrder = async (runner) => {
const { companyId } = await access(googleAuthId, runner);
const existingRows = await runner.query(listIdsSql, { attributeId: attrId, companyId });
const existing = new Set(existingRows.rows.map((r) => r.id));
if (attributevalueIds.length !== existing.size) {
throw new Error('Invalid attribute value order');
}
const seen = new Set();
for (const id of attributevalueIds) {
if (!Number.isFinite(id) || !existing.has(id) || seen.has(id)) {
throw new Error('Invalid attribute value order');
}
seen.add(id);
}
for (let sort = 0; sort < attributevalueIds.length; sort++) {
await runner.query(updateSortSql, {
id: attributevalueIds[sort],
sort,
});
}
return { attributevalueIds };
};
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,62 @@
import {
GET_BY_ID_AND_COMPANY as MSSQL_GET,
UPDATE_BY_ID as MSSQL_UPDATE,
} from '../../providers/mssql/queries/attributevalue.js';
import {
GET_BY_ID_AND_COMPANY as SQLITE_GET,
UPDATE_BY_ID as SQLITE_UPDATE,
} from '../../providers/sqlite3/queries/attributevalue.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 an attribute value.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @param {number} id - The attribute value id.
* @param {object} input - Update patch.
* @param {string} [input.value] - New value text.
* @param {number} [input.sort] - New sort order.
* @param {object} [tx] - Optional transaction.
* @returns {Promise<{id: number, attributeId: number, value: string, sort: number}>}
*/
return async (googleAuthId, id, input = {}, tx) => {
const valId = Number(id);
if (!Number.isInteger(valId) || valId <= 0) {
throw new Error('Invalid id');
}
const runner = makeRunner(provider, tx);
const { companyId } = await access(googleAuthId, tx);
const existing = await runner.query(getSql, { id: valId, companyId });
if (!existing.rows[0]) {
throw new Error('Attribute value not found');
}
const value = input.value !== undefined ? input.value?.trim() : existing.rows[0].value;
if (!value) {
throw new Error('Value required');
}
const sort = input.sort !== undefined && Number.isInteger(input.sort) ? input.sort : existing.rows[0].sort;
await runner.query(updateSql, {
id: valId,
value,
sort,
});
return {
id: valId,
attributeId: existing.rows[0].attributeId,
value,
sort,
};
};
}

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