Skip to content

Commit

Permalink
Export format
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 19, 2024
1 parent b2a22fe commit 9b4b19d
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions packages/jobs/src/transactions/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,22 @@ client.defineJob({
});

const attachments = await Promise.allSettled(
data?.flatMap((transaction, idx) => {
(data ?? []).flatMap((transaction, idx) => {
const rowId = idx + 1;

return transaction?.attachments?.map(
return (transaction.attachments ?? []).map(
async (attachment, idx2: number) => {
const extension = attachment.name.split(".").pop();
const filename = attachment.name?.split(".").at(0);
const extension = attachment.name?.split(".").at(-1);

const name =
idx2 > 0
? `${rowId}_${idx2}.${extension}`
: `${rowId}.${extension}`;
? `${filename}-${rowId}_${idx2}.${extension}`
: `${filename}-${rowId}.${extension}`;

const { data } = await download(client, {
bucket: "vault",
path: attachment.path.join("/"),
path: (attachment.path ?? []).join("/"),
});

return {
Expand All @@ -114,10 +116,11 @@ client.defineJob({
},
});

console.log(attachments);

const rows = data
?.sort((a, b) => a.date - b.date)
?.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime())
.map((transaction, idx) => [
transaction?.id,
transaction.date,
transaction.name,
Intl.NumberFormat(locale, {
Expand All @@ -132,24 +135,31 @@ client.defineJob({
: "",
transaction?.category?.name ?? "",
transaction?.category?.description ?? "",
transaction?.attachments?.length > 0 ? `${idx + 1}.pdf` : null,
transaction?.attachments?.length > 0 ? "✔️" : "❌",

attachments
.filter(
(a) => a.status === "fulfilled" && a.value?.id === transaction.id,
)
.map((a) => a.value?.name)
.filter(Boolean)
.join(", ") ?? "",

transaction?.balance ?? "",
transaction?.bank_account?.name ?? "",
transaction?.note ?? "",
]);

const csv = await writeToString(rows, {
headers: [
"ID",
"Date",
"Description",
"Amount",
"VAT",
"Category",
"Category description",
"Attachment name",
"Attachment",
"Status",
"Attachments",
"Balance",
"Account",
"Note",
Expand Down

0 comments on commit 9b4b19d

Please sign in to comment.