Skip to content

Commit

Permalink
feat(endpoint): support accountcode
Browse files Browse the repository at this point in the history
Account code may be set during endpoint creation. At the moment it
behaves in a similar fashion as the endpoint ID, meaning it is static
and can only be set during creation.

Closes #13.
  • Loading branch information
crazybolillo committed Sep 13, 2024
1 parent 3fa6983 commit c37506c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 19 deletions.
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ definitions:
type: object
model.Endpoint:
properties:
accountCode:
type: string
codecs:
items:
type: string
Expand Down Expand Up @@ -62,6 +64,8 @@ definitions:
type: object
model.NewEndpoint:
properties:
accountCode:
type: string
codecs:
items:
type: string
Expand Down
1 change: 1 addition & 0 deletions internal/handler/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func MustCreate(handler http.Handler) func(*testing.T) {

want := model.Endpoint{
Sid: got.Sid,
AccountCode: "zinniaelegans",
ID: endpoint.ID,
DisplayName: endpoint.DisplayName,
Transport: endpoint.Transport,
Expand Down
2 changes: 2 additions & 0 deletions internal/model/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
type Endpoint struct {
Sid int32 `json:"sid"`
ID string `json:"id"`
AccountCode string `json:"accountCode"`
DisplayName string `json:"displayName"`
Transport string `json:"transport"`
Context string `json:"context"`
Expand All @@ -13,6 +14,7 @@ type Endpoint struct {

type NewEndpoint struct {
ID string `json:"id"`
AccountCode string `json:"accountCode"`
Password string `json:"password"`
Transport string `json:"transport,omitempty"`
Context string `json:"context"`
Expand Down
13 changes: 8 additions & 5 deletions internal/service/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package service

import (
"cmp"
"context"
"crypto/md5"
"encoding/hex"
Expand Down Expand Up @@ -62,11 +63,12 @@ func (e *EndpointService) Create(ctx context.Context, payload model.NewEndpoint)
}

sid, err := queries.NewEndpoint(ctx, sqlc.NewEndpointParams{
ID: payload.ID,
Transport: db.Text(payload.Transport),
Context: db.Text(payload.Context),
Allow: db.Text(strings.Join(payload.Codecs, ",")),
Callerid: db.Text(fmt.Sprintf(`"%s" <%s>`, payload.DisplayName, payload.ID)),
ID: payload.ID,
Accountcode: db.Text(cmp.Or(payload.AccountCode, payload.ID)),
Transport: db.Text(payload.Transport),
Context: db.Text(payload.Context),
Allow: db.Text(strings.Join(payload.Codecs, ",")),
Callerid: db.Text(fmt.Sprintf(`"%s" <%s>`, payload.DisplayName, payload.ID)),
})
if err != nil {
return model.Endpoint{}, err
Expand Down Expand Up @@ -108,6 +110,7 @@ func (e *EndpointService) Read(ctx context.Context, sid int32) (model.Endpoint,
endpoint := model.Endpoint{
Sid: sid,
ID: row.ID,
AccountCode: row.Accountcode.String,
DisplayName: displayNameFromClid(row.Callerid.String),
Transport: row.Transport.String,
Context: row.Context.String,
Expand Down
2 changes: 1 addition & 1 deletion internal/sqlc/db.go

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

2 changes: 1 addition & 1 deletion internal/sqlc/models.go

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

22 changes: 13 additions & 9 deletions internal/sqlc/queries.sql.go

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

6 changes: 3 additions & 3 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ VALUES

-- name: NewEndpoint :one
INSERT INTO ps_endpoints
(id, transport, aors, auth, context, disallow, allow, callerid)
(id, transport, aors, auth, context, disallow, allow, callerid, accountcode)
VALUES
($1, $2, $1, $1, $3, 'all', $4, $5)
($1, $2, $1, $1, $3, 'all', $4, $5, $6)
RETURNING sid;

-- name: DeleteEndpoint :one
Expand Down Expand Up @@ -56,7 +56,7 @@ WHERE

-- name: GetEndpointByID :one
SELECT
pe.id, pe.callerid, pe.context, ee.extension, pe.transport, aor.max_contacts, pe.allow
pe.id, pe.accountcode, pe.callerid, pe.context, ee.extension, pe.transport, aor.max_contacts, pe.allow
FROM
ps_endpoints pe
INNER JOIN
Expand Down

0 comments on commit c37506c

Please sign in to comment.