import { LIST_BY_MANIFEST } from '../../sql/manifestattachment.js'; import resolveSelectedCompanyAccess from '../companyuser/resolveSelectedCompanyAccess.js'; import resolveManifestAccess from './resolveManifestAccess.js'; import request from '../request.js'; /** Parse the file.meta JSON text column into an object (string key/value). */ export function parseMeta(raw) { if (raw == null) return null; if (typeof raw === 'object') return raw; try { const parsed = JSON.parse(raw); return typeof parsed === 'object' && parsed !== null ? parsed : null; } catch { return null; } } function shape(row) { return { id: row.id, manifestId: row.manifestId, fileId: row.fileId, sort: row.sort, hash: row.hash, mimetype: row.mimetype, name: row.name, size: Number(row.size), created: row.created, modified: row.modified, thumbavif: row.thumbavif ?? null, thumbx: row.thumbx ?? null, thumby: row.thumby ?? null, meta: parseMeta(row.meta), }; } export default function listByManifestId(pool) { const access = resolveSelectedCompanyAccess(pool); const manifestAccess = resolveManifestAccess(pool); /** * Lists all attachments for a given manifest, without loading their full binary content. * * @param {number} googleAuthId - The id of the Google auth user. * @param {number} manifestId - The manifest ID. * @returns {Promise>} A list of attachment metadata objects. * @throws {Error} If the manifest is not found or the user is not authorized. * * @example * const attachments = await store.manifestattachment.listByManifestId(42, 1); * // => [{ id, manifestId, fileId, sort, hash, mimetype, name, size, created, modified, thumbavif, thumbx, thumby }] */ return async (googleAuthId, manifestId) => { const { companyId } = await access(googleAuthId); await manifestAccess(companyId, manifestId); const res = await request(pool) .input('manifestId', manifestId) .query(LIST_BY_MANIFEST); return res.recordset.map(shape); }; }