75 lines
3.1 KiB
JavaScript
75 lines
3.1 KiB
JavaScript
import {
|
|
LIST_BY_COMPANY as MSSQL_LIST_BY_COMPANY,
|
|
LIST_BY_COMPANY_AND_AGENTPRINTER as MSSQL_LIST_BY_COMPANY_AND_AGENTPRINTER,
|
|
LIST_BY_AGENT as MSSQL_LIST_BY_AGENT,
|
|
LIST_BY_AGENT_AND_STATE as MSSQL_LIST_BY_AGENT_AND_STATE,
|
|
} from '../../providers/mssql/queries/printerjob.js';
|
|
import {
|
|
LIST_BY_COMPANY as SQLITE_LIST_BY_COMPANY,
|
|
LIST_BY_COMPANY_AND_AGENTPRINTER as SQLITE_LIST_BY_COMPANY_AND_AGENTPRINTER,
|
|
LIST_BY_AGENT as SQLITE_LIST_BY_AGENT,
|
|
LIST_BY_AGENT_AND_STATE as SQLITE_LIST_BY_AGENT_AND_STATE,
|
|
} from '../../providers/sqlite3/queries/printerjob.js';
|
|
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
|
|
import makeRunner from '../runner.js';
|
|
import { shape } from './_shared.js';
|
|
|
|
export default function list(provider) {
|
|
const access = resolveSelectedCompanyAccess(provider);
|
|
const isSqlite = provider?.dialect === 'sqlite3';
|
|
const listCompanySql = isSqlite ? SQLITE_LIST_BY_COMPANY : MSSQL_LIST_BY_COMPANY;
|
|
const listCompanyApSql = isSqlite ? SQLITE_LIST_BY_COMPANY_AND_AGENTPRINTER : MSSQL_LIST_BY_COMPANY_AND_AGENTPRINTER;
|
|
const listAgentSql = isSqlite ? SQLITE_LIST_BY_AGENT : MSSQL_LIST_BY_AGENT;
|
|
const listAgentStateSql = isSqlite ? SQLITE_LIST_BY_AGENT_AND_STATE : MSSQL_LIST_BY_AGENT_AND_STATE;
|
|
|
|
/**
|
|
* Lists printer jobs.
|
|
* If caller is an agent ({ agentId: number }), lists jobs for that agent.
|
|
* If caller is a user (googleAuthId: number), lists jobs for the selected company.
|
|
*
|
|
* @param {number|{agentId: number}} auth - User's googleAuthId or { agentId }.
|
|
* @param {object} [filters]
|
|
* @param {number} [filters.agentprinterId] - Filter by agentprinterId (company callers only).
|
|
* @param {string} [filters.state] - Filter by job state (e.g. "pending").
|
|
* @param {object} [tx] - Optional transaction.
|
|
* @returns {Promise<Array<object>>}
|
|
*/
|
|
return async (auth, filters = {}, tx) => {
|
|
const runner = makeRunner(provider, tx);
|
|
|
|
if (typeof auth === 'object' && auth !== null && auth.agentId != null) {
|
|
const aId = Number(auth.agentId);
|
|
if (!Number.isInteger(aId) || aId <= 0) return [];
|
|
const state = typeof filters.state === 'string' && filters.state.trim()
|
|
? filters.state.trim().toLowerCase()
|
|
: null;
|
|
|
|
let result;
|
|
if (state) {
|
|
result = await runner.query(listAgentStateSql, { agentId: aId, state });
|
|
} else {
|
|
result = await runner.query(listAgentSql, { agentId: aId });
|
|
}
|
|
return result.rows.map(shape);
|
|
}
|
|
|
|
const { companyId } = await access(auth, runner);
|
|
const apId = filters.agentprinterId != null ? Number(filters.agentprinterId) : null;
|
|
|
|
let result;
|
|
if (apId && Number.isInteger(apId) && apId > 0) {
|
|
result = await runner.query(listCompanyApSql, { companyId, agentprinterId: apId });
|
|
} else {
|
|
result = await runner.query(listCompanySql, { companyId });
|
|
}
|
|
|
|
let rows = result.rows.map(shape);
|
|
if (filters.state) {
|
|
const targetState = String(filters.state).trim().toLowerCase();
|
|
rows = rows.filter((r) => r.state === targetState);
|
|
}
|
|
return rows;
|
|
};
|
|
}
|
|
|