Add coupon management functions

This commit is contained in:
2021-11-28 21:42:01 +01:00
parent 36c57fa1f0
commit ef4e550536
6 changed files with 221 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"html/template"
"net/http"
"strconv"
"time"
)
@@ -175,3 +176,68 @@ func (wc *WebController) knoxfsLinkForm(td *TemplateData, r *http.Request) (f Fo
}
return f
}
func (wc *WebController) couponForm(td *TemplateData, r *http.Request) (f Form) {
f.Name = "redeem_coupon"
f.Title = "Redeem coupon code"
f.SubmitLabel = "Redeem"
if r.FormValue("code") == "" {
f.Submitted = true
f.SubmitMessages = []template.HTML{"Coupon code not found"}
return f
}
coupon, err := td.PixelAPI.GetCouponID(r.FormValue("code"))
if err != nil && err.Error() == "not_found" {
f.Submitted = true
f.SubmitMessages = []template.HTML{"Coupon code not found"}
return f
} else if err != nil {
f.Submitted = true
formAPIError(err, &f)
return f
}
f.Fields = []Field{{
Name: "1",
Label: "",
DefaultValue: "",
Description: `When redeeming this coupon code the value will be ` +
`added to your pixeldrain account. Go to the ` +
`<a href="/user/subscription" target="_blank">subscription page</a> ` +
`afterwards to activate your subscription.`,
Type: FieldTypeDescription,
}, {
Name: "2",
Label: "Coupon value:",
DefaultValue: fmt.Sprintf("€ %.2f", float64(coupon.Credit)/1e6),
Type: FieldTypeDescription,
}, {
Name: "2",
Label: "Coupon uses left:",
DefaultValue: strconv.Itoa(coupon.Uses),
Type: FieldTypeDescription,
}}
if coupon.Uses < 1 {
f.Submitted = true
f.SubmitMessages = []template.HTML{"Coupon has been used up"}
return f
}
if f.ReadInput(r) {
if err := td.PixelAPI.PostCouponRedeem(r.FormValue("code")); err != nil {
formAPIError(err, &f)
} else {
// Request was a success
f.SubmitSuccess = true
f.SubmitMessages = []template.HTML{template.HTML(
`Success! Your account credit has been increased. Visit the ` +
`<a href="/user/subscription">subscription page</a> to ` +
`activate your subscription`,
)}
}
}
return f
}

View File

@@ -184,11 +184,14 @@ func New(
{GET, "user/password_reset_confirm" /**/, wc.serveForm(wc.passwordResetConfirmForm, handlerOpts{NoEmbed: true})},
{PST, "user/password_reset_confirm" /**/, wc.serveForm(wc.passwordResetConfirmForm, handlerOpts{NoEmbed: true})},
{GET, "patreon_activate" /* */, wc.serveForm(wc.patreonLinkForm, handlerOpts{Auth: true})},
{PST, "patreon_activate" /* */, wc.serveForm(wc.patreonLinkForm, handlerOpts{Auth: true})},
{GET, "patreon_activate", wc.serveForm(wc.patreonLinkForm, handlerOpts{Auth: true})},
{PST, "patreon_activate", wc.serveForm(wc.patreonLinkForm, handlerOpts{Auth: true})},
{GET, "knoxfs_activate" /* */, wc.serveForm(wc.knoxfsLinkForm, handlerOpts{Auth: true})},
{PST, "knoxfs_activate" /* */, wc.serveForm(wc.knoxfsLinkForm, handlerOpts{Auth: true})},
{GET, "knoxfs_activate", wc.serveForm(wc.knoxfsLinkForm, handlerOpts{Auth: true})},
{PST, "knoxfs_activate", wc.serveForm(wc.knoxfsLinkForm, handlerOpts{Auth: true})},
{GET, "coupon_redeem", wc.serveForm(wc.couponForm, handlerOpts{Auth: true})},
{PST, "coupon_redeem", wc.serveForm(wc.couponForm, handlerOpts{Auth: true})},
// Admin settings
{GET, "admin" /* */, wc.serveTemplate("admin", handlerOpts{Auth: true})},