Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-db/go/stripe_customer.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package db
6
7
import (
8
"context"
9
"errors"
10
"fmt"
11
"time"
12
13
"gorm.io/gorm"
14
)
15
16
type StripeCustomer struct {
17
StripeCustomerID string `gorm:"primary_key;column:stripeCustomerId;type:char;size:255;" json:"stripeCustomerId"`
18
AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"`
19
CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`
20
Currency string `gorm:"column:currency;type:varchar;size:3;" json:"currency"`
21
InvalidBillingAddress *bool `gorm:"column:invalidBillingAddress;type:tinyint;default:null;" json:"invalidBillingAddress"`
22
23
LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
24
// deleted is reserved for use by periodic deleter
25
_ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
26
}
27
28
// TableName sets the insert table name for this struct type
29
func (d *StripeCustomer) TableName() string {
30
return "d_b_stripe_customer"
31
}
32
33
func CreateStripeCustomer(ctx context.Context, conn *gorm.DB, customer StripeCustomer) error {
34
if !customer.CreationTime.IsSet() {
35
customer.CreationTime = NewVarCharTime(time.Now())
36
}
37
38
tx := conn.WithContext(ctx).Create(customer)
39
if tx.Error != nil {
40
return fmt.Errorf("failed to create StripeCustomer ID %s: %w", customer.StripeCustomerID, tx.Error)
41
}
42
43
return nil
44
}
45
46
func GetStripeCustomer(ctx context.Context, conn *gorm.DB, stripeCustomerID string) (StripeCustomer, error) {
47
var customer StripeCustomer
48
tx := conn.
49
WithContext(ctx).
50
Where("stripeCustomerId = ?", stripeCustomerID).
51
First(&customer)
52
if err := tx.Error; err != nil {
53
if errors.Is(err, gorm.ErrRecordNotFound) {
54
return StripeCustomer{}, fmt.Errorf("stripe customer with ID %s does not exist: %w", stripeCustomerID, ErrorNotFound)
55
}
56
57
return StripeCustomer{}, fmt.Errorf("failed to lookup stripe customer with ID %s: %w", stripeCustomerID, err)
58
}
59
60
return customer, nil
61
}
62
63
func GetStripeCustomerByAttributionID(ctx context.Context, conn *gorm.DB, attributionID AttributionID) (StripeCustomer, error) {
64
var customer StripeCustomer
65
tx := conn.
66
WithContext(ctx).
67
Where("attributionId = ?", string(attributionID)).
68
Order("creationTime DESC").
69
First(&customer)
70
if err := tx.Error; err != nil {
71
if errors.Is(err, gorm.ErrRecordNotFound) {
72
return StripeCustomer{}, fmt.Errorf("stripe customer with attribution ID %s does not exist: %w", attributionID, ErrorNotFound)
73
}
74
75
return StripeCustomer{}, fmt.Errorf("failed to lookup stripe customer with attribution ID %s", attributionID)
76
}
77
78
return customer, nil
79
}
80
81
func UpdateStripeCustomerInvalidBillingAddress(ctx context.Context, conn *gorm.DB, stripeCustomerID string, invalidBillingAddress bool) (StripeCustomer, error) {
82
tx := conn.
83
WithContext(ctx).
84
Model(&StripeCustomer{}).
85
Where("stripeCustomerId = ?", stripeCustomerID).
86
Where("deleted = ?", 0).
87
Update("invalidBillingAddress", BoolPointer(invalidBillingAddress))
88
89
if err := tx.Error; err != nil {
90
return StripeCustomer{}, fmt.Errorf("failed to update stripe customer with ID %s: %w", stripeCustomerID, err)
91
}
92
93
return GetStripeCustomer(ctx, conn, stripeCustomerID)
94
}
95
96