Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 6754e9a

Browse files
committed
started first refactor to try and make things easier for contribution
1 parent 2f3d698 commit 6754e9a

File tree

16 files changed

+526
-448
lines changed

16 files changed

+526
-448
lines changed

account.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"strings"
1111
"time"
1212

13+
"staticbackend/internal"
14+
"staticbackend/middleware"
15+
1316
"github.com/stripe/stripe-go/v71"
1417
"github.com/stripe/stripe-go/v71/billingportal/session"
1518
"github.com/stripe/stripe-go/v71/customer"
@@ -27,16 +30,6 @@ var (
2730

2831
type accounts struct{}
2932

30-
type Customer struct {
31-
ID primitive.ObjectID `bson:"_id" json:"id"`
32-
Email string `bson:"email" json:"email"`
33-
StripeID string `bson:"stripeId" json:"stripeId"`
34-
SubscriptionID string `bson:"subId" json:"subId"`
35-
IsActive bool `bson:"active" json:"-"`
36-
MonthlyEmailSent int `bson:"mes" json:"-"`
37-
Created time.Time `bson:"created" json:"created"`
38-
}
39-
4033
func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
4134
email := strings.ToLower(r.URL.Query().Get("email"))
4235
// TODO: cheap email validation
@@ -90,15 +83,15 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
9083

9184
// create the account
9285
acctID := primitive.NewObjectID()
93-
doc := Customer{
86+
doc := internal.Customer{
9487
ID: acctID,
9588
Email: email,
9689
StripeID: stripeCustomerID,
9790
SubscriptionID: subID,
9891
Created: time.Now(),
9992
}
10093

101-
if _, err := db.Collection("accounts").InsertOne(ctx, doc); err != nil {
94+
if err := internal.CreateAccount(db, doc); err != nil {
10295
http.Error(w, err.Error(), http.StatusInternalServerError)
10396
return
10497
}
@@ -119,7 +112,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
119112
break
120113
}
121114

122-
base := BaseConfig{
115+
base := middleware.BaseConfig{
123116
ID: primitive.NewObjectID(),
124117
SBID: acctID,
125118
Name: dbName,
@@ -156,9 +149,8 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
156149
signUpURL = s.URL
157150
}
158151

159-
sr := db.Collection("sb_tokens").FindOne(context.Background(), bson.M{"email": email})
160-
var token Token
161-
if err := sr.Decode(&token); err != nil {
152+
token, err := internal.FindTokenByEmail(db, email)
153+
if err != nil {
162154
http.Error(w, err.Error(), http.StatusInternalServerError)
163155
return
164156
}
@@ -194,7 +186,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
194186
}
195187

196188
func (a *accounts) auth(w http.ResponseWriter, r *http.Request) {
197-
_, auth, err := extract(r, true)
189+
_, auth, err := middleware.Extract(r, true)
198190
if err != nil {
199191
http.Error(w, err.Error(), http.StatusBadRequest)
200192
return
@@ -204,18 +196,16 @@ func (a *accounts) auth(w http.ResponseWriter, r *http.Request) {
204196
}
205197

206198
func (a *accounts) portal(w http.ResponseWriter, r *http.Request) {
207-
conf, _, err := extract(r, true)
199+
conf, _, err := middleware.Extract(r, true)
208200
if err != nil {
209201
http.Error(w, err.Error(), http.StatusBadRequest)
210202
return
211203
}
212204

213205
db := client.Database("sbsys")
214206

215-
var cus Customer
216-
filter := bson.M{fieldID: conf.SBID}
217-
sr := db.Collection("accounts").FindOne(context.Background(), filter)
218-
if err := sr.Decode(&cus); err != nil {
207+
cus, err := internal.FindAccount(db, conf.SBID)
208+
if err != nil {
219209
http.Error(w, err.Error(), http.StatusInternalServerError)
220210
return
221211
}

auth.go

Lines changed: 0 additions & 208 deletions
This file was deleted.

cors.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)