fake-sms/internal/command/store_sms.go

53 lines
1.0 KiB
Go

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 {
From string
Body string
Recipient string
Metadata map[string]interface{}
}
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
sms.Metadata = req.Metadata
sms.From = req.From
if err := db.Save(sms); err != nil {
return errors.Wrap(err, "could not save email")
}
return nil
}