Class SendSmsOneTimeCodeUseCase

java.lang.Object
com.irurueta.authserver.authorization.application.sms.SendSmsOneTimeCodeUseCase

@Service @Transactional(noRollbackFor=SmsRefusedException.class) public class SendSmsOneTimeCodeUseCase extends Object
Requests an SMS inside the cost policy: the country must be served and the spending of the window and of the day, including this message, must stay within their caps.

The cost is reserved by SmsCostLedgerRepository.tryRecord(java.time.Instant, java.lang.String, long, com.irurueta.authserver.authorization.domain.sms.SmsSpendCap, com.irurueta.authserver.authorization.domain.sms.SmsSpendCap), which checks both caps and records the message in one atomic step, so concurrent senders cannot overshoot a budget and a refused message costs nothing. The message itself is not sent here: an SmsRequested event is published and sent after the transaction commits, off the request thread, so the gateway is never called inside a transaction and a rolled-back request sends nothing. A refusal does not roll the surrounding transaction back (noRollbackFor): this use case is joined by its callers, and a rollback-only mark here would undo what they saved.

  • Field Details

    • phoneCountryResolver

      private final PhoneCountryResolver phoneCountryResolver
      Port that finds the country of a number.
    • smsCostPolicy

      private final SmsCostPolicy smsCostPolicy
      Policy of countries, costs and caps.
    • smsCostLedgerRepository

      private final SmsCostLedgerRepository smsCostLedgerRepository
      Port that records the spend.
    • eventPublisher

      private final org.springframework.context.ApplicationEventPublisher eventPublisher
      Publishes the request to send the message.
    • clock

      private final Clock clock
      Source of the current time.
  • Constructor Details

    • SendSmsOneTimeCodeUseCase

      public SendSmsOneTimeCodeUseCase()
  • Method Details

    • execute

      public void execute(PhoneNumber to, String text)
      Reserves the cost of a message and requests it.
      Parameters:
      to - destination number.
      text - text of the message.
      Throws:
      SmsCountryNotAllowedException - if the country of the number is unknown or not served.
      SmsBudgetExceededException - if the message would exceed the cap of the window or of the day.