Skip to content

Commit 171da3e

Browse files
committed
Fix 3cx handler logic
1 parent 5c21681 commit 171da3e

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

api/cmd/experiments/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import (
66
"log"
77
"os"
88
"sync"
9+
"time"
10+
11+
"github.com/NdoleStudio/httpsms/pkg/di"
12+
"github.com/NdoleStudio/httpsms/pkg/entities"
13+
"github.com/google/uuid"
914

1015
"github.com/carlmjohnson/requests"
1116

@@ -18,7 +23,19 @@ func main() {
1823
log.Fatal("Error loading .env file")
1924
}
2025

21-
loadTest()
26+
container := di.NewLiteContainer()
27+
repo := container.Integration3CXRepository()
28+
29+
err = repo.Save(context.Background(), &entities.Integration3CX{
30+
ID: uuid.MustParse("b0b1acdc-69dd-4aee-8277-ba4adc5d2e90"),
31+
UserID: "XtABz6zdeFMoBLoltz6SREDvRSh2",
32+
WebhookURL: "https://lagomtest.3cx.com.au/sms/generic/155e125bf7874f8fae5adbcaac49fdf8",
33+
CreatedAt: time.Now().UTC(),
34+
UpdatedAt: time.Now().UTC(),
35+
})
36+
if err != nil {
37+
container.Logger().Fatal(err)
38+
}
2239
}
2340

2441
func loadTest() {

api/pkg/di/container.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,16 @@ func (container *Container) MessageRepository() (repository repositories.Message
560560
)
561561
}
562562

563+
// Integration3CXRepository creates a new instance of repositories.Integration3CxRepository
564+
func (container *Container) Integration3CXRepository() (repository repositories.Integration3CxRepository) {
565+
container.logger.Debug("creating GORM repositories.Integration3CxRepository")
566+
return repositories.NewGormIntegration3CXRepository(
567+
container.Logger(),
568+
container.Tracer(),
569+
container.DB(),
570+
)
571+
}
572+
563573
// PhoneRepository creates a new instance of repositories.PhoneRepository
564574
func (container *Container) PhoneRepository() (repository repositories.PhoneRepository) {
565575
container.logger.Debug("creating GORM repositories.PhoneRepository")

api/pkg/entities/integration_3cx.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ type Integration3CX struct {
1414
CreatedAt time.Time `json:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
1515
UpdatedAt time.Time `json:"updated_at" example:"2022-06-05T14:26:10.303278+03:00"`
1616
}
17+
18+
// TableName overrides the table name used by Integration3CX
19+
func (Integration3CX) TableName() string {
20+
return "integration_3cx"
21+
}

api/pkg/handlers/integration_3cx_handler.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,20 @@ func (h *Integration3CXHandler) Messages(c *fiber.Ctx) error {
7272
return h.responsePaymentRequired(c, *msg)
7373
}
7474

75+
request.Sanitize()
7576
message, err := h.messageService.SendMessage(ctx, request.ToMessageSendParams(h.userIDFomContext(c), c.OriginalURL()))
7677
if err != nil {
7778
msg := fmt.Sprintf("cannot send [3cx] message with paylod [%s]", c.Body())
7879
ctxLogger.Error(stacktrace.Propagate(err, msg))
7980
return h.responseInternalServerError(c)
8081
}
8182

82-
return h.responseOK(c, "message added to queue", message)
83+
ctxLogger.Info(fmt.Sprintf("[3cx] message sent with ID [%s]", message.ID))
84+
return c.Status(fiber.StatusOK).JSON(fiber.Map{
85+
"data": fiber.Map{
86+
"payload": fiber.Map{
87+
"id": message.ID.String(),
88+
},
89+
},
90+
})
8391
}

0 commit comments

Comments
 (0)