Skip to content

Commit

Permalink
Add messenger package to send emails.
Browse files Browse the repository at this point in the history
  • Loading branch information
v0ctor committed Aug 27, 2023
1 parent aaf1b45 commit d1fb8db
Show file tree
Hide file tree
Showing 28 changed files with 1,610 additions and 43 deletions.
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ COCKROACHDB_DATABASE=defaultdb
COCKROACHDB_TLS_MODE=disable

HCAPTCHA_SECRET=0x0000000000000000000000000000000000000000

MAIL_SOURCE=
MAIL_REPLY_TO=

AWS_REGION=eu-west-1
AWS_KEY_ID=
AWS_KEY_SECRET=

ORG_NAME='Associació Valenciana pel Transport Públic'
ORG_LINK=https://avptp.org
ORG_LOGO=https://cdn.avptp.org/brand/imagotype.png
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ require (
github.com/99designs/gqlgen v0.17.36
github.com/BurntSushi/toml v1.3.2
github.com/alexedwards/argon2id v0.0.0-20230305115115-4b3c3280a736
github.com/aws/aws-sdk-go v1.44.332
github.com/brianvoe/gofakeit/v6 v6.23.1
github.com/caarlos0/env/v9 v9.0.0
github.com/criptalia/spanish_dni_validator v0.0.0-20230502125532-3278e5ffc050
github.com/google/uuid v1.3.1
github.com/hashicorp/go-multierror v1.1.1
github.com/kataras/hcaptcha v0.0.2
github.com/lib/pq v1.10.9
github.com/matcornic/hermes/v2 v2.1.0
github.com/nicksnyder/go-i18n/v2 v2.2.1
github.com/nyaruka/phonenumbers v1.1.8
github.com/oklog/ulid/v2 v2.1.0
Expand All @@ -30,23 +32,39 @@ require (
)

require (
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.16.0+incompatible // indirect
github.com/PuerkitoBio/goquery v1.5.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/aokoli/goutils v1.0.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/hashicorp/hcl/v2 v2.17.0 // indirect
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jaytaylor/html2text v0.0.0-20180606194806-57d518f124b0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/vanng822/css v0.0.0-20190504095207-a21e860bcd04 // indirect
github.com/vanng822/go-premailer v0.0.0-20191214114701-be27abe028fe // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
Expand Down
57 changes: 57 additions & 0 deletions go.sum

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions internal/api/resolvers/person.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions internal/api/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package resolvers

import (
"github.com/avptp/brain/internal/generated/data"
"github.com/avptp/brain/internal/messaging"
)

type Resolver struct {
data *data.Client
data *data.Client
messenger messaging.Messenger
}

func NewResolver(data *data.Client) *Resolver {
func NewResolver(data *data.Client, messenger messaging.Messenger) *Resolver {
return &Resolver{
data,
messenger,
}
}
26 changes: 19 additions & 7 deletions internal/api/resolvers/resolvers_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/avptp/brain/internal/generated/data/factories"
"github.com/avptp/brain/internal/generated/data/privacy"
_ "github.com/avptp/brain/internal/generated/data/runtime"
"github.com/avptp/brain/internal/messaging/messaging_test"
"github.com/avptp/brain/internal/services"
"github.com/avptp/brain/internal/transport"

"github.com/99designs/gqlgen/client"
Expand Down Expand Up @@ -51,21 +53,31 @@ func init() {
type TestSuite struct {
suite.Suite

ctn *container.Container
log *slog.Logger
data *data.Client
factory *factories.Factory
api *client.Client
allowCtx context.Context
ctn *container.Container
log *slog.Logger
data *data.Client
messenger *messaging_test.MockedMessenger
factory *factories.Factory
api *client.Client
allowCtx context.Context
}

func (t *TestSuite) SetupSuite() {
ctn, err := container.NewContainer()
builder, err := container.NewBuilder()

if err != nil {
panic(err) // unrecoverable situation
}

t.messenger = &messaging_test.MockedMessenger{}
builder.Set(services.Messenger, t.messenger)

if err != nil {
panic(err) // unrecoverable situation
}

ctn := builder.Build()

t.ctn = ctn
t.log = ctn.GetLogger()
t.data = ctn.GetData()
Expand Down
10 changes: 10 additions & 0 deletions internal/api/resolvers/resolvers_test/person_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"github.com/99designs/gqlgen/client"
"github.com/alexedwards/argon2id"
"github.com/avptp/brain/internal/api/reporting"
"github.com/avptp/brain/internal/generated/data"
"github.com/avptp/brain/internal/generated/data/person"
"github.com/avptp/brain/internal/messaging/templates"
"github.com/stretchr/testify/mock"
)

func (t *TestSuite) TestPerson() {
Expand Down Expand Up @@ -45,6 +48,12 @@ func (t *TestSuite) TestPerson() {
t.Run("create", func() {
input := t.factory.Person().Fields

t.messenger.On(
"Send",
mock.IsType(&templates.Welcome{}),
mock.IsType(&data.Person{}),
).Return(nil).Once()

var response create
t.api.MustPost(
createMutation,
Expand Down Expand Up @@ -76,6 +85,7 @@ func (t *TestSuite) TestPerson() {
t.Equal(input.FirstName, p.FirstName)
t.Equal(input.LastName, p.LastName)
t.Equal(input.Language, p.Language)
t.messenger.AssertExpectations(t.T())
})

t.Run("create_with_already_used_email", func() {
Expand Down
11 changes: 11 additions & 0 deletions internal/config/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ type Config struct {
CockroachDBTLSCA string `env:"COCKROACHDB_TLS_CA"`

HcaptchaSecret string `env:"HCAPTCHA_SECRET"`

MailSource string `env:"MAIL_SOURCE"`
MailReplyTo string `env:"MAIL_REPLY_TO"`

AwsRegion string `env:"AWS_REGION"`
AwsKeyId string `env:"AWS_KEY_ID"`
AwsKeySecret string `env:"AWS_KEY_SECRET"`

OrgName string `env:"ORG_NAME"`
OrgLink string `env:"ORG_LINK"`
OrgLogo string `env:"ORG_LOGO"`
}
17 changes: 17 additions & 0 deletions internal/data/templates/models.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{{/* gotype: entgo.io/ent/entc/gen.Type */}}

{{ define "model/additional/person" }}
{{- if eq $.Name "Person" -}}
{{- $receiver := $.Receiver -}}
func ({{ $receiver }} *{{ $.Name }}) FullName() string {
parts := []string{
{{ $receiver }}.FirstName,
}

if v := {{ $receiver }}.LastName; v != nil {
parts = append(parts, *v)
}

return strings.Join(parts, " ")
}
{{- end -}}
{{ end }}

{{ define "model/additional/authentication" }}
{{- if eq $.Name "Authentication" -}}
{{- range $f := $.Fields -}}
Expand Down
Loading

0 comments on commit d1fb8db

Please sign in to comment.