92 lines
2.7 KiB
Go
92 lines
2.7 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"git.techease.ru/Smart-search/smart-search-back/pkg/errors"
|
|
pb "git.techease.ru/Smart-search/smart-search-back/pkg/pb/request"
|
|
"github.com/google/uuid"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func (h *RequestHandler) CreateTZ(ctx context.Context, req *pb.CreateTZRequest) (*pb.CreateTZResponse, error) {
|
|
requestTxt := req.RequestTxt
|
|
if len(req.FileData) > 0 {
|
|
requestTxt += "\n[File: " + req.FileName + "]"
|
|
}
|
|
|
|
requestID, tzText, err := h.requestService.CreateTZ(ctx, int(req.UserId), requestTxt)
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.CreateTZ")
|
|
}
|
|
|
|
return &pb.CreateTZResponse{
|
|
RequestId: requestID.String(),
|
|
TzText: tzText,
|
|
}, nil
|
|
}
|
|
|
|
func (h *RequestHandler) ApproveTZ(ctx context.Context, req *pb.ApproveTZRequest) (*pb.ApproveTZResponse, error) {
|
|
requestID, err := uuid.Parse(req.RequestId)
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.ApproveTZ")
|
|
}
|
|
|
|
_, err = h.requestService.ApproveTZ(ctx, requestID, req.FinalTz, int(req.UserId))
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.ApproveTZ")
|
|
}
|
|
|
|
return &pb.ApproveTZResponse{
|
|
Success: true,
|
|
MailingStatus: "sent",
|
|
}, nil
|
|
}
|
|
|
|
func (h *RequestHandler) GetMailingList(ctx context.Context, req *pb.GetMailingListRequest) (*pb.GetMailingListResponse, error) {
|
|
requests, err := h.requestService.GetMailingList(ctx, int(req.UserId))
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.GetMailingList")
|
|
}
|
|
|
|
items := make([]*pb.MailingItem, 0, len(requests))
|
|
for _, r := range requests {
|
|
items = append(items, &pb.MailingItem{
|
|
RequestId: r.ID.String(),
|
|
RequestTxt: r.RequestTxt,
|
|
FinalTz: r.FinalTZ,
|
|
MailingStatus: r.MailingStatus,
|
|
CreatedAt: timestamppb.New(r.CreatedAt),
|
|
SuppliersFound: 0,
|
|
})
|
|
}
|
|
|
|
return &pb.GetMailingListResponse{
|
|
Items: items,
|
|
}, nil
|
|
}
|
|
|
|
func (h *RequestHandler) GetMailingListByID(ctx context.Context, req *pb.GetMailingListByIDRequest) (*pb.GetMailingListByIDResponse, error) {
|
|
requestID, err := uuid.Parse(req.RequestId)
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.GetMailingListByID")
|
|
}
|
|
|
|
detail, err := h.requestService.GetMailingListByID(ctx, requestID)
|
|
if err != nil {
|
|
return nil, errors.ToGRPCError(err, h.logger, "RequestService.GetMailingListByID")
|
|
}
|
|
|
|
return &pb.GetMailingListByIDResponse{
|
|
Item: &pb.MailingItem{
|
|
RequestId: detail.RequestID.String(),
|
|
RequestTxt: detail.Title,
|
|
FinalTz: detail.MailText,
|
|
MailingStatus: "sent",
|
|
CreatedAt: timestamppb.New(time.Now()),
|
|
SuppliersFound: int32(len(detail.Suppliers)),
|
|
},
|
|
}, nil
|
|
}
|