2021-04-11 09:48:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-05-02 22:25:12 +00:00
|
|
|
"bitbucket.org/skeris/heruvym/dal"
|
|
|
|
"bitbucket.org/skeris/heruvym/jwt_adapter"
|
|
|
|
"bitbucket.org/skeris/heruvym/model"
|
|
|
|
"bitbucket.org/skeris/heruvym/tools"
|
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-04-11 09:48:15 +00:00
|
|
|
"github.com/themakers/hlog"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Heruvym struct {
|
|
|
|
logger hlog.Logger
|
|
|
|
dal *dal.DAL
|
2021-05-01 17:50:03 +00:00
|
|
|
ral rAL.LayerMongoDb
|
2021-04-11 09:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 17:50:03 +00:00
|
|
|
func New(dataAccessLayer *dal.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-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-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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var request CreateTicketReq
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
ticketID, err := h.dal.CreateTicket(
|
|
|
|
ctx,
|
|
|
|
session.User,
|
|
|
|
session.ID,
|
|
|
|
request.Title,
|
2021-05-02 22:25:12 +00:00
|
|
|
request.Message,
|
|
|
|
[]string{},
|
2021-04-11 09:48:15 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "CannotCreateTicket", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-02 22:25:12 +00:00
|
|
|
if _, err := h.dal.PutMessage(ctx,
|
2021-04-11 09:48:15 +00:00
|
|
|
request.Message,
|
|
|
|
session.User,
|
|
|
|
session.Session,
|
|
|
|
ticketID,
|
|
|
|
[]string{},
|
|
|
|
); err != nil {
|
|
|
|
http.Error(w, "CannotCreateMessage", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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{} {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
go h.hasNoRole(output)
|
|
|
|
}
|
2021-05-01 17:50:03 +00:00
|
|
|
|
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{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
if err := h.dal.WatchActiveTickets(ctx, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot watch all tickets")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) userTickets(ctx context.Context, userID string, output chan interface{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
if err := h.dal.YieldUserTickets(ctx, userID, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot get tickets")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchTickets(ctx, userID, func(ticket model.Ticket) error {
|
|
|
|
output <- ticket
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot watch tickets")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) hasNoRole(output chan interface{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
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{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ticket := range tickets {
|
|
|
|
output <- ticket
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
message, err := h.dal.PutMessage(
|
|
|
|
ctx,
|
|
|
|
request.Message,
|
|
|
|
sess.User,
|
|
|
|
sess.Session,
|
|
|
|
request.TicketID,
|
|
|
|
request.Files,
|
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ tools.DataEmitter = (&Heruvym{}).Subscribe
|
|
|
|
|
|
|
|
func (h *Heruvym) Subscribe(ctx context.Context) chan interface{} {
|
|
|
|
sess := jwt_adapter.Get(ctx)
|
|
|
|
|
|
|
|
url := ctx.Value(tools.ContextURLKey).([]string)
|
|
|
|
ticketID := url[len(url)-1]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} 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
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.User); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} 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
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.User); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.dal.WatchMessages(ctx, ticketID,
|
|
|
|
func(message model.Message) error {
|
|
|
|
output <- message
|
|
|
|
|
|
|
|
if err := h.dal.SetShown(ctx, message.ID, sess.Session); err != nil {
|
|
|
|
output <- errors.New("cannot read message")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
output <- errors.New("cannot read messages")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Heruvym) handleOwnMessages(output chan interface{}) {
|
|
|
|
defer close(output)
|
|
|
|
|
|
|
|
}
|