From 0fac2e2d1672c0c167222a029329f3f941cdcd99 Mon Sep 17 00:00:00 2001 From: Nadir Hamid Date: Wed, 26 Apr 2023 22:40:31 -0600 Subject: [PATCH 1/2] add code for extension history --- extension/extension.go | 11 +++++++++++ handler/call.go | 26 ++++++++++++++++++++++++++ handler/handler.go | 4 +++- main.go | 3 ++- model/call.go | 2 ++ model/extension.go | 5 +++++ store/call.go | 4 ++-- store/extension.go | 38 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 extension/extension.go create mode 100644 model/extension.go create mode 100644 store/extension.go diff --git a/extension/extension.go b/extension/extension.go new file mode 100644 index 0000000..87cad33 --- /dev/null +++ b/extension/extension.go @@ -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) +} diff --git a/handler/call.go b/handler/call.go index 61a130b..f8a319f 100644 --- a/handler/call.go +++ b/handler/call.go @@ -8,8 +8,25 @@ 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, extensionValue string) (error) { + exten, err := extensionStore.GetExtensionByUsername(call.WorkspaceId, extensionValue) + if err != nil { + return err + } + + switch call.Direction { + case "inbound": + call.ToExtensionId = &exten.Id + case "outbound": + call.FromExtensionId = &exten.Id + default: + } + return nil +} + /* Input: Call model Todo : Create new call and store to db @@ -34,6 +51,15 @@ func (h *Handler) CreateCall(c echo.Context) error { go h.callStore.CheckIsMakingOutboundCallFirstTime(call) } + populateExtensionDetails := c.QueryParam("populateExtensionDetails") + extensionValue := c.QueryParam("extensionValue") + if populateExtensionDetails != "" && populateExtensionDetails == "1" { + err := applyExtensionValues(h.extensionStore, &call, extensionValue) + 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) diff --git a/handler/handler.go b/handler/handler.go index 4069794..c32e4cc 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -9,6 +9,7 @@ import ( "lineblocs.com/api/logger" "lineblocs.com/api/recording" "lineblocs.com/api/user" + "lineblocs.com/api/extension" ) /* @@ -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, diff --git a/main.go b/main.go index 2477dbe..294b024 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/model/call.go b/model/call.go index 726d7cb..8b1c7c6 100644 --- a/model/call.go +++ b/model/call.go @@ -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"` } diff --git a/model/extension.go b/model/extension.go new file mode 100644 index 0000000..6bc09b5 --- /dev/null +++ b/model/extension.go @@ -0,0 +1,5 @@ +package model + +type Extension struct { + Id int `json:"id"` +} \ No newline at end of file diff --git a/store/call.go b/store/call.go index e28c1ce..f73a4e4 100644 --- a/store/call.go +++ b/store/call.go @@ -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 diff --git a/store/extension.go b/store/extension.go new file mode 100644 index 0000000..366a9ae --- /dev/null +++ b/store/extension.go @@ -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 +} \ No newline at end of file From 690258114d4122f66ac574462da872eb2dbbfdf5 Mon Sep 17 00:00:00 2001 From: Nadir Hamid Date: Wed, 26 Apr 2023 23:00:09 -0600 Subject: [PATCH 2/2] improve some code --- handler/call.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/handler/call.go b/handler/call.go index f8a319f..5dcf590 100644 --- a/handler/call.go +++ b/handler/call.go @@ -11,19 +11,27 @@ import ( "lineblocs.com/api/extension" ) -func applyExtensionValues(extensionStore extension.Store, call *model.Call, extensionValue string) (error) { - exten, err := extensionStore.GetExtensionByUsername(call.WorkspaceId, extensionValue) - if err != nil { - return err - } +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 } @@ -52,9 +60,8 @@ func (h *Handler) CreateCall(c echo.Context) error { } populateExtensionDetails := c.QueryParam("populateExtensionDetails") - extensionValue := c.QueryParam("extensionValue") if populateExtensionDetails != "" && populateExtensionDetails == "1" { - err := applyExtensionValues(h.extensionStore, &call, extensionValue) + err := applyExtensionValues(h.extensionStore, &call) if err != nil { utils.Log(logrus.InfoLevel, "CreateCall populate extension details failed. err: " + err.Error()) }