import { LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY } from '../../providers/mssql/queries/unit.js'; import { LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY } from '../../providers/sqlite3/queries/unit.js'; import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js'; import shapeUnit from './shapeUnit.js'; import makeRunner from '../runner.js'; export default function list(provider) { const access = resolveSelectedCompanyAccess(provider); const sql = provider?.dialect === 'sqlite3' ? SQLITE_LIST_BY_COMPANY : MSSQL_LIST_BY_COMPANY; /** * Lists the units available for the user's selected company. * * @param {number} googleAuthId - The id of the Google auth user. * @param {object} [tx] - Optional transaction to run within. * @returns {Promise>} The units. * * @example * const units = await store.unit.list(42); * // => [{ id, si, basesi, basesifactor, symbol, name }] */ return async (googleAuthId, tx) => { const runner = makeRunner(provider, tx); const { companyId, languageId } = await access(googleAuthId, tx); const result = await runner.query(sql, { companyId, languageId, }); return result.rows.map(shapeUnit); }; }