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
2 changes: 1 addition & 1 deletion app/cc_audit_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (app *App) RateAuditForm(ctx context.Context, domainId int64, userId int64,
case rate.CallId != nil && rate.ConversationId != nil:
return nil, model.NewBadRequestError("app.audit.rate.valid.source", "only one of call_id or conversation_id may be set")
case rate.CallId != nil:
rateUserId, rateCreatedAt, err = app.Store.Call().GetOwnerUserCall(ctx, *rate.CallId)
rateUserId, rateCreatedAt, err = app.Store.AuditRate().ValidateCallRate(ctx, domainId, *rate.CallId)
case rate.ConversationId != nil:
rateUserId, rateCreatedAt, err = app.Store.AuditRate().ValidateChatRate(ctx, domainId, *rate.ConversationId)
default:
Expand Down
1,869 changes: 1,064 additions & 805 deletions gen/engine/call.pb.go

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions grpc_api/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,8 @@ func toEngineHistoryCall(src *model.HistoryCall, minHideString, pref, suff int,
if src.ScoreRequired != nil {
item.ScoreRequired = *src.ScoreRequired
}
item.Rates = toCallRates(src.Rates)
item.EvaluationAgents = toEvaluationAgents(src.EvaluationAgents)

if src.AttemptId != nil {
item.AttemptId = *src.AttemptId
Expand Down Expand Up @@ -1286,6 +1288,55 @@ func toCallFileTranscriptLookups(src []*model.CallFileTranscriptLookup) []*engin
return res
}

func toCallRates(src []*model.CallRate) []*engine.HistoryCallRate {
if src == nil {
return nil
}

res := make([]*engine.HistoryCallRate, 0, len(src))
for _, v := range src {
r := &engine.HistoryCallRate{
Id: v.Id,
CallId: v.CallId,
RatedUser: GetProtoLookup(v.RatedUser),
RatedBy: GetProtoLookup(v.RatedBy),
CreatedAt: v.CreatedAt,
}
if v.ScoreRequired != nil {
r.ScoreRequired = *v.ScoreRequired
}
if v.ScoreOptional != nil {
r.ScoreOptional = *v.ScoreOptional
}
res = append(res, r)
}

return res
}

func toEvaluationAgents(src []*model.EvaluationAgent) []*engine.HistoryCallEvaluationAgent {
if src == nil {
return nil
}

res := make([]*engine.HistoryCallEvaluationAgent, 0, len(src))
for _, v := range src {
r := &engine.HistoryCallEvaluationAgent{
CallId: v.CallId,
User: GetProtoLookup(v.User),
}
if v.BridgedAt != nil {
r.BridgedAt = *v.BridgedAt
}
if v.RateId != nil {
r.RateId = *v.RateId
}
res = append(res, r)
}

return res
}

func toCallFilesJob(src []*model.HistoryFileJob) []*engine.HistoryFileJob {
if src == nil {
return nil
Expand Down
31 changes: 25 additions & 6 deletions model/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,11 +588,13 @@ type HistoryCall struct {
AmdAiResult *string `json:"amd_ai_result" db:"amd_ai_result"`
AmdAiLogs StringArray `json:"amd_ai_logs" db:"amd_ai_logs"`

RateId *int64 `json:"rate_id" db:"rate_id"`
RatedUser *Lookup `json:"rated_user" db:"rated_user"`
RatedBy *Lookup `json:"rated_by" db:"rated_by"`
ScoreOptional *float32 `json:"score_optional" db:"score_optional"`
ScoreRequired *float32 `json:"score_required" db:"score_required"`
RateId *int64 `json:"rate_id" db:"rate_id"`
RatedUser *Lookup `json:"rated_user" db:"rated_user"`
RatedBy *Lookup `json:"rated_by" db:"rated_by"`
ScoreOptional *float32 `json:"score_optional" db:"score_optional"`
ScoreRequired *float32 `json:"score_required" db:"score_required"`
Rates []*CallRate `json:"rates" db:"rates"`
EvaluationAgents []*EvaluationAgent `json:"evaluation_agents" db:"evaluation_agents"`

AttemptId *int64 `json:"attempt_id" db:"attempt_id"`
AllowEvaluation *bool `json:"allow_evaluation" db:"allow_evaluation"`
Expand All @@ -619,6 +621,23 @@ type BlindTransfer struct {
User *Lookup `json:"user"`
}

type CallRate struct {
Id int64 `json:"id"`
CallId string `json:"call_id"`
RatedUser *Lookup `json:"rated_user"`
RatedBy *Lookup `json:"rated_by"`
ScoreRequired *float32 `json:"score_required"`
ScoreOptional *float32 `json:"score_optional"`
CreatedAt int64 `json:"created_at"`
}

type EvaluationAgent struct {
CallId string `json:"call_id"`
User *Lookup `json:"user"`
BridgedAt *int64 `json:"bridged_at"`
RateId *int64 `json:"rate_id"`
}

type HistoryCallPatch struct {
Variables *Variables `json:"variables" db:"variables"`
HideMissed *bool
Expand All @@ -638,7 +657,7 @@ func (c HistoryCall) AllowFields() []string {
"transcripts", "talk_sec", "grantee", "amd_ai_logs", "amd_ai_result", "rate_id", "rated_by", "rated_user", "score_optional", "score_required",
"attempt_id", "allow_evaluation", "form_fields", "bridged_id", "contact", "hide_missed", "redial_id", "schemas",
"hangup_phrase", "blind_transfers", "from_number", "to_number", "destination_name", "forms", "conversation_id", "meeting_id", "quality_metrics",
"user_agent", "hide_number",
"user_agent", "hide_number", "rates", "evaluation_agents",
}
}

Expand Down
59 changes: 16 additions & 43 deletions store/sqlstore/call_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"slices"
"strings"
"time"

sq "github.com/Masterminds/squirrel"
"github.com/lib/pq"
Expand Down Expand Up @@ -454,19 +453,18 @@ func (s SqlCallStore) GetHistory(ctx context.Context, domainId int64, search *mo
case when not :Rated::bool then not exists(
select 1
from call_center.cc_audit_rate ar
where ar.call_id = t.id::text)
else exists(
where ar.call_root_id = t.id or ar.call_id = t.id::text)
else exists(
select 1
from call_center.cc_audit_rate ar
where ar.call_id = t.id::text
where (ar.call_root_id = t.id or ar.call_id = t.id::text)
and (:RatedUserIds::int8[] isnull or ar.rated_user_id = any(:RatedUserIds::int8[]))
and (:RatedByIds::int8[] isnull or ar.created_by = any(:RatedByIds::int8[]))
and ( :ScoreOptionalFrom::numeric isnull or score_optional >= :ScoreOptionalFrom::numeric )
and ( :ScoreOptionalTo::numeric isnull or score_optional <= :ScoreOptionalTo::numeric )
and ( :ScoreRequiredFrom::numeric isnull or score_required >= :ScoreRequiredFrom::numeric )
and ( :ScoreRequiredTo::numeric isnull or score_required <= :ScoreRequiredTo::numeric )
)
end
and ( :ScoreOptionalFrom::numeric isnull or ar.score_optional >= :ScoreOptionalFrom::numeric )
and ( :ScoreOptionalTo::numeric isnull or ar.score_optional <= :ScoreOptionalTo::numeric )
and ( :ScoreRequiredFrom::numeric isnull or ar.score_required >= :ScoreRequiredFrom::numeric )
and ( :ScoreRequiredTo::numeric isnull or ar.score_required <= :ScoreRequiredTo::numeric ))
end
)
and (:ExcludedQueueTypes::int[] is null or queue_type is null or queue_type <> all(:ExcludedQueueTypes::int[]))
`
Expand Down Expand Up @@ -679,19 +677,18 @@ func (s SqlCallStore) GetHistoryByGroups(ctx context.Context, domainId, userSupe
case when not :Rated::bool then not exists(
select 1
from call_center.cc_audit_rate ar
where ar.call_id = t.id::text)
else exists(
where ar.call_root_id = t.id or ar.call_id = t.id::text)
else exists(
select 1
from call_center.cc_audit_rate ar
where ar.call_id = t.id::text
where (ar.call_root_id = t.id or ar.call_id = t.id::text)
and (:RatedUserIds::int8[] isnull or ar.rated_user_id = any(:RatedUserIds::int8[]))
and (:RatedByIds::int8[] isnull or ar.created_by = any(:RatedByIds::int8[]))
and ( :ScoreOptionalFrom::numeric isnull or score_optional >= :ScoreOptionalFrom::numeric )
and ( :ScoreOptionalTo::numeric isnull or score_optional <= :ScoreOptionalTo::numeric )
and ( :ScoreRequiredFrom::numeric isnull or score_required >= :ScoreRequiredFrom::numeric )
and ( :ScoreRequiredTo::numeric isnull or score_required <= :ScoreRequiredTo::numeric )
)
end
and ( :ScoreOptionalFrom::numeric isnull or ar.score_optional >= :ScoreOptionalFrom::numeric )
and ( :ScoreOptionalTo::numeric isnull or ar.score_optional <= :ScoreOptionalTo::numeric )
and ( :ScoreRequiredFrom::numeric isnull or ar.score_required >= :ScoreRequiredFrom::numeric )
and ( :ScoreRequiredTo::numeric isnull or ar.score_required <= :ScoreRequiredTo::numeric ))
end
)
and (
(t.user_id = any (call_center.cc_calls_rbac_users(:Domain::int8, :UserSupervisorId::int8) || :Groups::int[])
Expand Down Expand Up @@ -1368,30 +1365,6 @@ where c.domain_id = :DomainId and c.id = :Id::uuid and c.state in ('active', 'br
return res, nil
}

func (s SqlCallStore) GetOwnerUserCall(ctx context.Context, id string) (*int64, time.Time, model.AppError) {
var userId *int64
var createdAt time.Time

row := s.GetReplica().WithContext(ctx).QueryRow(`select coalesce(c.user_id, p.user_id) as rate_user,
case when c.user_id notnull or q.type = 2 then c.created_at else p.created_at end as created_at
from call_center.cc_calls_history c
left join call_center.cc_queue q on q.id = c.queue_id
left join call_center.cc_calls_history p on p.id = c.bridged_id
where c.id = $1::uuid`, id)

err := row.Err()
if err != nil {
return userId, createdAt, model.NewCustomCodeError("store.sql_call.get.owner.app_error", err.Error(), extractCodeFromErr(err))
}

err = row.Scan(&userId, &createdAt)
if err != nil {
return userId, createdAt, model.NewCustomCodeError("store.sql_call.get.owner.app_error", err.Error(), extractCodeFromErr(err))
}

return userId, createdAt, nil
}

func (s SqlCallStore) UpdateHistoryCall(ctx context.Context, domainId int64, id string, upd *model.HistoryCallPatch) model.AppError {
res, err := s.GetMaster().WithContext(ctx).Exec(`update call_center.cc_calls_history c
set payload = coalesce(payload, '{}') || :Vars::jsonb,
Expand Down
58 changes: 54 additions & 4 deletions store/sqlstore/cc_audit_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"errors"
"time"

"github.com/webitel/engine/pkg/wbt/auth_manager"
"net/http"

"github.com/webitel/engine/pkg/wbt/auth_manager"

"github.com/lib/pq"
"github.com/webitel/engine/model"
"github.com/webitel/engine/store"
Expand Down Expand Up @@ -53,9 +54,9 @@ func (s *SqlAuditRateStore) CheckAccess(ctx context.Context, domainId, rateId in
func (s *SqlAuditRateStore) Create(ctx context.Context, domainId int64, rate *model.AuditRate) (*model.AuditRate, model.AppError) {
err := s.GetMaster().WithContext(ctx).SelectOne(&rate, `with r as (
insert into call_center.cc_audit_rate (domain_id, form_id, created_at, created_by, updated_at, updated_by, answers, score_required, score_optional,
comment, call_id, conversation_id, call_created_at, rated_user_id, select_yes_count, critical_count)
comment, call_id, call_root_id, conversation_id, call_created_at, rated_user_id, select_yes_count, critical_count)
values (:DomainId, :FormId, :CreatedAt, :CreatedBy, :UpdatedAt, :UpdatedBy, :Answers, :ScoreRequired, :ScoreOptional,
:Comment, :CallId, :ConversationId, :CallCreatedAt, :RatedUserId, :SelectYesCount, :CriticalCount)
:Comment, :CallId, (select r.id from call_center.cc_call_root(:CallId::uuid, :CallCreatedAt::timestamptz) r), :ConversationId, :CallCreatedAt, :RatedUserId, :SelectYesCount, :CriticalCount)
returning *
)
select r.id,
Expand Down Expand Up @@ -98,12 +99,19 @@ from r
})

if err != nil {
return nil, model.NewInternalError("store.sql_audit_rate.save.app_error", err.Error())
if e, ok := err.(*pq.Error); ok && (e.Constraint == "cc_audit_rate_call_id_uindex" || e.Constraint == "cc_audit_rate_conversation_id_uindex") {
return nil, errAlreadyRated()
}
return nil, model.NewCustomCodeError("store.sql_audit_rate.save.app_error", err.Error(), extractCodeFromErr(err))
}

return rate, nil
}

func errAlreadyRated() model.AppError {
return model.NewBadRequestError("store.sql_audit_rate.save.already_rated", "already rated")
}

func (s *SqlAuditRateStore) GetAllPage(ctx context.Context, domainId int64, search *model.SearchAuditRate) ([]*model.AuditRate, model.AppError) {
var list []*model.AuditRate

Expand Down Expand Up @@ -319,3 +327,45 @@ group by c.created_at, c.closed_at`, conversationId, domainId)

return userId, createdAt, nil
}

func (s *SqlAuditRateStore) ValidateCallRate(ctx context.Context, domainId int64, callId string) (*int64, time.Time, model.AppError) {
var res struct {
UserId *int64 `db:"user_id"`
CreatedAt time.Time `db:"created_at"`
Allowed bool `db:"allowed"`
Rated bool `db:"rated"`
}

err := s.GetReplica().WithContext(ctx).SelectOne(&res, `select a.user_id,
case when c.user_id notnull or q.type = 2 then c.created_at else p.created_at end as created_at,
a.call_id notnull as allowed,
exists (select 1
from call_center.cc_audit_rate ar
where (ar.call_root_id = r.id or ar.call_id = c.id::text)
and (ar.call_id = c.id::text or ar.rated_user_id = a.user_id)) as rated
from call_center.cc_calls_history c
left join call_center.cc_queue q on q.id = c.queue_id
left join call_center.cc_calls_history p on p.id = c.bridged_id
cross join lateral call_center.cc_call_root(c.id, c.created_at) r
left join lateral (select e.call_id, e.user_id
from call_center.cc_call_evaluation_agents(r.id, r.created_at) e
where e.call_id = c.id) a on true
where c.id = :CallId::uuid and c.domain_id = :DomainId::int8`, map[string]any{
"CallId": callId,
"DomainId": domainId,
})

if err != nil {
return nil, res.CreatedAt, model.NewCustomCodeError("store.sql_audit_rate.validate_call.app_error", err.Error(), extractCodeFromErr(err))
}

if !res.Allowed {
return nil, res.CreatedAt, model.NewBadRequestError("store.sql_audit_rate.validate_call.not_allowed", "call leg is not evaluable")
}

if res.Rated {
return nil, res.CreatedAt, errAlreadyRated()
}

return res.UserId, res.CreatedAt, nil
}
2 changes: 2 additions & 0 deletions store/sqlstore/supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ func (me typeConverter) FromDb(target any) (gorp.CustomScanner, bool) {
*model.QuestionAnswers,
*[]*model.BlindTransfer,
*[]*model.CallForm,
*[]*model.CallRate,
*[]*model.EvaluationAgent,
**model.QualityMetrics:
binder := func(holder, target any) error {
s, ok := holder.(*[]byte)
Expand Down
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ type CallStore interface {
DeleteAnnotation(ctx context.Context, id int64) model.AppError
GetEavesdropInfo(ctx context.Context, domainId int64, id string) (*model.EavesdropInfo, model.AppError)

GetOwnerUserCall(ctx context.Context, id string) (*int64, time.Time, model.AppError)
UpdateHistoryCall(ctx context.Context, domainId int64, id string, upd *model.HistoryCallPatch) model.AppError
SetContactId(ctx context.Context, domainId int64, id string, contactId int64) (*model.CallDomain, model.AppError)

Expand Down Expand Up @@ -514,6 +513,7 @@ type AuditRateStore interface {
Update(ctx context.Context, domainId int64, rate *model.AuditRate) (*model.AuditRate, model.AppError)
Delete(ctx context.Context, domainId, id int64) model.AppError
ValidateChatRate(ctx context.Context, domainId int64, conversationId string) (*int64, time.Time, model.AppError)
ValidateCallRate(ctx context.Context, domainId int64, callId string) (*int64, time.Time, model.AppError)
}

type PresetQueryStore interface {
Expand Down
Loading