| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  | package services | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2025-09-12 01:15:09 +08:00
										 |  |  | 	"tyapi-server/internal/config" | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  | 	"tyapi-server/internal/domains/api/entities" | 
					
						
							|  |  |  | 	repo "tyapi-server/internal/domains/api/repositories" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ApiUserAggregateService interface { | 
					
						
							|  |  |  | 	CreateApiUser(ctx context.Context, apiUserId string) error | 
					
						
							|  |  |  | 	UpdateWhiteList(ctx context.Context, apiUserId string, whiteList []string) error | 
					
						
							|  |  |  | 	AddToWhiteList(ctx context.Context, apiUserId string, entry string) error | 
					
						
							|  |  |  | 	RemoveFromWhiteList(ctx context.Context, apiUserId string, entry string) error | 
					
						
							|  |  |  | 	FreezeApiUser(ctx context.Context, apiUserId string) error | 
					
						
							|  |  |  | 	UnfreezeApiUser(ctx context.Context, apiUserId string) error | 
					
						
							|  |  |  | 	LoadApiUserByUserId(ctx context.Context, apiUserId string) (*entities.ApiUser, error) | 
					
						
							|  |  |  | 	LoadApiUserByAccessId(ctx context.Context, accessId string) (*entities.ApiUser, error) | 
					
						
							|  |  |  | 	SaveApiUser(ctx context.Context, apiUser *entities.ApiUser) error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type ApiUserAggregateServiceImpl struct { | 
					
						
							|  |  |  | 	repo repo.ApiUserRepository | 
					
						
							| 
									
										
										
										
											2025-09-12 01:15:09 +08:00
										 |  |  | 	cfg  *config.Config | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-12 01:15:09 +08:00
										 |  |  | func NewApiUserAggregateService(repo repo.ApiUserRepository, cfg *config.Config) ApiUserAggregateService { | 
					
						
							|  |  |  | 	return &ApiUserAggregateServiceImpl{repo: repo, cfg: cfg} | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) CreateApiUser(ctx context.Context, apiUserId string) error { | 
					
						
							| 
									
										
										
										
											2025-09-12 01:15:09 +08:00
										 |  |  | 	apiUser, err := entities.NewApiUser(apiUserId, s.cfg.Wallet.BalanceAlert.DefaultEnabled, s.cfg.Wallet.BalanceAlert.DefaultThreshold) | 
					
						
							| 
									
										
										
										
											2025-07-28 01:46:39 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := apiUser.Validate(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.repo.Create(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) UpdateWhiteList(ctx context.Context, apiUserId string, whiteList []string) error { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiUser.UpdateWhiteList(whiteList) | 
					
						
							|  |  |  | 	return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) AddToWhiteList(ctx context.Context, apiUserId string, entry string) error { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = apiUser.AddToWhiteList(entry) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) RemoveFromWhiteList(ctx context.Context, apiUserId string, entry string) error { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err = apiUser.RemoveFromWhiteList(entry) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) FreezeApiUser(ctx context.Context, apiUserId string) error { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiUser.Freeze() | 
					
						
							|  |  |  | 	return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) UnfreezeApiUser(ctx context.Context, apiUserId string) error { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	apiUser.Unfreeze() | 
					
						
							|  |  |  | 	return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) LoadApiUserByAccessId(ctx context.Context, accessId string) (*entities.ApiUser, error) { | 
					
						
							|  |  |  | 	return s.repo.FindByAccessId(ctx, accessId) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) LoadApiUserByUserId(ctx context.Context, apiUserId string) (*entities.ApiUser, error) { | 
					
						
							|  |  |  | 	apiUser, err := s.repo.FindByUserId(ctx, apiUserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// 确保WhiteList不为nil | 
					
						
							|  |  |  | 	if apiUser.WhiteList == nil { | 
					
						
							|  |  |  | 		apiUser.WhiteList = []string{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return apiUser, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *ApiUserAggregateServiceImpl) SaveApiUser(ctx context.Context, apiUser *entities.ApiUser) error { | 
					
						
							|  |  |  | 	exists, err := s.repo.FindByUserId(ctx, apiUser.UserId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if exists != nil { | 
					
						
							|  |  |  | 		// 确保WhiteList不为nil | 
					
						
							|  |  |  | 		if apiUser.WhiteList == nil { | 
					
						
							|  |  |  | 			apiUser.WhiteList = []string{} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return s.repo.Update(ctx, apiUser) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return s.repo.Create(ctx, apiUser) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |