Path: blob/main/components/gitpod-db/go/stripe_customer.go
2498 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package db56import (7"context"8"errors"9"fmt"10"time"1112"gorm.io/gorm"13)1415type StripeCustomer struct {16StripeCustomerID string `gorm:"primary_key;column:stripeCustomerId;type:char;size:255;" json:"stripeCustomerId"`17AttributionID AttributionID `gorm:"column:attributionId;type:varchar;size:255;" json:"attributionId"`18CreationTime VarcharTime `gorm:"column:creationTime;type:varchar;size:255;" json:"creationTime"`19Currency string `gorm:"column:currency;type:varchar;size:3;" json:"currency"`20InvalidBillingAddress *bool `gorm:"column:invalidBillingAddress;type:tinyint;default:null;" json:"invalidBillingAddress"`2122LastModified time.Time `gorm:"->;column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`23// deleted is reserved for use by periodic deleter24_ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`25}2627// TableName sets the insert table name for this struct type28func (d *StripeCustomer) TableName() string {29return "d_b_stripe_customer"30}3132func CreateStripeCustomer(ctx context.Context, conn *gorm.DB, customer StripeCustomer) error {33if !customer.CreationTime.IsSet() {34customer.CreationTime = NewVarCharTime(time.Now())35}3637tx := conn.WithContext(ctx).Create(customer)38if tx.Error != nil {39return fmt.Errorf("failed to create StripeCustomer ID %s: %w", customer.StripeCustomerID, tx.Error)40}4142return nil43}4445func GetStripeCustomer(ctx context.Context, conn *gorm.DB, stripeCustomerID string) (StripeCustomer, error) {46var customer StripeCustomer47tx := conn.48WithContext(ctx).49Where("stripeCustomerId = ?", stripeCustomerID).50First(&customer)51if err := tx.Error; err != nil {52if errors.Is(err, gorm.ErrRecordNotFound) {53return StripeCustomer{}, fmt.Errorf("stripe customer with ID %s does not exist: %w", stripeCustomerID, ErrorNotFound)54}5556return StripeCustomer{}, fmt.Errorf("failed to lookup stripe customer with ID %s: %w", stripeCustomerID, err)57}5859return customer, nil60}6162func GetStripeCustomerByAttributionID(ctx context.Context, conn *gorm.DB, attributionID AttributionID) (StripeCustomer, error) {63var customer StripeCustomer64tx := conn.65WithContext(ctx).66Where("attributionId = ?", string(attributionID)).67Order("creationTime DESC").68First(&customer)69if err := tx.Error; err != nil {70if errors.Is(err, gorm.ErrRecordNotFound) {71return StripeCustomer{}, fmt.Errorf("stripe customer with attribution ID %s does not exist: %w", attributionID, ErrorNotFound)72}7374return StripeCustomer{}, fmt.Errorf("failed to lookup stripe customer with attribution ID %s", attributionID)75}7677return customer, nil78}7980func UpdateStripeCustomerInvalidBillingAddress(ctx context.Context, conn *gorm.DB, stripeCustomerID string, invalidBillingAddress bool) (StripeCustomer, error) {81tx := conn.82WithContext(ctx).83Model(&StripeCustomer{}).84Where("stripeCustomerId = ?", stripeCustomerID).85Where("deleted = ?", 0).86Update("invalidBillingAddress", BoolPointer(invalidBillingAddress))8788if err := tx.Error; err != nil {89return StripeCustomer{}, fmt.Errorf("failed to update stripe customer with ID %s: %w", stripeCustomerID, err)90}9192return GetStripeCustomer(ctx, conn, stripeCustomerID)93}949596