u
This commit is contained in:
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,
|
||||
}));
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user