From d35a122d057bc73c9da087e583ffd459fd762927 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Fri, 22 Nov 2024 18:13:31 -0600 Subject: [PATCH] Moving invoice status to enums --- migrations/0001_initial.down.sql | 1 + migrations/0001_initial.up.sql | 13 ++++++++++++- models/invoice.go | 12 ++++++++---- models/models.go | 2 +- models/schema.sql | 8 +++++++- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/migrations/0001_initial.down.sql b/migrations/0001_initial.down.sql index 49e1f98..5a803d4 100644 --- a/migrations/0001_initial.down.sql +++ b/migrations/0001_initial.down.sql @@ -35,3 +35,4 @@ DROP TABLE IF EXISTS users; DROP TYPE IF EXISTS domain_level; DROP TYPE IF EXISTS domain_service; DROP TYPE IF EXISTS domain_status; +DROP TYPE IF EXISTS invoice_status; diff --git a/migrations/0001_initial.up.sql b/migrations/0001_initial.up.sql index c594f54..80d56cb 100644 --- a/migrations/0001_initial.up.sql +++ b/migrations/0001_initial.up.sql @@ -40,6 +40,17 @@ EXCEPTION WHEN duplicate_object THEN null; END $$; +DO $$ BEGIN + CREATE TYPE invoice_status AS ENUM ( + 'PENDING', + 'PAID', + 'REFUNDED', + 'PARTIAL_REFUND' + ); +EXCEPTION + WHEN duplicate_object THEN null; +END $$; + CREATE TABLE users ( id SERIAL PRIMARY KEY, @@ -500,7 +511,7 @@ CREATE TRIGGER update_subscriptions_modtime BEFORE UPDATE ON subscriptions FOR E CREATE TABLE invoices ( id SERIAL PRIMARY KEY, - status INT DEFAULT 0, + status invoice_status DEFAULT 'PENDING', user_id INT REFERENCES users (id) ON DELETE CASCADE NOT NULL, org_id INT REFERENCES organizations (id) ON DELETE CASCADE NOT NULL, stripe_id VARCHAR(150), diff --git a/models/invoice.go b/models/invoice.go index 03c7963..ddca9d0 100644 --- a/models/invoice.go +++ b/models/invoice.go @@ -12,10 +12,10 @@ import ( ) const ( - InvoiceStatusPending int = iota - InvoiceStatusPaid - InvoiceStatusRefunded - InvoiceStatusRefundedPartial + InvoiceStatusPending string = "PENDING" + InvoiceStatusPaid string = "PAID" + InvoiceStatusRefunded string = "REFUNDED" + InvoiceStatusRefundedPartial string = "PARTIAL_REFUND" ) // GetInvoices returns payment methods @@ -118,6 +118,10 @@ func (i *Invoice) Store(ctx context.Context) error { err := database.WithTx(ctx, nil, func(tx *sql.Tx) error { var err error if i.ID == 0 { + // Added for fail safes after migration to enums. Can be removed later + if i.Status == "" { + i.Status = InvoiceStatusPending + } err = sq. Insert("invoices"). Columns("status", "user_id", "org_id", "stripe_id", "subscription_id", "currency", "amount", "amount_paid", diff --git a/models/models.go b/models/models.go index b713d22..9841afb 100644 --- a/models/models.go +++ b/models/models.go @@ -400,7 +400,7 @@ type Subscription struct { type Invoice struct { ID int `db:"id"` UserID int `db:"user_id"` - Status int `db:"status"` + Status string `db:"status"` OrgID int `db:"org_id"` SubscriptionID int `db:"subscription_id"` StripeID string `db:"stripe_id"` diff --git a/models/schema.sql b/models/schema.sql index 4e53242..dc38ea8 100644 --- a/models/schema.sql +++ b/models/schema.sql @@ -27,6 +27,12 @@ CREATE TYPE domain_status AS ENUM ( 'ERROR' ); +CREATE TYPE invoice_status AS ENUM ( + 'PENDING', + 'PAID', + 'REFUNDED', + 'PARTIAL_REFUND' +); CREATE TABLE users ( id SERIAL PRIMARY KEY, @@ -487,7 +493,7 @@ CREATE TRIGGER update_subscriptions_modtime BEFORE UPDATE ON subscriptions FOR E CREATE TABLE invoices ( id SERIAL PRIMARY KEY, - status INT DEFAULT 0, + status invoice_status DEFAULT 'PENDING', user_id INT REFERENCES users (id) ON DELETE CASCADE NOT NULL, org_id INT REFERENCES organizations (id) ON DELETE CASCADE NOT NULL, stripe_id VARCHAR(150), -- 2.45.2