From 67e5d1314cddec6f0e0fce24a1d393037687b6cc Mon Sep 17 00:00:00 2001 From: Aleksandr Soloshenko Date: Fri, 11 Oct 2024 09:29:26 +0700 Subject: [PATCH] [api] add device self-info endpoint --- .dockerignore | 5 +- .gitignore | 14 +++-- Makefile | 2 +- api/mobile.http | 4 ++ api/swagger.json | 57 +++++++++++++++-- api/swagger.yaml | 43 +++++++++++-- deployments/docker-compose/docker-compose.yml | 16 ++--- go.mod | 3 +- go.sum | 10 +-- .../handlers/converters/devices.go | 21 +++++++ .../handlers/converters/devices_test.go | 61 +++++++++++++++++++ internal/sms-gateway/handlers/mobile.go | 52 ++++++++++++---- .../sms-gateway/handlers/webhooks/3rdparty.go | 4 +- internal/sms-gateway/models/models.go | 8 +++ internal/sms-gateway/models/models_test.go | 43 +++++++++++++ .../modules/webhooks/converters.go | 6 +- .../sms-gateway/modules/webhooks/models.go | 6 +- .../sms-gateway/modules/webhooks/service.go | 8 +-- 18 files changed, 301 insertions(+), 62 deletions(-) create mode 100644 internal/sms-gateway/handlers/converters/devices.go create mode 100644 internal/sms-gateway/handlers/converters/devices_test.go create mode 100644 internal/sms-gateway/models/models_test.go diff --git a/.dockerignore b/.dockerignore index c453429..db555c5 100644 --- a/.dockerignore +++ b/.dockerignore @@ -51,4 +51,7 @@ deployments/ build/ # Example requests files -*.http \ No newline at end of file +*.http + +# Ignore Go workspace +*.work diff --git a/.gitignore b/.gitignore index e51309b..0c5d740 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # File created using '.gitignore Generator' for Visual Studio Code: https://bit.ly/vscode-gig -# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos,linux,go,terraform -# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,macos,linux,go,terraform +# Created by https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,terraform,macos,linux,go,dotenv +# Edit at https://www.toptal.com/developers/gitignore?templates=windows,visualstudiocode,terraform,macos,linux,go,dotenv + +### dotenv ### +.env ### Go ### # If you prefer the allow list template instead of the deny list, see community template: @@ -47,7 +50,8 @@ go.work .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* @@ -153,7 +157,7 @@ $RECYCLE.BIN/ # Windows shortcuts *.lnk -# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,macos,linux,go,terraform +# End of https://www.toptal.com/developers/gitignore/api/windows,visualstudiocode,terraform,macos,linux,go,dotenv # Custom rules (everything added below won't be overriden by 'Generate .gitignore File' if you use 'Update' option) @@ -166,4 +170,4 @@ $RECYCLE.BIN/ /api/docs.go -.env +go.work* \ No newline at end of file diff --git a/Makefile b/Makefile index 6a0f830..6abc865 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ init: go mod download init-dev: init - go install github.com/cosmtrek/air@latest \ + go install github.com/air-verse/air@latest \ && go install github.com/swaggo/swag/cmd/swag@latest \ && go install github.com/pressly/goose/v3/cmd/goose@latest diff --git a/api/mobile.http b/api/mobile.http index e8e023e..faf07b0 100644 --- a/api/mobile.http +++ b/api/mobile.http @@ -2,6 +2,10 @@ @mobileToken={{$dotenv MOBILE__TOKEN}} @phone={{$dotenv PHONE}} +### +GET {{baseUrl}}/device HTTP/1.1 +Authorization: Bearer {{mobileToken}}1 + ### POST {{baseUrl}}/device HTTP/1.1 Authorization: Bearer 123456789 diff --git a/api/swagger.json b/api/swagger.json index 73a4099..2f75acf 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -423,6 +423,30 @@ } }, "/mobile/v1/device": { + "get": { + "description": "Returns device information", + "produces": [ + "application/json" + ], + "tags": [ + "Device" + ], + "summary": "Get device information", + "responses": { + "200": { + "description": "Device information", + "schema": { + "$ref": "#/definitions/smsgateway.MobileDeviceResponse" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "#/definitions/smsgateway.ErrorResponse" + } + } + } + }, "post": { "description": "Registers new device and returns credentials", "consumes": [ @@ -925,7 +949,7 @@ ] }, "simNumber": { - "description": "SIM card number (1-3)", + "description": "SIM card number (1-3), if not set - default SIM will be used", "type": "integer", "maximum": 3, "example": 1 @@ -997,6 +1021,23 @@ } } }, + "smsgateway.MobileDeviceResponse": { + "type": "object", + "properties": { + "device": { + "description": "Device information, empty if device is not registered on the server", + "allOf": [ + { + "$ref": "#/definitions/smsgateway.Device" + } + ] + }, + "externalIp": { + "description": "External IP", + "type": "string" + } + } + }, "smsgateway.MobileRegisterRequest": { "type": "object", "properties": { @@ -1165,7 +1206,7 @@ "description": "The type of event the webhook is triggered for.", "allOf": [ { - "$ref": "#/definitions/webhooks.EventType" + "$ref": "#/definitions/smsgateway.WebhookEvent" } ], "example": "sms:received" @@ -1183,17 +1224,21 @@ } } }, - "webhooks.EventType": { + "smsgateway.WebhookEvent": { "type": "string", "enum": [ "sms:received", "sms:sent", + "sms:delivered", + "sms:failed", "system:ping" ], "x-enum-varnames": [ - "EventTypeSmsReceived", - "EventTypeSmsSent", - "EventTypeSystemPing" + "WebhookEventSmsReceived", + "WebhookEventSmsSent", + "WebhookEventSmsDelivered", + "WebhookEventSmsFailed", + "WebhookEventSystemPing" ] } }, diff --git a/api/swagger.yaml b/api/swagger.yaml index 9e9863c..a4597f6 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -155,7 +155,7 @@ definitions: minItems: 1 type: array simNumber: - description: SIM card number (1-3) + description: SIM card number (1-3), if not set - default SIM will be used example: 1 maximum: 3 type: integer @@ -211,6 +211,17 @@ definitions: - recipients - state type: object + smsgateway.MobileDeviceResponse: + properties: + device: + allOf: + - $ref: '#/definitions/smsgateway.Device' + description: Device information, empty if device is not registered on the + server + externalIp: + description: External IP + type: string + type: object smsgateway.MobileRegisterRequest: properties: name: @@ -331,7 +342,7 @@ definitions: properties: event: allOf: - - $ref: '#/definitions/webhooks.EventType' + - $ref: '#/definitions/smsgateway.WebhookEvent' description: The type of event the webhook is triggered for. example: sms:received id: @@ -347,16 +358,20 @@ definitions: - event - url type: object - webhooks.EventType: + smsgateway.WebhookEvent: enum: - sms:received - sms:sent + - sms:delivered + - sms:failed - system:ping type: string x-enum-varnames: - - EventTypeSmsReceived - - EventTypeSmsSent - - EventTypeSystemPing + - WebhookEventSmsReceived + - WebhookEventSmsSent + - WebhookEventSmsDelivered + - WebhookEventSmsFailed + - WebhookEventSystemPing host: api.sms-gate.app info: contact: @@ -635,6 +650,22 @@ paths: - User - Webhooks /mobile/v1/device: + get: + description: Returns device information + produces: + - application/json + responses: + "200": + description: Device information + schema: + $ref: '#/definitions/smsgateway.MobileDeviceResponse' + "500": + description: Internal server error + schema: + $ref: '#/definitions/smsgateway.ErrorResponse' + summary: Get device information + tags: + - Device patch: consumes: - application/json diff --git a/deployments/docker-compose/docker-compose.yml b/deployments/docker-compose/docker-compose.yml index c762978..1876c4d 100644 --- a/deployments/docker-compose/docker-compose.yml +++ b/deployments/docker-compose/docker-compose.yml @@ -26,7 +26,7 @@ services: condition: service_healthy db: - image: mariadb:10.11 + image: mariadb:11.4 environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=sms @@ -37,17 +37,11 @@ services: - mariadb-data:/var/lib/mysql restart: 'unless-stopped' healthcheck: - test: - [ - "CMD", - "mysqladmin", - "ping", - "-proot", - "-h", - "127.0.0.1" - ] + test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"] + start_period: 10s + interval: 10s timeout: 5s - retries: 10 + retries: 3 volumes: mariadb-data: diff --git a/go.mod b/go.mod index 30365fc..c146078 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,11 @@ go 1.22.0 require ( firebase.google.com/go/v4 v4.12.1 - github.com/android-sms-gateway/client-go v1.0.5 + github.com/android-sms-gateway/client-go v1.1.0 github.com/ansrivas/fiberprometheus/v2 v2.6.1 github.com/capcom6/go-helpers v0.0.0-20240521035631-865ee2879fa3 github.com/capcom6/go-infra-fx v0.0.2 + github.com/go-playground/assert/v2 v2.2.0 github.com/go-playground/validator/v10 v10.16.0 github.com/go-sql-driver/mysql v1.7.1 github.com/gofiber/fiber/v2 v2.52.5 diff --git a/go.sum b/go.sum index 77c2f11..859d91b 100644 --- a/go.sum +++ b/go.sum @@ -26,10 +26,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= -github.com/android-sms-gateway/client-go v1.0.4 h1:QZ72TRBJKm11WL/jim+ba7m2J5RLBaICMcy7f/RVfuQ= -github.com/android-sms-gateway/client-go v1.0.4/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= -github.com/android-sms-gateway/client-go v1.0.5 h1:0jUnRJHk1VqV5K8kla1d8rOO/lXshHG5uChxj7YTzWE= -github.com/android-sms-gateway/client-go v1.0.5/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= +github.com/android-sms-gateway/client-go v1.1.0 h1:mAPsueRrY/qOdQAU5yO3uLQAb7Po+3jBxB1tiqyClvg= +github.com/android-sms-gateway/client-go v1.1.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+MnDOJZ3SK66kXM= @@ -193,8 +191,6 @@ github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/nyaruka/phonenumbers v1.3.0 h1:IFyyJfF2Elg8xGKFghWrRXzb6qAHk+Q3uPqmIgS20JQ= -github.com/nyaruka/phonenumbers v1.3.0/go.mod h1:4jyKp/BFUokLbCHyoZag+T3S1KezFVoEKtgnbpzItC4= github.com/nyaruka/phonenumbers v1.4.0 h1:ddhWiHnHCIX3n6ETDA58Zq5dkxkjlvgrDWM2OHHPCzU= github.com/nyaruka/phonenumbers v1.4.0/go.mod h1:gv+CtldaFz+G3vHHnasBSirAi3O2XLqZzVWz4V1pl2E= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= @@ -305,8 +301,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI= golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4= -golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d h1:N0hmiNbwsSNwHBAvR3QB5w25pUwH4tK0Y/RltD1j1h4= golang.org/x/exp v0.0.0-20240525044651-4c93da0ed11d/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/internal/sms-gateway/handlers/converters/devices.go b/internal/sms-gateway/handlers/converters/devices.go new file mode 100644 index 0000000..6e1844c --- /dev/null +++ b/internal/sms-gateway/handlers/converters/devices.go @@ -0,0 +1,21 @@ +package converters + +import ( + "github.com/android-sms-gateway/client-go/smsgateway" + "github.com/capcom6/sms-gateway/internal/sms-gateway/models" +) + +func DeviceToDTO(device *models.Device) *smsgateway.Device { + if device.IsEmpty() { + return nil + } + + return &smsgateway.Device{ + ID: device.ID, + Name: *device.Name, + CreatedAt: device.CreatedAt, + UpdatedAt: device.UpdatedAt, + DeletedAt: device.DeletedAt, + LastSeen: device.LastSeen, + } +} diff --git a/internal/sms-gateway/handlers/converters/devices_test.go b/internal/sms-gateway/handlers/converters/devices_test.go new file mode 100644 index 0000000..0d9fca5 --- /dev/null +++ b/internal/sms-gateway/handlers/converters/devices_test.go @@ -0,0 +1,61 @@ +package converters_test + +import ( + "testing" + "time" + + "github.com/android-sms-gateway/client-go/smsgateway" + "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/converters" + "github.com/capcom6/sms-gateway/internal/sms-gateway/models" + "github.com/capcom6/sms-gateway/pkg/types" + "github.com/go-playground/assert/v2" +) + +func TestDeviceToDTO(t *testing.T) { + createdAt := time.Now() + updatedAt := time.Now() + lastSeenAt := time.Now() + + tests := []struct { + name string + device *models.Device + expected *smsgateway.Device + }{ + { + name: "empty device", + device: &models.Device{}, + expected: nil, + }, + { + name: "non-empty device", + device: &models.Device{ + ID: "test-id", + Name: types.AsPointer("test-name"), + LastSeen: lastSeenAt, + TimedModel: models.TimedModel{ + CreatedAt: createdAt, + UpdatedAt: updatedAt, + }, + }, + expected: &smsgateway.Device{ + ID: "test-id", + Name: "test-name", + CreatedAt: createdAt, + UpdatedAt: updatedAt, + LastSeen: lastSeenAt, + }, + }, + { + name: "nil device", + device: nil, + expected: nil, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + actual := converters.DeviceToDTO(test.device) + assert.Equal(t, test.expected, actual) + }) + } +} diff --git a/internal/sms-gateway/handlers/mobile.go b/internal/sms-gateway/handlers/mobile.go index ff49f76..8838f0c 100644 --- a/internal/sms-gateway/handlers/mobile.go +++ b/internal/sms-gateway/handlers/mobile.go @@ -8,9 +8,11 @@ import ( "github.com/android-sms-gateway/client-go/smsgateway" "github.com/capcom6/go-infra-fx/http/apikey" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base" + "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/converters" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/webhooks" "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth" + "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/devices" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/messages" "github.com/capcom6/sms-gateway/internal/sms-gateway/repositories" "github.com/go-playground/validator/v10" @@ -32,6 +34,24 @@ type mobileHandler struct { idGen func() string } +// @Summary Get device information +// @Description Returns device information +// @Tags Device +// @Produce json +// @Success 200 {object} smsgateway.MobileDeviceResponse "Device information" +// @Failure 500 {object} smsgateway.ErrorResponse "Internal server error" +// @Router /mobile/v1/device [get] +// +// Get device information +func (h *mobileHandler) getDevice(device models.Device, c *fiber.Ctx) error { + res := smsgateway.MobileDeviceResponse{ + Device: converters.DeviceToDTO(&device), + ExternalIP: c.IP(), + } + + return c.JSON(res) +} + // @Summary Register device // @Description Registers new device and returns credentials // @Tags Device @@ -169,17 +189,16 @@ func (h *mobileHandler) Register(router fiber.Router) { }, }), h.postDevice) - router.Use(apikey.New(apikey.Config{ - Authorizer: func(token string) bool { - return len(token) > 0 - }, - }), func(c *fiber.Ctx) error { - token := c.Locals("token").(string) - - device, err := h.authSvc.AuthorizeDevice(token) - if err != nil { - h.Logger.Error("Can't authorize device", zap.Error(err)) - return fiber.ErrUnauthorized + router.Use(func(c *fiber.Ctx) (err error) { + header := c.Get(fiber.HeaderAuthorization) + device := models.Device{} + if len(header) > 7 && header[:7] == "Bearer " { + token := header[7:] + device, err = h.authSvc.AuthorizeDevice(token) + if err != nil && err != devices.ErrNotFound { + h.Logger.Error("Can't authorize device", zap.Error(err)) + return fiber.ErrUnauthorized + } } c.Locals("device", device) @@ -187,6 +206,17 @@ func (h *mobileHandler) Register(router fiber.Router) { return c.Next() }) + router.Get("/device", auth.WithDevice(h.getDevice)) + + router.Use(func(c *fiber.Ctx) error { + device := c.Locals("device").(models.Device) + if device.IsEmpty() { + return fiber.ErrUnauthorized + } + + return c.Next() + }) + router.Patch("/device", auth.WithDevice(h.patchDevice)) router.Get("/message", auth.WithDevice(h.getMessage)) diff --git a/internal/sms-gateway/handlers/webhooks/3rdparty.go b/internal/sms-gateway/handlers/webhooks/3rdparty.go index ab7d71a..e4f1aab 100644 --- a/internal/sms-gateway/handlers/webhooks/3rdparty.go +++ b/internal/sms-gateway/handlers/webhooks/3rdparty.go @@ -3,7 +3,7 @@ package webhooks import ( "fmt" - dto "github.com/android-sms-gateway/client-go/smsgateway/webhooks" + "github.com/android-sms-gateway/client-go/smsgateway" "github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base" "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth" @@ -64,7 +64,7 @@ func (h *ThirdPartyController) get(user models.User, c *fiber.Ctx) error { // // Register webhook func (h *ThirdPartyController) post(user models.User, c *fiber.Ctx) error { - dto := &dto.Webhook{} + dto := &smsgateway.Webhook{} if err := h.BodyParserValidator(c, dto); err != nil { return err diff --git a/internal/sms-gateway/models/models.go b/internal/sms-gateway/models/models.go index f629e8c..dccc10e 100644 --- a/internal/sms-gateway/models/models.go +++ b/internal/sms-gateway/models/models.go @@ -41,6 +41,14 @@ type Device struct { TimedModel } +func (d *Device) IsEmpty() bool { + if d == nil { + return true + } + + return d.ID == "" +} + type Message struct { ID uint64 `gorm:"primaryKey;type:BIGINT UNSIGNED;autoIncrement"` DeviceID string `gorm:"not null;type:char(21);uniqueIndex:unq_messages_id_device,priority:2;index:idx_messages_device_state"` diff --git a/internal/sms-gateway/models/models_test.go b/internal/sms-gateway/models/models_test.go new file mode 100644 index 0000000..777d8c5 --- /dev/null +++ b/internal/sms-gateway/models/models_test.go @@ -0,0 +1,43 @@ +package models_test + +import ( + "testing" + + "github.com/capcom6/sms-gateway/internal/sms-gateway/models" +) + +func TestDevice_IsEmpty(t *testing.T) { + tests := []struct { + name string + d *models.Device + want bool + }{ + { + name: "nil Device", + d: nil, + want: true, + }, + { + name: "empty ID", + d: &models.Device{ + ID: "", + }, + want: true, + }, + { + name: "non-empty ID", + d: &models.Device{ + ID: "some-id", + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := tt.d.IsEmpty(); got != tt.want { + t.Errorf("IsEmpty() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/internal/sms-gateway/modules/webhooks/converters.go b/internal/sms-gateway/modules/webhooks/converters.go index ae78553..c2be2fb 100644 --- a/internal/sms-gateway/modules/webhooks/converters.go +++ b/internal/sms-gateway/modules/webhooks/converters.go @@ -1,11 +1,11 @@ package webhooks import ( - "github.com/android-sms-gateway/client-go/smsgateway/webhooks" + "github.com/android-sms-gateway/client-go/smsgateway" ) -func webhookToDTO(model *Webhook) webhooks.Webhook { - return webhooks.Webhook{ +func webhookToDTO(model *Webhook) smsgateway.Webhook { + return smsgateway.Webhook{ ID: model.ExtID, URL: model.URL, Event: model.Event, diff --git a/internal/sms-gateway/modules/webhooks/models.go b/internal/sms-gateway/modules/webhooks/models.go index 1a9922b..4d10667 100644 --- a/internal/sms-gateway/modules/webhooks/models.go +++ b/internal/sms-gateway/modules/webhooks/models.go @@ -1,7 +1,7 @@ package webhooks import ( - "github.com/android-sms-gateway/client-go/smsgateway/webhooks" + "github.com/android-sms-gateway/client-go/smsgateway" "github.com/capcom6/sms-gateway/internal/sms-gateway/models" "gorm.io/gorm" ) @@ -11,8 +11,8 @@ type Webhook struct { ExtID string `json:"id" gorm:"not null;type:varchar(36);uniqueIndex:unq_webhooks_user_extid,priority:2"` UserID string `json:"-" gorm:"<-:create;not null;type:varchar(32);uniqueIndex:unq_webhooks_user_extid,priority:1"` - URL string `json:"url" validate:"required,http_url" gorm:"not null;type:varchar(256)"` - Event webhooks.EventType `json:"event" gorm:"not null;type:varchar(32)"` + URL string `json:"url" validate:"required,http_url" gorm:"not null;type:varchar(256)"` + Event smsgateway.WebhookEvent `json:"event" gorm:"not null;type:varchar(32)"` User models.User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"` diff --git a/internal/sms-gateway/modules/webhooks/service.go b/internal/sms-gateway/modules/webhooks/service.go index 16798ba..3ba0bea 100644 --- a/internal/sms-gateway/modules/webhooks/service.go +++ b/internal/sms-gateway/modules/webhooks/service.go @@ -3,7 +3,7 @@ package webhooks import ( "fmt" - "github.com/android-sms-gateway/client-go/smsgateway/webhooks" + "github.com/android-sms-gateway/client-go/smsgateway" "github.com/capcom6/go-helpers/slices" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/db" "github.com/capcom6/sms-gateway/internal/sms-gateway/modules/devices" @@ -46,7 +46,7 @@ func NewService(params ServiceParams) *Service { } } -func (s *Service) Select(userID string, filters ...SelectFilter) ([]webhooks.Webhook, error) { +func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.Webhook, error) { filters = append(filters, WithUserID(userID)) items, err := s.webhooks.Select(filters...) @@ -57,8 +57,8 @@ func (s *Service) Select(userID string, filters ...SelectFilter) ([]webhooks.Web return slices.Map(items, webhookToDTO), nil } -func (s *Service) Replace(userID string, webhook *webhooks.Webhook) error { - if !webhooks.IsValidEventType(webhook.Event) { +func (s *Service) Replace(userID string, webhook *smsgateway.Webhook) error { + if !smsgateway.IsValidWebhookEvent(webhook.Event) { return newValidationError("event", string(webhook.Event), fmt.Errorf("enum value expected")) }