diff --git a/internal/store/postgres/kyc_repository.go b/internal/store/postgres/kyc_repository.go index 2b4077dca..ac89a6a8c 100644 --- a/internal/store/postgres/kyc_repository.go +++ b/internal/store/postgres/kyc_repository.go @@ -206,7 +206,7 @@ func (r OrgKycRepository) List(ctx context.Context) ([]kyc.KYC, error) { orgKycs := goqu.T(TABLE_ORGANIZATIONS_KYC) // Build query with join condition and COALESCE expressions - query, params, err := dialect.From(orgs). + query, params, err := fromLive(TABLE_ORGANIZATIONS). LeftJoin(orgKycs, goqu.On(orgs.Col(COLUMN_ID).Eq(orgKycs.Col(COLUMN_ORG_ID)), live(TABLE_ORGANIZATIONS_KYC))). Select( orgs.Col(COLUMN_ID), diff --git a/internal/store/postgres/kyc_repository_test.go b/internal/store/postgres/kyc_repository_test.go index 723d5f37c..7b837adeb 100644 --- a/internal/store/postgres/kyc_repository_test.go +++ b/internal/store/postgres/kyc_repository_test.go @@ -427,6 +427,27 @@ func (s *OrgKycRepositoryTestSuite) TestListIgnoresSoftDeletedKyc() { s.Assert().Equal("abcd", byOrg[liveOrgID].Link) } +func (s *OrgKycRepositoryTestSuite) TestListSkipsSoftDeletedOrgs() { + s.Require().NoError(s.cleanup()) + + live, err := s.orgRepository.Create(s.ctx, organization.Organization{Name: "kyc-live-org"}) + s.Require().NoError(err) + deleted, err := s.orgRepository.Create(s.ctx, organization.Organization{Name: "kyc-gone-org"}) + s.Require().NoError(err) + + _, err = s.repository.Upsert(s.ctx, kyc.KYC{OrgID: deleted.ID, Status: true, Link: "gone-link"}) + s.Require().NoError(err) + + _, err = s.client.ExecContext(s.ctx, "UPDATE organizations SET deleted_at = now() WHERE id = $1", deleted.ID) + s.Require().NoError(err) + + got, err := s.repository.List(s.ctx) + s.Require().NoError(err) + + s.Require().Len(got, 1) + s.Assert().Equal(live.ID, got[0].OrgID) +} + func TestOrganizationKYCRepository(t *testing.T) { suite.Run(t, new(OrgKycRepositoryTestSuite)) } diff --git a/internal/store/postgres/org_billing_repository.go b/internal/store/postgres/org_billing_repository.go index 914cc1f45..6cd36182e 100644 --- a/internal/store/postgres/org_billing_repository.go +++ b/internal/store/postgres/org_billing_repository.go @@ -309,11 +309,14 @@ func getSubQuery() *goqu.SelectDataset { goqu.I(TABLE_BILLING_SUBSCRIPTIONS+"."+COLUMN_CREATED_AT)).As(COLUMN_ROW_NUM), } - rankedSubscriptions := dialect.From(TABLE_ORGANIZATIONS). + rankedSubscriptions := fromLive(TABLE_ORGANIZATIONS). Select(subquerySelects...). LeftJoin( goqu.T(TABLE_BILLING_CUSTOMERS), - goqu.On(goqu.I(TABLE_ORGANIZATIONS+"."+COLUMN_ID).Eq(goqu.I(TABLE_BILLING_CUSTOMERS+"."+COLUMN_ORG_ID))), + goqu.On( + goqu.I(TABLE_ORGANIZATIONS+"."+COLUMN_ID).Eq(goqu.I(TABLE_BILLING_CUSTOMERS+"."+COLUMN_ORG_ID)), + live(TABLE_BILLING_CUSTOMERS), + ), ). LeftJoin( goqu.T(TABLE_BILLING_SUBSCRIPTIONS), @@ -321,6 +324,7 @@ func getSubQuery() *goqu.SelectDataset { goqu.And( goqu.I(TABLE_BILLING_SUBSCRIPTIONS+"."+COLUMN_CUSTOMER_ID).Eq(goqu.I(TABLE_BILLING_CUSTOMERS+"."+COLUMN_ID)), goqu.I(TABLE_BILLING_SUBSCRIPTIONS+"."+COLUMN_STATE).Neq("canceled"), + live(TABLE_BILLING_SUBSCRIPTIONS), ), )). LeftJoin( diff --git a/internal/store/postgres/org_billing_repository_pg_test.go b/internal/store/postgres/org_billing_repository_pg_test.go new file mode 100644 index 000000000..7dabd245d --- /dev/null +++ b/internal/store/postgres/org_billing_repository_pg_test.go @@ -0,0 +1,118 @@ +package postgres_test + +import ( + "context" + "fmt" + "testing" + + "github.com/raystack/frontier/core/aggregates/orgbilling" + "github.com/raystack/frontier/internal/store/postgres" + "github.com/raystack/frontier/pkg/db" + "github.com/raystack/salt/rql" + "github.com/stretchr/testify/suite" +) + +type OrgBillingRepositoryPGTestSuite struct { + suite.Suite + ctx context.Context + client *db.Client + repository *postgres.OrgBillingRepository +} + +func (s *OrgBillingRepositoryPGTestSuite) SetupSuite() { + var err error + s.client, err = newTestClient() + if err != nil { + s.T().Fatal(err) + } + s.ctx = context.TODO() + s.repository = postgres.NewOrgBillingRepository(s.client) +} + +func (s *OrgBillingRepositoryPGTestSuite) TearDownSuite() { + if err := closeTestClient(s.client); err != nil { + s.T().Fatal(err) + } +} + +func (s *OrgBillingRepositoryPGTestSuite) TearDownTest() { + queries := []string{} + for _, table := range []string{postgres.TABLE_BILLING_SUBSCRIPTIONS, postgres.TABLE_BILLING_CUSTOMERS, + postgres.TABLE_BILLING_PLANS, postgres.TABLE_ORGANIZATIONS} { + queries = append(queries, fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", table)) + } + if err := execQueries(s.ctx, s.client, queries); err != nil { + s.T().Fatal(err) + } +} + +func (s *OrgBillingRepositoryPGTestSuite) TestSearchSkipsDeletedOrgs() { + execSQL(s.T(), s.ctx, s.client, `INSERT INTO organizations (name, title, avatar) VALUES ('ob-live', 'Live Org', ''), ('ob-gone', 'Gone Org', '')`) + execSQL(s.T(), s.ctx, s.client, `UPDATE organizations SET deleted_at = now() WHERE name = 'ob-gone'`) + + got, err := s.repository.Search(s.ctx, &rql.Query{Limit: 10}) + s.Require().NoError(err) + + s.Require().Len(got.Organizations, 1) + s.Assert().Equal("ob-live", got.Organizations[0].Name) +} + +func (s *OrgBillingRepositoryPGTestSuite) seedBilling(org string, creditMin int) { + s.T().Helper() + execSQL(s.T(), s.ctx, s.client, `INSERT INTO organizations (name, title, avatar) VALUES ($1, $1, '')`, org) + execSQL(s.T(), s.ctx, s.client, `INSERT INTO billing_customers (org_id, provider_id, name, email, credit_min) + VALUES ((SELECT id FROM organizations WHERE name = $1), $1, $1, $1, $2)`, org, creditMin) + execSQL(s.T(), s.ctx, s.client, `INSERT INTO billing_plans (name, description) VALUES ('plan-'||$1::text, 'plan')`, org) + execSQL(s.T(), s.ctx, s.client, `INSERT INTO billing_subscriptions (customer_id, provider_id, plan_id, state) + VALUES ((SELECT id FROM billing_customers WHERE name = $1), $1, + (SELECT id FROM billing_plans WHERE name = 'plan-'||$1::text), 'active')`, org) +} + +func (s *OrgBillingRepositoryPGTestSuite) searchOne(org string) orgbilling.AggregatedOrganization { + s.T().Helper() + got, err := s.repository.Search(s.ctx, &rql.Query{Limit: 10}) + s.Require().NoError(err) + for _, o := range got.Organizations { + if o.Name == org { + return o + } + } + s.T().Fatalf("org %s not listed", org) + return orgbilling.AggregatedOrganization{} +} + +func (s *OrgBillingRepositoryPGTestSuite) TestSearchKeepsLiveBillingRows() { + s.seedBilling("ob-live-bill", -10) + + got := s.searchOne("ob-live-bill") + + s.Assert().Equal("plan-ob-live-bill", got.PlanName) + s.Assert().Equal("active", got.SubscriptionState) + s.Assert().Equal("postpaid", got.PaymentMode) +} + +func (s *OrgBillingRepositoryPGTestSuite) TestSearchIgnoresDeletedBillingCustomer() { + s.seedBilling("ob-no-cust", -10) + execSQL(s.T(), s.ctx, s.client, `UPDATE billing_customers SET deleted_at = now() WHERE name = 'ob-no-cust'`) + + got := s.searchOne("ob-no-cust") + + s.Assert().Empty(got.PlanName) + s.Assert().Empty(got.SubscriptionState) + s.Assert().Empty(got.PaymentMode) +} + +func (s *OrgBillingRepositoryPGTestSuite) TestSearchIgnoresDeletedSubscription() { + s.seedBilling("ob-no-sub", -10) + execSQL(s.T(), s.ctx, s.client, `UPDATE billing_subscriptions SET deleted_at = now() WHERE provider_id = 'ob-no-sub'`) + + got := s.searchOne("ob-no-sub") + + s.Assert().Empty(got.PlanName) + s.Assert().Empty(got.SubscriptionState) + s.Assert().Equal("postpaid", got.PaymentMode) +} + +func TestOrgBillingRepositoryPG(t *testing.T) { + suite.Run(t, new(OrgBillingRepositoryPGTestSuite)) +} diff --git a/internal/store/postgres/org_billing_repository_test.go b/internal/store/postgres/org_billing_repository_test.go index 2da801066..45f90e8a5 100644 --- a/internal/store/postgres/org_billing_repository_test.go +++ b/internal/store/postgres/org_billing_repository_test.go @@ -22,7 +22,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 10, Offset: 0, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE ("row_num" = $2) LIMIT $3`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE ("row_num" = $2) LIMIT $3`, wantParameters: []any{"canceled", int64(1), int64(10)}, wantErr: false, }, @@ -39,7 +39,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 10, Offset: 0, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("state" = $3)) LIMIT $4`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("state" = $3)) LIMIT $4`, wantParameters: []any{"canceled", int64(1), "active", int64(10)}, wantErr: false, }, @@ -50,7 +50,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 10, Offset: 0, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ((CAST("id" AS TEXT) ILIKE $3) OR (CAST("title" AS TEXT) ILIKE $4) OR (CAST("name" AS TEXT) ILIKE $5) OR (CAST("state" AS TEXT) ILIKE $6) OR (CAST("plan_name" AS TEXT) ILIKE $7) OR (CAST("subscription_state" AS TEXT) ILIKE $8) OR (CAST("plan_interval" AS TEXT) ILIKE $9))) LIMIT $10`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ((CAST("id" AS TEXT) ILIKE $3) OR (CAST("title" AS TEXT) ILIKE $4) OR (CAST("name" AS TEXT) ILIKE $5) OR (CAST("state" AS TEXT) ILIKE $6) OR (CAST("plan_name" AS TEXT) ILIKE $7) OR (CAST("subscription_state" AS TEXT) ILIKE $8) OR (CAST("plan_interval" AS TEXT) ILIKE $9))) LIMIT $10`, wantParameters: []any{"canceled", int64(1), "%test%", "%test%", "%test%", "%test%", "%test%", "%test%", "%test%", int64(10)}, wantErr: false, }, @@ -66,7 +66,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 10, Offset: 0, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE ("row_num" = $2) ORDER BY "created_at" DESC LIMIT $3`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE ("row_num" = $2) ORDER BY "created_at" DESC LIMIT $3`, wantParameters: []any{"canceled", int64(1), int64(10)}, wantErr: false, }, @@ -104,7 +104,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 20, Offset: 40, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("state" = $3) AND (CAST("plan_name" AS TEXT) IN ($4, $5)) AND (("subscription_state" IS NOT NULL) AND ("subscription_state" != $6)) AND ((CAST("id" AS TEXT) ILIKE $7) OR (CAST("title" AS TEXT) ILIKE $8) OR (CAST("name" AS TEXT) ILIKE $9) OR (CAST("state" AS TEXT) ILIKE $10) OR (CAST("plan_name" AS TEXT) ILIKE $11) OR (CAST("subscription_state" AS TEXT) ILIKE $12) OR (CAST("plan_interval" AS TEXT) ILIKE $13))) ORDER BY "created_at" DESC, "title" ASC LIMIT $14 OFFSET $15`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("state" = $3) AND (CAST("plan_name" AS TEXT) IN ($4, $5)) AND (("subscription_state" IS NOT NULL) AND ("subscription_state" != $6)) AND ((CAST("id" AS TEXT) ILIKE $7) OR (CAST("title" AS TEXT) ILIKE $8) OR (CAST("name" AS TEXT) ILIKE $9) OR (CAST("state" AS TEXT) ILIKE $10) OR (CAST("plan_name" AS TEXT) ILIKE $11) OR (CAST("subscription_state" AS TEXT) ILIKE $12) OR (CAST("plan_interval" AS TEXT) ILIKE $13))) ORDER BY "created_at" DESC, "title" ASC LIMIT $14 OFFSET $15`, wantParameters: []any{"canceled", int64(1), "active", "free", "premium", "", "%test%", "%test%", "%test%", "%test%", "%test%", "%test%", "%test%", int64(20), int64(40)}, wantErr: false, }, @@ -121,7 +121,7 @@ func TestPrepareDataQuery(t *testing.T) { Limit: 10, Offset: 0, }, - wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("payment_mode" = $3)) LIMIT $4`, + wantSQL: `SELECT "id", "title", "name", "state", "avatar", "updated_at", "created_at", "created_by", "country", "plan_id", "plan_name", "subscription_state", "subscription_cycle_end_at", "plan_interval", "payment_mode" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE (("row_num" = $2) AND ("payment_mode" = $3)) LIMIT $4`, wantParameters: []any{"canceled", int64(1), "postpaid", int64(10)}, wantErr: false, }, @@ -167,7 +167,7 @@ func TestPrepareGroupByQuery(t *testing.T) { rqlQuery: &rql.Query{ GroupBy: []string{"state"}, }, - wantSQL: `SELECT COUNT(*) AS "count", "state" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "state"`, + wantSQL: `SELECT COUNT(*) AS "count", "state" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "state"`, wantParameters: []any{"canceled", int64(1)}, wantErr: false, }, @@ -176,7 +176,7 @@ func TestPrepareGroupByQuery(t *testing.T) { rqlQuery: &rql.Query{ GroupBy: []string{"plan_name"}, }, - wantSQL: `SELECT COUNT(*) AS "count", "plan_name" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "plan_name"`, + wantSQL: `SELECT COUNT(*) AS "count", "plan_name" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "plan_name"`, wantParameters: []any{"canceled", int64(1)}, wantErr: false, }, @@ -185,7 +185,7 @@ func TestPrepareGroupByQuery(t *testing.T) { rqlQuery: &rql.Query{ GroupBy: []string{"payment_mode"}, }, - wantSQL: `SELECT COUNT(*) AS "count", "payment_mode" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON ("organizations"."id" = "billing_customers"."org_id") LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id")) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "payment_mode"`, + wantSQL: `SELECT COUNT(*) AS "count", "payment_mode" AS "values" FROM (SELECT "organizations"."id" AS "id", "organizations"."title" AS "title", "organizations"."name" AS "name", "organizations"."avatar" AS "avatar", "organizations"."created_at" AS "created_at", "organizations"."updated_at" AS "updated_at", "organizations"."state" AS "state", organizations.metadata->>'country' AS "country", organizations.metadata->>'poc' AS "created_by", "billing_plans"."id" AS "plan_id", "billing_plans"."name" AS "plan_name", "billing_plans"."interval" AS "plan_interval", "billing_subscriptions"."state" AS "subscription_state", "billing_subscriptions"."trial_ends_at", "billing_subscriptions"."current_period_end_at" AS "subscription_cycle_end_at", "billing_customers"."payment_mode" AS "payment_mode", ROW_NUMBER() OVER (PARTITION BY "organizations"."id" ORDER BY "billing_subscriptions"."created_at" DESC) AS "row_num" FROM "organizations" LEFT JOIN "billing_customers" ON (("organizations"."id" = "billing_customers"."org_id") AND ("billing_customers"."deleted_at" IS NULL)) LEFT JOIN "billing_subscriptions" ON (("billing_subscriptions"."customer_id" = "billing_customers"."id") AND ("billing_subscriptions"."state" != $1) AND ("billing_subscriptions"."deleted_at" IS NULL)) LEFT JOIN "billing_plans" ON ("billing_plans"."id" = "billing_subscriptions"."plan_id") WHERE ("organizations"."deleted_at" IS NULL)) AS "ranked_subscriptions" WHERE ("row_num" = $2) GROUP BY "payment_mode"`, wantParameters: []any{"canceled", int64(1)}, wantErr: false, },