Skip to content

Commit

Permalink
Merge pull request #146 from optriment/refactor/update-chats
Browse files Browse the repository at this point in the history
Refactor chats
  • Loading branch information
gruz0 authored Nov 26, 2022
2 parents d8bdc93 + 0151d52 commit 2942694
Show file tree
Hide file tree
Showing 9 changed files with 928 additions and 10 deletions.
899 changes: 896 additions & 3 deletions intest/chat_test.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions pkg/controller/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ func (cont *Chat) list(c echo.Context) error {
}

m, err := cont.svc.ListByParticipant(c.Request().Context(), uc.Subject.ID)
if err == nil {
return c.JSON(http.StatusOK, m)
if err != nil {
return err
}
return err

return c.JSON(http.StatusOK, m)
}
3 changes: 3 additions & 0 deletions pkg/db/pgdao/chats.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/db/sqlc/queries/chats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ select
, p.id as person_id
, p.display_name as person_display_name
, p.ethereum_address as person_ethereum_address
, m1.created_at as last_message_at
from chats c
join chats_participants cp on c.id = cp.chat_id
join persons p on p.id = cp.person_id
Expand Down
3 changes: 3 additions & 0 deletions pkg/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,9 @@ const docTemplate = `{
"kind": {
"type": "string"
},
"last_message_at": {
"type": "string"
},
"participants": {
"type": "array",
"items": {
Expand Down
3 changes: 3 additions & 0 deletions pkg/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,9 @@
"kind": {
"type": "string"
},
"last_message_at": {
"type": "string"
},
"participants": {
"type": "array",
"items": {
Expand Down
2 changes: 2 additions & 0 deletions pkg/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ definitions:
type: string
kind:
type: string
last_message_at:
type: string
participants:
items:
$ref: '#/definitions/model.ParticipantDTO'
Expand Down
1 change: 1 addition & 0 deletions pkg/model/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type (
ApplicationID string `json:"application_id,omitempty"`
ContractID string `json:"contract_id,omitempty"`
Participants []*ParticipantDTO `json:"participants,omitempty"`
LastMessageAt time.Time `json:"last_message_at"`
}

// ParticipantDTO represents is a chat participant
Expand Down
19 changes: 15 additions & 4 deletions pkg/service/pgsvc/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,18 @@ func (s *ChatSvc) AddMessage(ctx context.Context, chatID, participantID, text st

var result *model.Message
return result, doWithQueries(ctx, s.db, defaultRwTxOpts, func(queries *pgdao.Queries) error {
chat, err := queries.ChatGet(ctx, chatID)

if errors.Is(err, sql.ErrNoRows) {
return model.ErrEntityNotFound
}

if err != nil {
return fmt.Errorf("unable to get chat %s info: %w", chatID, err)
}

if _, e := queries.ChatParticipantGet(ctx, pgdao.ChatParticipantGetParams{
ChatID: chatID,
ChatID: chat.ID,
PersonID: participantID,
}); e != nil {
if errors.Is(e, sql.ErrNoRows) {
Expand All @@ -139,7 +149,7 @@ func (s *ChatSvc) AddMessage(ctx context.Context, chatID, participantID, text st

r, err := queries.MessageAdd(ctx, pgdao.MessageAddParams{
ID: pgdao.NewID(),
ChatID: chatID,
ChatID: chat.ID,
CreatedBy: participantID,
Text: strings.TrimSpace(text),
})
Expand Down Expand Up @@ -201,15 +211,15 @@ func (s *ChatSvc) Get(ctx context.Context, chatID, participantID string) (*model

// ListByParticipant implements service.Chat
func (s *ChatSvc) ListByParticipant(ctx context.Context, participantID string) ([]*model.ChatDTO, error) {
var result []*model.ChatDTO
result := make([]*model.ChatDTO, 0)

return result, doWithQueries(ctx, s.db, defaultRwTxOpts, func(queries *pgdao.Queries) error {
cc, err := queries.ChatsListByParticipant(ctx, participantID)
if err != nil {
return err
}

for _, c := range cc {

var (
kind, id = topicParts(c.Topic)
jobID = ""
Expand Down Expand Up @@ -252,6 +262,7 @@ func (s *ChatSvc) ListByParticipant(ctx context.Context, participantID string) (
JobID: jobID,
ApplicationID: applicationID,
ContractID: contractID,
LastMessageAt: c.LastMessageAt,
Participants: []*model.ParticipantDTO{
{
ID: c.PersonID,
Expand Down

0 comments on commit 2942694

Please sign in to comment.