tsk-54: Tweak instant message endpoint to save contact and message identification (#56)
Closes #54 Reviewed-on: phoenix/textsender-api#56 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -54,14 +54,14 @@ func (c *ContactHandler) AddContact(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
newContact := contact.Contact{PhoneNumber: req.PhoneNumber, UserId: req.UserId}
|
||||
newContact := contact.Contact{PhoneNumber: req.PhoneNumber, UserId: &req.UserId}
|
||||
|
||||
var statusCode int
|
||||
var resp AddContactResponse
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
if exists, err := c.ContactStore.ContactExists(ctx, newContact.PhoneNumber, newContact.UserId); err != nil {
|
||||
if exists, err := c.ContactStore.ContactExists(ctx, newContact.PhoneNumber, *newContact.UserId); err != nil {
|
||||
fmt.Printf("Error: %v", err)
|
||||
statusCode = http.StatusInternalServerError
|
||||
resp.Message = err.Error()
|
||||
@@ -157,7 +157,7 @@ func (c *ContactHandler) GetContact(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("Checking with User Id")
|
||||
if contacts, err := c.ContactStore.GetAllContacts(ctx); err == nil {
|
||||
for _, con := range contacts {
|
||||
if con.UserId == userId {
|
||||
if *con.UserId == userId {
|
||||
resp.Data = append(resp.Data, *con)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,9 @@ func (s *SendInstantMessageHandler) Send(w http.ResponseWriter, r *http.Request)
|
||||
mer.Response = data
|
||||
mer.UserId = req.UserId
|
||||
mer.Sent = parsedTime
|
||||
mer.Status = message.Message_Event_Response_Status_Instant
|
||||
mer.ContactId = c.Id
|
||||
mer.MessageId = &msg.Id
|
||||
if err := s.MERStore.Create(ctx, &mer); err != nil {
|
||||
statusCode = http.StatusInternalServerError
|
||||
resp.Message = err.Error()
|
||||
|
||||
@@ -26,6 +26,9 @@ type RecordEventRequest struct {
|
||||
Response types.JSONB `json:"response"`
|
||||
UserId uuid.UUID `json:"user_id"`
|
||||
Sent *time.Time `json:"sent,omitempty"`
|
||||
Status string `json:"status"`
|
||||
ContactId *uuid.UUID `json:"contact_id"`
|
||||
MessageId *uuid.UUID `json:"message_id"`
|
||||
}
|
||||
|
||||
type RecordEventResponse struct {
|
||||
@@ -62,6 +65,14 @@ func (e *EventResponseHandler) RecordResponse(w http.ResponseWriter, r *http.Req
|
||||
rp.UserId = req.UserId
|
||||
rp.Sent = *req.Sent
|
||||
|
||||
if req.Status == message.Message_Event_Response_Status_Instant {
|
||||
rp.Status = req.Status
|
||||
rp.MessageId = req.MessageId
|
||||
rp.ContactId = req.ContactId
|
||||
} else if req.Status == message.Message_Event_Response_Status_Scheduled {
|
||||
rp.Status = req.Status
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
if err := e.MessageEventResponseStore.Create(ctx, &rp); err != nil {
|
||||
resp.Message = err.Error()
|
||||
|
||||
@@ -60,7 +60,7 @@ func TestRecordMessageEventResponseWithMock(t *testing.T) {
|
||||
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\"}}")
|
||||
|
||||
testReq := RecordEventRequest{ScheduledMessageEventId: event.Id, Response: bytes, UserId: schMsg.UserId, Sent: &sent}
|
||||
testReq := RecordEventRequest{ScheduledMessageEventId: event.Id, Response: bytes, UserId: schMsg.UserId, Sent: &sent, Status: message.Message_Event_Response_Status_Scheduled}
|
||||
jsonValue, _ := json.Marshal(testReq)
|
||||
|
||||
req, _ := http.NewRequest("POST", endpoint.RecordEventResponse, strings.NewReader(string(jsonValue)))
|
||||
|
||||
Reference in New Issue
Block a user