all subscriptions

This commit is contained in:
vinchent 2024-08-21 23:27:33 +02:00
parent f8ee76939e
commit 8fb1844dcd
6 changed files with 84 additions and 5 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -1,6 +1,6 @@
{{ template "base" . }}
{{ define "title" }}
All Subscriptions
All Sales
{{ end }}
{{ define "content" }}
<h2 class="mt-5">All Sales</h2>

View File

@ -7,7 +7,23 @@ All Subscriptions
{{define "content"}}
<h2 class="mt-5">All Subscriptions</h2>
<hr>
<table id="subscriptions-table" class="table table-striped">
<thead>
<tr>
<th>Transaction</th>
<th>Customer</th>
<th>Product</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{{end}}
{{define "js"}}
<script type="module">
import {showTable} from "/static/js/all-sales.js"
showTable({{.API}});
</script>
{{end}}

View File

@ -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
}

View File

@ -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 = `<a href="/admin/subscriptions/${i.id}">Order ${i.id}</a>`;
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",
});
}