Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions extension/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package extension

import "lineblocs.com/api/model"

/*
Interface of Call Store.
Implementation of Call Store is located /store/call
*/
type Store interface {
GetExtensionByUsername(workspaceId int, username string) (*model.Extension, error)
}
33 changes: 33 additions & 0 deletions handler/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,33 @@ import (
"github.com/sirupsen/logrus"
"lineblocs.com/api/model"
"lineblocs.com/api/utils"
"lineblocs.com/api/extension"
)

func applyExtensionValues(extensionStore extension.Store, call *model.Call) (error) {
var extensionValue string

switch call.Direction {
case "inbound":
extensionValue = call.To
exten, err := extensionStore.GetExtensionByUsername(call.WorkspaceId, extensionValue)
if err != nil {
return err
}
call.ToExtensionId = &exten.Id
case "outbound":
extensionValue = call.From
exten, err := extensionStore.GetExtensionByUsername(call.WorkspaceId, extensionValue)
if err != nil {
return err
}
call.FromExtensionId = &exten.Id
default:
}

return nil
}

/*
Input: Call model
Todo : Create new call and store to db
Expand All @@ -34,6 +59,14 @@ func (h *Handler) CreateCall(c echo.Context) error {
go h.callStore.CheckIsMakingOutboundCallFirstTime(call)
}

populateExtensionDetails := c.QueryParam("populateExtensionDetails")
if populateExtensionDetails != "" && populateExtensionDetails == "1" {
err := applyExtensionValues(h.extensionStore, &call)
if err != nil {
utils.Log(logrus.InfoLevel, "CreateCall populate extension details failed. err: " + err.Error())
}
}
// check if we need to populate the extension details
callId, err := h.callStore.CreateCall(&call)
if err != nil {
return utils.HandleInternalErr("CreateCall Could not execute query", err, c)
Expand Down
4 changes: 3 additions & 1 deletion handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"lineblocs.com/api/logger"
"lineblocs.com/api/recording"
"lineblocs.com/api/user"
"lineblocs.com/api/extension"
)

/*
Expand All @@ -25,9 +26,10 @@ type Handler struct {
loggerStore logger.Store
recordingStore recording.Store
userStore user.Store
extensionStore extension.Store
}

func NewHandler(as admin.Store, cs call.Store, crs carrier.Store, ds debit.Store, fs fax.Store, ls logger.Store, rs recording.Store, us user.Store) *Handler {
func NewHandler(as admin.Store, cs call.Store, crs carrier.Store, ds debit.Store, fs fax.Store, ls logger.Store, rs recording.Store, us user.Store, es extension.Store) *Handler {
return &Handler{
adminStore: as,
callStore: cs,
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func startServer() {
ls := store.NewLoggerStore(db)
rs := store.NewRecordingStore(db)
us := store.NewUserStore(db)
h := handler.NewHandler(as, cs, crs, ds, fs, ls, rs, us)
es := store.NewExtensionStore(db)
h := handler.NewHandler(as, cs, crs, ds, fs, ls, rs, us, es)

// Register Handler for Echo context
h.Register(r)
Expand Down
2 changes: 2 additions & 0 deletions model/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Call struct {
StartedAt string `json:"started_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
FromExtensionId *int `json:"from_extension_id"`
ToExtensionId *int `json:"to_extension_id"`
PlanSnapshot string `json:"plan_snapshot"`
}

Expand Down
5 changes: 5 additions & 0 deletions model/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model

type Extension struct {
Id int `json:"id"`
}
4 changes: 2 additions & 2 deletions store/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (cs *CallStore) CreateCall(call *model.Call) (string, error) {
return "-1", err
}

stmt, err := cs.db.Prepare("INSERT INTO calls ( `from`, `to`, `channel_id`, `status`, `direction`, `duration`, `sip_call_id`, `user_id`, `workspace_id`, `started_at`, `created_at`, `updated_at`, `api_id`, `plan_snapshot`, `notes`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '' )")
stmt, err := cs.db.Prepare("INSERT INTO calls ( `from`, `to`, `channel_id`, `status`, `direction`, `duration`, `sip_call_id`, `user_id`, `workspace_id`, `started_at`, `created_at`, `updated_at`, `api_id`, `plan_snapshot`, `notes`, `from_extension_id`, `to_extension_id`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, '', ?, ? )")
if err != nil {
return "-1", err
}
defer stmt.Close()

res, err := stmt.Exec(call.From, call.To, call.ChannelId, call.Status, call.Direction, "8", call.SIPCallId, call.UserId, call.WorkspaceId, now, now, now, call.APIId, workspace.Plan)
res, err := stmt.Exec(call.From, call.To, call.ChannelId, call.Status, call.Direction, "8", call.SIPCallId, call.UserId, call.WorkspaceId, now, now, now, call.APIId, workspace.Plan, call.FromExtensionId, call.ToExtensionId)

if err != nil {
return "-1", err
Expand Down
38 changes: 38 additions & 0 deletions store/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package store

import (
"database/sql"
"strconv"

"lineblocs.com/api/model"
)

/*
Implementation of Extension Store
*/

type ExtensionStore struct {
db *sql.DB
}

func NewExtensionStore(db *sql.DB) *ExtensionStore {
return &ExtensionStore{
db: db,
}
}

/*
Input: CallRate model, Extension Model
Todo : Create new user_extension and store to db
Output: If success return nil else return err
*/
func (ds *ExtensionStore) GetExtensionByUsername(workspaceId int, username string) (*model.Extension, error) {
row := ds.db.QueryRow("SELECT id FROM extensions WHERE username = ? AND workspace_id = ?", username, strconv.Itoa(workspaceId))
extension := model.Extension{}
err := row.Scan(
&extension.Id)
if err == sql.ErrNoRows {
return nil, err
}
return &extension, nil
}