2024-02-19 17:48:04 +00:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2024-07-11 09:06:24 +00:00
|
|
|
|
"encoding/json"
|
2024-06-03 14:22:48 +00:00
|
|
|
|
"errors"
|
2024-07-11 09:06:24 +00:00
|
|
|
|
"fmt"
|
2024-02-19 17:48:04 +00:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-06-07 14:53:45 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/penahub_common/log_mw"
|
2024-03-13 16:57:12 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/middleware"
|
2024-02-19 17:48:04 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
|
2024-06-03 14:22:48 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/pj_errors"
|
2024-06-12 13:27:42 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core/brokers"
|
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core/models"
|
2024-06-10 17:15:18 +00:00
|
|
|
|
"strconv"
|
2024-02-19 17:48:04 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreateAccountReq struct {
|
|
|
|
|
UserID string `json:"userId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type CreateAccountResp struct {
|
|
|
|
|
CreatedAccount model.Account `json:"created_account"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteAccountResp struct {
|
|
|
|
|
DeletedAccountID string `json:"account_Id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetPrivilegeByUserIDReq struct {
|
|
|
|
|
UserID string `json:"userId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteAccountByUserIDReq struct {
|
|
|
|
|
UserID string `json:"userId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DeleteAccountByUserIDResp struct {
|
|
|
|
|
DeletedAccountUserID string `json:"userId"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetAccountsReq struct {
|
|
|
|
|
Limit uint64 `json:"limit"`
|
|
|
|
|
Page uint64 `json:"page"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetAccountsResp struct {
|
|
|
|
|
Count uint64 `json:"count"`
|
|
|
|
|
Items []model.Account `json:"items"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getCurrentAccount обработчик для получения текущего аккаунта
|
|
|
|
|
func (s *Service) getCurrentAccount(ctx *fiber.Ctx) error {
|
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
account, err := s.dal.AccountRepo.GetAccountByID(ctx.Context(), accountID)
|
2024-03-26 19:37:39 +00:00
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: fix this later
|
|
|
|
|
if account.ID == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("no account")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(account)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// createAccount обработчик для создания нового аккаунта
|
|
|
|
|
func (s *Service) createAccount(ctx *fiber.Ctx) error {
|
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
2024-06-07 14:53:45 +00:00
|
|
|
|
hlogger := log_mw.ExtractLogger(ctx)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
|
|
|
|
|
existingAccount, err := s.dal.AccountRepo.GetAccountByID(ctx.Context(), accountID)
|
|
|
|
|
if err != nil && err != sql.ErrNoRows {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
if existingAccount.ID != "" {
|
|
|
|
|
return ctx.Status(fiber.StatusConflict).SendString("user with this ID already exists")
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 08:56:47 +00:00
|
|
|
|
email, err := s.authClient.GetUserEmail(accountID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 17:48:04 +00:00
|
|
|
|
newAccount := model.Account{
|
|
|
|
|
UserID: accountID,
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
Deleted: false,
|
|
|
|
|
Privileges: map[string]model.ShortPrivilege{
|
|
|
|
|
"quizUnlimTime": {
|
|
|
|
|
PrivilegeID: "quizUnlimTime",
|
|
|
|
|
PrivilegeName: "Безлимит Опросов",
|
|
|
|
|
Amount: 14,
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-02 09:36:22 +00:00
|
|
|
|
createdAcc, err := s.dal.AccountRepo.CreateAccount(ctx.Context(), &newAccount)
|
2024-06-10 16:23:52 +00:00
|
|
|
|
if err != nil {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
2024-06-12 10:30:11 +00:00
|
|
|
|
}
|
|
|
|
|
_, err = s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
|
|
|
|
|
AccountID: accountID,
|
|
|
|
|
Target: email,
|
|
|
|
|
Type: model.LeadTargetEmail,
|
|
|
|
|
QuizID: 0,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
2024-02-19 17:48:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-02 09:36:22 +00:00
|
|
|
|
hlogger.Emit(models.InfoAccountCreated{
|
|
|
|
|
CtxUserID: accountID,
|
|
|
|
|
CtxAccountID: createdAcc.ID,
|
|
|
|
|
})
|
|
|
|
|
|
2024-04-26 18:51:34 +00:00
|
|
|
|
err = s.producer.ToMailNotify(ctx.Context(), brokers.Message{
|
|
|
|
|
AccountID: accountID,
|
|
|
|
|
Email: email,
|
|
|
|
|
ServiceKey: s.serviceName,
|
|
|
|
|
SendAt: time.Now(),
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 17:48:04 +00:00
|
|
|
|
return ctx.JSON(CreateAccountResp{
|
|
|
|
|
CreatedAccount: newAccount,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteAccount обработчик для удаления текущего аккаунта
|
|
|
|
|
func (s *Service) deleteAccount(ctx *fiber.Ctx) error {
|
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
account, err := s.dal.AccountRepo.GetAccountByID(ctx.Context(), accountID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.dal.AccountRepo.DeleteAccount(ctx.Context(), account.ID); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.JSON(DeleteAccountResp{
|
|
|
|
|
DeletedAccountID: accountID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getPrivilegeByUserID обработчик для получения привилегий аккаунта по ID пользователя
|
|
|
|
|
func (s *Service) getPrivilegeByUserID(ctx *fiber.Ctx) error {
|
|
|
|
|
var req GetPrivilegeByUserIDReq
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
privilege, err := s.dal.AccountRepo.GetPrivilegesByAccountID(ctx.Context(), req.UserID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(privilege)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteAccountByUserID обработчик для удаления аккаунта по ID пользователя
|
|
|
|
|
func (s *Service) deleteAccountByUserID(ctx *fiber.Ctx) error {
|
|
|
|
|
var req DeleteAccountByUserIDReq
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
existingAccount, err := s.dal.AccountRepo.GetAccountByID(ctx.Context(), req.UserID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if existingAccount.ID == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("user with this ID not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.dal.AccountRepo.DeleteAccount(ctx.Context(), existingAccount.ID); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.JSON(DeleteAccountByUserIDResp{
|
|
|
|
|
DeletedAccountUserID: req.UserID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getAccounts обработчик для получения списка аккаунтов с пагинацией
|
|
|
|
|
func (s *Service) getAccounts(ctx *fiber.Ctx) error {
|
|
|
|
|
var req GetAccountsReq
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accounts, totalCount, err := s.dal.AccountRepo.GetAccounts(ctx.Context(), req.Limit, req.Page)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response := GetAccountsResp{
|
|
|
|
|
Count: totalCount,
|
|
|
|
|
Items: accounts,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(response)
|
|
|
|
|
}
|
2024-06-03 13:21:21 +00:00
|
|
|
|
|
|
|
|
|
func (s *Service) ManualDone(ctx *fiber.Ctx) error {
|
|
|
|
|
var req struct {
|
|
|
|
|
Id string `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.Id == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("User id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 14:22:48 +00:00
|
|
|
|
err := s.dal.AccountRepo.ManualDone(ctx.Context(), req.Id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("user don't have this privilege")
|
|
|
|
|
}
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 13:21:21 +00:00
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|
2024-06-10 17:15:18 +00:00
|
|
|
|
|
|
|
|
|
func (s *Service) PostLeadTarget(ctx *fiber.Ctx) error {
|
|
|
|
|
var req struct {
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
QuizID int32 `json:"quizID"`
|
|
|
|
|
Target string `json:"target"`
|
2024-07-11 09:06:24 +00:00
|
|
|
|
Name string `json:"name"`
|
2024-06-10 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 08:42:26 +00:00
|
|
|
|
if _, ok := model.ValidLeadTargetTypes[req.Type]; !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid type")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 17:15:18 +00:00
|
|
|
|
if req.Type == "" || req.Target == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Type and Target don't be nil")
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 09:06:24 +00:00
|
|
|
|
switch req.Type {
|
|
|
|
|
case "mail":
|
|
|
|
|
_, err := s.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
|
|
|
|
|
AccountID: accountID,
|
|
|
|
|
Target: req.Target,
|
|
|
|
|
Type: model.LeadTargetType(req.Type),
|
|
|
|
|
QuizID: req.QuizID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
case "telegram":
|
2024-07-11 13:07:00 +00:00
|
|
|
|
// todo before saving task to redis need check this quizID and type
|
2024-07-11 09:06:24 +00:00
|
|
|
|
task := model.TgRedisTask{
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
QuizID: req.QuizID,
|
|
|
|
|
AccountID: accountID,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskKey := fmt.Sprintf("telegram_task:%d", time.Now().UnixNano())
|
|
|
|
|
taskData, err := json.Marshal(task)
|
|
|
|
|
if err != nil {
|
2024-07-11 09:08:13 +00:00
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
2024-07-11 09:06:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.redisClient.Set(ctx.Context(), taskKey, taskData, 0).Err(); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
case "whatsapp":
|
|
|
|
|
return ctx.Status(fiber.StatusOK).SendString("todo")
|
2024-06-10 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 09:06:24 +00:00
|
|
|
|
return nil
|
2024-06-10 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) DeleteLeadTarget(ctx *fiber.Ctx) error {
|
|
|
|
|
leadIDStr := ctx.Params("id")
|
|
|
|
|
leadID, err := strconv.ParseInt(leadIDStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid lead ID format")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = s.dal.AccountRepo.DeleteLeadTarget(ctx.Context(), leadID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) GetLeadTarget(ctx *fiber.Ctx) error {
|
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 15:16:18 +00:00
|
|
|
|
quizIDStr := ctx.Params("quizID")
|
|
|
|
|
quizID, err := strconv.ParseInt(quizIDStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid quiz ID format")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := s.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID, int32(quizID))
|
2024-06-10 17:15:18 +00:00
|
|
|
|
if err != nil {
|
2024-06-11 16:12:54 +00:00
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, pj_errors.ErrNotFound):
|
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("this lead target not found")
|
|
|
|
|
default:
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
2024-06-10 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Service) UpdateLeadTarget(ctx *fiber.Ctx) error {
|
|
|
|
|
var req struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
Target string `json:"target"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if req.ID == 0 || req.Target == "" {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("ID and Target don't be nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, err := s.dal.AccountRepo.UpdateLeadTarget(ctx.Context(), model.LeadTarget{
|
|
|
|
|
ID: req.ID,
|
|
|
|
|
Target: req.Target,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2024-06-11 16:12:54 +00:00
|
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, pj_errors.ErrNotFound):
|
|
|
|
|
return ctx.Status(fiber.StatusNotFound).SendString("this lead target not found")
|
|
|
|
|
default:
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
2024-06-10 17:15:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(result)
|
|
|
|
|
}
|