mirror of
https://github.com/makayabou/asg-server.git
synced 2026-05-02 17:43:36 +02:00
[api] add device self-info endpoint
This commit is contained in:
parent
7dd333e775
commit
67e5d1314c
@ -52,3 +52,6 @@ build/
|
||||
|
||||
# Example requests files
|
||||
*.http
|
||||
|
||||
# Ignore Go workspace
|
||||
*.work
|
||||
|
||||
12
.gitignore
vendored
12
.gitignore
vendored
@ -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:
|
||||
@ -49,6 +52,7 @@ go.work
|
||||
# Icon must end with two \r
|
||||
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*
|
||||
2
Makefile
2
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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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:
|
||||
|
||||
3
go.mod
3
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
|
||||
|
||||
10
go.sum
10
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=
|
||||
|
||||
21
internal/sms-gateway/handlers/converters/devices.go
Normal file
21
internal/sms-gateway/handlers/converters/devices.go
Normal file
@ -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,
|
||||
}
|
||||
}
|
||||
61
internal/sms-gateway/handlers/converters/devices_test.go
Normal file
61
internal/sms-gateway/handlers/converters/devices_test.go
Normal file
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -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,24 +189,34 @@ 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 {
|
||||
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)
|
||||
|
||||
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))
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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"`
|
||||
|
||||
43
internal/sms-gateway/models/models_test.go
Normal file
43
internal/sms-gateway/models/models_test.go
Normal file
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
|
||||
@ -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"
|
||||
)
|
||||
@ -12,7 +12,7 @@ type Webhook struct {
|
||||
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)"`
|
||||
Event smsgateway.WebhookEvent `json:"event" gorm:"not null;type:varchar(32)"`
|
||||
|
||||
User models.User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE"`
|
||||
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user