Skip to content

Commit

Permalink
Merge pull request #38 from nyaruka/server_cleanup
Browse files Browse the repository at this point in the history
Server cleanup
  • Loading branch information
rowanseymour authored Feb 15, 2023
2 parents 2c8f390 + 5eef188 commit 81bef06
Show file tree
Hide file tree
Showing 24 changed files with 198 additions and 330 deletions.
1 change: 0 additions & 1 deletion cmd/mailroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
_ "github.com/nyaruka/mailroom/services/tickets/zendesk"
_ "github.com/nyaruka/mailroom/web/contact"
_ "github.com/nyaruka/mailroom/web/docs"
_ "github.com/nyaruka/mailroom/web/expression"
_ "github.com/nyaruka/mailroom/web/flow"
_ "github.com/nyaruka/mailroom/web/ivr"
_ "github.com/nyaruka/mailroom/web/msg"
Expand Down
4 changes: 2 additions & 2 deletions services/tickets/mailgun/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func init() {
base := "/mr/tickets/types/mailgun"

web.RegisterJSONRoute(http.MethodPost, base+"/receive", web.WithHTTPLogs(handleReceive))
web.RegisterRoute(http.MethodPost, base+"/receive", web.JSONRequestResponse(web.WithHTTPLogs(handleReceive)))
}

