2024-02-19 16:33:15 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
2025-02-21 13:40:10 +00:00
|
|
|
"gitea.pena/SQuiz/common/dal/sqlcgen"
|
|
|
|
"gitea.pena/SQuiz/common/model"
|
|
|
|
"gitea.pena/SQuiz/common/pj_errors"
|
2024-02-19 16:33:15 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Deps struct {
|
2024-06-03 08:37:28 +00:00
|
|
|
Queries *sqlcgen.Queries
|
|
|
|
Pool *sql.DB
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AccountRepository struct {
|
2024-06-03 08:37:28 +00:00
|
|
|
queries *sqlcgen.Queries
|
|
|
|
pool *sql.DB
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewAccountRepository(deps Deps) *AccountRepository {
|
|
|
|
return &AccountRepository{
|
2024-06-03 08:37:28 +00:00
|
|
|
queries: deps.Queries,
|
|
|
|
pool: deps.Pool,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) GetAccountByID(ctx context.Context, userID string) (model.Account, error) {
|
2024-07-14 07:46:28 +00:00
|
|
|
accountRows, err := r.queries.GetAccountWithPrivileges(ctx, userID)
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return model.Account{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var account model.Account
|
|
|
|
privileges := make(map[string]model.ShortPrivilege)
|
|
|
|
|
|
|
|
for _, row := range accountRows {
|
|
|
|
if account.ID == "" {
|
|
|
|
account.ID = row.ID.String()
|
2024-07-14 07:46:28 +00:00
|
|
|
account.UserID = row.UserID
|
|
|
|
account.CreatedAt = row.CreatedAt
|
|
|
|
account.Deleted = row.Deleted
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 16:21:37 +00:00
|
|
|
if row.PrivilegeID != 0 {
|
2024-02-19 16:33:15 +00:00
|
|
|
privilege := model.ShortPrivilege{
|
2024-04-07 09:17:26 +00:00
|
|
|
ID: fmt.Sprintf("%d", row.PrivilegeID),
|
|
|
|
PrivilegeID: row.Privilegeid,
|
2024-03-13 16:21:37 +00:00
|
|
|
PrivilegeName: row.PrivilegeName,
|
|
|
|
Amount: uint64(row.Amount),
|
2024-07-14 07:46:28 +00:00
|
|
|
CreatedAt: row.PrivilegeCreatedAt,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
2024-04-07 10:48:21 +00:00
|
|
|
privileges[privilege.PrivilegeID] = privilege
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.ID == "" {
|
|
|
|
return model.Account{}, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
|
|
|
|
account.Privileges = privileges
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) GetPrivilegesByAccountID(ctx context.Context, userID string) ([]model.ShortPrivilege, error) {
|
|
|
|
|
2024-07-14 07:46:28 +00:00
|
|
|
privilegeRows, err := r.queries.GetPrivilegesByAccountIDWC(ctx, userID)
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var privileges []model.ShortPrivilege
|
|
|
|
|
|
|
|
for _, row := range privilegeRows {
|
|
|
|
privilege := model.ShortPrivilege{
|
|
|
|
ID: fmt.Sprintf("%d", row.ID),
|
2024-07-14 07:46:28 +00:00
|
|
|
PrivilegeID: row.Privilegeid,
|
|
|
|
PrivilegeName: row.PrivilegeName,
|
|
|
|
Amount: uint64(row.Amount),
|
|
|
|
CreatedAt: row.CreatedAt,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
privileges = append(privileges, privilege)
|
|
|
|
}
|
|
|
|
|
|
|
|
return privileges, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo test
|
2024-06-02 08:50:40 +00:00
|
|
|
func (r *AccountRepository) CreateAccount(ctx context.Context, data *model.Account) (model.Account, error) {
|
2024-02-19 16:33:15 +00:00
|
|
|
data.ID = uuid.NewString()
|
2024-06-02 08:50:40 +00:00
|
|
|
row, err := r.queries.CreateAccount(ctx, sqlcgen.CreateAccountParams{
|
2024-02-19 16:33:15 +00:00
|
|
|
ID: uuid.MustParse(data.ID),
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: data.UserID,
|
|
|
|
CreatedAt: data.CreatedAt,
|
|
|
|
Deleted: data.Deleted,
|
2024-02-19 16:33:15 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-06-02 08:50:40 +00:00
|
|
|
return model.Account{}, fmt.Errorf("failed to create account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAccount := model.Account{
|
|
|
|
ID: row.ID.String(),
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: row.UserID,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, privilege := range data.Privileges {
|
|
|
|
err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
Privilegeid: privilege.PrivilegeID,
|
|
|
|
AccountID: uuid.MustParse(data.ID),
|
|
|
|
PrivilegeName: privilege.PrivilegeName,
|
|
|
|
Amount: int32(privilege.Amount),
|
|
|
|
CreatedAt: privilege.CreatedAt,
|
2024-02-19 16:33:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2024-06-02 08:50:40 +00:00
|
|
|
return model.Account{}, fmt.Errorf("failed to insert privilege: %w", err)
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-02 08:50:40 +00:00
|
|
|
return createdAccount, nil
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccountRepository) DeleteAccount(ctx context.Context, accountID string) error {
|
|
|
|
tx, err := r.pool.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.DeleteAccountById(ctx, uuid.MustParse(accountID))
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-14 07:46:28 +00:00
|
|
|
err = r.queries.DeletePrivilegeByAccID(ctx, uuid.MustParse(accountID))
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) GetAccounts(ctx context.Context, limit uint64, offset uint64) ([]model.Account, uint64, error) {
|
|
|
|
rows, err := r.queries.AccountPagination(ctx, sqlcgen.AccountPaginationParams{
|
|
|
|
Limit: int32(limit),
|
|
|
|
Offset: int32(offset),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var accounts []model.Account
|
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
account := model.Account{
|
|
|
|
ID: row.ID.String(),
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: row.UserID,
|
|
|
|
CreatedAt: row.CreatedAt,
|
|
|
|
Deleted: row.Deleted,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
accounts = append(accounts, account)
|
|
|
|
}
|
|
|
|
|
|
|
|
return accounts, uint64(len(accounts)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) UpdatePrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error {
|
|
|
|
err := r.queries.UpdatePrivilege(ctx, sqlcgen.UpdatePrivilegeParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
Amount: int32(privilege.Amount),
|
|
|
|
CreatedAt: privilege.CreatedAt,
|
|
|
|
AccountID: uuid.MustParse(accountID),
|
|
|
|
Privilegeid: privilege.PrivilegeID,
|
2024-02-19 16:33:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) InsertPrivilege(ctx context.Context, privilege *model.ShortPrivilege, accountID string) error {
|
2024-04-07 08:53:55 +00:00
|
|
|
err := r.queries.InsertPrivilege(ctx, sqlcgen.InsertPrivilegeParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
Amount: int32(privilege.Amount),
|
|
|
|
CreatedAt: privilege.CreatedAt,
|
|
|
|
AccountID: uuid.MustParse(accountID),
|
|
|
|
Privilegeid: privilege.PrivilegeID,
|
|
|
|
PrivilegeName: privilege.PrivilegeName,
|
2024-02-19 16:33:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
2024-06-03 08:53:33 +00:00
|
|
|
func (r *AccountRepository) GetExpired(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) {
|
2024-07-14 07:46:28 +00:00
|
|
|
rows, err := r.queries.GetExpiredDayPrivilege(ctx, privilegeID)
|
2024-06-03 08:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-03 08:53:33 +00:00
|
|
|
var expiredRecords []model.ExpiredPrivileges
|
2024-06-03 08:37:28 +00:00
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
privilege := model.ShortPrivilege{
|
|
|
|
ID: fmt.Sprintf("%d", row.ID),
|
2024-07-14 07:46:28 +00:00
|
|
|
PrivilegeID: row.Privilegeid,
|
|
|
|
PrivilegeName: row.PrivilegeName,
|
|
|
|
Amount: uint64(row.Amount),
|
|
|
|
CreatedAt: row.CreatedAt,
|
2024-06-03 08:37:28 +00:00
|
|
|
}
|
2024-06-03 08:53:33 +00:00
|
|
|
expiredRecords = append(expiredRecords, model.ExpiredPrivileges{
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: row.UserID,
|
2024-06-03 08:53:33 +00:00
|
|
|
Privilege: privilege,
|
|
|
|
})
|
2024-06-03 08:37:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return expiredRecords, nil
|
|
|
|
}
|
|
|
|
|
2024-06-03 08:53:33 +00:00
|
|
|
func (r *AccountRepository) GetExpiredCount(ctx context.Context, privilegeID string) ([]model.ExpiredPrivileges, error) {
|
2024-07-14 07:46:28 +00:00
|
|
|
rows, err := r.queries.GetExpiredCountPrivilege(ctx, privilegeID)
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-03 08:53:33 +00:00
|
|
|
var expiredRecords []model.ExpiredPrivileges
|
2024-02-19 16:33:15 +00:00
|
|
|
|
|
|
|
for _, row := range rows {
|
|
|
|
privilege := model.ShortPrivilege{
|
|
|
|
ID: fmt.Sprintf("%d", row.ID),
|
2024-07-14 07:46:28 +00:00
|
|
|
PrivilegeID: row.Privilegeid,
|
|
|
|
PrivilegeName: row.PrivilegeName,
|
|
|
|
Amount: uint64(row.Amount),
|
|
|
|
CreatedAt: row.CreatedAt,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
2024-06-03 08:53:33 +00:00
|
|
|
expiredRecords = append(expiredRecords, model.ExpiredPrivileges{
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: row.UserID,
|
2024-06-03 08:53:33 +00:00
|
|
|
Privilege: privilege,
|
|
|
|
})
|
2024-02-19 16:33:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return expiredRecords, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccountRepository) CheckAndAddDefault(ctx context.Context, amount uint64, privilegeID string, zeroAmount uint64) error {
|
|
|
|
err := r.queries.CheckAndAddDefault(ctx, sqlcgen.CheckAndAddDefaultParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
Amount: int32(amount),
|
|
|
|
PrivilegeName: privilegeID,
|
|
|
|
Amount_2: int32(zeroAmount),
|
2024-02-19 16:33:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error executing SQL query: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) DeletePrivilegeByID(ctx context.Context, id string) error {
|
|
|
|
intID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert id to integer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.queries.DeletePrivilegeByID(ctx, int32(intID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) UpdatePrivilegeAmount(ctx context.Context, ID string, newAmount uint64) error {
|
|
|
|
intID, err := strconv.Atoi(ID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to convert id to integer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.queries.UpdatePrivilegeAmount(ctx, sqlcgen.UpdatePrivilegeAmountParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
Amount: int32(newAmount),
|
2024-02-19 16:33:15 +00:00
|
|
|
ID: int32(intID),
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// test +
|
|
|
|
func (r *AccountRepository) GetAccAndPrivilegeByEmail(ctx context.Context, email string) (model.Account, []model.ShortPrivilege, error) {
|
|
|
|
var account model.Account
|
|
|
|
var privileges []model.ShortPrivilege
|
|
|
|
|
2024-07-14 07:46:28 +00:00
|
|
|
row, err := r.queries.GetAccAndPrivilegeByEmail(ctx, email)
|
2024-02-19 16:33:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return account, privileges, err
|
|
|
|
}
|
|
|
|
|
|
|
|
account.ID = row.ID.String()
|
2024-07-14 07:46:28 +00:00
|
|
|
account.UserID = row.UserID
|
|
|
|
account.CreatedAt = row.CreatedAt
|
2024-02-19 16:33:15 +00:00
|
|
|
|
2024-03-13 16:21:37 +00:00
|
|
|
if row.ID_2 != 0 {
|
2024-02-19 16:33:15 +00:00
|
|
|
privilege := model.ShortPrivilege{
|
2024-03-13 16:21:37 +00:00
|
|
|
ID: fmt.Sprint(row.ID_2),
|
|
|
|
PrivilegeID: row.Privilegeid,
|
|
|
|
Amount: uint64(row.Amount),
|
2024-07-14 07:46:28 +00:00
|
|
|
CreatedAt: row.CreatedAt_2,
|
2024-02-19 16:33:15 +00:00
|
|
|
}
|
|
|
|
privileges = append(privileges, privilege)
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, privileges, nil
|
|
|
|
}
|
2024-03-22 11:31:37 +00:00
|
|
|
|
|
|
|
func (r *AccountRepository) GetQidOwner(ctx context.Context, qId string) (string, error) {
|
2024-03-22 11:36:13 +00:00
|
|
|
qUUID, err := uuid.Parse(qId)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
qNullUUID := uuid.NullUUID{UUID: qUUID, Valid: true}
|
|
|
|
|
|
|
|
userID, err := r.queries.GetQidOwner(ctx, qNullUUID)
|
2024-03-22 11:31:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userID, nil
|
|
|
|
}
|
2024-06-03 14:01:27 +00:00
|
|
|
|
|
|
|
func (r *AccountRepository) ManualDone(ctx context.Context, userID string) error {
|
|
|
|
_, err := r.queries.DecrementManual(ctx, sqlcgen.DecrementManualParams{
|
2024-07-14 07:46:28 +00:00
|
|
|
UserID: userID,
|
|
|
|
Privilegeid: "quizManual",
|
2024-06-03 14:01:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return pj_errors.ErrNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-10 16:56:29 +00:00
|
|
|
|
|
|
|
func (r *AccountRepository) PostLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) {
|
2024-06-10 17:05:34 +00:00
|
|
|
row, err := r.queries.CreateLeadTarget(ctx, sqlcgen.CreateLeadTargetParams{
|
2024-07-11 13:32:42 +00:00
|
|
|
Accountid: req.AccountID,
|
|
|
|
Type: req.Type,
|
|
|
|
Quizid: req.QuizID,
|
|
|
|
Target: req.Target,
|
|
|
|
Invitelink: req.InviteLink,
|
2024-06-10 17:05:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return model.LeadTarget{}, err
|
|
|
|
}
|
2024-06-12 08:31:34 +00:00
|
|
|
|
|
|
|
var targetType model.LeadTargetType
|
|
|
|
v := string(row.Type.([]byte))
|
|
|
|
targetType = model.LeadTargetType(v)
|
|
|
|
|
2024-06-10 17:05:34 +00:00
|
|
|
return model.LeadTarget{
|
2024-07-11 13:32:42 +00:00
|
|
|
ID: row.ID,
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
Type: targetType,
|
|
|
|
QuizID: row.Quizid,
|
|
|
|
Target: row.Target,
|
|
|
|
Deleted: row.Deleted,
|
|
|
|
CreatedAt: row.Createdat,
|
|
|
|
InviteLink: row.Invitelink,
|
2024-06-10 17:05:34 +00:00
|
|
|
}, nil
|
2024-06-10 16:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccountRepository) DeleteLeadTarget(ctx context.Context, id int64) error {
|
2024-06-10 17:05:34 +00:00
|
|
|
err := r.queries.DeleteLeadTarget(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-10 16:56:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-11 13:56:31 +00:00
|
|
|
func (r *AccountRepository) GetLeadTarget(ctx context.Context, accountID string, quizID int32) ([]model.LeadTarget, error) {
|
|
|
|
rows, err := r.queries.GetLeadTarget(ctx, sqlcgen.GetLeadTargetParams{
|
|
|
|
Accountid: accountID,
|
|
|
|
Quizid: quizID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-06-11 16:08:22 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2024-06-24 13:26:38 +00:00
|
|
|
return []model.LeadTarget{}, pj_errors.ErrNotFound
|
2024-06-11 16:08:22 +00:00
|
|
|
}
|
2024-06-24 13:26:38 +00:00
|
|
|
return []model.LeadTarget{}, err
|
2024-06-11 13:56:31 +00:00
|
|
|
}
|
|
|
|
var leadTargets []model.LeadTarget
|
|
|
|
for _, row := range rows {
|
2024-06-12 08:31:34 +00:00
|
|
|
var targetType model.LeadTargetType
|
|
|
|
v := string(row.Type.([]byte))
|
|
|
|
targetType = model.LeadTargetType(v)
|
|
|
|
|
2024-06-11 13:56:31 +00:00
|
|
|
leadTargets = append(leadTargets, model.LeadTarget{
|
2024-07-11 13:32:42 +00:00
|
|
|
ID: row.ID,
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
Type: targetType,
|
|
|
|
QuizID: row.Quizid,
|
|
|
|
Target: row.Target,
|
|
|
|
Deleted: row.Deleted,
|
|
|
|
CreatedAt: row.Createdat,
|
|
|
|
InviteLink: row.Invitelink,
|
2024-06-11 13:56:31 +00:00
|
|
|
})
|
|
|
|
}
|
2024-06-10 16:56:29 +00:00
|
|
|
|
2024-06-11 13:56:31 +00:00
|
|
|
return leadTargets, nil
|
2024-06-10 16:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AccountRepository) UpdateLeadTarget(ctx context.Context, req model.LeadTarget) (model.LeadTarget, error) {
|
2024-06-10 17:05:34 +00:00
|
|
|
row, err := r.queries.UpdateLeadTarget(ctx, sqlcgen.UpdateLeadTargetParams{
|
2024-07-11 13:32:42 +00:00
|
|
|
Invitelink: req.InviteLink,
|
|
|
|
Target: req.Target,
|
|
|
|
ID: req.ID,
|
2024-06-10 17:05:34 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-06-11 16:08:22 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return model.LeadTarget{}, pj_errors.ErrNotFound
|
|
|
|
}
|
|
|
|
return model.LeadTarget{}, err
|
2024-06-10 17:05:34 +00:00
|
|
|
}
|
2024-06-12 08:31:34 +00:00
|
|
|
|
|
|
|
var targetType model.LeadTargetType
|
|
|
|
v := string(row.Type.([]byte))
|
|
|
|
targetType = model.LeadTargetType(v)
|
|
|
|
|
2024-06-10 17:05:34 +00:00
|
|
|
return model.LeadTarget{
|
2024-07-11 13:32:42 +00:00
|
|
|
ID: row.ID,
|
|
|
|
AccountID: row.Accountid,
|
|
|
|
Type: targetType,
|
|
|
|
QuizID: row.Quizid,
|
|
|
|
Target: row.Target,
|
|
|
|
Deleted: row.Deleted,
|
|
|
|
CreatedAt: row.Createdat,
|
|
|
|
InviteLink: row.Invitelink,
|
2024-06-10 17:05:34 +00:00
|
|
|
}, nil
|
2024-06-10 16:56:29 +00:00
|
|
|
}
|