Skip to content

Commit 5ab501a

Browse files
committed
Fix incorrect perspectives on messages + volunteer acceptance message
1 parent 76b06e7 commit 5ab501a

File tree

4 files changed

+102
-11
lines changed

4 files changed

+102
-11
lines changed

internal/conversations/message_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type MessageVolunteerRequestProfile struct {
1414
// MessageTypeVolunteerRequestAcceptance represents the message sent when a user is accepted to an opportunity.
1515
type MessageTypeVolunteerRequestAcceptance struct {
1616
UserID int64 `json:"userId"`
17+
AccepterID int64 `json:"accepterId"`
1718
OpportunityID int64 `json:"opportunityId"`
1819
}
1920

internal/conversations/service.go

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Service interface {
4949
SendHoursRequestAcceptedMessage(ctx context.Context, userID, requestID int64) (int64, error)
5050
// SendHoursRequestDeclinedMessage sends an hours request decline message to a user's organization message.
5151
SendHoursRequestDeclinedMessage(ctx context.Context, userID, requestID int64) (int64, error)
52+
// SendVolunteerRequestAcceptanceMessage sends a VolunteerRequestAcceptance message based on request ID and opportunity ID.
53+
SendVolunteerRequestAcceptanceMessage(ctx context.Context, userID, accepterID, opportunityID int64) (int64, error)
5254
}
5355

5456
// service represents the internal implementation of the conversations Service.
@@ -469,7 +471,7 @@ func (s *service) parseMessage(ctx context.Context, messageType string, rawMessa
469471
return nil, err
470472
}
471473

472-
view, err := s.getMessageVolunteerRequestAcceptance(ctx, body.UserID, body.OpportunityID)
474+
view, err := s.getMessageVolunteerRequestAcceptance(ctx, body.UserID, body.AccepterID, body.OpportunityID)
473475
if err != nil {
474476
return nil, err
475477
}
@@ -593,15 +595,34 @@ func (s *service) getMessageVolunteerRequestProfileView(ctx context.Context, use
593595
}
594596

595597
// getMessageVolunteerRequestAcceptance gets a MessageTypeVolunteerRequestAcceptanceView by user ID and opportunity ID.
596-
func (s *service) getMessageVolunteerRequestAcceptance(ctx context.Context, userID, opportunityID int64) (*MessageTypeVolunteerRequestAcceptanceView, error) {
598+
func (s *service) getMessageVolunteerRequestAcceptance(ctx context.Context, userID, accepterID, opportunityID int64) (*MessageTypeVolunteerRequestAcceptanceView, error) {
597599
view := &MessageTypeVolunteerRequestAcceptanceView{}
598600

599601
opportunity, err := s.opportunityRepository.FindByID(ctx, opportunityID)
600602
if err != nil {
601603
return nil, err
602604
}
603605

604-
view.UserID = userID
606+
volunteer, err := s.userRepository.FindByID(userID)
607+
if err != nil {
608+
return nil, err
609+
}
610+
611+
accepter, err := s.userRepository.FindByID(accepterID)
612+
if err != nil {
613+
return nil, err
614+
}
615+
616+
view.Volunteer = &MessageUserWithName{
617+
ID: userID,
618+
FirstName: volunteer.FirstName,
619+
LastName: volunteer.LastName,
620+
}
621+
view.Accepter = &MessageUserWithName{
622+
ID: accepterID,
623+
FirstName: accepter.FirstName,
624+
LastName: accepter.LastName,
625+
}
605626
view.OpportunityID = opportunityID
606627
view.OpportunityTitle = opportunity.Title
607628

@@ -688,7 +709,7 @@ func (s *service) SendHoursRequestAcceptedMessage(ctx context.Context, userID, r
688709
message.ConversationID = conversation.ID
689710
message.SenderID = userID
690711
message.Type = models.MessageTypeHoursAccepted
691-
perspective := models.MessageSenderPerspectiveVolunteer
712+
perspective := models.MessageSenderPerspectiveOrganization
692713

693714
message.SenderPerspective = &perspective
694715

@@ -729,7 +750,7 @@ func (s *service) SendHoursRequestDeclinedMessage(ctx context.Context, userID, r
729750
message.ConversationID = conversation.ID
730751
message.SenderID = userID
731752
message.Type = models.MessageTypeHoursDeclined
732-
perspective := models.MessageSenderPerspectiveVolunteer
753+
perspective := models.MessageSenderPerspectiveOrganization
733754

734755
message.SenderPerspective = &perspective
735756

@@ -751,3 +772,46 @@ func (s *service) SendHoursRequestDeclinedMessage(ctx context.Context, userID, r
751772

752773
return message.ID, nil
753774
}
775+
776+
// SendVolunteerRequestAcceptanceMessage sends a VolunteerRequestAcceptance message based on request ID and opportunity ID.
777+
func (s *service) SendVolunteerRequestAcceptanceMessage(ctx context.Context, userID, accepterID, opportunityID int64) (int64, error) {
778+
opportunity, err := s.opportunityRepository.FindByID(ctx, opportunityID)
779+
if err != nil {
780+
return 0, NewErrServerError()
781+
}
782+
783+
conversation, err := s.conversationRepository.FindUserOrganizationConversation(ctx, userID, opportunity.OrganizationID)
784+
if err != nil {
785+
return 0, NewErrConversationNotFound()
786+
}
787+
788+
message := models.Message{}
789+
message.ID = s.snowflakeService.GenerateID()
790+
message.Timestamp = time.Now()
791+
message.ConversationID = conversation.ID
792+
message.SenderID = accepterID
793+
message.Type = models.MessageTypeVolunteerRequestAcceptance
794+
perspective := models.MessageSenderPerspectiveOrganization
795+
796+
message.SenderPerspective = &perspective
797+
798+
messageBody := MessageTypeVolunteerRequestAcceptance{
799+
UserID: userID,
800+
AccepterID: accepterID,
801+
OpportunityID: opportunityID,
802+
}
803+
804+
jsonBytes, err := marshalMessageBody(messageBody)
805+
if err != nil {
806+
return 0, NewErrServerError()
807+
}
808+
809+
message.Body = *jsonBytes
810+
message.Edited = false
811+
if err := s.sendMessage(ctx, message); err != nil {
812+
s.logger.Error().Err(err).Msg("Error creating message")
813+
return 0, NewErrServerError()
814+
}
815+
816+
return message.ID, nil
817+
}

internal/conversations/view.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@ type PreviousExperience struct {
4848

4949
// MessageTypeVolunteerRequestAcceptanceView represents a view of a message containing an opportunity acceptance.
5050
type MessageTypeVolunteerRequestAcceptanceView struct {
51-
UserID int64 `json:"userId"`
52-
OpportunityID int64 `json:"opportunityId"`
53-
OpportunityTitle string `json:"opportunityTitle"`
51+
Volunteer *MessageUserWithName `json:"volunteer"`
52+
Accepter *MessageUserWithName `json:"accepter"`
53+
OpportunityID int64 `json:"opportunityId"`
54+
OpportunityTitle string `json:"opportunityTitle"`
55+
}
56+
57+
// MessageUserWithName represents a first and last name pair.
58+
type MessageUserWithName struct {
59+
ID int64 `json:"id"`
60+
FirstName string `json:"firstName"`
61+
LastName string `json:"lastName"`
5462
}
5563

5664
// MessageTypeHoursRequestedView represents the message sent when a volunteer requests hours from an organization.

internal/core/handlers/opportunities/volunteers_accept_post.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ package opportunities
33
import (
44
"net/http"
55

6+
"github.com/joinimpact/api/internal/conversations"
67
"github.com/joinimpact/api/internal/core/middleware/auth"
78
"github.com/joinimpact/api/internal/opportunities"
89
"github.com/joinimpact/api/pkg/idctx"
910
"github.com/joinimpact/api/pkg/resp"
1011
)
1112

1213
// VolunteersAcceptPost accepts a volunteer's request.
13-
func VolunteersAcceptPost(opportunitiesService opportunities.Service) http.HandlerFunc {
14+
func VolunteersAcceptPost(opportunitiesService opportunities.Service, conversationsService conversations.Service) http.HandlerFunc {
1415
type response struct {
15-
Success bool `json:"success"`
16+
Success bool `json:"success"`
17+
MessageID int64 `json:"messageId"`
1618
}
1719
return func(w http.ResponseWriter, r *http.Request) {
1820
ctx := r.Context()
@@ -46,6 +48,22 @@ func VolunteersAcceptPost(opportunitiesService opportunities.Service) http.Handl
4648
return
4749
}
4850

49-
resp.OK(w, r, response{true})
51+
id, err := conversationsService.SendVolunteerRequestAcceptanceMessage(ctx, volunteerID, userID, opportunityID)
52+
if err != nil {
53+
switch err.(type) {
54+
case *conversations.ErrConversationNotFound, *conversations.ErrUserNotFound:
55+
resp.NotFound(w, r, resp.APIError(err, nil))
56+
case *conversations.ErrServerError:
57+
resp.ServerError(w, r, resp.APIError(err, nil))
58+
default:
59+
resp.ServerError(w, r, resp.UnknownError)
60+
}
61+
return
62+
}
63+
64+
resp.OK(w, r, response{
65+
Success: true,
66+
MessageID: id,
67+
})
5068
}
5169
}

0 commit comments

Comments
 (0)