u
This commit is contained in:
74
src/store/attributevalue/create.js
Normal file
74
src/store/attributevalue/create.js
Normal 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,
|
||||
};
|
||||
};
|
||||
}
|
||||
42
src/store/attributevalue/delete.js
Normal file
42
src/store/attributevalue/delete.js
Normal 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 });
|
||||
};
|
||||
}
|
||||
34
src/store/attributevalue/listByAttributeId.js
Normal file
34
src/store/attributevalue/listByAttributeId.js
Normal 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,
|
||||
}));
|
||||
};
|
||||
}
|
||||
72
src/store/attributevalue/order.js
Normal file
72
src/store/attributevalue/order.js
Normal 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));
|
||||
};
|
||||
}
|
||||
62
src/store/attributevalue/update.js
Normal file
62
src/store/attributevalue/update.js
Normal 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,
|
||||
};
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user