users_service_subscirber.go 2.2 KB
package subscriber

import (
	"github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
	domainx "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain/event"
	"time"
)

type UsersServiceSubscriber struct {
	TransactionContext *pgTransaction.TransactionContext
}

func (subscriber *UsersServiceSubscriber) HandleEvent(domainEvent domain.DomainEvent) error {
	userCreateService, _ := factory.CreateUserCreateService(map[string]interface{}{
		"transactionContext": subscriber.TransactionContext,
	})
	switch domainEvent.EventType() {
	case event.CompanyCreatedEvent:
		companyCreatedEvent := domainEvent.(*event.CompanyCreated)
		admin := companyCreatedEvent.Company.CompanyInfo.Admin
		if _, err := userCreateService.CreateUser(companyCreatedEvent.OperatorId, &domainx.User{
			UserType:  domainx.Manager,
			CompanyId: companyCreatedEvent.Company.CompanyId,
			UserInfo: &domainx.UserInfo{
				IsPrincipal: true,
				UserAccount: admin.UserAccount,
				UserName:    admin.UserName,
			},
			AdminType:   domainx.EnterpriseAdministrator,
			PartnerInfo: &domainx.PartnerInfo{},
			Status:      domainx.StatusEnable,
			CreateAt:    time.Now(),
		}); err != nil {
			return err
		}
	case event.CompanyAdminAssignedEvent:
		companyAdminAssigned := domainEvent.(*event.CompanyAdminAssigned)
		admin := companyAdminAssigned.Admin
		if _, err := userCreateService.CreateUser(companyAdminAssigned.OperatorId, &domainx.User{
			UserType:  domainx.Manager,
			CompanyId: companyAdminAssigned.CompanyId,
			UserInfo: &domainx.UserInfo{
				IsPrincipal: true,
				UserAccount: admin.UserAccount,
				UserName:    admin.UserName,
			},
			AdminType:   domainx.EnterpriseAdministrator,
			PartnerInfo: &domainx.PartnerInfo{},
			Status:      domainx.StatusEnable,
			CreateAt:    time.Now(),
		}); err != nil {
			return err
		}
	case event.UsersCreatedEvent:

	}
	return nil
}

func (subscriber *UsersServiceSubscriber) SubscribedToEventTypes() []string {
	return []string{
		event.CompanyAdminAssignedEvent,
		event.CompanyCreatedEvent,
	}
}