package com.red.circle.other.app.command.user;

import com.red.circle.external.inner.endpoint.message.SmsClient;
import com.red.circle.external.inner.model.cmd.message.SendSmsCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.dto.cmd.user.sms.SendSmsRegisterCmd;
import com.red.circle.other.infra.database.rds.service.user.user.MobileAuthService;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 发送注册-短信验证码.
 *
 * @author pengliang on 2021/3/25
 */
@Component
@RequiredArgsConstructor
public class SendRegisterSmsCmdExe {

  private final SmsClient smsClient;
  private final MobileAuthService mobileAuthService;

  public void execute(SendSmsRegisterCmd cmd) {
    // 手机号码已注册
    ResponseAssert.isNull(UserErrorCode.MOBILE_REGISTERED,
        mobileAuthService.getByPhone(cmd.getPhonePrefix(), cmd.getPhoneNumber()));

    smsClient.sendCode(new SendSmsCmd()
        .setPhonePrefix(cmd.getPhonePrefix())
        .setPhoneNumber(cmd.getPhoneNumber())
        .setCode(cmd.getCode())
    );
  }

}
