2021-04-11 09:48:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-05-01 17:50:03 +00:00
|
|
|
rAL "bitbucket.org/skeris/profile/dal"
|
2021-05-01 12:36:22 +00:00
|
|
|
"context"
|
2021-04-11 09:48:15 +00:00
|
|
|
"encoding/json"
|
2021-05-01 12:36:22 +00:00
|
|
|
"errors"
|
2021-05-15 20:10:07 +00:00
|
|
|
"fmt"
|
2021-09-05 15:24:13 +00:00
|
|
|
"github.com/rs/xid"
|
2021-04-11 09:48:15 +00:00
|
|
|
"github.com/themakers/hlog"
|
2021-09-05 15:24:13 +00:00
|
|
|
"heruvym/dal/minio"
|
|
|
|
"heruvym/dal/mongo"
|
|
|
|
"heruvym/jwt_adapter"
|
|
|
|
"heruvym/model"
|
|
|
|
"heruvym/tools"
|
2021-04-11 09:48:15 +00:00
|
|
|
"net/http"
|
2021-09-05 15:24:13 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-04-11 09:48:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Heruvym struct {
|
|
|
|
logger hlog.Logger
|
2021-09-05 15:24:13 +00:00
|
|
|
dal *mongo.DAL
|
2021-05-01 17:50:03 +00:00
|
|
|
ral rAL.LayerMongoDb
|
2021-09-05 15:24:13 +00:00
|
|
|
bs *minio.BlobStore
|
2021-04-11 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
func New(blobs *minio.BlobStore, dataAccessLayer *mongo.DAL, ral rAL.LayerMongoDb, log hlog.Logger) *Heruvym {
|
2021-04-11 09:48:15 +00:00
|
|
|
return &Heruvym{
|
|
|
|
logger: log.Module("Service"),
|
|
|
|
dal: dataAccessLayer,
|
2021-05-01 17:50:03 +00:00
|
|
|
ral: ral,
|
2021-09-05 15:24:13 +00:00
|
|
|
bs: blobs,
|
2021-04-11 09:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 17:50:03 +00:00
|
|
|
func (h Heruvym) Register(m *http.ServeMux) *http.ServeMux {
|
2021-04-11 09:48:15 +00:00
|
|
|
|
|
|
|
m.HandleFunc("/create", h.CreateTicket)
|
2021-05-02 22:25:12 +00:00
|
|
|
m.HandleFunc("/subscribe", tools.SseWrapper(h.GetList))
|
|
|
|
m.HandleFunc("/ticket", tools.SseWrapper(h.Subscribe))
|
|
|
|
m.HandleFunc("/send", tools.HandlerWrapper(h.PutMessage))
|
2021-05-11 10:57:58 +00:00
|
|
|
m.HandleFunc("/getTickets", tools.HandlerWrapper(h.GetTickets))
|
|
|
|
m.HandleFunc("/getMessages", tools.HandlerWrapper(h.GetMessages))
|
|
|
|
m.HandleFunc("/pick", tools.HandlerWrapper(h.Pick))
|
|
|
|
m.HandleFunc("/delegate", tools.HandlerWrapper(h.Delegate))
|
|
|
|
m.HandleFunc("/vote", tools.HandlerWrapper(h.Vote))
|
|
|
|
m.HandleFunc("/close", tools.HandlerWrapper(h.CloseTicket))
|
2021-04-11 09:48:15 +00:00
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateTicketReq struct {
|
|
|
|
Title string `json:"Title"`
|
|
|
|
Message string `json:"Message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateTicketResp struct {
|
|
|
|
Ticket string `json:"Ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) CreateTicket(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if err := r.Body.Close(); err != nil {
|
|
|
|
h.logger.Emit(ErrorClose{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
request CreateTicketReq
|
|
|
|
)
|
2021-04-11 09:48:15 +00:00
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
|
|
|
|
http.Error(w, "Invalid json", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Title == "" {
|
|
|
|
http.Error(w, "No Title", http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Message == "" {
|
|
|
|
http.Error(w, "No Message", http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
session := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
if session == nil {
|
|
|
|
http.Error(w, "No session", http.StatusMethodNotAllowed)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
var (
|
|
|
|
ticketID string
|
|
|
|
tickets []model.Ticket
|
2021-04-11 09:48:15 +00:00
|
|
|
)
|
2021-09-05 15:24:13 +00:00
|
|
|
if session.User == "" {
|
|
|
|
tickets, err = h.dal.GetTickets4Sess(ctx, session.Session)
|
2021-04-11 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
if err != nil || len(tickets) == 0 {
|
|
|
|
ticketID, err = h.dal.CreateTicket(
|
|
|
|
ctx,
|
|
|
|
session.User,
|
|
|
|
session.Session,
|
|
|
|
request.Title,
|
|
|
|
request.Message,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "CannotCreateTicket", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := h.dal.PutMessage(ctx,
|
|
|
|
request.Message,
|
|
|
|
session.User,
|
|
|
|
session.Session,
|
|
|
|
ticketID,
|
|
|
|
[]string{},
|
|
|
|
); err != nil {
|
|
|
|
http.Error(w, "CannotCreateMessage", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ticketID = tickets[0].ID
|
2021-04-11 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
|
|
|
|
|
2021-04-11 09:48:15 +00:00
|
|
|
response, err := json.Marshal(CreateTicketResp{Ticket: ticketID})
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Emit(ErrorMarshal{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
http.Error(w, "CannotMarshalMessage", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(response); err != nil {
|
|
|
|
h.logger.Emit(ErrorMarshal{
|
|
|
|
Err: err,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-05-01 10:05:45 +00:00
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
var _ tools.DataEmitter = (&Heruvym{}).GetList
|
2021-05-01 10:05:45 +00:00
|
|
|
|
2021-05-01 12:36:22 +00:00
|
|
|
func (h *Heruvym) GetList(ctx context.Context) chan interface{} {
|
2021-09-05 15:24:13 +00:00
|
|
|
defer func() {
|
|
|
|
if rec := recover(); rec != nil {
|
|
|
|
fmt.Println(rec)
|
|
|
|
}
|
|
|
|
}()
|
2021-05-01 12:36:22 +00:00
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
if sess == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:36:22 +00:00
|
|
|
output := make(chan interface{})
|
|
|
|
|
|
|
|
if sess.User == "" {
|
|
|
|
go h.unauthorizedTickets(ctx, sess.Session, output)
|
|
|
|
} else {
|
2021-05-02 22:25:12 +00:00
|
|
|
role, err := h.ral.GetProfileRole(ctx, sess.User)
|
2021-09-05 15:24:13 +00:00
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
if err != nil {
|
2022-02-27 15:27:44 +00:00
|
|
|
fmt.Println("HER ERR", err)
|
2021-05-02 22:25:12 +00:00
|
|
|
go h.hasNoRole(output)
|
|
|
|
}
|
2022-02-27 15:27:44 +00:00
|
|
|
fmt.Println("ALL TICKETS Sess ", sess.User, role)
|
2021-05-02 22:25:12 +00:00
|
|
|
if role == "admin" || role == "manager" {
|
|
|
|
go h.allTickets(ctx, output)
|
|
|
|
} else {
|
|
|
|
go h.userTickets(ctx, sess.User, output)
|
|
|
|
}
|
2021-05-01 12:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
2021-05-01 10:05:45 +00:00
|
|
|
}
|
2021-05-01 12:36:22 +00:00
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
func (h *Heruvym) allTickets(ctx context.Context, output chan interface{}) {
|
2021-09-05 15:24:13 +00:00
|
|
|
defer func() {
|
|
|
|
if v := recover(); v != nil {
|
|
|
|
fmt.Println("AllTicketsRec", v)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := h.dal.YieldActiveTickets(ctx, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot get tickets:" + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
if err := h.dal.WatchActiveTickets(ctx, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-09-05 15:24:13 +00:00
|
|
|
output <- errors.New("cannot watch all tickets" + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) userTickets(ctx context.Context, userID string, output chan interface{}) {
|
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
defer func() {
|
|
|
|
if v := recover(); v != nil {
|
|
|
|
fmt.Println("USERTICKS", v)
|
|
|
|
}
|
|
|
|
} ()
|
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
if err := h.dal.YieldUserTickets(ctx, userID, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2022-02-27 15:27:44 +00:00
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
output <- errors.New("cannot get tickets:" + err.Error())
|
2021-05-15 20:10:07 +00:00
|
|
|
return
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchTickets(ctx, userID, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot watch tickets")
|
2021-05-15 20:10:07 +00:00
|
|
|
return
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) hasNoRole(output chan interface{}) {
|
|
|
|
output <- errors.New("no role in profile")
|
|
|
|
}
|
|
|
|
|
2021-05-01 12:36:22 +00:00
|
|
|
func (h *Heruvym) unauthorizedTickets(ctx context.Context, sess string, output chan interface{}) {
|
2021-09-05 15:24:13 +00:00
|
|
|
//defer close(output)
|
2021-05-01 12:36:22 +00:00
|
|
|
|
|
|
|
tickets, err := h.dal.GetTickets4Sess(ctx, sess)
|
2021-05-01 17:50:03 +00:00
|
|
|
if err != nil {
|
2021-05-01 12:36:22 +00:00
|
|
|
output <- errors.New("no tickets for session")
|
2021-09-05 15:24:13 +00:00
|
|
|
return
|
2021-05-01 12:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
if tickets != nil {
|
|
|
|
for _, ticket := range tickets {
|
|
|
|
|
|
|
|
output <- ticket
|
|
|
|
}
|
2021-05-01 12:36:22 +00:00
|
|
|
}
|
2021-05-01 17:50:03 +00:00
|
|
|
}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
type ReqPutMessage struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
Files []string `json:"files"`
|
|
|
|
Lang string `json:"lang"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) PutMessage(
|
|
|
|
ctx context.Context,
|
|
|
|
request ReqPutMessage,
|
|
|
|
) (interface{}, int) {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
request.Files = []string{}
|
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
message, err := h.dal.PutMessage(
|
|
|
|
ctx,
|
|
|
|
request.Message,
|
|
|
|
sess.User,
|
|
|
|
sess.Session,
|
|
|
|
request.TicketID,
|
2022-02-27 15:27:44 +00:00
|
|
|
[]string{},
|
2021-05-02 22:25:12 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("can not put message"), http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.UpdateTopMessage(ctx, request.TicketID, message); err != nil {
|
|
|
|
return errors.New("can not update ticket"), http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, http.StatusOK
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
type ReqScreenshot struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
Lang string `json:"lang"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) RequestScreenshot(
|
|
|
|
ctx context.Context,
|
|
|
|
request ReqScreenshot,
|
|
|
|
) (interface{}, int) {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
_, err := h.dal.PutSCRequest(
|
|
|
|
ctx,
|
|
|
|
sess.User,
|
|
|
|
sess.Session,
|
|
|
|
request.TicketID,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("can not put message"), http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, http.StatusOK
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
var _ tools.DataEmitter = (&Heruvym{}).Subscribe
|
|
|
|
|
|
|
|
func (h *Heruvym) Subscribe(ctx context.Context) chan interface{} {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
2022-02-27 15:27:44 +00:00
|
|
|
fmt.Println("SESS Subsc", sess)
|
2021-05-02 22:25:12 +00:00
|
|
|
|
2021-05-15 20:10:07 +00:00
|
|
|
ticketID := ctx.Value(tools.ContextURLKey).(string)
|
2021-09-05 15:24:13 +00:00
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
output := make(chan interface{})
|
|
|
|
if sess.User == "" {
|
|
|
|
go func() {
|
|
|
|
ticket, err := h.dal.GetTicket4Sess(ctx, ticketID, sess.Session)
|
|
|
|
if err != nil || ticket == nil {
|
|
|
|
output <- errors.New("no tickets 4 session")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.YieldMessages(ctx, ticketID, func(message model.Message) error {
|
|
|
|
output <- message
|
2021-09-05 15:24:13 +00:00
|
|
|
fmt.Println("OOOOOOLd")
|
2021-05-02 22:25:12 +00:00
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
//
|
|
|
|
// output <- errors.New("cannot show message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
output <- errors.New("cannot read messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
|
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
// fmt.Println("3", err)
|
|
|
|
// output <- errors.New("cannot show watch message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
fmt.Println("4", err)
|
|
|
|
output <- errors.New("cannot watch messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
role, err := h.ral.GetProfileRole(ctx, sess.User)
|
|
|
|
if err != nil {
|
|
|
|
go h.hasNoRole(output)
|
|
|
|
}
|
|
|
|
|
|
|
|
if role == "admin" || role == "manager" {
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
if err := h.dal.YieldMessages(ctx, ticketID, func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.User); err != nil {
|
|
|
|
// fmt.Println("2", err)
|
|
|
|
// output <- errors.New("cannot show message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
fmt.Println("1", err)
|
|
|
|
output <- errors.New("cannot read messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
// fmt.Println("3", err)
|
|
|
|
// output <- errors.New("cannot show watch message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
fmt.Println("4", err)
|
|
|
|
output <- errors.New("cannot watch messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
ticket, err := h.dal.GetTicket4User(ctx, ticketID, sess.User)
|
|
|
|
if err != nil || ticket == nil {
|
|
|
|
output <- errors.New("no tickets 4 user")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.YieldMessages(ctx, ticketID, func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.User); err != nil {
|
|
|
|
// fmt.Println("2", err)
|
|
|
|
// output <- errors.New("cannot show message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
fmt.Println("1", err)
|
|
|
|
output <- errors.New("cannot read messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
//if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
// fmt.Println("3", err)
|
|
|
|
// output <- errors.New("cannot show watch message " + err.Error())
|
|
|
|
// return err
|
|
|
|
//}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2021-05-15 20:10:07 +00:00
|
|
|
fmt.Println("4", err)
|
|
|
|
output <- errors.New("cannot watch messages " + err.Error())
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) handleOwnMessages(output chan interface{}) {
|
|
|
|
defer close(output)
|
2021-05-11 10:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetTicketsReq struct {
|
|
|
|
Amount int64 `json:"amt"`
|
|
|
|
Page int64 `json:"page"`
|
|
|
|
Search string `json:"srch"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
2022-02-27 15:27:44 +00:00
|
|
|
type GetTicketsResp struct {
|
|
|
|
Data []model.Ticket `json:"data"`
|
|
|
|
Count int64 `json:"count"`
|
|
|
|
}
|
2021-05-11 10:57:58 +00:00
|
|
|
|
|
|
|
func (h *Heruvym) GetTickets(
|
|
|
|
ctx context.Context,
|
2022-02-27 15:27:44 +00:00
|
|
|
request GetTicketsReq) (GetTicketsResp, int) {
|
|
|
|
result, count, err := h.dal.GetTicketPage(ctx,
|
2021-05-11 10:57:58 +00:00
|
|
|
request.Status,
|
|
|
|
request.Search,
|
|
|
|
request.Amount,
|
|
|
|
request.Page,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-02-27 15:27:44 +00:00
|
|
|
return GetTicketsResp{}, http.StatusNoContent
|
2021-05-11 10:57:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
return GetTicketsResp{
|
|
|
|
Data: *result,
|
|
|
|
Count: count,
|
|
|
|
}, http.StatusOK
|
2021-05-11 10:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetMessagesReq struct {
|
|
|
|
Amount int64 `json:"amt"`
|
|
|
|
Page int64 `json:"page"`
|
|
|
|
Search string `json:"srch"`
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) GetMessages(
|
|
|
|
ctx context.Context,
|
|
|
|
request GetMessagesReq) ([]model.Message, int) {
|
|
|
|
result, err := h.dal.GetMessagesPage(ctx,
|
|
|
|
request.Search,
|
|
|
|
request.TicketID,
|
|
|
|
request.Amount,
|
|
|
|
request.Page,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, http.StatusNoContent
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
type CloseTicketReq struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
}
|
2022-02-27 15:27:44 +00:00
|
|
|
type CloseTicketResp struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
}
|
2021-05-11 10:57:58 +00:00
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
func (h *Heruvym) CloseTicket(ctx context.Context, req CloseTicketReq) (*CloseTicketResp, int) {
|
2021-05-11 10:57:58 +00:00
|
|
|
if err := h.dal.SetTicketStatus(ctx, req.TicketID, model.StateClose); err != nil {
|
2022-02-27 15:27:44 +00:00
|
|
|
return nil, http.StatusBadRequest
|
2021-05-11 10:57:58 +00:00
|
|
|
}
|
2021-05-02 22:25:12 +00:00
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
return &CloseTicketResp{
|
|
|
|
TicketID: req.TicketID,
|
|
|
|
}, http.StatusOK
|
2021-05-11 10:57:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type VoteReq struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
Rate int `json:"rate"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) Vote(ctx context.Context, req VoteReq) (error, int) {
|
|
|
|
if err := h.dal.SetRate(ctx, req.TicketID, req.Rate); err != nil {
|
|
|
|
return err, http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
type PickReq struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) Pick(ctx context.Context, req PickReq) (error, int) {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
if err := h.dal.SetAnswerer(ctx, req.TicketID, sess.User); err != nil {
|
|
|
|
return err, http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
type DelegateReq struct {
|
|
|
|
TicketID string `json:"ticket"`
|
|
|
|
AnswererID string `json:"answerer"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) Delegate(ctx context.Context, req DelegateReq) (error, int) {
|
|
|
|
if err := h.dal.SetAnswerer(ctx, req.TicketID, req.AnswererID); err != nil {
|
|
|
|
return err, http.StatusBadRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, http.StatusOK
|
2021-05-02 22:25:12 +00:00
|
|
|
}
|
2021-09-05 15:24:13 +00:00
|
|
|
// MB Size constants
|
|
|
|
const (
|
|
|
|
MB = 1 << 20
|
|
|
|
)
|
|
|
|
|
|
|
|
type PutFileReq struct {
|
|
|
|
Ticket string `json:"ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PutFileResp struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) PutFile(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err := r.ParseMultipartForm(10 * MB); err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not parse multipart "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.MultipartForm == nil {
|
|
|
|
if _, err := w.Write([]byte("no multipart")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.MultipartForm.File == nil {
|
|
|
|
if _, err := w.Write([]byte("no file")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filesCount:= len(r.MultipartForm.File)
|
|
|
|
|
|
|
|
if filesCount == 0 {
|
|
|
|
if _, err := w.Write([]byte("no files")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := jwt_adapter.Get(r.Context())
|
|
|
|
if sess == nil {
|
|
|
|
if _, err := w.Write([]byte("not authorized")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req PutFileReq
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not decode "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fileIDs, filenames []string
|
|
|
|
errFile error
|
|
|
|
)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
m:=new(sync.Mutex)
|
|
|
|
|
|
|
|
wg.Add(filesCount)
|
|
|
|
for name, file := range r.MultipartForm.File {
|
|
|
|
file := file
|
|
|
|
name := name
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
freader, err := file[0].Open()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("can not open ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := freader.Close(); err != nil {
|
|
|
|
errFile = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
splitted := strings.Split(name, ".")
|
|
|
|
filename := fmt.Sprintf("%s.%s", xid.New().String(), splitted[len(splitted) - 1])
|
|
|
|
|
|
|
|
if err := h.bs.PutFile(
|
|
|
|
r.Context(),
|
|
|
|
filename,
|
|
|
|
freader,
|
|
|
|
file[0].Size); err != nil {
|
|
|
|
errFile = err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
fileIDs = append(fileIDs, filename)
|
|
|
|
filenames = append(filenames, name)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if errFile != nil {
|
|
|
|
if _, err := w.Write([]byte("can not store files "+errFile.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := h.dal.PutMessage(
|
|
|
|
r.Context(),
|
|
|
|
strings.Join(filenames, ", "),
|
|
|
|
sess.User,
|
|
|
|
sess.Session,
|
|
|
|
req.Ticket,
|
|
|
|
fileIDs,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if err := h.bs.DeleteFile(r.Context(), filename); err != nil {
|
|
|
|
fmt.Println("can not delete", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write([]byte("can not store message "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(&PutFileResp{Message: message.ID})
|
|
|
|
if err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not marshal resp "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-27 15:27:44 +00:00
|
|
|
if _, err := w.Write(resp); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PutSCReq struct {
|
|
|
|
Ticket string `json:"ticket"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PutSCResp struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) PutSC(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if err := r.ParseMultipartForm(10 * MB); err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not parse multipart "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.MultipartForm == nil {
|
|
|
|
if _, err := w.Write([]byte("no multipart")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.MultipartForm.File == nil {
|
|
|
|
if _, err := w.Write([]byte("no file")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filesCount:= len(r.MultipartForm.File)
|
|
|
|
|
|
|
|
if filesCount == 0 {
|
|
|
|
if _, err := w.Write([]byte("no files")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sess := jwt_adapter.Get(r.Context())
|
|
|
|
if sess == nil {
|
|
|
|
if _, err := w.Write([]byte("not authorized")); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req PutFileReq
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not decode "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
fileIDs, filenames []string
|
|
|
|
errFile error
|
|
|
|
)
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
m:=new(sync.Mutex)
|
|
|
|
|
|
|
|
wg.Add(filesCount)
|
|
|
|
for name, file := range r.MultipartForm.File {
|
|
|
|
file := file
|
|
|
|
name := name
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
freader, err := file[0].Open()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("can not open ", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := freader.Close(); err != nil {
|
|
|
|
errFile = err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
splitted := strings.Split(name, ".")
|
|
|
|
filename := fmt.Sprintf("%s.%s", xid.New().String(), splitted[len(splitted) - 1])
|
|
|
|
|
|
|
|
if err := h.bs.PutFile(
|
|
|
|
r.Context(),
|
|
|
|
filename,
|
|
|
|
freader,
|
|
|
|
file[0].Size); err != nil {
|
|
|
|
errFile = err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
fileIDs = append(fileIDs, filename)
|
|
|
|
filenames = append(filenames, name)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if errFile != nil {
|
|
|
|
if _, err := w.Write([]byte("can not store files "+errFile.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := h.dal.PutSCResponse(
|
|
|
|
r.Context(),
|
|
|
|
strings.Join(filenames, ", "),
|
|
|
|
sess.User,
|
|
|
|
sess.Session,
|
|
|
|
req.Ticket,
|
|
|
|
fileIDs,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
for _, filename := range filenames {
|
|
|
|
if err := h.bs.DeleteFile(r.Context(), filename); err != nil {
|
|
|
|
fmt.Println("can not delete", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write([]byte("can not store message "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := json.Marshal(&PutFileResp{Message: message.ID})
|
|
|
|
if err != nil {
|
|
|
|
if _, err := w.Write([]byte("can not marshal resp "+err.Error())); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 15:24:13 +00:00
|
|
|
if _, err := w.Write(resp); err != nil {
|
|
|
|
fmt.Println("CAN NOT WRITE", err)
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|