Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor fix: 조직 생성시 전송하는 email contents 수정 #60

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions internal/aws/ses/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var Client *awsSes.Client

const (
senderEmailAddress = "[email protected]"

thanksContent = "TKS Cloud Service를 이용해 주셔서 감사합니다.\nTKS Cloud Service Team 드림"
)

func Initialize() error {
Expand Down Expand Up @@ -55,7 +57,7 @@ func SendEmailForVerityIdentity(client *awsSes.Client, targetEmailAddress string
subject := "[TKS][인증번호:" + code + "] – 요청하신 인증번호를 알려드립니다."
body := "아래의 인증번호를 인증번호 입력창에 입력해 주세요.\n\n" +
"인증번호: " + code + "\n\n" +
"TKS를 이용해 주셔서 감사합니다.\nTKS Team 드림"
thanksContent

input := &awsSes.SendEmailInput{
Destination: &types.Destination{
Expand Down Expand Up @@ -87,7 +89,44 @@ func SendEmailForTemporaryPassword(client *awsSes.Client, targetEmailAddress str
body := "임시 비밀번호가 발급되었습니다.\n" +
"로그인 후 비밀번호를 변경하여 사용하십시오.\n\n" +
"임시 비밀번호: " + randomPassword + "\n\n" +
"TKS를 이용해 주셔서 감사합니다.\nTKS Team 드림"
thanksContent

input := &awsSes.SendEmailInput{
Destination: &types.Destination{
ToAddresses: []string{targetEmailAddress},
},
Message: &types.Message{
Body: &types.Body{
Text: &types.Content{
Data: aws.String(body),
},
},
Subject: &types.Content{
Data: aws.String(subject),
},
},
Source: aws.String(senderEmailAddress),
}

if _, err := client.SendEmail(context.Background(), input); err != nil {
log.Errorf("failed to send email, %v", err)
return err
}

return nil
}

func SendEmailForGeneratingOrganization(client *awsSes.Client, organizationId string, organizationName string,
targetEmailAddress string, userAccountId string, randomPassword string) error {
subject := "[TKS] 조직이 생성되었습니다."
body := "조직이 생성되었습니다. \n" +
"조직 ID: " + organizationId +
"조직 이름: " + organizationName +
"아래 관리자 계정 정보로 로그인 후 사용바랍니다.\n" +
"관리자 ID: " + userAccountId + "\n" +
"관리자 이름: admin\n" +
"비밀번호: " + randomPassword + "\n\n" +
thanksContent

input := &awsSes.SendEmailInput{
Destination: &types.Destination{
Expand Down
80 changes: 43 additions & 37 deletions internal/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ type IUserUsecase interface {
}

type UserUsecase struct {
repo repository.IUserRepository
kc keycloak.IKeycloak
userRepository repository.IUserRepository
organizationRepository repository.IOrganizationRepository
kc keycloak.IKeycloak
}

func (u *UserUsecase) RenewalPasswordExpiredTime(ctx context.Context, userId uuid.UUID) error {
user, err := u.repo.GetByUuid(userId)
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status != http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "", "")
}
return httpErrors.NewInternalServerError(err, "", "")
}

err = u.repo.UpdatePassword(userId, user.Organization.ID, user.Password, false)
err = u.userRepository.UpdatePassword(userId, user.Organization.ID, user.Password, false)
if err != nil {
log.Errorf("failed to update password expired time: %v", err)
return httpErrors.NewInternalServerError(err, "", "")
Expand All @@ -68,7 +69,7 @@ func (u *UserUsecase) RenewalPasswordExpiredTime(ctx context.Context, userId uui
}

func (u *UserUsecase) RenewalPasswordExpiredTimeByAccountId(ctx context.Context, accountId string, organizationId string) error {
user, err := u.repo.Get(accountId, organizationId)
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status != http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "", "")
Expand All @@ -83,7 +84,7 @@ func (u *UserUsecase) RenewalPasswordExpiredTimeByAccountId(ctx context.Context,
}

func (u *UserUsecase) ResetPassword(userId uuid.UUID) error {
user, err := u.repo.GetByUuid(userId)
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "", "")
Expand Down Expand Up @@ -112,7 +113,7 @@ func (u *UserUsecase) ResetPassword(userId uuid.UUID) error {
if user.Password, err = helper.HashPassword(randomPassword); err != nil {
return httpErrors.NewInternalServerError(err, "", "")
}
if err = u.repo.UpdatePassword(userId, user.Organization.ID, user.Password, true); err != nil {
if err = u.userRepository.UpdatePassword(userId, user.Organization.ID, user.Password, true); err != nil {
return httpErrors.NewInternalServerError(err, "", "")
}

Expand All @@ -124,7 +125,7 @@ func (u *UserUsecase) ResetPassword(userId uuid.UUID) error {
}

func (u *UserUsecase) ResetPasswordByAccountId(accountId string, organizationId string) error {
user, err := u.repo.Get(accountId, organizationId)
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "", "")
Expand All @@ -139,7 +140,7 @@ func (u *UserUsecase) ResetPasswordByAccountId(accountId string, organizationId
}

func (u *UserUsecase) ValidateAccount(userId uuid.UUID, password string, organizationId string) error {
user, err := u.repo.GetByUuid(userId)
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
return err
}
Expand All @@ -156,7 +157,7 @@ func (u *UserUsecase) DeleteAll(ctx context.Context, organizationId string) erro
// TODO: implement me as transaction
// TODO: clean users in keycloak

err := u.repo.Flush(organizationId)
err := u.userRepository.Flush(organizationId)
if err != nil {
return err
}
Expand All @@ -180,7 +181,7 @@ func (u *UserUsecase) DeleteAdmin(organizationId string) error {
return errors.Wrap(err, "parse user id failed")
}

err = u.repo.DeleteWithUuid(userUuid)
err = u.userRepository.DeleteWithUuid(userUuid)
if err != nil {
return errors.Wrap(err, "delete user failed")
}
Expand Down Expand Up @@ -235,7 +236,7 @@ func (u *UserUsecase) CreateAdmin(orgainzationId string, email string) (*domain.
return nil, err
}

roles, err := u.repo.FetchRoles()
roles, err := u.userRepository.FetchRoles()
if err != nil {
return nil, err
}
Expand All @@ -248,13 +249,17 @@ func (u *UserUsecase) CreateAdmin(orgainzationId string, email string) (*domain.
if err != nil {
return nil, err
}
resUser, err := u.repo.CreateWithUuid(userUuid, user.AccountId, user.Name, hashedPassword, user.Email,
resUser, err := u.userRepository.CreateWithUuid(userUuid, user.AccountId, user.Name, hashedPassword, user.Email,
user.Department, user.Description, user.Organization.ID, roleUuid)
if err != nil {
return nil, err
}

if err = ses.SendEmailForTemporaryPassword(ses.Client, user.Email, randomPassword); err != nil {
organizationInfo, err := u.organizationRepository.Get(orgainzationId)
if err != nil {
return nil, err
}
if err = ses.SendEmailForGeneratingOrganization(ses.Client, orgainzationId, organizationInfo.Name, user.Email, user.AccountId, randomPassword); err != nil {
return nil, err
}

Expand Down Expand Up @@ -288,7 +293,7 @@ func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId s

// update password in DB

user, err := u.repo.Get(accountId, organizationId)
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
return errors.Wrap(err, "getting user from repository failed")
}
Expand All @@ -301,7 +306,7 @@ func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId s
return errors.Wrap(err, "hashing password failed")
}

err = u.repo.UpdatePassword(userUuid, organizationId, hashedPassword, false)
err = u.userRepository.UpdatePassword(userUuid, organizationId, hashedPassword, false)
if err != nil {
return errors.Wrap(err, "updating user in repository failed")
}
Expand All @@ -310,7 +315,7 @@ func (u *UserUsecase) UpdatePasswordByAccountId(ctx context.Context, accountId s
}

func (u *UserUsecase) List(ctx context.Context, organizationId string) (*[]domain.User, error) {
users, err := u.repo.List(u.repo.OrganizationFilter(organizationId))
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId))
if err != nil {
return nil, err
}
Expand All @@ -319,7 +324,7 @@ func (u *UserUsecase) List(ctx context.Context, organizationId string) (*[]domai
}

func (u *UserUsecase) Get(userId uuid.UUID) (*domain.User, error) {
user, err := u.repo.GetByUuid(userId)
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
return nil, httpErrors.NewBadRequestError(fmt.Errorf("user not found"), "", "")
Expand All @@ -331,8 +336,8 @@ func (u *UserUsecase) Get(userId uuid.UUID) (*domain.User, error) {
}

func (u *UserUsecase) GetByAccountId(ctx context.Context, accountId string, organizationId string) (*domain.User, error) {
users, err := u.repo.List(u.repo.OrganizationFilter(organizationId),
u.repo.AccountIdFilter(accountId))
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
return nil, err
}
Expand All @@ -341,8 +346,8 @@ func (u *UserUsecase) GetByAccountId(ctx context.Context, accountId string, orga
}

func (u *UserUsecase) GetByEmail(ctx context.Context, email string, organizationId string) (*domain.User, error) {
users, err := u.repo.List(u.repo.OrganizationFilter(organizationId),
u.repo.EmailFilter(email))
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(organizationId),
u.userRepository.EmailFilter(email))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -383,8 +388,8 @@ func (u *UserUsecase) UpdateByAccountId(ctx context.Context, accountId string, u
}
}

users, err := u.repo.List(u.repo.OrganizationFilter(userInfo.GetOrganizationId()),
u.repo.AccountIdFilter(accountId))
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
return nil, httpErrors.NewNotFoundError(httpErrors.NotFound, "", "")
Expand All @@ -409,7 +414,7 @@ func (u *UserUsecase) UpdateByAccountId(ctx context.Context, accountId string, u
return nil, err
}

*user, err = u.repo.UpdateWithUuid(userUuid, user.AccountId, user.Name, originPassword, roleUuid, user.Email,
*user, err = u.userRepository.UpdateWithUuid(userUuid, user.AccountId, user.Name, originPassword, roleUuid, user.Email,
user.Department, user.Description)
if err != nil {
return nil, errors.Wrap(err, "updating user in repository failed")
Expand All @@ -419,12 +424,12 @@ func (u *UserUsecase) UpdateByAccountId(ctx context.Context, accountId string, u
}

func (u *UserUsecase) Delete(userId uuid.UUID, organizationId string) error {
user, err := u.repo.GetByUuid(userId)
user, err := u.userRepository.GetByUuid(userId)
if err != nil {
return httpErrors.NewBadRequestError(fmt.Errorf("not found user"), "", "")
}

err = u.repo.DeleteWithUuid(userId)
err = u.userRepository.DeleteWithUuid(userId)
if err != nil {
return err
}
Expand All @@ -438,7 +443,7 @@ func (u *UserUsecase) Delete(userId uuid.UUID, organizationId string) error {
return nil
}
func (u *UserUsecase) DeleteByAccountId(ctx context.Context, accountId string, organizationId string) error {
user, err := u.repo.Get(accountId, organizationId)
user, err := u.userRepository.Get(accountId, organizationId)
if err != nil {
return err
}
Expand All @@ -447,7 +452,7 @@ func (u *UserUsecase) DeleteByAccountId(ctx context.Context, accountId string, o
if err != nil {
return err
}
err = u.repo.DeleteWithUuid(userUuid)
err = u.userRepository.DeleteWithUuid(userUuid)
if err != nil {
return err
}
Expand Down Expand Up @@ -498,7 +503,7 @@ func (u *UserUsecase) Create(ctx context.Context, user *domain.User) (*domain.Us
return nil, err
}

roles, err := u.repo.FetchRoles()
roles, err := u.userRepository.FetchRoles()
if err != nil {
return nil, err
}
Expand All @@ -512,7 +517,7 @@ func (u *UserUsecase) Create(ctx context.Context, user *domain.User) (*domain.Us
return nil, err
}

resUser, err := u.repo.CreateWithUuid(userUuid, user.AccountId, user.Name, hashedPassword, user.Email,
resUser, err := u.userRepository.CreateWithUuid(userUuid, user.AccountId, user.Name, hashedPassword, user.Email,
user.Department, user.Description, user.Organization.ID, roleUuid)
if err != nil {
return nil, err
Expand All @@ -539,8 +544,8 @@ func (u *UserUsecase) UpdateByAccountIdByAdmin(ctx context.Context, accountId st
}
}

users, err := u.repo.List(u.repo.OrganizationFilter(userInfo.GetOrganizationId()),
u.repo.AccountIdFilter(accountId))
users, err := u.userRepository.List(u.userRepository.OrganizationFilter(userInfo.GetOrganizationId()),
u.userRepository.AccountIdFilter(accountId))
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
return nil, httpErrors.NewNotFoundError(httpErrors.NotFound, "", "")
Expand Down Expand Up @@ -572,7 +577,7 @@ func (u *UserUsecase) UpdateByAccountIdByAdmin(ctx context.Context, accountId st

originPassword := (*users)[0].Password

roles, err := u.repo.FetchRoles()
roles, err := u.userRepository.FetchRoles()
if err != nil {
return nil, err
}
Expand All @@ -586,7 +591,7 @@ func (u *UserUsecase) UpdateByAccountIdByAdmin(ctx context.Context, accountId st
return nil, err
}

*user, err = u.repo.UpdateWithUuid(userUuid, user.AccountId, user.Name, originPassword, roleUuid, user.Email,
*user, err = u.userRepository.UpdateWithUuid(userUuid, user.AccountId, user.Name, originPassword, roleUuid, user.Email,
user.Department, user.Description)
if err != nil {
return nil, errors.Wrap(err, "updating user in repository failed")
Expand All @@ -597,7 +602,8 @@ func (u *UserUsecase) UpdateByAccountIdByAdmin(ctx context.Context, accountId st

func NewUserUsecase(r repository.Repository, kc keycloak.IKeycloak) IUserUsecase {
return &UserUsecase{
repo: r.User,
kc: kc,
userRepository: r.User,
kc: kc,
organizationRepository: r.Organization,
}
}