Added approvals extra datta #76
|
|
@ -7,6 +7,7 @@ import (
|
|||
adminhandler "bitrix_balancer_api/rest_admin_service/handlers/admin"
|
||||
approvalstepshandler "bitrix_balancer_api/rest_admin_service/handlers/approval_steps"
|
||||
approvaltaskshandler "bitrix_balancer_api/rest_admin_service/handlers/approval_tasks"
|
||||
approvalextrashandler "bitrix_balancer_api/rest_admin_service/handlers/approvalextras"
|
||||
approvalshandler "bitrix_balancer_api/rest_admin_service/handlers/approvals"
|
||||
authhandler "bitrix_balancer_api/rest_admin_service/handlers/auth"
|
||||
dashboardhandler "bitrix_balancer_api/rest_admin_service/handlers/dashboard"
|
||||
|
|
@ -223,6 +224,7 @@ func registerRoutes(router chi.Router, ipc *typesm.InternalProviderControl) erro
|
|||
{"dashboard", dashboardhandler.RegisterDashboardRoutes},
|
||||
{"notifications", notificationshandler.RegisterNotificationRoutes},
|
||||
{"flownodes", nodeshandler.RegisterFlowNodesRoutes},
|
||||
{"approvalextras", approvalextrashandler.RegisterApprovalExtrasRoutes},
|
||||
}
|
||||
|
||||
// Регистрация всех маршрутов
|
||||
|
|
|
|||
|
|
@ -0,0 +1,303 @@
|
|||
package approvalextrashandler
|
||||
|
||||
import (
|
||||
"bitrix_balancer_api/core/typescore"
|
||||
"bitrix_balancer_api/rest_admin_service/handlers"
|
||||
typesm "bitrix_balancer_api/rest_admin_service/types"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type ApprovalExtrasReg struct {
|
||||
ipc *typesm.InternalProviderControl
|
||||
}
|
||||
|
||||
// RegisterApprovalExtrasRoutes регистрирует данные о заявке, которые НЕ
|
||||
// хранятся прямо на самой Approval (или пока не отдаются в основном ответе
|
||||
// GET /api/approvals/{id}) — доп. согласующие, комментарии, наблюдатели
|
||||
// (вычисляются динамически по категории/теме, не хранимая связь) и активный
|
||||
// запрос на отзыв.
|
||||
func RegisterApprovalExtrasRoutes(
|
||||
r chi.Router,
|
||||
ipc *typesm.InternalProviderControl,
|
||||
) error {
|
||||
s := &ApprovalExtrasReg{ipc: ipc}
|
||||
|
||||
r.Route("/api/approvals/{id}", func(r chi.Router) {
|
||||
r.Use(handlers.AccessCodeVerifier(ipc.Config))
|
||||
r.Use(handlers.JWTVerifier(ipc.Config, ipc.Database.Admin, ipc.RedisClient))
|
||||
|
||||
handlers.RegisterRoute(r, http.MethodGet, "/additional-approvers", s.GetAdditionalApproversHandler)
|
||||
handlers.RegisterRoute(r, http.MethodGet, "/comments", s.GetCommentsHandler)
|
||||
handlers.RegisterRoute(r, http.MethodGet, "/observers", s.GetObserversHandler)
|
||||
handlers.RegisterRoute(r, http.MethodGet, "/revocation-request", s.GetRevocationRequestHandler)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseApprovalID(r *http.Request) (uint64, error) {
|
||||
idStr := chi.URLParam(r, "id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
return 0, errors.New("invalid approval id")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func userName(u *typescore.User) string {
|
||||
if u != nil && u.FullName != nil {
|
||||
return *u.FullName
|
||||
}
|
||||
return "—"
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GET /api/approvals/{id}/additional-approvers
|
||||
// ============================================================
|
||||
|
||||
type additionalApproverDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
ApproverID uint64 `json:"approver_id"`
|
||||
ApproverName string `json:"approver_name"`
|
||||
Status string `json:"status"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
ActedAt *time.Time `json:"acted_at,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ApprovalExtrasReg) GetAdditionalApproversHandler(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
approvalID, err := parseApprovalID(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
records, _, err := s.ipc.Database.ApprovalAdditionalApprover.GetApprovalAdditionalApproversListDB(ctx, typescore.ListDbOptions{
|
||||
Filtering: &typescore.ApprovalAdditionalApprover{ApprovalID: &approvalID},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]additionalApproverDTO, 0, len(records))
|
||||
for _, rec := range records {
|
||||
if rec.ApproverID == nil {
|
||||
continue
|
||||
}
|
||||
approver, _ := s.ipc.Database.User.GetUserByIDDB(ctx, *rec.ApproverID)
|
||||
|
||||
dto := additionalApproverDTO{
|
||||
ApproverID: *rec.ApproverID,
|
||||
ApproverName: userName(approver),
|
||||
Status: "pending",
|
||||
ActedAt: rec.ActedAt,
|
||||
}
|
||||
if rec.ID != nil {
|
||||
dto.ID = *rec.ID
|
||||
}
|
||||
if rec.Status != nil {
|
||||
dto.Status = *rec.Status
|
||||
}
|
||||
if rec.Comment != nil {
|
||||
dto.Comment = *rec.Comment
|
||||
}
|
||||
result = append(result, dto)
|
||||
}
|
||||
|
||||
return &typescore.ApiResponseBase{
|
||||
TotalCount: uint64(len(result)),
|
||||
Data: result,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GET /api/approvals/{id}/comments
|
||||
// ============================================================
|
||||
|
||||
type commentDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
UserID uint64 `json:"user_id"`
|
||||
UserName string `json:"user_name"`
|
||||
Comment string `json:"comment"`
|
||||
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ApprovalExtrasReg) GetCommentsHandler(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
approvalID, err := parseApprovalID(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
records, _, err := s.ipc.Database.ApprovalComment.GetApprovalCommentsListDB(ctx, typescore.ListDbOptions{
|
||||
Filtering: &typescore.ApprovalComment{ApprovalID: &approvalID},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]commentDTO, 0, len(records))
|
||||
for _, rec := range records {
|
||||
dto := commentDTO{CreatedAt: rec.CreatedAt}
|
||||
if rec.ID != nil {
|
||||
dto.ID = *rec.ID
|
||||
}
|
||||
if rec.UserID != nil {
|
||||
dto.UserID = *rec.UserID
|
||||
user, _ := s.ipc.Database.User.GetUserByIDDB(ctx, *rec.UserID)
|
||||
dto.UserName = userName(user)
|
||||
}
|
||||
if rec.Comment != nil {
|
||||
dto.Comment = *rec.Comment
|
||||
}
|
||||
result = append(result, dto)
|
||||
}
|
||||
|
||||
return &typescore.ApiResponseBase{
|
||||
TotalCount: uint64(len(result)),
|
||||
Data: result,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GET /api/approvals/{id}/observers
|
||||
//
|
||||
// Наблюдатели НЕ хранятся как связь на самой заявке — они вычисляются по
|
||||
// категории/теме через права (см. GetObserversForApprovalDB в
|
||||
// telegram_service). Здесь та же логика: сначала получаем код категории
|
||||
// темы этой заявки, затем спрашиваем БД.
|
||||
// ============================================================
|
||||
|
||||
type observerDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
FullName string `json:"full_name"`
|
||||
}
|
||||
|
||||
func (s *ApprovalExtrasReg) GetObserversHandler(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
approvalID, err := parseApprovalID(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
approval, err := s.ipc.Database.Approval.GetApprovalByIDDB(ctx, approvalID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if approval == nil {
|
||||
return nil, errors.New("approval not found")
|
||||
}
|
||||
|
||||
categoryCode := ""
|
||||
if approval.TopicID != nil {
|
||||
topic, err := s.ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *approval.TopicID)
|
||||
if err == nil && topic != nil && topic.CategoryID != nil {
|
||||
category, err := s.ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *topic.CategoryID)
|
||||
if err == nil && category != nil && category.Code != nil {
|
||||
categoryCode = *category.Code
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
observers, err := s.ipc.Database.User.GetObserversForApprovalDB(ctx, categoryCode, approval.TopicID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]observerDTO, 0, len(observers))
|
||||
for _, obs := range observers {
|
||||
if obs.ID == nil {
|
||||
continue
|
||||
}
|
||||
result = append(result, observerDTO{
|
||||
ID: *obs.ID,
|
||||
FullName: userName(obs),
|
||||
})
|
||||
}
|
||||
|
||||
return &typescore.ApiResponseBase{
|
||||
TotalCount: uint64(len(result)),
|
||||
Data: result,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// GET /api/approvals/{id}/revocation-request
|
||||
//
|
||||
// Возвращает {"data": null}, если активного (pending) запроса на отзыв нет —
|
||||
// это нормальный случай для большинства заявок, не ошибка.
|
||||
// ============================================================
|
||||
|
||||
type revocationApproverDTO struct {
|
||||
ApproverID uint64 `json:"approver_id"`
|
||||
ApproverName string `json:"approver_name"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type revocationRequestDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
Status string `json:"status"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
RequestedByID uint64 `json:"requested_by_id"`
|
||||
RequestedByName string `json:"requested_by_name"`
|
||||
Approvers []revocationApproverDTO `json:"approvers"`
|
||||
}
|
||||
|
||||
func (s *ApprovalExtrasReg) GetRevocationRequestHandler(w http.ResponseWriter, r *http.Request) (interface{}, error) {
|
||||
approvalID, err := parseApprovalID(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
request, err := s.ipc.Database.ApprovalRevocation.GetPendingRevocationRequestForApprovalDB(ctx, approvalID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if request == nil {
|
||||
return &typescore.ApiResponseBase{Data: nil}, nil
|
||||
}
|
||||
|
||||
dto := revocationRequestDTO{Status: "pending"}
|
||||
if request.ID != nil {
|
||||
dto.ID = *request.ID
|
||||
}
|
||||
if request.Status != nil {
|
||||
dto.Status = *request.Status
|
||||
}
|
||||
if request.Reason != nil {
|
||||
dto.Reason = *request.Reason
|
||||
}
|
||||
if request.RequestedByID != nil {
|
||||
dto.RequestedByID = *request.RequestedByID
|
||||
requestedBy, _ := s.ipc.Database.User.GetUserByIDDB(ctx, *request.RequestedByID)
|
||||
dto.RequestedByName = userName(requestedBy)
|
||||
}
|
||||
|
||||
if request.ID != nil {
|
||||
approvers, err := s.ipc.Database.ApprovalRevocation.GetRevocationApproversByRequestIDDB(ctx, *request.ID)
|
||||
if err == nil {
|
||||
for _, a := range approvers {
|
||||
if a.ApproverID == nil {
|
||||
continue
|
||||
}
|
||||
approverUser, _ := s.ipc.Database.User.GetUserByIDDB(ctx, *a.ApproverID)
|
||||
status := "pending"
|
||||
if a.Status != nil {
|
||||
status = *a.Status
|
||||
}
|
||||
dto.Approvers = append(dto.Approvers, revocationApproverDTO{
|
||||
ApproverID: *a.ApproverID,
|
||||
ApproverName: userName(approverUser),
|
||||
Status: status,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &typescore.ApiResponseBase{Data: dto}, nil
|
||||
}
|
||||
Loading…
Reference in New Issue