2022-07-28 15:00:43 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-09-15 13:53:55 +00:00
|
|
|
"github.com/Pena-Co-Ltd/amocrm_templategen_back/dal/model"
|
2022-07-28 15:00:43 +00:00
|
|
|
"net/http"
|
2022-11-12 10:24:48 +00:00
|
|
|
"strconv"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type ReqTemplateSet struct {
|
2022-11-21 15:07:49 +00:00
|
|
|
LeadId int64 `json:"lead_id"` // Required for insert/update
|
|
|
|
TemplateId string `json:"template_id"` // Required for update - может потом удалить ?
|
|
|
|
Name string `json:"name"`
|
|
|
|
File string `json:"file"` // Required.
|
|
|
|
StorageID string `json:"storage_id"` // Required. id gdisk or yadisk
|
|
|
|
StorageType string `json:"storage_type"` // Required. yadisk, gdisk - Может удалить потом?
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type RespTemplateSet struct {
|
2022-11-19 13:24:30 +00:00
|
|
|
ID string `json:"id"`
|
2022-11-12 10:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TemplateSet - устанавливает/обновляет шаблон для сделки
|
|
|
|
func (h *Handlers) TemplateSet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req ReqTemplateSet
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
amoData := getAmoByJwt(r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
err := decodePost(&req, r)
|
2022-07-28 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-21 15:07:49 +00:00
|
|
|
if req.StorageType == "" || req.StorageID == "" {
|
2022-11-12 10:24:48 +00:00
|
|
|
h.reportError(w, errors.New("bad request"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.File == "" {
|
|
|
|
h.reportError(w, errors.New("file required"), http.StatusBadRequest)
|
2022-07-28 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
// Search/update template
|
|
|
|
|
|
|
|
update := &model.Template{
|
2022-11-21 15:07:49 +00:00
|
|
|
ID: req.TemplateId,
|
|
|
|
UserID: amoData.UserID,
|
|
|
|
LeadId: strconv.FormatInt(req.LeadId, 10),
|
|
|
|
Name: req.Name,
|
|
|
|
File: req.File,
|
|
|
|
StorageID: req.StorageID,
|
|
|
|
StorageType: req.StorageType,
|
|
|
|
IsDeleted: false,
|
2022-11-12 10:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateId := ""
|
|
|
|
if req.TemplateId == "" {
|
2022-11-19 13:24:30 +00:00
|
|
|
if req.LeadId > 0 {
|
2022-11-12 10:24:48 +00:00
|
|
|
err = h.dal.Template.UpdateByLeadID(r.Context(), update)
|
|
|
|
} else {
|
|
|
|
templateId, err = h.dal.Template.Insert(r.Context(), update)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = h.dal.Template.UpdateByID(r.Context(), update)
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-11-12 10:24:48 +00:00
|
|
|
|
|
|
|
sendResponse(w, 200, RespTemplateSet{templateId})
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) TemplateGetListByUser(w http.ResponseWriter, r *http.Request) {
|
2022-11-12 10:24:48 +00:00
|
|
|
amoData := getAmoByJwt(r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2022-11-12 10:24:48 +00:00
|
|
|
result, err := h.dal.Template.GetListByUserID(r.Context(), amoData.UserID)
|
2022-07-28 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(w, 200, result)
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type ReqTemplateGetListByGroup struct {
|
|
|
|
GroupID string `json:"group_id"`
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
func (h *Handlers) TemplateGetListByGroup(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req ReqTemplateGetListByGroup
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
amoData := getAmoByJwt(r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
err := decodePost(&req, r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:24:30 +00:00
|
|
|
//if req.GroupID == "" {
|
|
|
|
// h.reportError(w, errors.New("group_id required"), http.StatusBadRequest)
|
|
|
|
// return
|
|
|
|
//}
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
resp, err := h.dal.Template.GetListByGroupID(r.Context(), req.GroupID, amoData.UserID)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
sendResponse(w, http.StatusOK, resp)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type ReqTemplateGet struct {
|
|
|
|
LeadId int64 `json:"lead_id"`
|
|
|
|
TemplateId string `json:"template_id"`
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
func (h *Handlers) TemplateGet(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req ReqTemplateGet
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
amoData := getAmoByJwt(r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
err := decodePost(&req, r)
|
2022-07-28 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if req.TemplateId == "" && strconv.FormatInt(req.LeadId, 10) == "" {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
var template *model.Template
|
|
|
|
|
|
|
|
if req.TemplateId != "" {
|
|
|
|
template, err = h.dal.Template.GetByID(r.Context(), req.TemplateId)
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if strconv.FormatInt(req.LeadId, 10) != "" {
|
|
|
|
template, err = h.dal.Template.GetByLeadId(r.Context(), strconv.FormatInt(req.LeadId, 10))
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if template == nil {
|
|
|
|
templateId, err := h.dal.Template.Insert(r.Context(), &model.Template{
|
|
|
|
UserID: amoData.UserID,
|
|
|
|
LeadId: strconv.FormatInt(req.LeadId, 10),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
template = &model.Template{ID: templateId}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
sendResponse(w, 200, template)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReqTemplateUpdate struct {
|
|
|
|
ID string `json:"id"` // Template ID
|
|
|
|
Name string `json:"name"` // Имя сделки
|
|
|
|
File string `json:"file"` //
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) TemplateUpdate(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req ReqTemplateUpdate
|
|
|
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
|
|
|
|
if amoData == nil {
|
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := decodePost(&req, r)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.ID == "" {
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
record := &model.Template{
|
|
|
|
ID: req.ID,
|
|
|
|
UserID: amoData.UserID,
|
|
|
|
Name: req.Name,
|
|
|
|
File: req.File,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.dal.Template.UpdateByID(r.Context(), record)
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(w, 200, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type ReqTemplateDelete struct {
|
|
|
|
ID string `json:"id"`
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) TemplateDelete(w http.ResponseWriter, r *http.Request) {
|
2022-11-12 10:24:48 +00:00
|
|
|
var req ReqTemplateDelete
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
amoData := getAmoByJwt(r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if amoData == nil {
|
2022-07-28 15:00:43 +00:00
|
|
|
h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2022-11-12 10:24:48 +00:00
|
|
|
err := decodePost(&req, r)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
if req.ID == "" {
|
|
|
|
h.reportError(w, errors.New("id required"), http.StatusBadRequest)
|
2022-07-28 15:00:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
err = h.dal.Template.DeleteByID(r.Context(), req.ID)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendResponse(w, 200, nil)
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
type ReqTemplateInit struct {
|
2022-07-28 15:00:43 +00:00
|
|
|
EventName string `json:"event_name"`
|
|
|
|
Data map[string]interface{} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) TemplateInit(w http.ResponseWriter, r *http.Request) {
|
2022-11-12 10:24:48 +00:00
|
|
|
//var req ReqTemplateInit
|
2022-09-15 13:53:55 +00:00
|
|
|
//
|
|
|
|
//user := getJwtUser(r)
|
|
|
|
//
|
|
|
|
//if user == nil {
|
|
|
|
// h.reportError(w, ErrorUnauthorized, http.StatusUnauthorized)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//err := decodePost(&resp, r)
|
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusBadRequest)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// Ищем ивент
|
2022-11-12 10:24:48 +00:00
|
|
|
//t, err := h.dal.Template.GetByNameAndUserID(r.Context(), resp.EventName, amoData.UserID)
|
2022-09-15 13:53:55 +00:00
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//if t == nil {
|
|
|
|
// h.reportError(w, errors.New("template not found"), http.StatusNotFound)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
2022-11-12 10:24:48 +00:00
|
|
|
//YaDiskData, err := h.dal.YaDisk.GetListByUserID(r.Context(), amoData.UserID)
|
2022-09-15 13:53:55 +00:00
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//if YaDiskData == nil {
|
|
|
|
// h.reportError(w, errors.New("YaDisk not found"), http.StatusNotFound)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// download file
|
|
|
|
//token := &oauth2.Token{
|
|
|
|
// AccessToken: YaDiskData.AccessToken,
|
|
|
|
// TokenType: YaDiskData.TokenType,
|
|
|
|
// RefreshToken: YaDiskData.RefreshToken,
|
|
|
|
// Expiry: YaDiskData.ExpiresIn,
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//if !token.Valid() {
|
|
|
|
// h.reportError(w, errors.New("invalid yandex token"), http.StatusForbidden)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//client := h.YaDisk.NewClient(token)
|
|
|
|
//
|
|
|
|
//res, err := client.GetResources(YaDiskData.TemplateFolder + "/" + t.Filename)
|
|
|
|
//
|
|
|
|
//if res == nil {
|
|
|
|
// h.reportError(w, errors.New("resource in yandex disk not found"), http.StatusNotFound)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//downloadFileName := fmt.Sprintf("./tmp/downloaded/%v_%v", t.UserID, t.Filename)
|
|
|
|
//
|
|
|
|
//err = templategen.DownloadDocument(downloadFileName, res.File)
|
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// parsing file
|
|
|
|
//dc, err := docTemp.GetTemplate(downloadFileName)
|
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//dc.Parse()
|
|
|
|
//
|
|
|
|
//saveFilename := fmt.Sprintf("%v_%v_%v", t.UserID, time.Now().Unix(), t.Filename)
|
|
|
|
//err = dc.Execute("./tmp/parsed/"+saveFilename, resp.Data)
|
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// Send to Yandex Disk
|
|
|
|
//
|
2022-10-11 10:07:29 +00:00
|
|
|
//err = client.UploadResources(YaDiskData.SaveFolder+"/"+saveFilename, "https://solweb.site/tmp/parsed/"+saveFilename)
|
2022-09-15 13:53:55 +00:00
|
|
|
//if err != nil {
|
|
|
|
// h.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
// return
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//fmt.Println("https://solweb.site/tmp/parsed/" + saveFilename)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|