2020-12-21 15:56:56 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/model"
|
|
|
|
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
|
|
|
|
"gitlab.com/wpetit/goweb/cqrs"
|
|
|
|
"gitlab.com/wpetit/goweb/middleware/container"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StoreSMSRequest struct {
|
2020-12-22 15:00:42 +01:00
|
|
|
From string
|
2020-12-21 15:56:56 +01:00
|
|
|
Body string
|
|
|
|
Recipient string
|
2020-12-22 15:00:42 +01:00
|
|
|
Metadata map[string]interface{}
|
2020-12-21 15:56:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleStoreSMS(ctx context.Context, cmd cqrs.Command) error {
|
|
|
|
req, ok := cmd.Request().(*StoreSMSRequest)
|
|
|
|
if !ok {
|
|
|
|
return cqrs.ErrUnexpectedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
ctn, err := container.From(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve service container")
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := storm.From(ctn)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not retrieve storm service")
|
|
|
|
}
|
|
|
|
|
|
|
|
sms := &model.SMS{
|
|
|
|
SentAt: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
sms.Body = req.Body
|
|
|
|
sms.Recipient = req.Recipient
|
2020-12-22 15:00:42 +01:00
|
|
|
sms.Metadata = req.Metadata
|
|
|
|
sms.From = req.From
|
2020-12-21 15:56:56 +01:00
|
|
|
|
|
|
|
if err := db.Save(sms); err != nil {
|
|
|
|
return errors.Wrap(err, "could not save email")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|