Skip to content

Commit

Permalink
Merge pull request #103 from nyaruka/latest_goflow
Browse files Browse the repository at this point in the history
Update to latest goflow and simplify how we turn orgs into engine env…
  • Loading branch information
rowanseymour authored Aug 17, 2023
2 parents 6344d51 + a6604ae commit 4b6e985
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 75 deletions.
2 changes: 1 addition & 1 deletion core/models/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func GetOrgAssetsWithRefresh(ctx context.Context, rt *runtime.Runtime, orgID Org

func (a *OrgAssets) OrgID() OrgID { return a.orgID }

func (a *OrgAssets) Env() envs.Environment { return a.org }
func (a *OrgAssets) Env() envs.Environment { return a.org.env }

func (a *OrgAssets) Org() *Org { return a.org }

Expand Down
64 changes: 10 additions & 54 deletions core/models/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,41 +80,8 @@ func (o *Org) ID() OrgID { return o.o.ID }
// Suspended returns whether the org has been suspended
func (o *Org) Suspended() bool { return o.o.Suspended }

// DateFormat returns the date format for this org
func (o *Org) DateFormat() envs.DateFormat { return o.env.DateFormat() }

// NumberFormat returns the date format for this org
func (o *Org) NumberFormat() *envs.NumberFormat { return envs.DefaultNumberFormat }

// TimeFormat returns the time format for this org
func (o *Org) TimeFormat() envs.TimeFormat { return o.env.TimeFormat() }

// Timezone returns the timezone for this org
func (o *Org) Timezone() *time.Location { return o.env.Timezone() }

// DefaultLanguage returns the primary language for this org
func (o *Org) DefaultLanguage() envs.Language { return o.env.DefaultLanguage() }

// AllowedLanguages returns the list of supported languages for this org
func (o *Org) AllowedLanguages() []envs.Language { return o.env.AllowedLanguages() }

// RedactionPolicy returns the redaction policy (are we anonymous) for this org
func (o *Org) RedactionPolicy() envs.RedactionPolicy { return o.env.RedactionPolicy() }

// DefaultCountry returns the default country for this organization (mostly used for number parsing)
func (o *Org) DefaultCountry() envs.Country { return o.env.DefaultCountry() }

// Now returns the current time in the current timezone for this org
func (o *Org) Now() time.Time { return o.env.Now() }

// DefaultLocale combines the default languages and countries into a locale
func (o *Org) DefaultLocale() envs.Locale { return o.env.DefaultLocale() }

// LocationResolver returns a resolver for locations
func (o *Org) LocationResolver() envs.LocationResolver { return o.env.LocationResolver() }

// Equal return whether we are equal to the passed in environment
func (o *Org) Equal(env envs.Environment) bool { return o.env.Equal(env) }
// Environment returns this org as an engine environment
func (o *Org) Environment() envs.Environment { return o.env }

// MarshalJSON is our custom marshaller so that our inner env get output
func (o *Org) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -247,25 +214,14 @@ SELECT ROW_TO_JSON(o) FROM (SELECT
timezone,
(SELECT CASE is_anon WHEN TRUE THEN 'urns' WHEN FALSE THEN 'none' END) AS redaction_policy,
flow_languages AS allowed_languages,
'{}'::varchar[] AS input_cleaners,
COALESCE(
(
SELECT
country
FROM
channels_channel c
WHERE
c.org_id = o.id AND
c.is_active = TRUE AND
c.country IS NOT NULL
GROUP BY
c.country
ORDER BY
count(c.country) desc,
country
LIMIT 1
), '') AS default_country
FROM
orgs_org o
WHERE
o.id = $1
SELECT country FROM channels_channel c
WHERE c.org_id = o.id AND c.is_active = TRUE AND c.country IS NOT NULL
GROUP BY c.country ORDER BY count(c.country) desc, country LIMIT 1
), ''
) AS default_country
FROM orgs_org o
WHERE o.id = $1
) o`
24 changes: 12 additions & 12 deletions core/models/orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ func TestOrgs(t *testing.T) {

assert.Equal(t, models.OrgID(1), org.ID())
assert.False(t, org.Suspended())
assert.Equal(t, envs.DateFormatDayMonthYear, org.DateFormat())
assert.Equal(t, envs.TimeFormatHourMinute, org.TimeFormat())
assert.Equal(t, envs.RedactionPolicyNone, org.RedactionPolicy())
assert.Equal(t, string(envs.Country("US")), string(org.DefaultCountry()))
assert.Equal(t, tz, org.Timezone())
assert.Equal(t, []envs.Language{"fra", "eng"}, org.AllowedLanguages())
assert.Equal(t, envs.Language("fra"), org.DefaultLanguage())
assert.Equal(t, "fr-US", org.DefaultLocale().ToBCP47())
assert.Equal(t, envs.DateFormatDayMonthYear, org.Environment().DateFormat())
assert.Equal(t, envs.TimeFormatHourMinute, org.Environment().TimeFormat())
assert.Equal(t, envs.RedactionPolicyNone, org.Environment().RedactionPolicy())
assert.Equal(t, string(envs.Country("US")), string(org.Environment().DefaultCountry()))
assert.Equal(t, tz, org.Environment().Timezone())
assert.Equal(t, []envs.Language{"fra", "eng"}, org.Environment().AllowedLanguages())
assert.Equal(t, envs.Language("fra"), org.Environment().DefaultLanguage())
assert.Equal(t, "fr-US", org.Environment().DefaultLocale().ToBCP47())

org, err = models.LoadOrg(ctx, rt.Config, tx, testdata.Org2.ID)
assert.NoError(t, err)
assert.Equal(t, envs.DateFormatMonthDayYear, org.DateFormat())
assert.Equal(t, []envs.Language{}, org.AllowedLanguages())
assert.Equal(t, envs.NilLanguage, org.DefaultLanguage())
assert.Equal(t, "", org.DefaultLocale().ToBCP47())
assert.Equal(t, envs.DateFormatMonthDayYear, org.Environment().DateFormat())
assert.Equal(t, []envs.Language{}, org.Environment().AllowedLanguages())
assert.Equal(t, envs.NilLanguage, org.Environment().DefaultLanguage())
assert.Equal(t, "", org.Environment().DefaultLocale().ToBCP47())

_, err = models.LoadOrg(ctx, rt.Config, tx, 99)
assert.Error(t, err)
Expand Down
10 changes: 5 additions & 5 deletions core/models/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,10 @@ func InsertTickets(ctx context.Context, tx Queryer, oa *OrgAssets, tickets []*Ti
return err
}

if err := insertTicketDailyCounts(ctx, tx, TicketDailyCountOpening, oa.Org().Timezone(), openingCounts); err != nil {
if err := insertTicketDailyCounts(ctx, tx, TicketDailyCountOpening, oa.Env().Timezone(), openingCounts); err != nil {
return err
}
if err := insertTicketDailyCounts(ctx, tx, TicketDailyCountAssignment, oa.Org().Timezone(), assignmentCounts); err != nil {
if err := insertTicketDailyCounts(ctx, tx, TicketDailyCountAssignment, oa.Env().Timezone(), assignmentCounts); err != nil {
return err
}

Expand Down Expand Up @@ -466,7 +466,7 @@ func TicketsAssign(ctx context.Context, db Queryer, oa *OrgAssets, userID UserID
return nil, errors.Wrap(err, "error inserting notifications")
}

err = insertTicketDailyCounts(ctx, db, TicketDailyCountAssignment, oa.Org().Timezone(), assignmentCounts)
err = insertTicketDailyCounts(ctx, db, TicketDailyCountAssignment, oa.Env().Timezone(), assignmentCounts)
if err != nil {
return nil, errors.Wrap(err, "error inserting assignment counts")
}
Expand Down Expand Up @@ -928,12 +928,12 @@ func RecordTicketReply(ctx context.Context, db Queryer, oa *OrgAssets, ticketID
}
}

if err := insertTicketDailyCounts(ctx, db, TicketDailyCountReply, oa.Org().Timezone(), replyCounts); err != nil {
if err := insertTicketDailyCounts(ctx, db, TicketDailyCountReply, oa.Env().Timezone(), replyCounts); err != nil {
return err
}

if firstReplyTime >= 0 {
if err := insertTicketDailyTiming(ctx, db, TicketDailyTimingFirstReply, oa.Org().Timezone(), scopeOrg(oa), firstReplyTime); err != nil {
if err := insertTicketDailyTiming(ctx, db, TicketDailyTimingFirstReply, oa.Env().Timezone(), scopeOrg(oa), firstReplyTime); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/lib/pq v1.10.9
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.38.2
github.com/nyaruka/goflow v0.190.1
github.com/nyaruka/goflow v0.191.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
github.com/nyaruka/null/v2 v2.0.3
github.com/nyaruka/redisx v0.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.38.2 h1:67GwC2XRIaPZV5uSif0PkQ6XI327dL18v/GaZYu7SG0=
github.com/nyaruka/gocommon v1.38.2/go.mod h1:wdDXnl5UrjFHOmsxJNID4h4U92u4hFto8xsXFBRfzdA=
github.com/nyaruka/goflow v0.190.1 h1:ln2UO+v3zHZs/5ZtScdLZSMfapBBDCYzQzKOnux0jpk=
github.com/nyaruka/goflow v0.190.1/go.mod h1:tpi8zlxpBK/CpAmJc7an8NoajoKAMowoLHNJBXDbhx8=
github.com/nyaruka/goflow v0.191.0 h1:IWZtsq9T0XTcl5Gk3/1pMKpdH5r/glJjzAHGcVn2z00=
github.com/nyaruka/goflow v0.191.0/go.mod h1:tpi8zlxpBK/CpAmJc7an8NoajoKAMowoLHNJBXDbhx8=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg=
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc=
Expand Down

0 comments on commit 4b6e985

Please sign in to comment.