feat: mail service
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package internalhttp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"galaxy/mail/internal/service/acceptauthdelivery"
|
||||
)
|
||||
|
||||
// AcceptLoginCodeDeliveryUseCase accepts one auth login-code delivery request.
|
||||
type AcceptLoginCodeDeliveryUseCase interface {
|
||||
// Execute durably accepts one normalized auth login-code delivery command.
|
||||
Execute(context.Context, acceptauthdelivery.Input) (acceptauthdelivery.Result, error)
|
||||
}
|
||||
|
||||
func newAcceptLoginCodeDeliveryHandler(useCase AcceptLoginCodeDeliveryUseCase) http.HandlerFunc {
|
||||
return func(writer http.ResponseWriter, request *http.Request) {
|
||||
ctx := request.Context()
|
||||
|
||||
command, err := DecodeLoginCodeDeliveryCommand(request)
|
||||
if err != nil {
|
||||
writeErrorResponse(writer, http.StatusBadRequest, ErrorCodeInvalidRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
result, err := useCase.Execute(ctx, acceptauthdelivery.Input{
|
||||
IdempotencyKey: command.IdempotencyKey,
|
||||
Email: command.Email,
|
||||
Code: command.Code,
|
||||
Locale: command.Locale,
|
||||
})
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, acceptauthdelivery.ErrConflict):
|
||||
writeErrorResponse(writer, http.StatusConflict, ErrorCodeConflict, "request conflicts with current state")
|
||||
case errors.Is(err, acceptauthdelivery.ErrServiceUnavailable):
|
||||
writeErrorResponse(writer, http.StatusServiceUnavailable, ErrorCodeServiceUnavailable, "service is unavailable")
|
||||
default:
|
||||
writeErrorResponse(writer, http.StatusInternalServerError, ErrorCodeInternalError, "internal server error")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err := result.Validate(); err != nil {
|
||||
writeErrorResponse(writer, http.StatusInternalServerError, ErrorCodeInternalError, "internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
response := LoginCodeDeliveryResponse{
|
||||
Outcome: LoginCodeDeliveryOutcome(result.Outcome),
|
||||
}
|
||||
if err := response.Validate(); err != nil {
|
||||
writeErrorResponse(writer, http.StatusInternalServerError, ErrorCodeInternalError, "internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(writer).Encode(response)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user