Skip to content

Commit

Permalink
add support for other campaigns to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
jakehobbs committed Mar 9, 2024
1 parent 76fa7ba commit fc08f0e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
Binary file added bun.lockb
Binary file not shown.
9 changes: 9 additions & 0 deletions service/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ func getEnvWithFallback(key string, fallback string) string {
}
return v
}

var EmailSettings = map[string]struct {
FromDomain string
Subject string
To string
}{
"duck": {FromDomain: "mail.helptheducks.com", Subject: "Prosecute Reichardt Duck Farm for Animal Abuse", To: "[email protected]"},
"sonoma": {FromDomain: "mail.righttorescue.com", Subject: "Prosecute animal cruelty, not animal rescuers", To: "[email protected]"},
}
5 changes: 5 additions & 0 deletions service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type CreateMessageInput struct {
City string `json:"city,omitempty"`
Message string `json:"message"`
Token string `json:"token"`
Campaign string `json:"campaign,omitempty"`
}

func createMessageHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -77,6 +78,10 @@ func createMessageHandler(w http.ResponseWriter, r *http.Request) {
String: r.RemoteAddr,
Valid: r.RemoteAddr != "",
},
Campaign: sql.NullString{
String: body.Campaign,
Valid: body.Campaign != "",
},
}

err = model.InsertMessage(db, message)
Expand Down
14 changes: 10 additions & 4 deletions service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"http://localhost:5173", "https://helptheducks.com"},
AllowedOrigins: []string{"http://localhost:5173", "https://helptheducks.com", "https://righttorescue.com"},
AllowedMethods: []string{"GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: false,
Expand Down Expand Up @@ -78,12 +78,18 @@ func processNewMessages() {

for _, message := range messages {
fmt.Printf("Processing message id: %v\n", message.ID)
fromEmail := strings.Join(strings.Split(strings.ToLower(message.Name), " "), ".") + "@mail.helptheducks.com"

settings, ok := config.EmailSettings[message.Campaign.String]
if !ok {
settings = config.EmailSettings["duck"]
}

fromEmail := strings.Join(strings.Split(strings.ToLower(message.Name), " "), ".") + "@" + settings.FromDomain
err := mailer.Send(mailClient, mailer.SendOptions{
From: fmt.Sprintf("%s <%s>", message.Name, fromEmail),
ReplyTo: message.Email,
To: "[email protected]",
Subject: "Prosecute Reichardt Duck Farm for Animal Abuse",
To: settings.To,
Subject: settings.Subject,
Body: message.Message,
})
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions service/model/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Message struct {
Zip sql.NullString `db:"zip"`
City sql.NullString `db:"city"`
Message string `db:"message"`
Campaign sql.NullString `db:"campaign"`
Status string `db:"status"`
}

Expand All @@ -33,7 +34,8 @@ func InsertMessage(db *sqlx.DB, message Message) error {
zip,
city,
message,
status
campaign,
status,
) VALUES (
:ip_address,
:name,
Expand All @@ -43,6 +45,7 @@ func InsertMessage(db *sqlx.DB, message Message) error {
:zip,
:city,
:message,
:campaign,
'PENDING'
)`,
message,
Expand All @@ -58,7 +61,7 @@ func GetMessagesToProcess(db *sqlx.DB) ([]Message, error) {
var messages []Message
err = tx.Select(
&messages,
`SELECT id, name, email, message FROM messages WHERE status = 'PENDING' LIMIT 50 FOR UPDATE`,
`SELECT id, name, email, message, campaign FROM messages WHERE status = 'PENDING' LIMIT 50 FOR UPDATE`,
)
if err != nil {
tx.Rollback()
Expand Down
14 changes: 14 additions & 0 deletions src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// This file is auto-generated by TanStack Router

import { FileRoute, lazyRouteComponent } from '@tanstack/react-router'

// Import Routes

import { Route as rootRoute } from './routes/__root'

// Create Virtual Routes

const IndexComponentImport = new FileRoute('/').createRoute()

// Create/Update Routes

const IndexComponentRoute = IndexComponentImport.update({
path: '/',
getParentRoute: () => rootRoute,
Expand All @@ -13,6 +21,9 @@ const IndexComponentRoute = IndexComponentImport.update({
'component',
),
})

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
Expand All @@ -21,4 +32,7 @@ declare module '@tanstack/react-router' {
}
}
}

// Create and export the route tree

export const routeTree = rootRoute.addChildren([IndexComponentRoute])

0 comments on commit fc08f0e

Please sign in to comment.