Skip to content

Commit

Permalink
models/catalog_product: add struct and method to get active catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
rasoro committed Oct 10, 2023
1 parent 49c33af commit 05c1f59
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
61 changes: 61 additions & 0 deletions core/models/catalog_products.go
Original file line number Diff line number Diff line change
@@ -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
}
60 changes: 60 additions & 0 deletions core/models/catalog_products_test.go
Original file line number Diff line number Diff line change
@@ -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
);
`
)

0 comments on commit 05c1f59

Please sign in to comment.