forked from rapidpro/mailroom
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rapidpro#427 from nyaruka/resend_endpoint
Resend messages endpoint
- Loading branch information
Showing
5 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package msg | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/nyaruka/goflow/flows" | ||
"github.com/nyaruka/goflow/utils" | ||
"github.com/nyaruka/mailroom/core/models" | ||
"github.com/nyaruka/mailroom/core/msgio" | ||
"github.com/nyaruka/mailroom/web" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func init() { | ||
web.RegisterJSONRoute(http.MethodPost, "/mr/msg/resend", web.RequireAuthToken(handleResend)) | ||
} | ||
|
||
// Request to resend failed messages. | ||
// | ||
// { | ||
// "org_id": 1, | ||
// "msg_ids": [123456, 345678] | ||
// } | ||
// | ||
type resendRequest struct { | ||
OrgID models.OrgID `json:"org_id" validate:"required"` | ||
MsgIDs []models.MsgID `json:"msg_ids" validate:"required"` | ||
} | ||
|
||
// handles a request to resend the given messages | ||
func handleResend(ctx context.Context, s *web.Server, r *http.Request) (interface{}, int, error) { | ||
request := &resendRequest{} | ||
if err := utils.UnmarshalAndValidateWithLimit(r.Body, request, web.MaxRequestBytes); err != nil { | ||
return errors.Wrapf(err, "request failed validation"), http.StatusBadRequest, nil | ||
} | ||
|
||
// grab our org | ||
oa, err := models.GetOrgAssets(s.CTX, s.DB, request.OrgID) | ||
if err != nil { | ||
return nil, http.StatusInternalServerError, errors.Wrapf(err, "unable to load org assets") | ||
} | ||
|
||
msgs, err := models.LoadMessages(ctx, s.DB, request.OrgID, models.DirectionOut, request.MsgIDs) | ||
if err != nil { | ||
return nil, http.StatusInternalServerError, errors.Wrap(err, "error loading messages to resend") | ||
} | ||
|
||
err = models.ResendMessages(ctx, s.DB, s.RP, oa, msgs) | ||
if err != nil { | ||
return nil, http.StatusInternalServerError, errors.Wrap(err, "error resending messages") | ||
} | ||
|
||
msgio.SendMessages(ctx, s.DB, s.RP, nil, msgs) | ||
|
||
// response is the ids of the messages that were actually resent | ||
resentMsgIDs := make([]flows.MsgID, len(msgs)) | ||
for i, m := range msgs { | ||
resentMsgIDs[i] = m.ID() | ||
} | ||
return map[string]interface{}{"msg_ids": resentMsgIDs}, http.StatusOK, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package msg_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nyaruka/mailroom/web" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
web.RunWebTests(t, "testdata/resend.json") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
[ | ||
{ | ||
"label": "illegal method", | ||
"method": "GET", | ||
"path": "/mr/msg/resend", | ||
"status": 405, | ||
"response": { | ||
"error": "illegal method: GET" | ||
} | ||
}, | ||
{ | ||
"label": "invalid org_id", | ||
"method": "POST", | ||
"path": "/mr/msg/resend", | ||
"body": { | ||
"org_id": 1234, | ||
"msg_ids": [ | ||
1234 | ||
] | ||
}, | ||
"status": 500, | ||
"response": { | ||
"error": "unable to load org assets: error loading environment for org 1234: no org with id: 1234" | ||
} | ||
}, | ||
{ | ||
"label": "response is the ids of the messages that were actually resent", | ||
"method": "POST", | ||
"path": "/mr/msg/resend", | ||
"body": { | ||
"org_id": 1, | ||
"msg_ids": [ | ||
10000, | ||
10001, | ||
10002 | ||
] | ||
}, | ||
"status": 200, | ||
"response": { | ||
"msg_ids": [ | ||
10001, | ||
10002 | ||
] | ||
}, | ||
"db_assertions": [ | ||
{ | ||
"query": "SELECT count(*) FROM msgs_msg WHERE status = 'P'", | ||
"count": 2 | ||
} | ||
] | ||
} | ||
] |