Skip to content
Merged
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
70 changes: 35 additions & 35 deletions apis/appointments.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,69 @@
package apis

import (
"io/ioutil"
"io"
"net/http"

"github.com/gorilla/mux"
"github.com/webitel/engine/model"
"github.com/webitel/engine/web"
"github.com/webitel/wlog"
)

var (
appointmentHeader = "X-WBT-KEY"
const appointmentHeader = "X-WBT-KEY"

var (
errAllowOrigin = model.NewForbiddenError("api.valid.origin", "Not allow")
)

func (api *API) InitAppointments() {
api.Routes.Root.Handle("/appointments/{id}", api.ApiHandlerTrustRequester(getAppointments)).Methods("GET")
api.Routes.Root.Handle("/appointments/{id}", api.ApiHandlerTrustRequester(listAppointments)).Methods("GET")
api.Routes.Root.Handle("/appointments/{id}", api.ApiHandlerTrustRequester(createAppointments)).Methods("POST")
api.Routes.Root.Handle("/appointments/{id}", api.ApiHandlerTrustRequester(cancelAppointments)).Methods("DELETE")
}

/*
list/get

add
cancel
*/

func getAppointments(c *Context, w http.ResponseWriter, r *http.Request) {

key, _ := getKeyFromRequest(r)

var appointment *model.Appointment
func listAppointments(c *Context, w http.ResponseWriter, r *http.Request) {
uri := getIdFromRequest(r)

widgetUri := getIdFromRequest(r)
ctx := r.Context()
widget, err := c.App.AppointmentWidget(r.Context(), uri)
if err != nil {
c.Err = err

var widget *model.AppointmentWidget
if widget, c.Err = c.App.AppointmentWidget(ctx, widgetUri); c.Err != nil {
return
}

if !widget.AllowOrigin(c.IpAddress) {
c.Err = errAllowOrigin

return
}

if key != "" {
if appointment, c.Err = c.App.GetAppointment(ctx, key); c.Err != nil {
if c.Err.GetStatusCode() == http.StatusNotFound {
c.Err = nil
} else {
return
key, ok := getKeyFromRequest(r)
if !ok {
if _, err := web.WriteJSON(w, widget.ComputedList); err != nil {
c.Log.Error("writing appointments list response", wlog.Err(err))
}

return
}

appointment, err := c.App.GetAppointment(r.Context(), key, widget)
if err != nil {
if err.GetStatusCode() == http.StatusNotFound {
if _, err := web.WriteJSON(w, widget.ComputedList); err != nil {
c.Log.Error("writing appointments list response", wlog.Err(err))
}

} else {
w.Write(appointment.Computed)
return
}
}

w.Write(widget.ComputedList)
c.Err = err
return
}

return
if _, err := web.WriteJSON(w, appointment.Computed); err != nil {
c.Log.Error("writing appointment response", wlog.Err(err))
}
}

func createAppointments(c *Context, w http.ResponseWriter, r *http.Request) {
Expand All @@ -72,11 +73,11 @@
key, _ := getKeyFromRequest(r)

if key != "" {
getAppointments(c, w, r)
listAppointments(c, w, r)
return
}

body, err = ioutil.ReadAll(r.Body)
body, err = io.ReadAll(r.Body)
if err != nil {
//todo error
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -113,7 +114,7 @@
}

w.Header().Set(appointmentHeader, appointment.Key)
w.Write(appointment.Computed)

Check failure

Code scanning / CodeQL

Reflected cross-site scripting High

Cross-site scripting vulnerability due to
user-provided value
.
}

func cancelAppointments(c *Context, w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -142,8 +143,7 @@
}

func getIdFromRequest(r *http.Request) string {
props := mux.Vars(r)
return "/" + props["id"]
return "/" + mux.Vars(r)["id"]
}

func getKeyFromRequest(r *http.Request) (string, bool) {
Expand Down
2 changes: 1 addition & 1 deletion apis/displays.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func createDisplays(c *Context, w http.ResponseWriter, r *http.Request) {
return
}

displays, appErr := c.App.CreateOutboundResourceDisplays(r.Context(), resourceId, mappedData)
displays, appErr := c.App.CreateOutboundResourceDisplays(r.Context(), resourceId, session.Domain(0), mappedData)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Використання c.Session для отримання домену сесії замість невизначеної змінної session.

Suggested change
displays, appErr := c.App.CreateOutboundResourceDisplays(r.Context(), resourceId, session.Domain(0), mappedData)
displays, appErr := c.App.CreateOutboundResourceDisplays(r.Context(), resourceId, c.Session.Domain(0), mappedData)

if appErr != nil {
c.Err = appErr
return
Expand Down
50 changes: 29 additions & 21 deletions app/appointments.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ func init() {
cacheAppointmentDate = utils.NewLruWithParams(sizeCacheAppointments, "List appointment date", 60, "")
}

func (app *App) GetAppointment(ctx context.Context, key string) (*model.Appointment, model.AppError) {
if a, ok := cacheAppointments.Get(key); ok {
func formatAppointmentCacheKey(domainID, queueID int64, key string) string {
return fmt.Sprintf("%d-%d-%s", domainID, queueID, key)
}

func (app *App) GetAppointment(ctx context.Context, key string, widget *model.AppointmentWidget) (*model.Appointment, model.AppError) {
if a, ok := cacheAppointments.Get(formatAppointmentCacheKey(widget.Profile.DomainId, int64(widget.Profile.QueueId), key)); ok {
return a.(*model.Appointment), nil
}

Expand All @@ -35,21 +39,24 @@ func (app *App) GetAppointment(ctx context.Context, key string) (*model.Appointm
return nil, appErr
}

res, err, shared := appointmentGroupRequest.Do(fmt.Sprintf("member-%d", memberId), func() (interface{}, error) {
a, err := app.Store.Member().GetAppointment(ctx, memberId)
if err != nil {
return nil, err
}

a.Computed = (&model.AppointmentResponse{
Timezone: a.Timezone,
Type: "appointment",
List: nil,
Appointment: a,
}).ToJSON()

return a, nil
})
res, err, shared := appointmentGroupRequest.Do(
fmt.Sprintf("member-%d-%d-%d", widget.Profile.DomainId, widget.Profile.QueueId, memberId),
func() (any, error) {
a, err := app.Store.Member().GetAppointment(ctx, memberId, widget.Profile.DomainId, int64(widget.Profile.QueueId))
if err != nil {
return nil, err
}

a.Computed = (&model.AppointmentResponse{
Timezone: a.Timezone,
Type: "appointment",
List: nil,
Appointment: a,
}).ToJSON()

return a, nil
},
)

if err != nil {
switch err.(type) {
Expand All @@ -61,7 +68,7 @@ func (app *App) GetAppointment(ctx context.Context, key string) (*model.Appointm
}

if !shared {
cacheAppointments.AddWithDefaultExpires(key, res)
cacheAppointments.AddWithDefaultExpires(formatAppointmentCacheKey(widget.Profile.DomainId, int64(widget.Profile.QueueId), key), res)
}

return res.(*model.Appointment), nil
Expand All @@ -76,7 +83,6 @@ func (app *App) AppointmentWidget(ctx context.Context, widgetUri string) (*model
}

func (app *App) appointmentWidget(ctx context.Context, widgetUri string) (*model.AppointmentWidget, model.AppError) {

res, err, shared := appointmentGroupRequest.Do(fmt.Sprintf("list-%s", widgetUri), func() (interface{}, error) {
a, err := app.Store.Member().GetAppointmentWidget(ctx, widgetUri)
if err != nil {
Expand Down Expand Up @@ -146,12 +152,14 @@ func (app *App) CreateAppointment(ctx context.Context, widget *model.Appointment
}

func (app *App) CancelAppointment(ctx context.Context, widget *model.AppointmentWidget, key string) (*model.Appointment, model.AppError) {
appointment, err := app.GetAppointment(ctx, key)
domainID := widget.Profile.DomainId

appointment, err := app.GetAppointment(ctx, key, widget)
if err != nil {
return nil, err
}

if err = app.Store.Member().CancelAppointment(ctx, appointment.Id, "cancel"); err != nil {
if err = app.Store.Member().CancelAppointment(ctx, appointment.Id, domainID, model.AppointmentCancelReason); err != nil {
return nil, err
}

Expand Down
50 changes: 4 additions & 46 deletions app/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,61 +817,19 @@ func (app *App) GetLastCallFile(ctx context.Context, domainId int64, callId stri
}

func (app *App) CreateCallAnnotation(ctx context.Context, domainId int64, annotation *model.CallAnnotation) (*model.CallAnnotation, model.AppError) {
_, err := app.Store.Call().GetHistory(ctx, domainId, &model.SearchHistoryCall{
Ids: []string{annotation.CallId},
})
if err != nil {
return nil, err
}

return app.Store.Call().CreateAnnotation(ctx, annotation)
return app.Store.Call().CreateAnnotation(ctx, domainId, annotation)
}

func (app *App) UpdateCallAnnotation(ctx context.Context, domainId int64, annotation *model.CallAnnotation) (*model.CallAnnotation, model.AppError) {
_, err := app.Store.Call().GetHistory(ctx, domainId, &model.SearchHistoryCall{
Ids: []string{annotation.CallId},
})
if err != nil {
return nil, err
}
var oldAnnotation *model.CallAnnotation
oldAnnotation, err = app.Store.Call().GetAnnotation(ctx, annotation.Id)
if err != nil {
return nil, err
}

oldAnnotation.UpdatedAt = annotation.UpdatedAt
oldAnnotation.UpdatedBy = annotation.UpdatedBy
oldAnnotation.Note = annotation.Note
oldAnnotation.StartSec = annotation.StartSec
oldAnnotation.EndSec = annotation.EndSec

if err = oldAnnotation.IsValid(); err != nil {
if err := annotation.IsValid(); err != nil {
return nil, err
}

return app.Store.Call().UpdateAnnotation(ctx, domainId, oldAnnotation)
return app.Store.Call().UpdateAnnotation(ctx, domainId, annotation)
}

func (app *App) DeleteCallAnnotation(ctx context.Context, domainId, id int64, callId string) (*model.CallAnnotation, model.AppError) {
_, err := app.Store.Call().GetHistory(ctx, domainId, &model.SearchHistoryCall{
Ids: []string{callId},
})
if err != nil {
return nil, err
}
var annotation *model.CallAnnotation
annotation, err = app.Store.Call().GetAnnotation(ctx, id)
if err != nil {
return nil, err
}

err = app.Store.Call().DeleteAnnotation(ctx, id)
if err != nil {
return nil, err
}

return annotation, nil
return app.Store.Call().DeleteAnnotation(ctx, id, domainId, callId)
}

func (app *App) UpdateHistoryCall(ctx context.Context, domainId int64, id string, p *model.HistoryCallPatch) (*model.HistoryCall, model.AppError) {
Expand Down
8 changes: 4 additions & 4 deletions app/cc_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func (a *App) RemoveList(ctx context.Context, domainId, id int64) (*model.List,
return list, nil
}

func (a *App) CreateListCommunication(ctx context.Context, comm *model.ListCommunication) (*model.ListCommunication, model.AppError) {
return a.Store.List().CreateCommunication(ctx, comm)
func (a *App) CreateListCommunication(ctx context.Context, domainID int64, comm *model.ListCommunication) (*model.ListCommunication, model.AppError) {
return a.Store.List().CreateCommunication(ctx, domainID, comm)
}

func (a *App) BulkCreateListCommunication(ctx context.Context, listID int64, communications []*model.ListCommunication) ([]*model.ListCommunication, model.AppError) {
return a.Store.List().BulkCreateCommunication(ctx, listID, communications)
func (a *App) BulkCreateListCommunication(ctx context.Context, listID, domainID int64, communications []*model.ListCommunication) ([]*model.ListCommunication, model.AppError) {
return a.Store.List().BulkCreateCommunication(ctx, listID, domainID, communications)
}

func (a *App) GetListCommunicationPage(ctx context.Context, domainId, listId int64, search *model.SearchListCommunication) ([]*model.ListCommunication, bool, model.AppError) {
Expand Down
9 changes: 4 additions & 5 deletions app/cc_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import (
"github.com/webitel/wlog"
)

func (app *App) resolveCommunicationTypeChannels(ctx context.Context, domainID int64, communications []*model.MemberCommunication,
func (app *App) resolveCommunicationTypeChannels(
ctx context.Context,
domainID int64,
communications []*model.MemberCommunication,
fillDefaults bool,
) (map[int]string, model.AppError) {
ids := make([]uint32, 0, len(communications))
Expand Down Expand Up @@ -306,10 +309,6 @@ func (app *App) ResetMembersCount(ctx context.Context, domainID int64, query *mo
return app.Store.Member().ResetMembersCount(ctx, domainID, query)
}

func (app *App) GetMemberAttempts(ctx context.Context, memberId int64) ([]*model.MemberAttempt, model.AppError) {
return app.Store.Member().AttemptsList(ctx, memberId)
}

func (app *App) SearchAttemptsHistory(ctx context.Context, domainId int64, search *model.SearchAttempts) ([]*model.AttemptHistory, bool, model.AppError) {
list, err := app.Store.Member().SearchAttemptsHistory(ctx, domainId, search)
if err != nil {
Expand Down
25 changes: 19 additions & 6 deletions app/cc_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"fmt"

"github.com/webitel/engine/model"
"github.com/webitel/engine/pkg/wbt/auth_manager"
Expand Down Expand Up @@ -101,12 +102,20 @@ func (a *App) RemoveOutboundResource(ctx context.Context, domainId, id int64) (*
return resource, nil
}

func (a *App) CreateOutboundResourceDisplay(ctx context.Context, display *model.ResourceDisplay) (*model.ResourceDisplay, model.AppError) {
return a.Store.OutboundResource().SaveDisplay(ctx, display)
func (a *App) CreateOutboundResourceDisplay(ctx context.Context, domainID int64, display *model.ResourceDisplay) (*model.ResourceDisplay, model.AppError) {
return a.Store.OutboundResource().SaveDisplay(ctx, domainID, display)
}

func (a *App) CreateOutboundResourceDisplays(ctx context.Context, resourceId int64, displays []*model.ResourceDisplay) ([]*model.ResourceDisplay, model.AppError) {
return a.Store.OutboundResource().SaveDisplays(ctx, resourceId, displays)
func (a *App) CreateOutboundResourceDisplays(ctx context.Context, resourceId, domainID int64, displays []*model.ResourceDisplay) ([]*model.ResourceDisplay, model.AppError) {
if _, err := a.Store.OutboundResource().Get(ctx, domainID, resourceId); err != nil {
return nil, err
}

if len(displays) == 0 {
return nil, model.NewBadRequestError("app.cc_resource.create_outbound_displays", "zero resource displays")
}

return a.Store.OutboundResource().SaveDisplays(ctx, resourceId, domainID, displays)
}

func (a *App) GetOutboundResourceDisplayPage(ctx context.Context, domainId, resourceId int64, search *model.SearchResourceDisplay) ([]*model.ResourceDisplay, bool, model.AppError) {
Expand Down Expand Up @@ -152,6 +161,10 @@ func (a *App) RemoveOutboundResourceDisplay(ctx context.Context, domainId, resou
return display, nil
}

func (a *App) RemoveOutboundResourceDisplays(ctx context.Context, resourceId int64, ids []int64) model.AppError {
return a.Store.OutboundResource().DeleteDisplays(ctx, resourceId, ids)
func (a *App) RemoveOutboundResourceDisplays(ctx context.Context, resourceId, domainID int64, ids []int64) model.AppError {
if resourceId <= 0 {
return model.NewBadRequestError("app.cc_resource.displays.validation.resource_id", fmt.Sprintf("resource_id is not greater than zero: %d", resourceId))
}

return a.Store.OutboundResource().DeleteDisplays(ctx, resourceId, domainID, ids)
}
5 changes: 2 additions & 3 deletions app/cc_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,9 @@ func (a *App) RemoveTrigger(ctx context.Context, domainId int64, id int32) (*mod
}

func (a *App) GetTriggerJobList(ctx context.Context, domainId int64, triggerId int32, search *model.SearchTriggerJob) ([]*model.TriggerJob, bool, model.AppError) {
var list []*model.TriggerJob
_, err := a.Store.Trigger().Get(ctx, domainId, triggerId)
search.DomainId = domainId

list, err = a.Store.Trigger().GetAllJobs(ctx, triggerId, search)
list, err := a.Store.Trigger().GetAllJobs(ctx, triggerId, search)
if err != nil {
return nil, false, err
}
Expand Down
2 changes: 2 additions & 0 deletions controller/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (c *Controller) LoginAgentFromRequest(ctx context.Context, session *auth_ma
return err
}

r.DomainID = session.Domain(0)

permission := session.GetPermission(model.PERMISSION_SCOPE_CC_AGENT)
if !permission.CanRead() {
return c.app.MakePermissionError(session, permission, auth_manager.PERMISSION_ACCESS_READ)
Expand Down
Loading
Loading