type receiveRequest struct {
Expand Down Expand Up @@ -61,7 +61,7 @@ type receiveResponse struct {

var addressRegex = regexp.MustCompile(`^ticket\+([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})@.*$`)

func handleReceive(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (interface{}, int, error) {
func handleReceive(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (any, int, error) {
request := &receiveRequest{}
if err := web.DecodeAndValidateForm(request, r); err != nil {
return errors.Wrapf(err, "error decoding form"), http.StatusBadRequest, nil
Expand Down
4 changes: 2 additions & 2 deletions services/tickets/rocketchat/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
func init() {
base := "/mr/tickets/types/rocketchat"

web.RegisterJSONRoute(http.MethodPost, base+"/event_callback/{ticketer:[a-f0-9\\-]+}", web.WithHTTPLogs(handleEventCallback))
web.RegisterRoute(http.MethodPost, base+"/event_callback/{ticketer:[a-f0-9\\-]+}", web.JSONRequestResponse(web.WithHTTPLogs(handleEventCallback)))
}

type eventCallbackRequest struct {
Expand All @@ -37,7 +37,7 @@ type agentMessageData struct {
} `json:"attachments"`
}

func handleEventCallback(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (interface{}, int, error) {
func handleEventCallback(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (any, int, error) {
ticketerUUID := assets.TicketerUUID(chi.URLParam(r, "ticketer"))

// look up ticketer
Expand Down
13 changes: 6 additions & 7 deletions services/tickets/zendesk/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ import (
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/services/tickets"
"github.com/nyaruka/mailroom/web"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

func init() {
base := "/mr/tickets/types/zendesk"

web.RegisterJSONRoute(http.MethodPost, base+"/channelback", handleChannelback)
web.RegisterJSONRoute(http.MethodPost, base+"/event_callback", web.WithHTTPLogs(handleEventCallback))
web.RegisterJSONRoute(http.MethodPost, base+`/target/{ticketer:[a-f0-9\-]+}`, web.WithHTTPLogs(handleTicketerTarget))
web.RegisterRoute(http.MethodPost, base+"/channelback", web.JSONRequestResponse(handleChannelback))
web.RegisterRoute(http.MethodPost, base+"/event_callback", web.JSONRequestResponse(web.WithHTTPLogs(handleEventCallback)))
web.RegisterRoute(http.MethodPost, base+`/target/{ticketer:[a-f0-9\-]+}`, web.JSONRequestResponse(web.WithHTTPLogs(handleTicketerTarget)))
}

type integrationMetadata struct {
Expand All @@ -48,7 +47,7 @@ type channelbackResponse struct {
AllowChannelback bool `json:"allow_channelback"`
}

func handleChannelback(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleChannelback(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &channelbackRequest{}
if err := web.DecodeAndValidateForm(request, r); err != nil {
return errors.Wrapf(err, "error decoding form"), http.StatusBadRequest, nil
Expand Down Expand Up @@ -131,7 +130,7 @@ type eventCallbackRequest struct {
Events []*channelEvent `json:"events" validate:"required"`
}

func handleEventCallback(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (interface{}, int, error) {
func handleEventCallback(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (any, int, error) {
request := &eventCallbackRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return err, http.StatusBadRequest, nil
Expand Down Expand Up @@ -252,7 +251,7 @@ type targetRequest struct {
Status string `json:"status"`
}

func handleTicketerTarget(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (interface{}, int, error) {
func handleTicketerTarget(ctx context.Context, rt *runtime.Runtime, r *http.Request, l *models.HTTPLogger) (any, int, error) {
ticketerUUID := assets.TicketerUUID(chi.URLParam(r, "ticketer"))

// look up our ticketer
Expand Down
44 changes: 22 additions & 22 deletions web/contact/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

func init() {
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/create", web.RequireAuthToken(handleCreate))
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/modify", web.RequireAuthToken(handleModify))
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/resolve", web.RequireAuthToken(handleResolve))
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/interrupt", web.RequireAuthToken(handleInterrupt))
web.RegisterRoute(http.MethodPost, "/mr/contact/create", web.RequireAuthToken(web.JSONRequestResponse(handleCreate)))
web.RegisterRoute(http.MethodPost, "/mr/contact/modify", web.RequireAuthToken(web.JSONRequestResponse(handleModify)))
web.RegisterRoute(http.MethodPost, "/mr/contact/resolve", web.RequireAuthToken(web.JSONRequestResponse(handleResolve)))
web.RegisterRoute(http.MethodPost, "/mr/contact/interrupt", web.RequireAuthToken(web.JSONRequestResponse(handleInterrupt)))
}

// Request to create a new contact.
Expand All @@ -42,7 +42,7 @@ type createRequest struct {
}

// handles a request to create the given contact
func handleCreate(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleCreate(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &createRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
Expand All @@ -51,7 +51,7 @@ func handleCreate(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
// grab our org
oa, err := models.GetOrgAssets(ctx, rt, request.OrgID)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets")
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

c, err := SpecToCreation(request.Contact, oa.Env(), oa.SessionAssets())
Expand All @@ -67,10 +67,10 @@ func handleCreate(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
modifiersByContact := map[*flows.Contact][]flows.Modifier{contact: c.Mods}
_, err = models.ApplyModifiers(ctx, rt, oa, request.UserID, modifiersByContact)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrap(err, "error modifying new contact")
return nil, 0, errors.Wrap(err, "error modifying new contact")
}

return map[string]interface{}{"contact": contact}, http.StatusOK, nil
return map[string]any{"contact": contact}, http.StatusOK, nil
}

// Request that a set of contacts is modified.
Expand Down Expand Up @@ -117,7 +117,7 @@ type modifyResult struct {
}

// handles a request to apply the passed in actions
func handleModify(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleModify(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &modifyRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
Expand All @@ -126,35 +126,35 @@ func handleModify(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
// grab our org assets
oa, err := models.GetOrgAssets(ctx, rt, request.OrgID)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets")
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

// read the modifiers from the request
mods, err := goflow.ReadModifiers(oa.SessionAssets(), request.Modifiers, goflow.ErrorOnMissing)
if err != nil {
return nil, http.StatusBadRequest, err
return nil, 0, err
}

// load our contacts
contacts, err := models.LoadContacts(ctx, rt.DB, oa, request.ContactIDs)
if err != nil {
return nil, http.StatusBadRequest, errors.Wrapf(err, "unable to load contact")
return nil, 0, errors.Wrapf(err, "unable to load contact")
}

// convert to map of flow contacts to modifiers
modifiersByContact := make(map[*flows.Contact][]flows.Modifier, len(contacts))
for _, contact := range contacts {
flowContact, err := contact.FlowContact(oa)
if err != nil {
return nil, http.StatusBadRequest, errors.Wrapf(err, "error creating flow contact for contact: %d", contact.ID())
return nil, 0, errors.Wrapf(err, "error creating flow contact for contact: %d", contact.ID())
}

modifiersByContact[flowContact] = mods
}

eventsByContact, err := models.ApplyModifiers(ctx, rt, oa, request.UserID, modifiersByContact)
if err != nil {
return nil, http.StatusBadRequest, err
return nil, 0, err
}

// create our results
Expand Down Expand Up @@ -183,7 +183,7 @@ type resolveRequest struct {
}

// handles a request to resolve a contact
func handleResolve(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleResolve(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &resolveRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
Expand All @@ -192,7 +192,7 @@ func handleResolve(ctx context.Context, rt *runtime.Runtime, r *http.Request) (i
// grab our org
oa, err := models.GetOrgAssets(ctx, rt, request.OrgID)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets")
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

urn := request.URN.Normalize(string(oa.Env().DefaultCountry()))
Expand All @@ -202,12 +202,12 @@ func handleResolve(ctx context.Context, rt *runtime.Runtime, r *http.Request) (i
urn = urn.Normalize(string(oa.Env().DefaultCountry()))

if err := urn.Validate(); err != nil {
return errors.Wrapf(err, "URN failed validation"), http.StatusBadRequest, nil
return errors.Wrap(err, "URN failed validation"), http.StatusBadRequest, nil
}

_, contact, created, err := models.GetOrCreateContact(ctx, rt.DB, oa, []urns.URN{urn}, request.ChannelID)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error getting or creating contact")
return nil, 0, errors.Wrapf(err, "error getting or creating contact")
}

// find the URN on the contact
Expand All @@ -218,7 +218,7 @@ func handleResolve(ctx context.Context, rt *runtime.Runtime, r *http.Request) (i
}
}

return map[string]interface{}{
return map[string]any{
"contact": contact,
"urn": map[string]interface{}{
"id": models.GetURNInt(urn, "id"),
Expand All @@ -242,16 +242,16 @@ type interruptRequest struct {
}

// handles a request to interrupt a contact
func handleInterrupt(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleInterrupt(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &interruptRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
}

count, err := models.InterruptSessionsForContacts(ctx, rt.DB, []models.ContactID{request.ContactID})
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to interrupt contact")
return nil, 0, errors.Wrapf(err, "unable to interrupt contact")
}

return map[string]interface{}{"sessions": count}, http.StatusOK, nil
return map[string]any{"sessions": count}, http.StatusOK, nil
}
18 changes: 9 additions & 9 deletions web/contact/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
)

func init() {
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/search", web.RequireAuthToken(handleSearch))
web.RegisterJSONRoute(http.MethodPost, "/mr/contact/parse_query", web.RequireAuthToken(handleParseQuery))
web.RegisterRoute(http.MethodPost, "/mr/contact/search", web.RequireAuthToken(web.JSONRequestResponse(handleSearch)))
web.RegisterRoute(http.MethodPost, "/mr/contact/parse_query", web.RequireAuthToken(web.JSONRequestResponse(handleParseQuery)))
}

// Searches the contacts for an org
Expand Down Expand Up @@ -62,7 +62,7 @@ type SearchResponse struct {
}

// handles a contact search request
func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &searchRequest{
Offset: 0,
PageSize: 50,
Expand All @@ -75,7 +75,7 @@ func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
// grab our org assets
oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, request.OrgID, models.RefreshFields|models.RefreshGroups)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets")
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

var group *models.Group
Expand All @@ -93,7 +93,7 @@ func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
if isQueryError {
return qerr, http.StatusBadRequest, nil
}
return nil, http.StatusInternalServerError, err
return nil, 0, err
}

// normalize and inspect the query
Expand Down Expand Up @@ -152,7 +152,7 @@ type parseResponse struct {
}

// handles a query parsing request
func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request) (any, int, error) {
request := &parseRequest{}
if err := web.ReadAndValidateJSON(r, request); err != nil {
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil
Expand All @@ -161,7 +161,7 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request)
// grab our org assets
oa, err := models.GetOrgAssetsWithRefresh(ctx, rt, request.OrgID, models.RefreshFields|models.RefreshGroups)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets")
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

var group *models.Group
Expand All @@ -183,7 +183,7 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request)
if isQueryError {
return qerr, http.StatusBadRequest, nil
}
return nil, http.StatusInternalServerError, err
return nil, 0, err
}

// normalize and inspect the query
Expand All @@ -195,7 +195,7 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request)
eq := search.BuildElasticQuery(oa, group, models.NilContactStatus, nil, parsed)
elasticSource, err = eq.Source()
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrap(err, "error getting elastic source")
return nil, 0, errors.Wrap(err, "error getting elastic source")
}
}

Expand Down
43 changes: 0 additions & 43 deletions web/expression/expression.go

This file was deleted.

13 changes: 0 additions & 13 deletions web/expression/expression_test.go

This file was deleted.

Loading

0 comments on commit 81bef06

Please sign in to comment.