From 05c1f5943ebb09e3fedf1211e39fef8d2e93029a Mon Sep 17 00:00:00 2001 From: Rafael Soares Date: Wed, 4 Oct 2023 14:59:26 -0300 Subject: [PATCH] models/catalog_product: add struct and method to get active catalog --- core/models/catalog_products.go | 61 ++++++++++++++++++++++++++++ core/models/catalog_products_test.go | 60 +++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 core/models/catalog_products.go create mode 100644 core/models/catalog_products_test.go diff --git a/core/models/catalog_products.go b/core/models/catalog_products.go new file mode 100644 index 000000000..e193ad39c --- /dev/null +++ b/core/models/catalog_products.go @@ -0,0 +1,61 @@ +package models + +import ( + "context" + "database/sql" + "time" + + "github.com/jmoiron/sqlx" + "github.com/nyaruka/gocommon/uuids" + "github.com/nyaruka/null" + "github.com/pkg/errors" +) + +type CatalogID null.Int + +// CatalogProduct represents a product catalog from Whatsapp channels. +type CatalogProduct struct { + c struct { + ID CatalogID `db:"id"` + UUID uuids.UUID `db:"uuid"` + FacebookCatalogID string `db:"facebook_catalog_id"` + Name string `db:"name"` + CreatedOn time.Time `db:"created_on"` + ModifiedOn time.Time `db:"modified_on"` + IsActive bool `db:"is_active"` + ChannelID ChannelID `db:"channel_id"` + OrgID OrgID `db:"org_id"` + } +} + +func (c *CatalogProduct) ID() CatalogID { return c.c.ID } +func (c *CatalogProduct) UUID() uuids.UUID { return c.c.UUID } +func (c *CatalogProduct) FacebookCatalogID() string { return c.c.FacebookCatalogID } +func (c *CatalogProduct) Name() string { return c.c.Name } +func (c *CatalogProduct) CreatedOn() time.Time { return c.c.CreatedOn } +func (c *CatalogProduct) ModifiedOn() time.Time { return c.c.ModifiedOn } +func (c *CatalogProduct) IsActive() bool { return c.c.IsActive } +func (c *CatalogProduct) ChannelID() ChannelID { return c.c.ChannelID } +func (c *CatalogProduct) OrgID() OrgID { return c.c.OrgID } + +const getActiveCatalogSQL = ` +SELECT + id, uuid, facebook_catalog_id, name, created_on, modified_on, is_active, channel_id, org_id +FROM public.wpp_products_catalog +WHERE channel_id = $1 AND is_active = true +` + +// GetActiveCatalogFromChannel returns the active catalog from the given channel +func GetActiveCatalogFromChannel(ctx context.Context, db sqlx.DB, channelID ChannelID) (*CatalogProduct, error) { + var catalog CatalogProduct + + err := db.GetContext(ctx, &catalog.c, getActiveCatalogSQL, channelID) + if err != nil { + if err == sql.ErrNoRows { + return nil, nil + } + return nil, errors.Wrapf(err, "error getting active catalog for channelID: %d", channelID) + } + + return &catalog, nil +} diff --git a/core/models/catalog_products_test.go b/core/models/catalog_products_test.go new file mode 100644 index 000000000..ff25b6f6e --- /dev/null +++ b/core/models/catalog_products_test.go @@ -0,0 +1,60 @@ +package models_test + +import ( + "testing" + + "github.com/nyaruka/mailroom/core/models" + "github.com/nyaruka/mailroom/testsuite" + "github.com/nyaruka/mailroom/testsuite/testdata" + "github.com/stretchr/testify/assert" +) + +func TestCatalogProducts(t *testing.T) { + ctx, _, db, _ := testsuite.Get() + defer testsuite.Reset(testsuite.ResetDB) + + _, err := db.Exec(catalogProductDDL) + if err != nil { + t.Fatal(err) + } + + _, err = db.Exec(`INSERT INTO public.wpp_products_catalog + (uuid, facebook_catalog_id, "name", created_on, modified_on, is_active, channel_id, org_id) + VALUES('2be9092a-1c97-4b24-906f-f0fbe3e1e93e', '123456789', 'Catalog Dummy', now(), now(), true, $1, $2); + `, testdata.Org2Channel.ID, testdata.Org2.ID) + assert.NoError(t, err) + + ctp, err := models.GetActiveCatalogFromChannel(ctx, *db, testdata.Org2Channel.ID) + if err != nil { + t.Fatal(err) + } + assert.Equal(t, true, ctp.IsActive()) + + _, err = db.Exec(`INSERT INTO public.wpp_products_catalog + (uuid, facebook_catalog_id, "name", created_on, modified_on, is_active, channel_id, org_id) + VALUES('9bbe354d-cea6-408b-ba89-9ce28999da3f', '1234567891', 'Catalog Dummy2', now(), now(), false, $1, $2); + `, 123, testdata.Org2.ID) + assert.NoError(t, err) + + ctpn, err := models.GetActiveCatalogFromChannel(ctx, *db, 123) + if err != nil { + t.Fatal(err) + } + assert.Nil(t, ctpn) +} + +const ( + catalogProductDDL = ` + CREATE TABLE public.wpp_products_catalog ( + id serial4 NOT NULL, + uuid uuid NOT NULL, + facebook_catalog_id varchar(30) NOT NULL, + "name" varchar(100) NOT NULL, + created_on timestamptz NOT NULL, + modified_on timestamptz NOT NULL, + is_active bool NOT NULL, + channel_id int4 NOT NULL, + org_id int4 NOT NULL + ); +` +)