lariv-rs 0.1.0

Compile-time plugin web application framework built on Axum, SeaORM, Maud, and HTMX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::collections::HashSet;
use std::io::{Cursor, Write};

use axum::{
    body::Body,
    extract::{Path, Query},
    http::{StatusCode, header},
    response::{IntoResponse, Redirect, Response},
};
use maud::{Markup, html};
use zip::write::SimpleFileOptions;

use crate::{
    components::{ButtonDownload, button_download, modal_keyed},
    http::Cap,
    plugins::filesystem::state::FilesystemState,
    plugins::users::middleware::RequireAuth,
};

use crate::plugins::finance_common::require_superuser;

use crate::plugins::finance_invoices::logic::invoice_pdf::{
    InvoicePdfError, InvoicePdfResult, render_cancelled_invoice_pdf, render_draft_invoice_pdf,
    render_paid_invoice_pdf, render_partially_paid_invoice_pdf, render_posted_invoice_pdf,
};
use crate::plugins::finance_invoices::{
    keys::InvoicePdfModalKey,
    routes::{
        CancelledInvoicePdfRouteTag, DraftInvoicePdfRouteTag, PaidInvoicePdfRouteTag,
        PartiallyPaidInvoicePdfRouteTag, PostedInvoicePdfRouteTag,
    },
    scope::{
        find_active_draft, find_active_paid, find_active_partial, find_active_posted, hub_tab_url,
    },
    state::InvoicesState,
};

fn pdf_error_response(err: InvoicePdfError) -> Response {
    match err {
        InvoicePdfError::NotFound => (StatusCode::NOT_FOUND, "Invoice not found").into_response(),
        InvoicePdfError::Message(msg) if msg.contains("Configure the invoice PDF template") => {
            (StatusCode::BAD_REQUEST, msg).into_response()
        }
        InvoicePdfError::Message(msg) => {
            tracing::error!("invoice pdf: {msg}");
            (StatusCode::INTERNAL_SERVER_ERROR, msg).into_response()
        }
    }
}

fn pdf_ok_response(
    result: crate::plugins::finance_invoices::logic::invoice_pdf::InvoicePdfResult,
) -> Response {
    let filename = format!("{}.pdf", result.filename_base);
    (
        StatusCode::OK,
        [
            (header::CONTENT_TYPE, "application/pdf".to_string()),
            (
                header::CONTENT_DISPOSITION,
                format!("inline; filename=\"{filename}\""),
            ),
        ],
        Body::from(result.bytes),
    )
        .into_response()
}

fn render_pdf_modal(title: &str, pdf_url: &str) -> Markup {
    modal_keyed::<InvoicePdfModalKey>(
        "max-w-6xl w-[95vw]",
        html! {
            div class="flex items-center justify-between gap-3 mb-3 pr-10" {
                h3 class="text-lg font-semibold" { (title) }
                (button_download(ButtonDownload {
                    label: "Download",
                    href: pdf_url,
                    classes: "btn-outline btn-sm",
                    ..Default::default()
                }))
            }
            div class="relative w-full h-[75vh]" x-data="{ loading: true }" {
                div
                    class="absolute inset-0 z-10 flex flex-col items-center justify-center gap-3 rounded border border-base-300 bg-base-100"
                    x-show="loading"
                    x-cloak
                {
                    span class="loading loading-spinner loading-lg" {}
                    p class="text-sm opacity-70" { "Generating PDF…" }
                }
                iframe
                    src=(pdf_url)
                    class="w-full h-full border border-base-300 rounded bg-white"
                    title=(title)
                    x-on:load="loading = false" {}
            }
        },
    )
}

fn render_pdf_modal_error(message: &str) -> Markup {
    modal_keyed::<InvoicePdfModalKey>(
        "max-w-2xl",
        html! {
            h3 class="text-lg font-semibold mb-2" { "Invoice PDF failed" }
            p class="text-error whitespace-pre-wrap" { (message) }
        },
    )
}

