From 8fb1844dcd3d3e99271800089f8f693872888559 Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 21 Aug 2024 23:27:33 +0200 Subject: [PATCH] all subscriptions --- cmd/api/handlers-api.go | 10 +++- cmd/api/routes-api.go | 1 + cmd/web/templates/all-sales.page.gohtml | 2 +- .../templates/all-subscriptions.page.gohtml | 16 ++++++ internal/models/models.go | 6 +-- static/js/all-subscriptions.js | 54 +++++++++++++++++++ 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 static/js/all-subscriptions.js diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go index 93934e4..5433875 100644 --- a/cmd/api/handlers-api.go +++ b/cmd/api/handlers-api.go @@ -522,9 +522,17 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) { } func (app *application) AllSales(w http.ResponseWriter, r *http.Request) { - allSales, err := app.DB.GetAllOrders() + allSales, err := app.DB.GetAllOrders(false) if err != nil { app.badRequest(w, r, err) } app.writeJSON(w, http.StatusOK, allSales) } + +func (app *application) AllSubscriptions(w http.ResponseWriter, r *http.Request) { + allSubscriptions, err := app.DB.GetAllOrders(true) + if err != nil { + app.badRequest(w, r, err) + } + app.writeJSON(w, http.StatusOK, allSubscriptions) +} diff --git a/cmd/api/routes-api.go b/cmd/api/routes-api.go index 679ce2b..496143f 100644 --- a/cmd/api/routes-api.go +++ b/cmd/api/routes-api.go @@ -33,6 +33,7 @@ func (app *application) routes() http.Handler { mux.Post("/virtual-terminal-succeeded", app.VirtualTerminalPaymentSucceeded) mux.Post("/all-sales", app.AllSales) + mux.Post("/all-subscriptions", app.AllSubscriptions) }) mux.Post("/api/forgot-password", app.SendPasswordResetEmail) mux.Post("/api/reset-password", app.ResetPassword) diff --git a/cmd/web/templates/all-sales.page.gohtml b/cmd/web/templates/all-sales.page.gohtml index e170303..05009fc 100644 --- a/cmd/web/templates/all-sales.page.gohtml +++ b/cmd/web/templates/all-sales.page.gohtml @@ -1,6 +1,6 @@ {{ template "base" . }} {{ define "title" }} -All Subscriptions +All Sales {{ end }} {{ define "content" }}

All Sales

diff --git a/cmd/web/templates/all-subscriptions.page.gohtml b/cmd/web/templates/all-subscriptions.page.gohtml index 34d2eb0..5045218 100644 --- a/cmd/web/templates/all-subscriptions.page.gohtml +++ b/cmd/web/templates/all-subscriptions.page.gohtml @@ -7,7 +7,23 @@ All Subscriptions {{define "content"}}

All Subscriptions


+ + + + + + + + + + + +
TransactionCustomerProductAmount
{{end}} {{define "js"}} + {{end}} diff --git a/internal/models/models.go b/internal/models/models.go index 021cbe7..41f6ac0 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -297,7 +297,7 @@ func (m *DBModel) UpdatePasswordForUser(u User, hash string) error { return nil } -func (m *DBModel) GetAllOrders() ([]*Order, error) { +func (m *DBModel) GetAllOrders(isRecurring bool) ([]*Order, error) { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() @@ -315,12 +315,12 @@ func (m *DBModel) GetAllOrders() ([]*Order, error) { LEFT JOIN transactions t on (o.transaction_id = t.id) LEFT JOIN customers c on (o.customer_id = c.id) WHERE - w.is_recurring = 0 + w.is_recurring = ? ORDER BY o.created_at DESC ` - rows, err := m.DB.QueryContext(ctx, query) + rows, err := m.DB.QueryContext(ctx, query, isRecurring) if err != nil { return nil, err } diff --git a/static/js/all-subscriptions.js b/static/js/all-subscriptions.js new file mode 100644 index 0000000..babbe5f --- /dev/null +++ b/static/js/all-subscriptions.js @@ -0,0 +1,54 @@ +export function showTable(api) { + let token = localStorage.getItem("token"); + let tbody = document.getElementById("subscriptions-table").getElementsByTagName("tbody")[0]; + + const requestOptions = { + method: 'post', + headers: { + 'Accept': `application/json`, + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + token, + }, + }; + + fetch(api + "/api/admin/all-subscriptions", requestOptions) + .then(response => response.json()) + .then(function (data) { + if (data) { + data.forEach(function (i) { + let newRow = tbody.insertRow(); + let newCell = newRow.insertCell(); + + newCell.innerHTML = `Order ${i.id}`; + + newCell = newRow.insertCell(); + let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name); + newCell.appendChild(item) + + newCell = newRow.insertCell(); + item = document.createTextNode(i.widget.name); + newCell.appendChild(item) + + let cur = formatCurrency(i.transaction.amount) + newCell = newRow.insertCell(); + item = document.createTextNode(cur + "/month"); + newCell.appendChild(item) + + }); + } else { + let newRow = tbody.insertRow(); + let newCell = newRow.insertCell(); + newCell.setAttribute("colspan", "4"); + newCell.innerHTML = "No data available"; + } + }); +} + +function formatCurrency(amount) { + let c = parseFloat(amount/100); + return c.toLocaleString("fr-FR", { + style:"currency", + currency: "EUR", + }); +} +