refactor: retire Auto Import schedules, add Import Guide + duplicate notice

- Auto Import page replaced with a static Import Guide: step-by-step
  instructions for CGD/Trade Republic/generic CSV export, duplicate
  detection explanation, and a curl example for headless automation
- Removed POST /auto-import and DELETE /auto-import/{id} routes and
  their handler logic; schedule CRUD was misleading since no bank
  exposes an unauthenticated CSV endpoint
- Nav label changed from "Auto Import" to "Import Guide"
- Transactions page now renders an amber banner when redirected with
  ?notice=all_duplicates (every row in the uploaded file was skipped)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gonçalo Rodrigues 2026-06-13 18:20:48 +01:00
parent 2170457528
commit 7f7e696b4d
4 changed files with 94 additions and 154 deletions

View File

@ -642,19 +642,20 @@ func (h *Handler) Transactions(w http.ResponseWriter, r *http.Request) {
} }
render(w, txnsTmpl, map[string]interface{}{ render(w, txnsTmpl, map[string]interface{}{
"UserID": a.UserID, "UserID": a.UserID,
"Email": a.Email, "Email": a.Email,
"Title": "Transactions", "Title": "Transactions",
"Route": "transactions", "Route": "transactions",
"IsOwner": true, "IsOwner": true,
"Txns": txns, "Txns": txns,
"Categories": cats, "Categories": cats,
"Accounts": accounts, "Accounts": accounts,
"AccountNames": accountNames, "AccountNames": accountNames,
"CategoryColors": catColors, "CategoryColors": catColors,
"Cat": cat, "Cat": cat,
"Search": search, "Search": search,
"Days": daysStr, "Days": daysStr,
"Notice": r.URL.Query().Get("notice"),
}) })
} }
@ -2241,48 +2242,13 @@ func (h *Handler) Household(w http.ResponseWriter, r *http.Request) {
func (h *Handler) AutoImport(w http.ResponseWriter, r *http.Request) { func (h *Handler) AutoImport(w http.ResponseWriter, r *http.Request) {
auth := getAuth(r) auth := getAuth(r)
ctx := r.Context() accounts, _ := h.store.getAccounts(r.Context(), auth.UserID)
if r.Method == http.MethodPost {
_ = r.ParseForm()
sched := &ImportSchedule{
ID: bson.NewObjectID().Hex(),
UserID: auth.UserID,
AccountID: r.FormValue("account_id"),
Label: r.FormValue("label"),
Format: r.FormValue("format"),
URL: r.FormValue("url"),
Active: r.FormValue("active") == "on",
CreatedAt: time.Now(),
}
if err := h.store.createImportSchedule(ctx, sched); err != nil {
http.Error(w, "failed to create schedule", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/auto-import", http.StatusSeeOther)
return
}
if r.Method == http.MethodDelete {
id := r.PathValue("id")
if err := h.store.deleteImportSchedule(ctx, id, auth.UserID); err != nil {
http.Error(w, "failed to delete schedule", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
return
}
schedules, _ := h.store.getImportSchedules(ctx, auth.UserID)
accounts, _ := h.store.getAccounts(ctx, auth.UserID)
render(w, autoImportTmpl, &AutoImportData{ render(w, autoImportTmpl, &AutoImportData{
UserID: auth.UserID, UserID: auth.UserID,
Email: auth.Email, Email: auth.Email,
Title: "Auto Import", Title: "Import Guide",
Route: "/auto-import", Route: "/auto-import",
Accounts: accounts, Accounts: accounts,
Schedules: schedules,
}) })
} }
@ -2320,8 +2286,6 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("POST /household", h.Household) mux.HandleFunc("POST /household", h.Household)
mux.HandleFunc("DELETE /household", h.Household) mux.HandleFunc("DELETE /household", h.Household)
mux.HandleFunc("GET /auto-import", h.AutoImport) mux.HandleFunc("GET /auto-import", h.AutoImport)
mux.HandleFunc("POST /auto-import", h.AutoImport)
mux.HandleFunc("DELETE /auto-import/{id}", h.AutoImport)
} }
func sortStrings(s []string) { func sortStrings(s []string) {

View File

@ -3,110 +3,80 @@
{{define "content"}} {{define "content"}}
{{$d := .}} {{$d := .}}
<style> <style>
.ai-form { background:var(--card); border:1px solid var(--border); border-radius:12px; padding:24px; max-width:560px; margin-bottom:32px; } .guide-step { background:var(--card); border:1px solid var(--border); border-radius:12px; padding:20px 24px; margin-bottom:16px; display:flex; gap:16px; }
.ai-form h2 { margin:0 0 16px; font-size:1.05rem; } .step-num { font-size:1.2rem; font-weight:800; color:var(--accent); min-width:28px; }
.field { margin-bottom:14px; } .step-body h3 { margin:0 0 6px; font-size:0.95rem; font-weight:700; }
.field label { display:block; font-size:0.82rem; color:var(--muted); margin-bottom:5px; } .step-body p { margin:0; font-size:0.85rem; color:var(--muted); line-height:1.5; }
.field input, .field select { width:100%; padding:8px 12px; border:1px solid var(--border); border-radius:8px; background:var(--bg); color:var(--text); font-size:0.9rem; box-sizing:border-box; } .format-grid { display:grid; grid-template-columns:repeat(auto-fill, minmax(200px, 1fr)); gap:12px; margin-top:12px; }
.field-row { display:flex; gap:12px; } .format-card { background:var(--bg); border:1px solid var(--border); border-radius:10px; padding:14px 16px; }
.field-row .field { flex:1; } .format-card .name { font-weight:700; font-size:0.9rem; margin-bottom:4px; }
.btn { padding:8px 20px; border:none; border-radius:8px; font-size:0.9rem; font-weight:600; cursor:pointer; } .format-card .desc { font-size:0.8rem; color:var(--muted); }
.btn-primary { background:var(--accent); color:#fff; } .tip { background:rgba(99,102,241,0.08); border:1px solid rgba(99,102,241,0.25); border-radius:10px; padding:14px 18px; font-size:0.85rem; color:var(--text); margin-top:20px; }
.btn-danger { background:#f44336; color:#fff; font-size:0.8rem; padding:5px 12px; } .tip strong { color:var(--accent); }
.sched-list { display:flex; flex-direction:column; gap:12px; } code { font-family:monospace; font-size:0.82rem; background:var(--bg); border:1px solid var(--border); border-radius:5px; padding:1px 6px; }
.sched-card { background:var(--card); border:1px solid var(--border); border-radius:12px; padding:16px 20px; display:flex; gap:16px; align-items:center; }
.sched-info { flex:1; }
.sched-label { font-weight:600; margin-bottom:4px; }
.sched-meta { font-size:0.8rem; color:var(--muted); }
.badge { display:inline-block; padding:2px 8px; border-radius:20px; font-size:0.72rem; font-weight:600; margin-left:6px; }
.badge-active { background:rgba(76,175,80,0.15); color:#4caf50; }
.badge-inactive { background:rgba(158,158,158,0.15); color:#9e9e9e; }
.empty { padding:32px; text-align:center; color:var(--muted); background:var(--card); border:1px solid var(--border); border-radius:12px; }
</style> </style>
<h1 style="margin:0 0 24px;">Auto Import</h1> <div style="display:flex; align-items:center; gap:14px; margin-bottom:28px;">
<div>
<div class="ai-form"> <h1 style="margin:0 0 4px;">Import Guide</h1>
<h2>New Schedule</h2> <p style="margin:0; font-size:0.85rem; color:var(--muted);">How to get your bank transactions into the app</p>
<p style="font-size:0.83rem; color:var(--muted); margin:0 0 18px;">
Configure a recurring CSV import source. The app will fetch and import it automatically on a daily schedule.
</p>
<form method="post" action="/auto-import">
<div class="field">
<label for="ai-label">Schedule label</label>
<input type="text" id="ai-label" name="label" placeholder="e.g. CGD Checking Monthly" required>
</div>
<div class="field-row">
<div class="field">
<label for="ai-account">Account</label>
<select id="ai-account" name="account_id" required>
<option value="">-- select --</option>
{{range $d.Accounts}}
<option value="{{.ID}}">{{.Name}} ({{.Type}})</option>
{{end}}
</select>
</div>
<div class="field">
<label for="ai-format">CSV format</label>
<select id="ai-format" name="format">
<option value="generic">Generic</option>
<option value="cgd">CGD</option>
<option value="traderepublic">Trade Republic</option>
</select>
</div>
</div>
<div class="field">
<label for="ai-url">Source URL <span style="color:var(--muted); font-weight:400;">(optional — or use manual webhook)</span></label>
<input type="url" id="ai-url" name="url" placeholder="https://…/export.csv">
</div>
<div class="field" style="display:flex; align-items:center; gap:8px; margin-bottom:18px;">
<input type="checkbox" id="ai-active" name="active" checked style="width:auto; margin:0;">
<label for="ai-active" style="margin:0; color:var(--text); font-size:0.9rem;">Enable immediately</label>
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Create Schedule</button>
</form>
</div>
<div style="font-size:1rem; font-weight:700; margin-bottom:12px;">Active Schedules</div>
{{if $d.Schedules}}
<div class="sched-list">
{{range $d.Schedules}}
<div class="sched-card">
<div class="sched-info">
<div class="sched-label">
{{.Label}}
{{if .Active}}<span class="badge badge-active">active</span>{{else}}<span class="badge badge-inactive">paused</span>{{end}}
</div>
<div class="sched-meta">
Format: {{.Format}} &bull; Account: {{.AccountID}}
{{if .URL}}&bull; <span style="font-family:monospace;">{{.URL}}</span>{{end}}
{{if not .LastRunAt.IsZero}}&bull; Last run: {{dateShort .LastRunAt}}{{end}}
</div>
</div>
<button class="btn btn-danger" onclick="deleteSchedule('{{.ID}}')">Delete</button>
</div> </div>
{{end}} <a href="/import" class="btn btn-primary" style="margin-left:auto;">Upload CSV →</a>
</div> </div>
{{else}}
<div class="empty">No import schedules yet. Create one above.</div>
{{end}}
<div style="margin-top:32px; background:var(--card); border:1px solid var(--border); border-radius:12px; padding:20px 24px;"> <div class="guide-step">
<div style="font-weight:600; margin-bottom:8px;">Webhook endpoint</div> <div class="step-num">1</div>
<p style="font-size:0.83rem; color:var(--muted); margin:0 0 12px;"> <div class="step-body">
You can also push a CSV file directly without scheduling. Use this endpoint from any automation tool (n8n, cron, etc.): <h3>Export a CSV from your bank</h3>
</p> <p>Log into your bank's online portal and download a transaction extract for the period you want. Most Portuguese banks offer this under <em>Movimentos</em> or <em>Extratos</em>.</p>
<div style="font-family:monospace; font-size:0.82rem; background:var(--bg); border:1px solid var(--border); border-radius:8px; padding:10px 14px; word-break:break-all;"> <div class="format-grid" style="margin-top:14px;">
POST /import/confirm &mdash; multipart/form-data with fields: <code>account_id</code>, <code>format</code>, <code>rows</code> (JSON array) <div class="format-card">
<div class="name">CGD (Caixa Geral de Depósitos)</div>
<div class="desc">Netbanco → Consultas → Movimentos de Conta → Exportar. Choose <strong>CSV</strong>.</div>
</div>
<div class="format-card">
<div class="name">Trade Republic</div>
<div class="desc">App → Profile → Documents → Activity. Export as CSV.</div>
</div>
<div class="format-card">
<div class="name">Generic / Other banks</div>
<div class="desc">Any CSV with columns: <code>Date</code>, <code>Description</code>, <code>Amount</code>. The importer auto-detects column order.</div>
</div>
</div>
</div> </div>
</div> </div>
<script> <div class="guide-step">
function deleteSchedule(id) { <div class="step-num">2</div>
if (!confirm('Delete this schedule?')) return; <div class="step-body">
fetch('/auto-import/' + id, { method: 'DELETE' }) <h3>Upload and preview</h3>
.then(r => { if (r.ok) location.reload(); }); <p>Go to <a href="/import" style="color:var(--accent);">Import</a>, pick the account and format, then select your file. You'll see a preview of every row with auto-suggested categories. Rows already in the database are shown greyed out and will be skipped automatically — safe to re-upload the same file.</p>
} </div>
</script> </div>
<div class="guide-step">
<div class="step-num">3</div>
<div class="step-body">
<h3>Review categories and confirm</h3>
<p>Adjust any auto-categorised rows using the dropdown selectors, then click <strong>Confirm Import</strong>. Only new transactions are saved.</p>
</div>
</div>
<div class="tip">
<strong>Tip — avoid duplicates:</strong> the importer fingerprints each row by date, description, amount, and account. Re-uploading a file that overlaps with a previous import is safe; duplicates are detected and skipped at both preview and confirm time.
</div>
<div style="margin-top:28px; background:var(--card); border:1px solid var(--border); border-radius:12px; padding:20px 24px;">
<h3 style="margin:0 0 8px; font-size:0.95rem;">Automate with a script</h3>
<p style="font-size:0.85rem; color:var(--muted); margin:0 0 14px;">
If you export your CSV on a schedule (e.g. via a cron job or n8n), you can push it directly to the import endpoint without going through the UI:
</p>
<pre style="background:var(--bg); border:1px solid var(--border); border-radius:8px; padding:14px 16px; font-size:0.8rem; overflow-x:auto; margin:0;">curl -X POST https://&lt;your-host&gt;/import/preview \
-H "X-Auth-User-Id: &lt;user-id&gt;" \
-H "X-Auth-Email: &lt;email&gt;" \
-F "account_id=&lt;account-id&gt;" \
-F "format=cgd" \
-F "file=@movements.csv"</pre>
<p style="font-size:0.8rem; color:var(--muted); margin:12px 0 0;">The preview endpoint returns an HTML page; for headless import pipe the confirmed rows to <code>POST /import/confirm</code> with the same <code>account_id</code>, <code>format</code>, <code>raw_data</code>, and <code>categories[]</code> fields.</p>
</div>
{{end}} {{end}}

View File

@ -404,7 +404,7 @@
<a href="/simulator" class="{{if eq .Route "simulator"}}active{{end}}">What If</a> <a href="/simulator" class="{{if eq .Route "simulator"}}active{{end}}">What If</a>
<a href="/tax" class="{{if eq .Route "tax"}}active{{end}}">Tax</a> <a href="/tax" class="{{if eq .Route "tax"}}active{{end}}">Tax</a>
<a href="/household" class="{{if eq .Route "household"}}active{{end}}">Household</a> <a href="/household" class="{{if eq .Route "household"}}active{{end}}">Household</a>
<a href="/auto-import" class="{{if eq .Route "auto-import"}}active{{end}}">Auto Import</a> <a href="/auto-import" class="{{if eq .Route "auto-import"}}active{{end}}">Import Guide</a>
<a href="/sharing" class="{{if eq .Route "sharing"}}active{{end}}">Sharing</a> <a href="/sharing" class="{{if eq .Route "sharing"}}active{{end}}">Sharing</a>
<div class="nav-spacer"></div> <div class="nav-spacer"></div>
<span class="nav-email">{{.Email}}</span> <span class="nav-email">{{.Email}}</span>

View File

@ -1,5 +1,11 @@
{{define "content"}} {{define "content"}}
{{$d := .}} {{$d := .}}
{{if eq $d.Notice "all_duplicates"}}
<div style="background:rgba(245,158,11,0.1); border:1px solid rgba(245,158,11,0.4); border-radius:10px; padding:12px 16px; margin-bottom:20px; display:flex; align-items:center; gap:10px;">
<span style="font-size:1.1rem;"></span>
<span style="font-size:0.9rem;">Every row in that file was already imported — nothing was added.</span>
</div>
{{end}}
<div style="display:flex; align-items:center; justify-content:space-between; margin-bottom:24px; flex-wrap:wrap; gap:12px;"> <div style="display:flex; align-items:center; justify-content:space-between; margin-bottom:24px; flex-wrap:wrap; gap:12px;">
<h1>Transactions</h1> <h1>Transactions</h1>
<button class="btn btn-primary" onclick="openAddModal()">+ Add Transaction</button> <button class="btn btn-primary" onclick="openAddModal()">+ Add Transaction</button>