@@ -10,6 +10,9 @@ import (
10
10
"strings"
11
11
"time"
12
12
13
+ "staticbackend/internal"
14
+ "staticbackend/middleware"
15
+
13
16
"github.com/stripe/stripe-go/v71"
14
17
"github.com/stripe/stripe-go/v71/billingportal/session"
15
18
"github.com/stripe/stripe-go/v71/customer"
27
30
28
31
type accounts struct {}
29
32
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
-
40
33
func (a * accounts ) create (w http.ResponseWriter , r * http.Request ) {
41
34
email := strings .ToLower (r .URL .Query ().Get ("email" ))
42
35
// TODO: cheap email validation
@@ -90,15 +83,15 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
90
83
91
84
// create the account
92
85
acctID := primitive .NewObjectID ()
93
- doc := Customer {
86
+ doc := internal. Customer {
94
87
ID : acctID ,
95
88
Email : email ,
96
89
StripeID : stripeCustomerID ,
97
90
SubscriptionID : subID ,
98
91
Created : time .Now (),
99
92
}
100
93
101
- if _ , err := db . Collection ( "accounts" ). InsertOne ( ctx , doc ); err != nil {
94
+ if err := internal . CreateAccount ( db , doc ); err != nil {
102
95
http .Error (w , err .Error (), http .StatusInternalServerError )
103
96
return
104
97
}
@@ -119,7 +112,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
119
112
break
120
113
}
121
114
122
- base := BaseConfig {
115
+ base := middleware. BaseConfig {
123
116
ID : primitive .NewObjectID (),
124
117
SBID : acctID ,
125
118
Name : dbName ,
@@ -156,9 +149,8 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
156
149
signUpURL = s .URL
157
150
}
158
151
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 {
162
154
http .Error (w , err .Error (), http .StatusInternalServerError )
163
155
return
164
156
}
@@ -194,7 +186,7 @@ func (a *accounts) create(w http.ResponseWriter, r *http.Request) {
194
186
}
195
187
196
188
func (a * accounts ) auth (w http.ResponseWriter , r * http.Request ) {
197
- _ , auth , err := extract (r , true )
189
+ _ , auth , err := middleware . Extract (r , true )
198
190
if err != nil {
199
191
http .Error (w , err .Error (), http .StatusBadRequest )
200
192
return
@@ -204,18 +196,16 @@ func (a *accounts) auth(w http.ResponseWriter, r *http.Request) {
204
196
}
205
197
206
198
func (a * accounts ) portal (w http.ResponseWriter , r * http.Request ) {
207
- conf , _ , err := extract (r , true )
199
+ conf , _ , err := middleware . Extract (r , true )
208
200
if err != nil {
209
201
http .Error (w , err .Error (), http .StatusBadRequest )
210
202
return
211
203
}
212
204
213
205
db := client .Database ("sbsys" )
214
206
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 {
219
209
http .Error (w , err .Error (), http .StatusInternalServerError )
220
210
return
221
211
}
0 commit comments