102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package rechargeagency
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"chatapp3-golang/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// GetProfile returns the current recharge agency seller extension profile.
|
|
func (s *Service) GetProfile(ctx context.Context, user AuthUser) (*SellerProfileResponse, error) {
|
|
sysOrigin, userID, err := normalizeSellerProfileUser(user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if s.db == nil {
|
|
return nil, NewAppError(http.StatusServiceUnavailable, "database_unavailable", "database is unavailable")
|
|
}
|
|
|
|
var row model.RechargeAgencySellerInfo
|
|
err = s.db.WithContext(ctx).
|
|
Where("sys_origin = ? AND user_id = ?", sysOrigin, userID).
|
|
First(&row).Error
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return &SellerProfileResponse{
|
|
SysOrigin: sysOrigin,
|
|
UserID: userID,
|
|
Whatsapp: "",
|
|
}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, NewAppError(http.StatusInternalServerError, "seller_profile_query_failed", err.Error())
|
|
}
|
|
|
|
return &SellerProfileResponse{
|
|
SysOrigin: sysOrigin,
|
|
UserID: userID,
|
|
Whatsapp: strings.TrimSpace(row.Whatsapp),
|
|
}, nil
|
|
}
|
|
|
|
// UpdateWhatsapp stores the current recharge agency seller WhatsApp contact.
|
|
func (s *Service) UpdateWhatsapp(ctx context.Context, user AuthUser, req UpdateWhatsappRequest) (*SellerProfileResponse, error) {
|
|
sysOrigin, userID, err := normalizeSellerProfileUser(user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if s.db == nil {
|
|
return nil, NewAppError(http.StatusServiceUnavailable, "database_unavailable", "database is unavailable")
|
|
}
|
|
|
|
whatsapp := strings.TrimSpace(req.Whatsapp)
|
|
if whatsapp == "" {
|
|
return nil, NewAppError(http.StatusBadRequest, "whatsapp_required", "whatsapp is required")
|
|
}
|
|
if len([]rune(whatsapp)) > 64 {
|
|
return nil, NewAppError(http.StatusBadRequest, "whatsapp_too_long", "whatsapp cannot exceed 64 characters")
|
|
}
|
|
|
|
now := time.Now()
|
|
row := model.RechargeAgencySellerInfo{
|
|
SysOrigin: sysOrigin,
|
|
UserID: userID,
|
|
Whatsapp: whatsapp,
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}
|
|
err = s.db.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "sys_origin"}, {Name: "user_id"}},
|
|
DoUpdates: clause.Assignments(map[string]any{
|
|
"whatsapp": whatsapp,
|
|
"update_time": now,
|
|
}),
|
|
}).Create(&row).Error
|
|
if err != nil {
|
|
return nil, NewAppError(http.StatusInternalServerError, "seller_profile_save_failed", err.Error())
|
|
}
|
|
|
|
return &SellerProfileResponse{
|
|
SysOrigin: sysOrigin,
|
|
UserID: userID,
|
|
Whatsapp: whatsapp,
|
|
}, nil
|
|
}
|
|
|
|
func normalizeSellerProfileUser(user AuthUser) (string, int64, error) {
|
|
sysOrigin := normalizeSysOrigin(user.SysOrigin)
|
|
if sysOrigin == "" {
|
|
return "", 0, NewAppError(http.StatusUnauthorized, "invalid_sys_origin", "authenticated sysOrigin is required")
|
|
}
|
|
if user.UserID <= 0 {
|
|
return "", 0, NewAppError(http.StatusUnauthorized, "invalid_user", "authenticated user is required")
|
|
}
|
|
return sysOrigin, user.UserID, nil
|
|
}
|