tsk-59: Get MessageEventResponse endpoint (#61)
Closes #59 Reviewed-on: phoenix/textsender-api#61 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -1,19 +1,28 @@
|
||||
package endpoint
|
||||
|
||||
const MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft"
|
||||
const ADD_MESSAGE = "/api/v1/message/new"
|
||||
const GET_MESSAGE = "/api/v1/message"
|
||||
const GET_CONTACT = "/api/v1/contact"
|
||||
const ADD_CONTACT_ENDPOINT = "/api/v1/contact/new"
|
||||
const ScheduleMessageEndpoint = "/api/v1/schedule/message"
|
||||
const GetScheduledMessageEndpoint = "/api/v1/schedule/message"
|
||||
const AddEventToScheduledMessageEndpoint = "/api/v1/schedule/message/event"
|
||||
const GetScheduledMessageEventEndpoint = "/api/v1/schedule/message/event"
|
||||
const DeleteScheduledMessageEventEndpoint = "/api/v1/schedule/message/event/{id}"
|
||||
const UpdateScheduledMessageStatusEndpoint = "/api/v1/schedule/message/status/update"
|
||||
const FetchNextScheduledMessageEndpoint = "/api/v1/schedule/message/fetch"
|
||||
// TODO: Tweak endpoint verbage for MessageEventResponse endpoint
|
||||
// TODO: Make RecordEventResponse more consistent with MessageEventResponse
|
||||
const RecordEventResponse = "/api/v1/schedule/message/event/response/record"
|
||||
const FetchMessageEventResponse = "/api/v1/schedule/message/event/response"
|
||||
const SendInstantMessageEndpoint = "/api/v1/instant/message"
|
||||
const (
|
||||
MESSAGE_DRAFT_ENDPOINT = "/api/v1/message/draft"
|
||||
|
||||
ADD_MESSAGE = "/api/v1/message/new"
|
||||
GET_MESSAGE = "/api/v1/message"
|
||||
|
||||
GET_CONTACT = "/api/v1/contact"
|
||||
ADD_CONTACT_ENDPOINT = "/api/v1/contact/new"
|
||||
|
||||
ScheduleMessageEndpoint = "/api/v1/schedule/message"
|
||||
GetScheduledMessageEndpoint = "/api/v1/schedule/message"
|
||||
|
||||
AddEventToScheduledMessageEndpoint = "/api/v1/schedule/message/event"
|
||||
GetScheduledMessageEventEndpoint = "/api/v1/schedule/message/event"
|
||||
DeleteScheduledMessageEventEndpoint = "/api/v1/schedule/message/event/{id}"
|
||||
UpdateScheduledMessageStatusEndpoint = "/api/v1/schedule/message/status/update"
|
||||
|
||||
FetchNextScheduledMessageEndpoint = "/api/v1/schedule/message/fetch"
|
||||
|
||||
// TODO: Tweak endpoint verbage for MessageEventResponse endpoint
|
||||
// TODO: Make RecordEventResponse more consistent with MessageEventResponse
|
||||
RecordEventResponse = "/api/v1/schedule/message/event/response/record"
|
||||
FetchMessageEventResponse = "/api/v1/schedule/message/event/response"
|
||||
|
||||
SendInstantMessageEndpoint = "/api/v1/instant/message"
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
@@ -74,3 +75,36 @@ func TestScheduledMessage(id uuid.UUID, userId uuid.UUID, now time.Time) schedul
|
||||
func TestScheduledMessageEvent(messageId, contactId, scheduledMessageId uuid.UUID) scheduling.ScheduledMessageEvent {
|
||||
return scheduling.ScheduledMessageEvent{MessageId: messageId, ContactId: contactId, ScheduledMessageId: scheduledMessageId}
|
||||
}
|
||||
|
||||
func TestMERResponseInString() string {
|
||||
return `{
|
||||
"body": "Whoknows?",
|
||||
"num_segments": "0",
|
||||
"direction": "outbound-api",
|
||||
"from": "+12243026041",
|
||||
"to": "+16303831708",
|
||||
"date_updated": "Sat,29Nov202519:06:59+0000",
|
||||
"uri": "/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a.json",
|
||||
"account_sid": "ACefa1ef516314c9d1a68cbd657de49277",
|
||||
"num_media": "0",
|
||||
"status": "scheduled",
|
||||
"messaging_service_sid": "MG803f3676706b92eb02e18dd820c447f2",
|
||||
"sid": "SM1193a529e7f7a840667cd1e0f13ea95a",
|
||||
"date_created": "Sat,29Nov202519:06:59+0000",
|
||||
"api_version": "2010-04-01",
|
||||
"subresource_uris": {
|
||||
"media": "/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a/Media.json"
|
||||
}
|
||||
}`
|
||||
}
|
||||
|
||||
func TestConvertStringToMapOfAny(response string) (map[string]any, error) {
|
||||
bytes := []byte(response)
|
||||
var merResponse map[string]any
|
||||
err := json.Unmarshal(bytes, &merResponse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return merResponse, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,18 @@ type RecordEventResponse struct {
|
||||
Data []*event.MessageEventResponse `json:"data"`
|
||||
}
|
||||
|
||||
// RecordMessageEventResponse godoc
|
||||
// @Summary Record MessageEventResponse
|
||||
// @Description Saves the result of sending a text message (requires JWT)
|
||||
// @Tags message
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param request body RecordEventRequest true "Data to add MessageEventResponse"
|
||||
// @Success 201 {object} RecordEventResponse
|
||||
// @Failure 400 {object} RecordEventResponse
|
||||
// @Failure 500 {object} RecordEventResponse
|
||||
// @Router /schedule/message/event/response/record [post]
|
||||
func (e *EventResponseHandler) RecordResponse(w http.ResponseWriter, r *http.Request) {
|
||||
var req RecordEventRequest
|
||||
if err := ExtractFromRequest(r, &req); err != nil {
|
||||
@@ -107,6 +119,18 @@ type GetMessageEventResponseFetchedResponse struct {
|
||||
Data []*event.MessageEventResponse `json:"data"`
|
||||
}
|
||||
|
||||
// FetchtMessageEventResponse godoc
|
||||
// @Summary Fetcht MessageEventResponse
|
||||
// @Description Fetches a MessageEventResponse given a user id (requires JWT)
|
||||
// @Tags message
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param user_id path string true "User Id"
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} GetMessageEventResponseFetchedResponse
|
||||
// @Failure 400 {object} GetMessageEventResponseFetchedResponse
|
||||
// @Failure 500 {object} GetMessageEventResponseFetchedResponse
|
||||
// @Router /schedule/message/event/response [get]
|
||||
func (e *EventResponseHandler) Fetch(w http.ResponseWriter, r *http.Request) {
|
||||
queryParams := r.URL.Query()
|
||||
var userId uuid.UUID
|
||||
|
||||
@@ -2,13 +2,14 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/message"
|
||||
evnt "git.kundeng.us/phoenix/textsender-models/tx0/message/event"
|
||||
"git.kundeng.us/phoenix/textsender-models/tx0/message/scheduling"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -30,12 +31,12 @@ func TestRecordMessageEventResponseWithMock(t *testing.T) {
|
||||
assert.NoError(t, err, "Error getting app")
|
||||
handler := NewEventResponseHandler(apiApp, merStore)
|
||||
|
||||
recipientId := uuid.New()
|
||||
contactId := uuid.New()
|
||||
messageId := uuid.New()
|
||||
scheduledMessageId := uuid.New()
|
||||
testUserId := uuid.New()
|
||||
|
||||
con := TestContact(recipientId, testUserId)
|
||||
con := TestContact(contactId, testUserId)
|
||||
msg := TestMessage(messageId, testUserId)
|
||||
schMsg := TestScheduledMessage(scheduledMessageId, testUserId, now)
|
||||
event := scheduling.ScheduledMessageEvent{}
|
||||
@@ -50,17 +51,19 @@ func TestRecordMessageEventResponseWithMock(t *testing.T) {
|
||||
assert.NoError(t, err, "Error creating scheduled message: %v", err)
|
||||
}
|
||||
|
||||
event.MessageId = msg.Id
|
||||
event.RecipientId = *con.Id
|
||||
event.MessageId = *msg.Id
|
||||
event.ContactId = *con.Id
|
||||
event.ScheduledMessageId = schMsg.Id
|
||||
if err := mockStore.CreateScheduledMessageEvent(ctx, &event); err != nil {
|
||||
assert.NoError(t, err, "Error creating scheduled message event: %v", err)
|
||||
}
|
||||
|
||||
sent := now.Add(30 * time.Minute)
|
||||
bytes := []byte("{\"body\":\"Whoknows?\",\"num_segments\":\"0\",\"direction\":\"outbound-api\",\"from\":\"+12243026041\",\"to\":\"+16303831708\",\"date_updated\":\"Sat,29Nov202519:06:59+0000\",\"uri\":\"/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a.json\",\"account_sid\":\"ACefa1ef516314c9d1a68cbd657de49277\",\"num_media\":\"0\",\"status\":\"scheduled\",\"messaging_service_sid\":\"MG803f3676706b92eb02e18dd820c447f2\",\"sid\":\"SM1193a529e7f7a840667cd1e0f13ea95a\",\"date_created\":\"Sat,29Nov202519:06:59+0000\",\"api_version\":\"2010-04-01\",\"subresource_uris\":{\"media\":\"/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a/Media.json\"}}")
|
||||
var merResponse map[string]any
|
||||
merResponse, err = TestConvertStringToMapOfAny(TestMERResponseInString())
|
||||
assert.NoError(t, err, "Error Converting to map[string]any")
|
||||
|
||||
testReq := RecordEventRequest{ScheduledMessageEventId: event.Id, Response: bytes, UserId: schMsg.UserId, Sent: &sent, Status: message.Message_Event_Response_Status_Scheduled}
|
||||
testReq := RecordEventRequest{ScheduledMessageEventId: event.Id, Response: merResponse, UserId: schMsg.UserId, Sent: &sent, Status: evnt.Message_Event_Response_Status_Scheduled}
|
||||
jsonValue, _ := json.Marshal(testReq)
|
||||
|
||||
req, _ := http.NewRequest("POST", endpoint.RecordEventResponse, strings.NewReader(string(jsonValue)))
|
||||
@@ -76,9 +79,84 @@ func TestRecordMessageEventResponseWithMock(t *testing.T) {
|
||||
|
||||
assert.NotEmpty(t, response.Data, "No message event response created")
|
||||
|
||||
var msgEventResp message.MessageEventResponse
|
||||
var msgEventResp evnt.MessageEventResponse
|
||||
msgEventResp = *response.Data[0]
|
||||
|
||||
assert.NotEmpty(t, msgEventResp.Response, "The response of the message event response is empty")
|
||||
assert.Equal(t, msgEventResp.Response, testReq.Response, "Responses do not match")
|
||||
}
|
||||
|
||||
func TestGetMessageEventResponse(t *testing.T) {
|
||||
now := time.Now()
|
||||
|
||||
mockStore := mock.NewMockScheduledMessageEventStore()
|
||||
contactStore := mock.NewMockContactStore()
|
||||
messageStore := mock.NewMockMessageStore()
|
||||
schMsgStore := mock.NewMockScheduledMessageStore()
|
||||
merStore := mock.NewMockMessageEventResponseStore()
|
||||
|
||||
apiApp, err := GetApp()
|
||||
assert.NoError(t, err, "Error getting app")
|
||||
handler := NewEventResponseHandler(apiApp, merStore)
|
||||
|
||||
contactId := uuid.New()
|
||||
messageId := uuid.New()
|
||||
scheduledMessageId := uuid.New()
|
||||
testUserId := uuid.New()
|
||||
|
||||
con := TestContact(contactId, testUserId)
|
||||
msg := TestMessage(messageId, testUserId)
|
||||
schMsg := TestScheduledMessage(scheduledMessageId, testUserId, now)
|
||||
event := scheduling.ScheduledMessageEvent{}
|
||||
|
||||
ctx := t.Context()
|
||||
|
||||
if err := contactStore.CreateContact(ctx, &con); err != nil {
|
||||
assert.NoError(t, err, "Error creating contact: %v", err)
|
||||
} else if err = messageStore.CreateMessage(ctx, &msg); err != nil {
|
||||
assert.NoError(t, err, "Error creating message: %v", err)
|
||||
} else if err = schMsgStore.CreateScheduledMessage(ctx, &schMsg); err != nil {
|
||||
assert.NoError(t, err, "Error creating scheduled message: %v", err)
|
||||
}
|
||||
|
||||
event.MessageId = *msg.Id
|
||||
event.ContactId = *con.Id
|
||||
event.ScheduledMessageId = schMsg.Id
|
||||
sent := now.Add(30 * time.Minute)
|
||||
if err := mockStore.CreateScheduledMessageEvent(ctx, &event); err != nil {
|
||||
assert.NoError(t, err, "Error creating scheduled message event: %v", err)
|
||||
} else {
|
||||
m := evnt.MessageEventResponse{}
|
||||
m.ScheduledMessageEventId = event.Id
|
||||
m.UserId = schMsg.UserId
|
||||
m.Status = evnt.Message_Event_Response_Status_Scheduled
|
||||
m.Sent = sent
|
||||
var merResponse map[string]any
|
||||
merResponse, err = TestConvertStringToMapOfAny(TestMERResponseInString())
|
||||
assert.NoError(t, err, "Error Converting to map[string]any")
|
||||
m.Response = merResponse
|
||||
|
||||
if err := merStore.Create(ctx, &m); err != nil {
|
||||
assert.NoError(t, err, "Error creating MessageEventResponse: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s?user_id=%s", endpoint.FetchMessageEventResponse, testUserId.String())
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
handler.RecordResponse(rr, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, rr.Code)
|
||||
|
||||
var response GetMessageEventResponseFetchedResponse
|
||||
err = json.Unmarshal(rr.Body.Bytes(), &response)
|
||||
assert.NoError(t, err, "Error parsing message event response: %v", err)
|
||||
|
||||
assert.NotEmpty(t, response.Data, "No message event response found")
|
||||
|
||||
var msgEventResp evnt.MessageEventResponse
|
||||
msgEventResp = *response.Data[0]
|
||||
|
||||
assert.NotEmpty(t, msgEventResp.Response, "The response of the message event response is empty")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user