This commit is contained in:
seb
2026-10-15 00:30:23 +02:00
parent bdd7ebf5b8
commit 8c55f899b5
27 changed files with 136 additions and 46 deletions

View File

@@ -1,23 +1,40 @@
import { getStore } from './store.js';
import { formatAttachments } from '../util/manifestAttachment.js';
async function formatDelivery(delivery) {
if (!delivery) return delivery;
if (Array.isArray(delivery.files) && delivery.files.length > 0) {
const files = await formatAttachments(delivery.files);
return { ...delivery, files };
}
return delivery;
}
export async function list(googleAuthId) {
const store = await getStore();
return store.delivery.list(googleAuthId);
const result = await store.delivery.list(googleAuthId);
if (Array.isArray(result)) {
return Promise.all(result.map(formatDelivery));
}
return result;
}
export async function getById(googleAuthId, id) {
const store = await getStore();
return store.delivery.getById(googleAuthId, id);
const delivery = await store.delivery.getById(googleAuthId, id);
return formatDelivery(delivery);
}
export async function create(googleAuthId, data) {
const store = await getStore();
return store.delivery.create(googleAuthId, data);
const delivery = await store.delivery.create(googleAuthId, data);
return formatDelivery(delivery);
}
export async function update(googleAuthId, id, data) {
const store = await getStore();
return store.delivery.update(googleAuthId, id, data);
const delivery = await store.delivery.update(googleAuthId, id, data);
return formatDelivery(delivery);
}
export async function remove(googleAuthId, id) {
@@ -32,12 +49,14 @@ export async function getFile(googleAuthId, deliveryId, fileId) {
export async function addAttachment(googleAuthId, deliveryId, data) {
const store = await getStore();
return store.manifestattachment.add(googleAuthId, deliveryId, data);
const raw = await store.manifestattachment.add(googleAuthId, deliveryId, data);
return formatAttachments([raw]).then((r) => r[0]);
}
export async function listAttachments(googleAuthId, deliveryId) {
const store = await getStore();
return store.manifestattachment.listByManifestId(googleAuthId, deliveryId);
const raw = await store.manifestattachment.listByManifestId(googleAuthId, deliveryId);
return formatAttachments(raw);
}
export async function removeAttachment(googleAuthId, deliveryId, fileId) {

View File

@@ -1,4 +1,5 @@
import { getStore } from './store.js';
import { formatAttachments } from '../util/manifestAttachment.js';
function round2(num) {
return Math.round((Number(num) || 0) * 100) / 100;
@@ -46,13 +47,14 @@ function calculateShippedQuantity(shipments = []) {
async function enrichSingleOrder(store, googleAuthId, order) {
if (!order) return order;
const [shippings, payments, shipments, files] = await Promise.all([
const [shippings, payments, shipments, rawFiles] = await Promise.all([
store.shipping.listByManifestId(googleAuthId, order.id).catch(() => []),
store.payment.listByManifestId(googleAuthId, order.id).catch(() => []),
store.shipment.listByOrderId(googleAuthId, order.id).catch(() => []),
store.manifestattachment.listByManifestId(googleAuthId, order.id).catch(() => []),
]);
const files = await formatAttachments(rawFiles);
const orderLines = Array.isArray(order.lines) ? order.lines : [];
const paymentsList = Array.isArray(payments) ? payments : [];
const shipmentsList = Array.isArray(shipments) ? shipments : [];
@@ -165,12 +167,14 @@ export async function getFile(googleAuthId, orderId, fileId) {
export async function addAttachment(googleAuthId, orderId, data) {
const store = await getStore();
return store.manifestattachment.add(googleAuthId, orderId, data);
const raw = await store.manifestattachment.add(googleAuthId, orderId, data);
return formatAttachments([raw]).then((r) => r[0]);
}
export async function listAttachments(googleAuthId, orderId) {
const store = await getStore();
return store.manifestattachment.listByManifestId(googleAuthId, orderId);
const raw = await store.manifestattachment.listByManifestId(googleAuthId, orderId);
return formatAttachments(raw);
}
export async function removeAttachment(googleAuthId, orderId, fileId) {

View File

@@ -1,4 +1,5 @@
import { getStore } from './store.js';
import { formatAttachments } from '../util/manifestAttachment.js';
function round2(num) {
return Math.round((Number(num) || 0) * 100) / 100;
@@ -47,13 +48,19 @@ export async function list(googleAuthId, opts) {
const result = await store.quote.list(googleAuthId, opts);
if (Array.isArray(result)) {
const filesList = await Promise.all(
result.map((q) => store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []))
result.map(async (q) => {
const raw = await store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []);
return formatAttachments(raw);
})
);
return result.map((q, i) => enrichQuote(q, filesList[i]));
}
if (result && Array.isArray(result.quotes)) {
const filesList = await Promise.all(
result.quotes.map((q) => store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []))
result.quotes.map(async (q) => {
const raw = await store.manifestattachment.listByManifestId(googleAuthId, q.id).catch(() => []);
return formatAttachments(raw);
})
);
return {
...result,
@@ -65,10 +72,11 @@ export async function list(googleAuthId, opts) {
export async function getById(googleAuthId, id) {
const store = await getStore();
const [quote, files] = await Promise.all([
const [quote, rawFiles] = await Promise.all([
store.quote.getById(googleAuthId, id),
store.manifestattachment.listByManifestId(googleAuthId, id).catch(() => []),
]);
const files = await formatAttachments(rawFiles);
return enrichQuote(quote, files);
}
@@ -80,10 +88,11 @@ export async function create(googleAuthId, data) {
export async function update(googleAuthId, id, data) {
const store = await getStore();
const [quote, files] = await Promise.all([
const [quote, rawFiles] = await Promise.all([
store.quote.update(googleAuthId, id, data),
store.manifestattachment.listByManifestId(googleAuthId, id).catch(() => []),
]);
const files = await formatAttachments(rawFiles);
return enrichQuote(quote, files);
}
@@ -104,12 +113,14 @@ export async function getFile(googleAuthId, quoteId, fileId) {
export async function addAttachment(googleAuthId, quoteId, data) {
const store = await getStore();
return store.manifestattachment.add(googleAuthId, quoteId, data);
const raw = await store.manifestattachment.add(googleAuthId, quoteId, data);
return formatAttachments([raw]).then((r) => r[0]);
}
export async function listAttachments(googleAuthId, quoteId) {
const store = await getStore();
return store.manifestattachment.listByManifestId(googleAuthId, quoteId);
const raw = await store.manifestattachment.listByManifestId(googleAuthId, quoteId);
return formatAttachments(raw);
}
export async function removeAttachment(googleAuthId, quoteId, fileId) {

View File

@@ -1,4 +1,5 @@
import { getStore } from './store.js';
import { formatAttachments } from '../util/manifestAttachment.js';
export async function create(googleAuthId, data) {
const store = await getStore();
@@ -7,11 +8,12 @@ export async function create(googleAuthId, data) {
export async function getById(googleAuthId, id) {
const store = await getStore();
const [shipment, files] = await Promise.all([
const [shipment, rawFiles] = await Promise.all([
store.shipment.getById(googleAuthId, id),
store.manifestattachment.listByManifestId(googleAuthId, id).catch(() => []),
]);
if (!shipment) return shipment;
const files = await formatAttachments(rawFiles);
return {
...shipment,
files: Array.isArray(files) ? files : [],
@@ -35,12 +37,14 @@ export async function getFile(googleAuthId, shipmentId, fileId) {
export async function addAttachment(googleAuthId, shipmentId, data) {
const store = await getStore();
return store.manifestattachment.add(googleAuthId, shipmentId, data);
const raw = await store.manifestattachment.add(googleAuthId, shipmentId, data);
return formatAttachments([raw]).then((r) => r[0]);
}
export async function listAttachments(googleAuthId, shipmentId) {
const store = await getStore();
return store.manifestattachment.listByManifestId(googleAuthId, shipmentId);
const raw = await store.manifestattachment.listByManifestId(googleAuthId, shipmentId);
return formatAttachments(raw);
}
export async function removeAttachment(googleAuthId, shipmentId, fileId) {