feat: implement EnsureExternalCustomer method in CustomerService and refactor customer creation logic
This commit is contained in:
@@ -113,7 +113,7 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha
|
|||||||
var conversation *models.Conversation
|
var conversation *models.Conversation
|
||||||
created := false
|
created := false
|
||||||
if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
||||||
customerID, err := s.ensureExternalCustomer(ctx.Tx, externalInfo)
|
customerID, err := CustomerService.EnsureExternalCustomer(ctx.Tx, externalInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -170,43 +170,6 @@ func (s *conversationService) Create(externalInfo openidentity.ExternalInfo, cha
|
|||||||
return s.Get(conversation.ID), nil
|
return s.Get(conversation.ID), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *conversationService) ensureExternalCustomer(db *gorm.DB, externalInfo openidentity.ExternalInfo) (int64, error) {
|
|
||||||
externalSource := externalInfo.ExternalSource
|
|
||||||
externalID := strings.TrimSpace(externalInfo.ExternalID)
|
|
||||||
if strings.TrimSpace(string(externalSource)) == "" || externalID == "" {
|
|
||||||
return 0, errorsx.Unauthorized("外部用户标识不能为空")
|
|
||||||
}
|
|
||||||
now := time.Now()
|
|
||||||
if identity := repositories.CustomerIdentityRepository.GetBy(db, externalSource, externalID); identity != nil {
|
|
||||||
_ = repositories.CustomerRepository.Updates(db, identity.CustomerID, map[string]any{
|
|
||||||
"last_active_at": now,
|
|
||||||
"updated_at": now,
|
|
||||||
})
|
|
||||||
return identity.CustomerID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
customer := &models.Customer{
|
|
||||||
Name: s.buildDefaultSubject(externalInfo),
|
|
||||||
LastActiveAt: &now,
|
|
||||||
Status: enums.StatusOk,
|
|
||||||
AuditFields: utils.BuildAuditFields(nil),
|
|
||||||
}
|
|
||||||
if err := repositories.CustomerRepository.Create(db, customer); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
identity := &models.CustomerIdentity{
|
|
||||||
CustomerID: customer.ID,
|
|
||||||
ExternalSource: externalSource,
|
|
||||||
ExternalID: externalID,
|
|
||||||
Status: enums.StatusOk,
|
|
||||||
AuditFields: utils.BuildAuditFields(nil),
|
|
||||||
}
|
|
||||||
if err := repositories.CustomerIdentityRepository.Create(db, identity); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return customer.ID, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *conversationService) AssignConversation(req request.AssignConversationRequest, operator *dto.AuthPrincipal) error {
|
func (s *conversationService) AssignConversation(req request.AssignConversationRequest, operator *dto.AuthPrincipal) error {
|
||||||
if operator == nil {
|
if operator == nil {
|
||||||
return errorsx.Unauthorized("未登录或登录已过期")
|
return errorsx.Unauthorized("未登录或登录已过期")
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"cs-agent/internal/pkg/dto/request"
|
"cs-agent/internal/pkg/dto/request"
|
||||||
"cs-agent/internal/pkg/enums"
|
"cs-agent/internal/pkg/enums"
|
||||||
"cs-agent/internal/pkg/errorsx"
|
"cs-agent/internal/pkg/errorsx"
|
||||||
|
"cs-agent/internal/pkg/openidentity"
|
||||||
"cs-agent/internal/pkg/utils"
|
"cs-agent/internal/pkg/utils"
|
||||||
"cs-agent/internal/repositories"
|
"cs-agent/internal/repositories"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -110,6 +111,49 @@ func (s *customerService) CountByCompanyIDs(companyIDs []int64) map[int64]int64
|
|||||||
return repositories.CustomerRepository.CountByCompanyIDs(sqls.DB(), companyIDs, int(enums.StatusDeleted))
|
return repositories.CustomerRepository.CountByCompanyIDs(sqls.DB(), companyIDs, int(enums.StatusDeleted))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *customerService) EnsureExternalCustomer(db *gorm.DB, externalInfo openidentity.ExternalInfo) (int64, error) {
|
||||||
|
externalSource := externalInfo.ExternalSource
|
||||||
|
externalID := strings.TrimSpace(externalInfo.ExternalID)
|
||||||
|
if strings.TrimSpace(string(externalSource)) == "" || externalID == "" {
|
||||||
|
return 0, errorsx.Unauthorized("外部用户标识不能为空")
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
if identity := repositories.CustomerIdentityRepository.GetBy(db, externalSource, externalID); identity != nil {
|
||||||
|
_ = repositories.CustomerRepository.Updates(db, identity.CustomerID, map[string]any{
|
||||||
|
"last_active_at": now,
|
||||||
|
"updated_at": now,
|
||||||
|
})
|
||||||
|
return identity.CustomerID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
customer := &models.Customer{
|
||||||
|
Name: buildExternalCustomerName(externalInfo),
|
||||||
|
LastActiveAt: &now,
|
||||||
|
Status: enums.StatusOk,
|
||||||
|
AuditFields: utils.BuildAuditFields(nil),
|
||||||
|
}
|
||||||
|
if err := repositories.CustomerRepository.Create(db, customer); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if err := repositories.CustomerIdentityRepository.Create(db, &models.CustomerIdentity{
|
||||||
|
CustomerID: customer.ID,
|
||||||
|
ExternalSource: externalSource,
|
||||||
|
ExternalID: externalID,
|
||||||
|
Status: enums.StatusOk,
|
||||||
|
AuditFields: utils.BuildAuditFields(nil),
|
||||||
|
}); err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return customer.ID, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildExternalCustomerName(externalInfo openidentity.ExternalInfo) string {
|
||||||
|
if strs.IsNotBlank(externalInfo.ExternalName) {
|
||||||
|
return externalInfo.ExternalName
|
||||||
|
}
|
||||||
|
return "访客" + hashUUID(externalInfo.ExternalID)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *customerService) CreateCustomer(req request.CreateCustomerRequest, operator *dto.AuthPrincipal) (*models.Customer, error) {
|
func (s *customerService) CreateCustomer(req request.CreateCustomerRequest, operator *dto.AuthPrincipal) (*models.Customer, error) {
|
||||||
if operator == nil {
|
if operator == nil {
|
||||||
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
return nil, errorsx.Unauthorized("未登录或登录已过期")
|
||||||
|
|||||||
Reference in New Issue
Block a user