using Application.Abstractions.Data; using Application.Abstractions.Cache; using Application.Abstractions.Messaging; using Application.Abstractions.Messaging.Email; using Application.Features.Config.Get; using SharedKernel.Results; using Microsoft.EntityFrameworkCore; namespace Application.Features.Api.Auth.Registration; internal sealed class Handler( IAppDbContext db, ICacheService cache, IMailService mailService ) : ICommandHandler { public async Task Handle(Command request, CancellationToken ct) { // 이메일 유효성 검사 if (string.IsNullOrWhiteSpace(request.Email)) { return Result.Failure(Error.Problem("Auth.EmailRequired", "이메일은 필수입니다.")); } var email = request.Email.Trim().ToLower(); // 회원 조회 var member = await db.Member.FirstOrDefaultAsync(m => m.Email == email, ct); if (member is null) { return Result.Failure(Error.NotFound("Auth.MemberNotFound", "등록된 회원 정보를 찾을 수 없습니다.")); } // Config에서 IsRegisterEmailAuth 설정 조회 var isRegisterEmailAuth = false; var config = await cache.GetAsync(CacheKeys.Config, ct); if (config is not null) { isRegisterEmailAuth = config.Account.IsRegisterEmailAuth; } else { var configEntity = await db.Config.AsNoTracking().OrderByDescending(x => x.ID).FirstOrDefaultAsync(ct); if (configEntity is not null) { isRegisterEmailAuth = configEntity.Account.IsRegisterEmailAuth; } } // 이메일 인증이 필요한 경우 쿠키 확인 및 이메일 인증 처리 if (isRegisterEmailAuth) { if (string.IsNullOrWhiteSpace(request.CookieValue) || request.CookieValue != "true") { return Result.Failure(Error.Problem("Auth.VerificationRequired", "사전 인증을 먼저 수행하세요.")); } // 이메일 인증 완료 처리 member.MarkEmailVerified(); await db.SaveChangesAsync(ct); } // 가입 완료 환영 이메일 발송 await mailService.SendAsync(new SendData( email, "[bitforum] 회원가입을 환영합니다", $"

회원가입이 완료되었습니다.

bitforum을 이용해 주셔서 감사합니다.

" ), ct); return Result.Success(); } }