This commit is contained in:
seb
2026-09-22 07:25:16 +02:00
parent 4ed6260e91
commit cca131d83a
148 changed files with 5343 additions and 3466 deletions

View File

@@ -1,45 +1,37 @@
import { DELIVERY_TYPE, LIST_DELIVERIES, LIST_DELIVERY_FILES } from '../../providers/mssql/queries/delivery.js';
import request from '../request.js';
import * as mssqlQueries from '../../providers/mssql/queries/delivery.js';
import * as sqliteQueries from '../../providers/sqlite3/queries/delivery.js';
import makeRunner from '../runner.js';
import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js';
import { shapeSummary } from './helpers.js';
import { shapeAttachmentMeta } from './attachments.js';
export default function list(pool) {
const access = resolveSelectedCompanyAccess(pool);
export default function list(provider) {
const access = resolveSelectedCompanyAccess(provider);
const isSqlite = provider?.dialect === 'sqlite3';
const queries = isSqlite ? sqliteQueries : mssqlQueries;
/**
* Lists the deliveries of the user's selected company.
*
* @param {number} googleAuthId - The id of the Google auth user.
* @returns {Promise<Array<{id: number, number: string, type: string, entityId: number, externalNumber: string|null, date: string}>>} The deliveries.
*
* @example
* const deliveries = await store.delivery.list(42);
* // => [{ id, number, type, entityId, externalNumber, date }]
*/
return async (googleAuthId) => {
const { companyId } = await access(googleAuthId);
const runner = makeRunner(provider);
const result = await pool
.request()
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(LIST_DELIVERIES);
const result = await runner(queries.LIST_DELIVERIES, {
companyId,
type: queries.DELIVERY_TYPE,
});
const filesResult = await pool
.request()
.input('companyId', companyId)
.input('type', DELIVERY_TYPE)
.query(LIST_DELIVERY_FILES);
const filesResult = await runner(queries.LIST_DELIVERY_FILES, {
companyId,
type: queries.DELIVERY_TYPE,
});
const filesByManifest = new Map();
for (const row of filesResult.recordset) {
for (const row of filesResult.rows) {
const list = filesByManifest.get(row.manifestId) ?? [];
list.push(shapeAttachmentMeta(row));
filesByManifest.set(row.manifestId, list);
}
return result.recordset.map((row) => ({
return result.rows.map((row) => ({
...shapeSummary(row),
files: filesByManifest.get(row.id) ?? [],
}));