/// GET modal: show invoice PDF in an iframe (bytes still served by the `/pdf/` file routes).
pub async fn draft_pdf_modal(
    Cap(state): Cap<InvoicesState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Markup {
    if !require_superuser(&ctx) {
        return render_pdf_modal_error("Forbidden");
    }
    if find_active_draft(&state.db, id).await.is_none() {
        return render_pdf_modal_error("Draft invoice not found");
    }
    render_pdf_modal(
        &format!("Draft invoice #{id} PDF"),
        &DraftInvoicePdfRouteTag::new(id).path(),
    )
}

pub async fn posted_pdf_modal(
    Cap(state): Cap<InvoicesState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Markup {
    if !require_superuser(&ctx) {
        return render_pdf_modal_error("Forbidden");
    }
    if find_active_posted(&state.db, id).await.is_none() {
        return render_pdf_modal_error("Posted invoice not found");
    }
    render_pdf_modal(
        &format!("Posted invoice #{id} PDF"),
        &PostedInvoicePdfRouteTag::new(id).path(),
    )
}

pub async fn cancelled_pdf_modal(RequireAuth(ctx): RequireAuth, Path(id): Path<i64>) -> Markup {
    if !require_superuser(&ctx) {
        return render_pdf_modal_error("Forbidden");
    }
    render_pdf_modal(
        &format!("Cancelled invoice #{id} PDF"),
        &CancelledInvoicePdfRouteTag::new(id).path(),
    )
}

pub async fn paid_pdf_modal(
    Cap(state): Cap<InvoicesState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Markup {
    if !require_superuser(&ctx) {
        return render_pdf_modal_error("Forbidden");
    }
    if find_active_paid(&state.db, id).await.is_none() {
        return render_pdf_modal_error("Paid invoice not found");
    }
    render_pdf_modal(
        &format!("Paid invoice #{id} PDF"),
        &PaidInvoicePdfRouteTag::new(id).path(),
    )
}

pub async fn partially_paid_pdf_modal(
    Cap(state): Cap<InvoicesState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Markup {
    if !require_superuser(&ctx) {
        return render_pdf_modal_error("Forbidden");
    }
    if find_active_partial(&state.db, id).await.is_none() {
        return render_pdf_modal_error("Partially paid invoice not found");
    }
    render_pdf_modal(
        &format!("Partially paid invoice #{id} PDF"),
        &PartiallyPaidInvoicePdfRouteTag::new(id).path(),
    )
}

pub async fn draft_pdf(
    Cap(state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    if find_active_draft(&state.db, id).await.is_none() {
        return Redirect::to(&hub_tab_url("drafts")).into_response();
    }
    match render_draft_invoice_pdf(&fs, id, &ctx.timezone).await {
        Ok(result) => pdf_ok_response(result),
        Err(e) => pdf_error_response(e),
    }
}

pub async fn posted_pdf(
    Cap(state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    let Some(posted) = find_active_posted(&state.db, id).await else {
        return Redirect::to(&hub_tab_url("posted")).into_response();
    };
    match render_posted_invoice_pdf(&fs, posted, &ctx.timezone).await {
        Ok(result) => pdf_ok_response(result),
        Err(e) => pdf_error_response(e),
    }
}

pub async fn cancelled_pdf(
    Cap(_state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    match render_cancelled_invoice_pdf(&fs, id, &ctx.timezone).await {
        Ok(result) => pdf_ok_response(result),
        Err(e) => pdf_error_response(e),
    }
}

pub async fn paid_pdf(
    Cap(state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    if find_active_paid(&state.db, id).await.is_none() {
        return Redirect::to(&hub_tab_url("paid")).into_response();
    }
    match render_paid_invoice_pdf(&fs, id, &ctx.timezone).await {
        Ok(result) => pdf_ok_response(result),
        Err(e) => pdf_error_response(e),
    }
}

pub async fn partially_paid_pdf(
    Cap(state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Path(id): Path<i64>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    if find_active_partial(&state.db, id).await.is_none() {
        return Redirect::to(&hub_tab_url("partial")).into_response();
    }
    match render_partially_paid_invoice_pdf(&fs, id, &ctx.timezone).await {
        Ok(result) => pdf_ok_response(result),
        Err(e) => pdf_error_response(e),
    }
}

#[derive(Debug, serde::Deserialize, Default)]
pub struct BulkPdfsQuery {
    #[serde(default)]
    pub ids: Option<String>,
    #[serde(default)]
    pub tab: Option<String>,
}

fn parse_bulk_ids(raw: &str) -> Vec<i64> {
    let mut ids: Vec<i64> = raw
        .split(',')
        .filter_map(|p| p.trim().parse().ok())
        .filter(|id| *id > 0)
        .collect();
    ids.sort_unstable();
    ids.dedup();
    ids
}

fn allocate_zip_pdf_name(base: &str, used: &mut HashSet<String>) -> String {
    let mut name = format!("{base}.pdf");
    if used.insert(name.clone()) {
        return name;
    }
    let mut n = 2u32;
    loop {
        name = format!("{base}-{n}.pdf");
        if used.insert(name.clone()) {
            return name;
        }
        n = n.saturating_add(1);
    }
}

fn zip_pdfs(entries: &[(String, Vec<u8>)]) -> Result<Vec<u8>, String> {
    let mut buf = Vec::new();
    {
        let mut writer = zip::ZipWriter::new(Cursor::new(&mut buf));
        let options = SimpleFileOptions::default();
        for (name, bytes) in entries {
            writer
                .start_file(name, options)
                .map_err(|e| e.to_string())?;
            writer.write_all(bytes).map_err(|e| e.to_string())?;
        }
        writer.finish().map_err(|e| e.to_string())?;
    }
    Ok(buf)
}

fn zip_ok_response(filename: &str, bytes: Vec<u8>) -> Response {
    (
        StatusCode::OK,
        [
            (header::CONTENT_TYPE, "application/zip".to_string()),
            (
                header::CONTENT_DISPOSITION,
                format!("attachment; filename=\"{filename}\""),
            ),
        ],
        Body::from(bytes),
    )
        .into_response()
}

async fn render_bulk_tab_pdf(
    state: &InvoicesState,
    fs: &FilesystemState,
    tab: &str,
    id: i64,
    tz: &str,
) -> Result<InvoicePdfResult, InvoicePdfError> {
    match tab {
        "drafts" => {
            if find_active_draft(&state.db, id).await.is_none() {
                return Err(InvoicePdfError::NotFound);
            }
            render_draft_invoice_pdf(fs, id, tz).await
        }
        "posted" => {
            let Some(posted) = find_active_posted(&state.db, id).await else {
                return Err(InvoicePdfError::NotFound);
            };
            render_posted_invoice_pdf(fs, posted, tz).await
        }
        "cancelled" => render_cancelled_invoice_pdf(fs, id, tz).await,
        "paid" => {
            if find_active_paid(&state.db, id).await.is_none() {
                return Err(InvoicePdfError::NotFound);
            }
            render_paid_invoice_pdf(fs, id, tz).await
        }
        "partial" => {
            if find_active_partial(&state.db, id).await.is_none() {
                return Err(InvoicePdfError::NotFound);
            }
            render_partially_paid_invoice_pdf(fs, id, tz).await
        }
        _ => Err(InvoicePdfError::Message(format!(
            "Unknown invoice tab: {tab}"
        ))),
    }
}

/// GET: zip of PDFs for selected hub invoices (`?tab=…&ids=1,2,3`).
pub async fn bulk_pdfs(
    Cap(state): Cap<InvoicesState>,
    Cap(fs): Cap<FilesystemState>,
    RequireAuth(ctx): RequireAuth,
    Query(q): Query<BulkPdfsQuery>,
) -> Response {
    if !require_superuser(&ctx) {
        return StatusCode::FORBIDDEN.into_response();
    }
    let tab = q.tab.as_deref().unwrap_or("drafts").trim();
    let tab = match tab {
        "posted" | "cancelled" | "paid" | "partial" | "drafts" => tab,
        _ => "drafts",
    };
    let ids = parse_bulk_ids(q.ids.as_deref().unwrap_or(""));
    if ids.is_empty() {
        return (StatusCode::BAD_REQUEST, "No invoices selected").into_response();
    }

    let mut entries: Vec<(String, Vec<u8>)> = Vec::with_capacity(ids.len());
    let mut used_names = HashSet::new();
    for id in ids {
        match render_bulk_tab_pdf(&state, &fs, tab, id, &ctx.timezone).await {
            Ok(result) => {
                let name = allocate_zip_pdf_name(&result.filename_base, &mut used_names);
                entries.push((name, result.bytes));
            }
            Err(e) => return pdf_error_response(e),
        }
    }

    match zip_pdfs(&entries) {
        Ok(bytes) => zip_ok_response(&format!("invoices-{tab}.zip"), bytes),
        Err(e) => {
            tracing::error!(error = %e, "failed to build invoice pdf zip");
            (StatusCode::INTERNAL_SERVER_ERROR, e).into_response()
        }
    }
}