2024-10-25 15:26:03 +00:00
|
|
|
|
package account
|
2024-02-19 17:48:04 +00:00
|
|
|
|
|
|
|
|
|
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"
|
2025-02-24 17:06:12 +00:00
|
|
|
|
"gitea.pena/PenaSide/common/log_mw"
|
|
|
|
|
"gitea.pena/SQuiz/common/dal"
|
|
|
|
|
"gitea.pena/SQuiz/common/middleware"
|
|
|
|
|
"gitea.pena/SQuiz/common/model"
|
|
|
|
|
"gitea.pena/SQuiz/common/pj_errors"
|
|
|
|
|
"gitea.pena/SQuiz/core/internal/brokers"
|
|
|
|
|
"gitea.pena/SQuiz/core/internal/clients/auth"
|
|
|
|
|
"gitea.pena/SQuiz/core/internal/models"
|
2024-10-25 15:26:03 +00:00
|
|
|
|
"github.com/go-redis/redis/v8"
|
2024-02-19 17:48:04 +00:00
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-06-10 17:15:18 +00:00
|
|
|
|
"strconv"
|
2024-02-19 17:48:04 +00:00
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
type Deps struct {
|
|
|
|
|
Dal *dal.DAL
|
|
|
|
|
AuthClient *auth.AuthClient
|
|
|
|
|
Producer *brokers.Producer
|
|
|
|
|
ServiceName string
|
|
|
|
|
RedisClient *redis.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Account struct {
|
|
|
|
|
dal *dal.DAL
|
|
|
|
|
authClient *auth.AuthClient
|
|
|
|
|
producer *brokers.Producer
|
|
|
|
|
serviceName string
|
|
|
|
|
redisClient *redis.Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAccountController(deps Deps) *Account {
|
|
|
|
|
return &Account{
|
|
|
|
|
dal: deps.Dal,
|
|
|
|
|
authClient: deps.AuthClient,
|
|
|
|
|
producer: deps.Producer,
|
|
|
|
|
serviceName: deps.ServiceName,
|
|
|
|
|
redisClient: deps.RedisClient,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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 обработчик для получения текущего аккаунта
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) GetCurrentAccount(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
account, err := r.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 обработчик для создания нового аккаунта
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) CreateAccount(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
existingAccount, err := r.dal.AccountRepo.GetAccountByID(ctx.Context(), accountID)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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-10-25 15:26:03 +00:00
|
|
|
|
email, err := r.authClient.GetUserEmail(accountID)
|
2024-04-16 08:56:47 +00:00
|
|
|
|
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-10-25 15:26:03 +00:00
|
|
|
|
createdAcc, err := r.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
|
|
|
|
}
|
2024-10-25 15:26:03 +00:00
|
|
|
|
_, err = r.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
|
2024-06-12 10:30:11 +00:00
|
|
|
|
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-10-25 15:26:03 +00:00
|
|
|
|
err = r.producer.ToMailNotify(ctx.Context(), brokers.Message{
|
2024-04-26 18:51:34 +00:00
|
|
|
|
AccountID: accountID,
|
|
|
|
|
Email: email,
|
2024-10-25 15:26:03 +00:00
|
|
|
|
ServiceKey: r.serviceName,
|
2024-04-26 18:51:34 +00:00
|
|
|
|
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 обработчик для удаления текущего аккаунта
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) DeleteAccount(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
accountID, ok := middleware.GetAccountId(ctx)
|
|
|
|
|
if !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusUnauthorized).SendString("account id is required")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
account, err := r.dal.AccountRepo.GetAccountByID(ctx.Context(), accountID)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
if err := r.dal.AccountRepo.DeleteAccount(ctx.Context(), account.ID); err != nil {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.JSON(DeleteAccountResp{
|
|
|
|
|
DeletedAccountID: accountID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getPrivilegeByUserID обработчик для получения привилегий аккаунта по ID пользователя
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) GetPrivilegeByUserID(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
var req GetPrivilegeByUserIDReq
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
privilege, err := r.dal.AccountRepo.GetPrivilegesByAccountID(ctx.Context(), req.UserID)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(privilege)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteAccountByUserID обработчик для удаления аккаунта по ID пользователя
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) DeleteAccountByUserID(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
var req DeleteAccountByUserIDReq
|
|
|
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid request data")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
existingAccount, err := r.dal.AccountRepo.GetAccountByID(ctx.Context(), req.UserID)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
if err := r.dal.AccountRepo.DeleteAccount(ctx.Context(), existingAccount.ID); err != nil {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.JSON(DeleteAccountByUserIDResp{
|
|
|
|
|
DeletedAccountUserID: req.UserID,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getAccounts обработчик для получения списка аккаунтов с пагинацией
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) GetAccounts(ctx *fiber.Ctx) error {
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
accounts, totalCount, err := r.dal.AccountRepo.GetAccounts(ctx.Context(), req.Limit, req.Page)
|
2024-02-19 17:48:04 +00:00
|
|
|
|
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
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) ManualDone(ctx *fiber.Ctx) error {
|
2024-06-03 13:21:21 +00:00
|
|
|
|
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-10-25 15:26:03 +00:00
|
|
|
|
err := r.dal.AccountRepo.ManualDone(ctx.Context(), req.Id)
|
2024-06-03 14:22:48 +00:00
|
|
|
|
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
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) PostLeadTarget(ctx *fiber.Ctx) error {
|
2024-06-10 17:15:18 +00:00
|
|
|
|
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-07-11 15:10:16 +00:00
|
|
|
|
//accountID := "64f2cd7a7047f28fdabf6d9e"
|
|
|
|
|
|
2024-06-12 08:42:26 +00:00
|
|
|
|
if _, ok := model.ValidLeadTargetTypes[req.Type]; !ok {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid type")
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 15:10:16 +00:00
|
|
|
|
if req.Type == "" || (req.Target == "" && req.Type != string(model.LeadTargetTg)) {
|
2024-06-10 17:15:18 +00:00
|
|
|
|
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":
|
2024-10-25 15:26:03 +00:00
|
|
|
|
_, err := r.dal.AccountRepo.PostLeadTarget(ctx.Context(), model.LeadTarget{
|
2024-07-11 09:06:24 +00:00
|
|
|
|
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-10-25 15:26:03 +00:00
|
|
|
|
targets, err := r.dal.AccountRepo.GetLeadTarget(ctx.Context(), accountID, req.QuizID)
|
2024-07-11 13:37:47 +00:00
|
|
|
|
if err != nil && !errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
if !errors.Is(err, pj_errors.ErrNotFound) {
|
|
|
|
|
for _, t := range targets {
|
|
|
|
|
if t.Type == model.LeadTargetTg {
|
|
|
|
|
return ctx.Status(fiber.StatusAlreadyReported).SendString("LeadTarget for this quiz already exist")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
if err := r.redisClient.Set(ctx.Context(), taskKey, taskData, 0).Err(); err != nil {
|
2024-07-11 09:06:24 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) DeleteLeadTarget(ctx *fiber.Ctx) error {
|
2024-06-10 17:15:18 +00:00
|
|
|
|
leadIDStr := ctx.Params("id")
|
|
|
|
|
leadID, err := strconv.ParseInt(leadIDStr, 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusBadRequest).SendString("Invalid lead ID format")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
err = r.dal.AccountRepo.DeleteLeadTarget(ctx.Context(), leadID)
|
2024-06-10 17:15:18 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) GetLeadTarget(ctx *fiber.Ctx) error {
|
2024-06-10 17:15:18 +00:00
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
result, err := r.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)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
func (r *Account) UpdateLeadTarget(ctx *fiber.Ctx) error {
|
2024-06-10 17:15:18 +00:00
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 15:26:03 +00:00
|
|
|
|
result, err := r.dal.AccountRepo.UpdateLeadTarget(ctx.Context(), model.LeadTarget{
|
2024-06-10 17:15:18 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|