127 lines
4.0 KiB
Go
127 lines
4.0 KiB
Go
|
|
package service
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"apskel-pos-be/internal/contract"
|
||
|
|
"apskel-pos-be/internal/models"
|
||
|
|
"apskel-pos-be/internal/processor"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CustomerAuthService interface {
|
||
|
|
CheckPhoneNumber(ctx context.Context, req *contract.CheckPhoneRequest) (*models.CheckPhoneResponse, error)
|
||
|
|
StartRegistration(ctx context.Context, req *contract.RegisterStartRequest) (*models.RegisterStartResponse, error)
|
||
|
|
VerifyOtp(ctx context.Context, req *contract.RegisterVerifyOtpRequest) (*models.RegisterVerifyOtpResponse, error)
|
||
|
|
SetPassword(ctx context.Context, req *contract.RegisterSetPasswordRequest) (*models.RegisterSetPasswordResponse, error)
|
||
|
|
Login(ctx context.Context, req *contract.CustomerLoginRequest) (*models.CustomerLoginResponse, error)
|
||
|
|
ResendOtp(ctx context.Context, req *contract.ResendOtpRequest) (*models.ResendOtpResponse, error)
|
||
|
|
}
|
||
|
|
|
||
|
|
type customerAuthService struct {
|
||
|
|
customerAuthProcessor processor.CustomerAuthProcessor
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewCustomerAuthService(customerAuthProcessor processor.CustomerAuthProcessor) CustomerAuthService {
|
||
|
|
return &customerAuthService{
|
||
|
|
customerAuthProcessor: customerAuthProcessor,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) CheckPhoneNumber(ctx context.Context, req *contract.CheckPhoneRequest) (*models.CheckPhoneResponse, error) {
|
||
|
|
// Validate request
|
||
|
|
if req.PhoneNumber == "" {
|
||
|
|
return nil, fmt.Errorf("phone number is required")
|
||
|
|
}
|
||
|
|
|
||
|
|
response, err := s.customerAuthProcessor.CheckPhoneNumber(ctx, req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to check phone number: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) StartRegistration(ctx context.Context, req *contract.RegisterStartRequest) (*models.RegisterStartResponse, error) {
|
||
|
|
// Validate request
|
||
|
|
if req.PhoneNumber == "" {
|
||
|
|
return nil, fmt.Errorf("phone number is required")
|
||
|
|
}
|
||
|
|
if req.Name == "" {
|
||
|
|
return nil, fmt.Errorf("name is required")
|
||
|
|
}
|
||
|
|
if req.BirthDate == "" {
|
||
|
|
return nil, fmt.Errorf("birth date is required")
|
||
|
|
}
|
||
|
|
|
||
|
|
response, err := s.customerAuthProcessor.StartRegistration(ctx, req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to start registration: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) VerifyOtp(ctx context.Context, req *contract.RegisterVerifyOtpRequest) (*models.RegisterVerifyOtpResponse, error) {
|
||
|
|
// Validate request
|
||
|
|
if req.RegistrationToken == "" {
|
||
|
|
return nil, fmt.Errorf("registration token is required")
|
||
|
|
}
|
||
|
|
if req.OtpCode == "" {
|
||
|
|
return nil, fmt.Errorf("OTP code is required")
|
||
|
|
}
|
||
|
|
|
||
|
|
response, err := s.customerAuthProcessor.VerifyOtp(ctx, req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to verify OTP: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) SetPassword(ctx context.Context, req *contract.RegisterSetPasswordRequest) (*models.RegisterSetPasswordResponse, error) {
|
||
|
|
// Validate request
|
||
|
|
if req.RegistrationToken == "" {
|
||
|
|
return nil, fmt.Errorf("registration token is required")
|
||
|
|
}
|
||
|
|
if req.Password == "" {
|
||
|
|
return nil, fmt.Errorf("password is required")
|
||
|
|
}
|
||
|
|
if req.ConfirmPassword == "" {
|
||
|
|
return nil, fmt.Errorf("confirm password is required")
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validate password strength
|
||
|
|
if len(req.Password) < 8 {
|
||
|
|
return nil, fmt.Errorf("password must be at least 8 characters long")
|
||
|
|
}
|
||
|
|
|
||
|
|
response, err := s.customerAuthProcessor.SetPassword(ctx, req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to set password: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) Login(ctx context.Context, req *contract.CustomerLoginRequest) (*models.CustomerLoginResponse, error) {
|
||
|
|
// Validate request
|
||
|
|
if req.PhoneNumber == "" {
|
||
|
|
return nil, fmt.Errorf("phone number is required")
|
||
|
|
}
|
||
|
|
if req.Password == "" {
|
||
|
|
return nil, fmt.Errorf("password is required")
|
||
|
|
}
|
||
|
|
|
||
|
|
response, err := s.customerAuthProcessor.Login(ctx, req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to login: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return response, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *customerAuthService) ResendOtp(ctx context.Context, req *contract.ResendOtpRequest) (*models.ResendOtpResponse, error) {
|
||
|
|
return s.customerAuthProcessor.ResendOtp(ctx, req)
|
||
|
|
}
|