user auth register
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"apskel-pos-be/internal/entities"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CustomerAuthRepository interface {
|
||||
GetCustomerByPhoneNumber(ctx context.Context, phoneNumber string) (*entities.Customer, error)
|
||||
GetCustomerByID(ctx context.Context, id string) (*entities.Customer, error)
|
||||
CreateCustomer(ctx context.Context, customer *entities.Customer) error
|
||||
UpdateCustomer(ctx context.Context, customer *entities.Customer) error
|
||||
CheckPhoneNumberExists(ctx context.Context, phoneNumber string) (bool, error)
|
||||
SetCustomerPassword(ctx context.Context, customerID string, passwordHash string) error
|
||||
}
|
||||
|
||||
type customerAuthRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewCustomerAuthRepository(db *gorm.DB) CustomerAuthRepository {
|
||||
return &customerAuthRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) GetCustomerByPhoneNumber(ctx context.Context, phoneNumber string) (*entities.Customer, error) {
|
||||
var customer entities.Customer
|
||||
if err := r.db.WithContext(ctx).Where("phone_number = ?", phoneNumber).First(&customer).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil // Customer not found, not an error
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get customer by phone number: %w", err)
|
||||
}
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) GetCustomerByID(ctx context.Context, id string) (*entities.Customer, error) {
|
||||
var customer entities.Customer
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&customer).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, fmt.Errorf("customer not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get customer by ID: %w", err)
|
||||
}
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) CreateCustomer(ctx context.Context, customer *entities.Customer) error {
|
||||
if err := r.db.WithContext(ctx).Create(customer).Error; err != nil {
|
||||
return fmt.Errorf("failed to create customer: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) UpdateCustomer(ctx context.Context, customer *entities.Customer) error {
|
||||
if err := r.db.WithContext(ctx).Save(customer).Error; err != nil {
|
||||
return fmt.Errorf("failed to update customer: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) CheckPhoneNumberExists(ctx context.Context, phoneNumber string) (bool, error) {
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).Model(&entities.Customer{}).Where("phone_number = ?", phoneNumber).Count(&count).Error; err != nil {
|
||||
return false, fmt.Errorf("failed to check phone number existence: %w", err)
|
||||
}
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (r *customerAuthRepository) SetCustomerPassword(ctx context.Context, customerID string, passwordHash string) error {
|
||||
if err := r.db.WithContext(ctx).Model(&entities.Customer{}).Where("id = ?", customerID).Update("password_hash", passwordHash).Error; err != nil {
|
||||
return fmt.Errorf("failed to set customer password: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"apskel-pos-be/internal/entities"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type OtpRepository interface {
|
||||
CreateOtpSession(ctx context.Context, otpSession *entities.OtpSession) error
|
||||
GetOtpSessionByToken(ctx context.Context, token string) (*entities.OtpSession, error)
|
||||
GetOtpSessionByPhoneAndPurpose(ctx context.Context, phoneNumber string, purpose string) (*entities.OtpSession, error)
|
||||
GetLastOtpSessionByPhoneAndPurpose(ctx context.Context, phoneNumber string, purpose string) (*entities.OtpSession, error)
|
||||
UpdateOtpSession(ctx context.Context, otpSession *entities.OtpSession) error
|
||||
DeleteOtpSession(ctx context.Context, token string) error
|
||||
DeleteExpiredOtpSessions(ctx context.Context) error
|
||||
InvalidateOtpSessionsByPhone(ctx context.Context, phoneNumber string, purpose string) error
|
||||
}
|
||||
|
||||
type otpRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewOtpRepository(db *gorm.DB) OtpRepository {
|
||||
return &otpRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *otpRepository) CreateOtpSession(ctx context.Context, otpSession *entities.OtpSession) error {
|
||||
if err := r.db.WithContext(ctx).Create(otpSession).Error; err != nil {
|
||||
return fmt.Errorf("failed to create OTP session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) GetOtpSessionByToken(ctx context.Context, token string) (*entities.OtpSession, error) {
|
||||
var otpSession entities.OtpSession
|
||||
if err := r.db.WithContext(ctx).Where("token = ?", token).First(&otpSession).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil // OTP session not found, not an error
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get OTP session by token: %w", err)
|
||||
}
|
||||
return &otpSession, nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) GetOtpSessionByPhoneAndPurpose(ctx context.Context, phoneNumber string, purpose string) (*entities.OtpSession, error) {
|
||||
var otpSession entities.OtpSession
|
||||
if err := r.db.WithContext(ctx).Where("phone_number = ? AND purpose = ? AND is_used = false AND expires_at > ?",
|
||||
phoneNumber, purpose, time.Now()).Order("created_at DESC").First(&otpSession).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil // No active OTP session found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get OTP session by phone and purpose: %w", err)
|
||||
}
|
||||
return &otpSession, nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) GetLastOtpSessionByPhoneAndPurpose(ctx context.Context, phoneNumber string, purpose string) (*entities.OtpSession, error) {
|
||||
var otpSession entities.OtpSession
|
||||
if err := r.db.WithContext(ctx).Where("phone_number = ? AND purpose = ?",
|
||||
phoneNumber, purpose).Order("created_at DESC").First(&otpSession).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil // No OTP session found
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get last OTP session by phone and purpose: %w", err)
|
||||
}
|
||||
return &otpSession, nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) UpdateOtpSession(ctx context.Context, otpSession *entities.OtpSession) error {
|
||||
if err := r.db.WithContext(ctx).Save(otpSession).Error; err != nil {
|
||||
return fmt.Errorf("failed to update OTP session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) DeleteOtpSession(ctx context.Context, token string) error {
|
||||
if err := r.db.WithContext(ctx).Where("token = ?", token).Delete(&entities.OtpSession{}).Error; err != nil {
|
||||
return fmt.Errorf("failed to delete OTP session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) DeleteExpiredOtpSessions(ctx context.Context) error {
|
||||
if err := r.db.WithContext(ctx).Where("expires_at < ?", time.Now()).Delete(&entities.OtpSession{}).Error; err != nil {
|
||||
return fmt.Errorf("failed to delete expired OTP sessions: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *otpRepository) InvalidateOtpSessionsByPhone(ctx context.Context, phoneNumber string, purpose string) error {
|
||||
if err := r.db.WithContext(ctx).Model(&entities.OtpSession{}).Where("phone_number = ? AND purpose = ? AND is_used = false",
|
||||
phoneNumber, purpose).Update("is_used", true).Error; err != nil {
|
||||
return fmt.Errorf("failed to invalidate OTP sessions: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user