update db

This commit is contained in:
lafetz 2025-04-01 16:47:06 +03:00
parent ca7aa9d67c
commit e105716f50
12 changed files with 77 additions and 26 deletions

View File

@ -32,7 +32,6 @@ import (
// @name Authorization // @name Authorization
// @BasePath / // @BasePath /
func main() { func main() {
cfg, err := config.NewConfig() cfg, err := config.NewConfig()
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())

View File

@ -1,6 +1,6 @@
-- name: CreateOtp :exec -- name: CreateOtp :exec
INSERT INTO otps (sent_to, medium, otp_for, otp, used, created_at, expires_at) INSERT INTO otps (sent_to, medium, otp_for, otp, used, created_at, expires_at)
VALUES ($1, $2, $3, $4, FALSE, CURRENT_TIMESTAMP, $5); VALUES ($1, $2, $3, $4, FALSE, $5, $6);
-- name: GetOtp :one -- name: GetOtp :one
SELECT id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at SELECT id, sent_to, medium, otp_for, otp, used, used_at, created_at, expires_at
@ -10,5 +10,5 @@ ORDER BY created_at DESC LIMIT 1;
-- name: MarkOtpAsUsed :exec -- name: MarkOtpAsUsed :exec
UPDATE otps UPDATE otps
SET used = TRUE, used_at = CURRENT_TIMESTAMP SET used = TRUE, used_at = $2
WHERE id = $1; WHERE id = $1;

View File

@ -1,7 +1,7 @@
-- name: CreateUser :one -- name: CreateUser :one
INSERT INTO users (first_name, last_name, email, phone_number, role, password, email_verified, phone_verified, created_at, updated_at) INSERT INTO users (first_name, last_name, email, phone_number, role, password, email_verified, phone_verified, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, first_name, last_name, email, phone_number, role, email_verified, phone_verified, created_at, updated_at; RETURNING id, first_name, last_name, email, phone_number, role, email_verified, phone_verified, created_at, updated_at;
-- name: GetUserByID :one -- name: GetUserByID :one
@ -15,8 +15,8 @@ FROM users;
-- name: UpdateUser :exec -- name: UpdateUser :exec
UPDATE users UPDATE users
SET first_name = $1, last_name = $2, email = $3, phone_number = $4, role = $5, updated_at = CURRENT_TIMESTAMP SET first_name = $1, last_name = $2, email = $3, phone_number = $4, role = $5, updated_at = $6
WHERE id = $6; WHERE id = $7;
-- name: DeleteUser :exec -- name: DeleteUser :exec
DELETE FROM users DELETE FROM users
@ -38,5 +38,5 @@ WHERE phone_number = $1;
-- name: UpdatePassword :exec -- name: UpdatePassword :exec
UPDATE users UPDATE users
SET password = $1, updated_at = CURRENT_TIMESTAMP SET password = $1, updated_at = $4
WHERE (email = $2 OR phone_number = $3); WHERE (email = $2 OR phone_number = $3);

View File

@ -13,7 +13,7 @@ import (
const CreateOtp = `-- name: CreateOtp :exec const CreateOtp = `-- name: CreateOtp :exec
INSERT INTO otps (sent_to, medium, otp_for, otp, used, created_at, expires_at) INSERT INTO otps (sent_to, medium, otp_for, otp, used, created_at, expires_at)
VALUES ($1, $2, $3, $4, FALSE, CURRENT_TIMESTAMP, $5) VALUES ($1, $2, $3, $4, FALSE, $5, $6)
` `
type CreateOtpParams struct { type CreateOtpParams struct {
@ -21,6 +21,7 @@ type CreateOtpParams struct {
Medium string Medium string
OtpFor string OtpFor string
Otp string Otp string
CreatedAt pgtype.Timestamptz
ExpiresAt pgtype.Timestamptz ExpiresAt pgtype.Timestamptz
} }
@ -30,6 +31,7 @@ func (q *Queries) CreateOtp(ctx context.Context, arg CreateOtpParams) error {
arg.Medium, arg.Medium,
arg.OtpFor, arg.OtpFor,
arg.Otp, arg.Otp,
arg.CreatedAt,
arg.ExpiresAt, arg.ExpiresAt,
) )
return err return err
@ -67,11 +69,16 @@ func (q *Queries) GetOtp(ctx context.Context, arg GetOtpParams) (Otp, error) {
const MarkOtpAsUsed = `-- name: MarkOtpAsUsed :exec const MarkOtpAsUsed = `-- name: MarkOtpAsUsed :exec
UPDATE otps UPDATE otps
SET used = TRUE, used_at = CURRENT_TIMESTAMP SET used = TRUE, used_at = $2
WHERE id = $1 WHERE id = $1
` `
func (q *Queries) MarkOtpAsUsed(ctx context.Context, id int64) error { type MarkOtpAsUsedParams struct {
_, err := q.db.Exec(ctx, MarkOtpAsUsed, id) ID int64
UsedAt pgtype.Timestamptz
}
func (q *Queries) MarkOtpAsUsed(ctx context.Context, arg MarkOtpAsUsedParams) error {
_, err := q.db.Exec(ctx, MarkOtpAsUsed, arg.ID, arg.UsedAt)
return err return err
} }

View File

@ -37,7 +37,7 @@ func (q *Queries) CheckPhoneEmailExist(ctx context.Context, arg CheckPhoneEmailE
const CreateUser = `-- name: CreateUser :one const CreateUser = `-- name: CreateUser :one
INSERT INTO users (first_name, last_name, email, phone_number, role, password, email_verified, phone_verified, created_at, updated_at) INSERT INTO users (first_name, last_name, email, phone_number, role, password, email_verified, phone_verified, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id, first_name, last_name, email, phone_number, role, email_verified, phone_verified, created_at, updated_at RETURNING id, first_name, last_name, email, phone_number, role, email_verified, phone_verified, created_at, updated_at
` `
@ -50,6 +50,8 @@ type CreateUserParams struct {
Password []byte Password []byte
EmailVerified bool EmailVerified bool
PhoneVerified bool PhoneVerified bool
CreatedAt pgtype.Timestamptz
UpdatedAt pgtype.Timestamptz
} }
type CreateUserRow struct { type CreateUserRow struct {
@ -75,6 +77,8 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateU
arg.Password, arg.Password,
arg.EmailVerified, arg.EmailVerified,
arg.PhoneVerified, arg.PhoneVerified,
arg.CreatedAt,
arg.UpdatedAt,
) )
var i CreateUserRow var i CreateUserRow
err := row.Scan( err := row.Scan(
@ -254,7 +258,7 @@ func (q *Queries) GetUserByPhone(ctx context.Context, phoneNumber pgtype.Text) (
const UpdatePassword = `-- name: UpdatePassword :exec const UpdatePassword = `-- name: UpdatePassword :exec
UPDATE users UPDATE users
SET password = $1, updated_at = CURRENT_TIMESTAMP SET password = $1, updated_at = $4
WHERE (email = $2 OR phone_number = $3) WHERE (email = $2 OR phone_number = $3)
` `
@ -262,17 +266,23 @@ type UpdatePasswordParams struct {
Password []byte Password []byte
Email pgtype.Text Email pgtype.Text
PhoneNumber pgtype.Text PhoneNumber pgtype.Text
UpdatedAt pgtype.Timestamptz
} }
func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) error { func (q *Queries) UpdatePassword(ctx context.Context, arg UpdatePasswordParams) error {
_, err := q.db.Exec(ctx, UpdatePassword, arg.Password, arg.Email, arg.PhoneNumber) _, err := q.db.Exec(ctx, UpdatePassword,
arg.Password,
arg.Email,
arg.PhoneNumber,
arg.UpdatedAt,
)
return err return err
} }
const UpdateUser = `-- name: UpdateUser :exec const UpdateUser = `-- name: UpdateUser :exec
UPDATE users UPDATE users
SET first_name = $1, last_name = $2, email = $3, phone_number = $4, role = $5, updated_at = CURRENT_TIMESTAMP SET first_name = $1, last_name = $2, email = $3, phone_number = $4, role = $5, updated_at = $6
WHERE id = $6 WHERE id = $7
` `
type UpdateUserParams struct { type UpdateUserParams struct {
@ -281,6 +291,7 @@ type UpdateUserParams struct {
Email pgtype.Text Email pgtype.Text
PhoneNumber pgtype.Text PhoneNumber pgtype.Text
Role string Role string
UpdatedAt pgtype.Timestamptz
ID int64 ID int64
} }
@ -291,6 +302,7 @@ func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) error {
arg.Email, arg.Email,
arg.PhoneNumber, arg.PhoneNumber,
arg.Role, arg.Role,
arg.UpdatedAt,
arg.ID, arg.ID,
) )
return err return err

View File

@ -3,9 +3,9 @@ package domain
type Role string type Role string
const ( const (
RoleAdmin Role = "admin"
RoleCustomer Role = "customer"
RoleSuperAdmin Role = "super_admin" RoleSuperAdmin Role = "super_admin"
RoleAdmin Role = "admin"
RoleBranchManager Role = "branch_manager" RoleBranchManager Role = "branch_manager"
RoleCustomer Role = "customer"
RoleCashier Role = "cashier" RoleCashier Role = "cashier"
) )

View File

@ -19,6 +19,10 @@ func (s *Store) CreateOtp(ctx context.Context, otp domain.Otp) error {
Time: otp.ExpiresAt, Time: otp.ExpiresAt,
Valid: true, Valid: true,
}, },
CreatedAt: pgtype.Timestamptz{
Time: otp.CreatedAt,
Valid: true,
},
}) })
} }
func (s *Store) GetOtp(ctx context.Context, sentTo string, sentfor domain.OtpFor, medium domain.OtpMedium) (domain.Otp, error) { func (s *Store) GetOtp(ctx context.Context, sentTo string, sentfor domain.OtpFor, medium domain.OtpMedium) (domain.Otp, error) {
@ -46,5 +50,11 @@ func (s *Store) GetOtp(ctx context.Context, sentTo string, sentfor domain.OtpFor
}, nil }, nil
} }
func (s *Store) MarkOtpAsUsed(ctx context.Context, otp domain.Otp) error { func (s *Store) MarkOtpAsUsed(ctx context.Context, otp domain.Otp) error {
return s.queries.MarkOtpAsUsed(ctx, otp.ID) return s.queries.MarkOtpAsUsed(ctx, dbgen.MarkOtpAsUsedParams{
ID: otp.ID,
UsedAt: pgtype.Timestamptz{
Time: otp.UsedAt,
Valid: true,
},
})
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"errors" "errors"
"fmt" "time"
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db" dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain" "github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
@ -12,7 +12,13 @@ import (
) )
func (s *Store) CreateUser(ctx context.Context, user domain.User, usedOtpId int64) (domain.User, error) { func (s *Store) CreateUser(ctx context.Context, user domain.User, usedOtpId int64) (domain.User, error) {
err := s.queries.MarkOtpAsUsed(ctx, usedOtpId) err := s.queries.MarkOtpAsUsed(ctx, dbgen.MarkOtpAsUsedParams{
ID: usedOtpId,
UsedAt: pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
},
})
if err != nil { if err != nil {
return domain.User{}, err return domain.User{}, err
} }
@ -31,6 +37,14 @@ func (s *Store) CreateUser(ctx context.Context, user domain.User, usedOtpId int6
Role: string(user.Role), Role: string(user.Role),
EmailVerified: user.EmailVerified, EmailVerified: user.EmailVerified,
PhoneVerified: user.PhoneVerified, PhoneVerified: user.PhoneVerified,
CreatedAt: pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
},
UpdatedAt: pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
},
}) })
if err != nil { if err != nil {
return domain.User{}, err return domain.User{}, err
@ -93,6 +107,7 @@ func (s *Store) UpdateUser(ctx context.Context, user domain.UpdateUserReq) error
// LastName: user.LastName, // LastName: user.LastName,
// Email: user.Email, // Email: user.Email,
// PhoneNumber: user.PhoneNumber, // PhoneNumber: user.PhoneNumber,
}) })
if err != nil { if err != nil {
return err return err
@ -107,7 +122,7 @@ func (s *Store) DeleteUser(ctx context.Context, id int64) error {
return nil return nil
} }
func (s *Store) CheckPhoneEmailExist(ctx context.Context, phoneNum, email string) (bool, bool, error) { func (s *Store) CheckPhoneEmailExist(ctx context.Context, phoneNum, email string) (bool, bool, error) {
fmt.Printf("phoneNum: %s, email: %s\n", phoneNum, email)
row, err := s.queries.CheckPhoneEmailExist(ctx, dbgen.CheckPhoneEmailExistParams{ row, err := s.queries.CheckPhoneEmailExist(ctx, dbgen.CheckPhoneEmailExistParams{
PhoneNumber: pgtype.Text{ PhoneNumber: pgtype.Text{
String: phoneNum, String: phoneNum,
@ -119,7 +134,7 @@ func (s *Store) CheckPhoneEmailExist(ctx context.Context, phoneNum, email string
Valid: email != "", Valid: email != "",
}, },
}) })
fmt.Printf("row: %+v\n", row)
if err != nil { if err != nil {
return false, false, err return false, false, err
} }
@ -168,7 +183,13 @@ func (s *Store) GetUserByPhone(ctx context.Context, phoneNum string) (domain.Use
} }
func (s *Store) UpdatePassword(ctx context.Context, identifier string, password []byte, usedOtpId int64) error { func (s *Store) UpdatePassword(ctx context.Context, identifier string, password []byte, usedOtpId int64) error {
err := s.queries.MarkOtpAsUsed(ctx, usedOtpId) err := s.queries.MarkOtpAsUsed(ctx, dbgen.MarkOtpAsUsedParams{
ID: usedOtpId,
UsedAt: pgtype.Timestamptz{
Time: time.Now(),
Valid: true,
},
})
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1 @@
package sportsbook

View File

@ -0,0 +1 @@
package sportsbook

View File

@ -0,0 +1 @@
package sportsbook

View File

@ -2,7 +2,6 @@ package user
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain" "github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
@ -31,7 +30,7 @@ func (s *Service) SendRegisterCode(ctx context.Context, medium domain.OtpMedium,
} }
func (s *Service) RegisterUser(ctx context.Context, registerReq domain.RegisterUserReq) (domain.User, error) { // normal func (s *Service) RegisterUser(ctx context.Context, registerReq domain.RegisterUserReq) (domain.User, error) { // normal
// get otp // get otp
fmt.Printf("registerReq: %+v\n", registerReq)
var sentTo string var sentTo string
if registerReq.OtpMedium == domain.OtpMediumEmail { if registerReq.OtpMedium == domain.OtpMediumEmail {
sentTo = registerReq.Email sentTo = registerReq.Email