2024-02-19 17:48:04 +00:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
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-04-26 18:51:34 +00:00
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/quiz/core.git/brokers"
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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(),
|
2024-04-26 18:51:34 +00:00
|
|
|
|
Email: email,
|
2024-02-19 17:48:04 +00:00
|
|
|
|
Deleted: false,
|
|
|
|
|
Privileges: map[string]model.ShortPrivilege{
|
|
|
|
|
"quizUnlimTime": {
|
|
|
|
|
PrivilegeID: "quizUnlimTime",
|
|
|
|
|
PrivilegeName: "Безлимит Опросов",
|
|
|
|
|
Amount: 14,
|
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.dal.AccountRepo.CreateAccount(ctx.Context(), &newAccount); err != nil {
|
|
|
|
|
return ctx.Status(fiber.StatusInternalServerError).SendString(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ctx.SendStatus(fiber.StatusOK)
|
|
|
|
|
}
|