| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Application.Abstractions.Messaging.Email;
- using Application.Common;
- using Domain.Entities.EmailVerification;
- using Domain.Entities.EmailVerification.ValueObject;
- using SharedKernel.Results;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Api.MyPage.Withdraw.RequestCode;
- internal sealed class Handler(
- IAppDbContext db,
- IMailService mailService
- ) : ICommandHandler<Command, Result>
- {
- public async Task<Result> Handle(Command request, CancellationToken ct)
- {
- var member = await db.Member.FirstOrDefaultAsync(m => m.ID == request.MemberID, ct);
- if (member is null)
- {
- return Result.Failure(Error.NotFound("MyPage.MemberNotFound", "회원 정보를 찾을 수 없습니다."));
- }
- if (member.IsWithdraw)
- {
- return Result.Failure(Error.Problem("MyPage.AlreadyWithdrawn", "이미 탈퇴한 회원입니다."));
- }
- if (member.IsDenied)
- {
- return Result.Failure(Error.Problem("MyPage.MemberDenied", "차단된 회원은 이용할 수 없습니다."));
- }
- var email = member.Email;
- // 기존 미인증 코드 제거
- var existing = await db.EmailVerifyNumber.Where(e => e.Email == email && e.Type == VerificationType.Withdraw && !e.IsVerified).ToListAsync(ct);
- db.EmailVerifyNumber.RemoveRange(existing);
- // 6자리 코드 발급 (10분 만료)
- var code = Random.Shared.Next(100000, 999999).ToString();
- var verifyNumber = EmailVerifyNumber.Create(
- VerificationType.Withdraw,
- email,
- code,
- DateTime.UtcNow.AddMinutes(10)
- );
- await db.EmailVerifyNumber.AddAsync(verifyNumber, ct);
- await db.SaveChangesAsync(ct);
- // 이메일 발송 — WithdrawVerify 템플릿 우선, 없으면 hardcoded fallback
- var emailTpl = (await db.Config.AsNoTracking().FirstOrDefaultAsync(ct))?.EmailTemplate;
- var subject = string.IsNullOrWhiteSpace(emailTpl?.WithdrawVerifyEmailFormTitle)
- ? "회원탈퇴 인증번호"
- : emailTpl!.WithdrawVerifyEmailFormTitle!;
- var content = string.IsNullOrWhiteSpace(emailTpl?.WithdrawVerifyEmailFormContent)
- ? $"<p>회원탈퇴 인증번호입니다.</p><p><strong>{code}</strong></p><p>이 코드는 10분간 유효합니다.</p>"
- : emailTpl!.WithdrawVerifyEmailFormContent!.Render(("Code", code), ("Email", email));
- await mailService.SendAsync(new SendData(email, subject, content), ct);
- return Result.Success();
- }
- }
|