From a6604ae123559ac8e8ddab3d17c0b9f483528d04 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 17 Aug 2023 09:39:32 -0500 Subject: [PATCH] Update to latest goflow and simplify how we turn orgs into engine environments --- core/models/assets.go | 2 +- core/models/orgs.go | 64 +++++++--------------------------------- core/models/orgs_test.go | 24 +++++++-------- core/models/tickets.go | 10 +++---- go.mod | 2 +- go.sum | 4 +-- 6 files changed, 31 insertions(+), 75 deletions(-) diff --git a/core/models/assets.go b/core/models/assets.go index 2e3051fc9..efe9bcd8b 100644 --- a/core/models/assets.go +++ b/core/models/assets.go @@ -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 } diff --git a/core/models/orgs.go b/core/models/orgs.go index b3b08762d..178f1df34 100644 --- a/core/models/orgs.go +++ b/core/models/orgs.go @@ -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) { @@ -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` diff --git a/core/models/orgs_test.go b/core/models/orgs_test.go index 112ec94bf..326cbbc51 100644 --- a/core/models/orgs_test.go +++ b/core/models/orgs_test.go @@ -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) diff --git a/core/models/tickets.go b/core/models/tickets.go index 96b4f7199..48f83f8b1 100644 --- a/core/models/tickets.go +++ b/core/models/tickets.go @@ -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 } @@ -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") } @@ -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 } } diff --git a/go.mod b/go.mod index 60f768e98..e9a9e89e6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 35125e128..dbdd1a9f3 100644 --- a/go.sum +++ b/go.sum @@ -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=