Skip to content

Commit

Permalink
chore: backend integrations refactor (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Apr 25, 2024
1 parent f69bad5 commit 3522a3f
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 48 deletions.
8 changes: 4 additions & 4 deletions backend/entities/auth/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (a *AuthService) ForgotPassword(email string) *errors.Error {
}

if activeToken != nil {
sendErr := a.Deps.Email.SendPasswordResetEmail(user.FirstName, user.Email, activeToken.Token)
sendErr := a.Integrations.Email.SendPasswordResetEmail(user.FirstName, user.Email, activeToken.Token)
if sendErr != nil {
return &errors.FailedToSendEmail
}
Expand All @@ -154,7 +154,7 @@ func (a *AuthService) ForgotPassword(email string) *errors.Error {
return saveErr
}

sendErr := a.Deps.Email.SendPasswordResetEmail(user.FirstName, user.Email, *token)
sendErr := a.Integrations.Email.SendPasswordResetEmail(user.FirstName, user.Email, *token)
if sendErr != nil {
return sendErr
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (a *AuthService) SendCode(email string) *errors.Error {
}

if activeOTP != nil {
sendErr := a.Deps.Email.SendEmailVerification(user.Email, activeOTP.Token)
sendErr := a.Integrations.Email.SendEmailVerification(user.Email, activeOTP.Token)
if sendErr != nil {
return &errors.FailedToSendEmail
}
Expand All @@ -244,7 +244,7 @@ func (a *AuthService) SendCode(email string) *errors.Error {
return saveErr
}

sendErr := a.Deps.Email.SendEmailVerification(user.Email, *otp)
sendErr := a.Integrations.Email.SendEmailVerification(user.Email, *otp)
if sendErr != nil {
return &errors.FailedToSendEmail
}
Expand Down
8 changes: 4 additions & 4 deletions backend/entities/clubs/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams) ([]models.Cl
return nil, &errors.FailedToValidatePage
}

return GetClubs(c.DB, c.Deps.Search, queryParams)
return GetClubs(c.DB, c.Integrations.Search, queryParams)
}

func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) {
Expand All @@ -46,7 +46,7 @@ func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models
return nil, &errors.FailedToMapRequestToModel
}

return CreateClub(c.DB, c.Deps.Search, clubBody.UserID, *club)
return CreateClub(c.DB, c.Integrations.Search, clubBody.UserID, *club)
}

func (c *ClubService) GetClub(id string) (*models.Club, *errors.Error) {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, &errors.FailedToMapRequestToModel
}

return UpdateClub(c.DB, c.Deps.Search, *idAsUUID, *club)
return UpdateClub(c.DB, c.Integrations.Search, *idAsUUID, *club)
}

func (c *ClubService) DeleteClub(id string) *errors.Error {
Expand All @@ -86,5 +86,5 @@ func (c *ClubService) DeleteClub(id string) *errors.Error {
return &errors.FailedToValidateID
}

return DeleteClub(c.DB, c.Deps.Search, *idAsUUID)
return DeleteClub(c.DB, c.Integrations.Search, *idAsUUID)
}
2 changes: 1 addition & 1 deletion backend/entities/clubs/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package base
import (
stdliberrors "errors"

"github.com/GenerateNU/sac/backend/search"
"github.com/GenerateNU/sac/backend/integrations/search"
"github.com/sahilm/fuzzy"

"github.com/GenerateNU/sac/backend/entities/models"
Expand Down
12 changes: 6 additions & 6 deletions backend/entities/clubs/poc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/GenerateNU/sac/backend/entities/models"

"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/file"
"github.com/GenerateNU/sac/backend/integrations/file"
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
)
Expand Down Expand Up @@ -67,7 +67,7 @@ func (cpoc *ClubPointOfContactService) CreateClubPointOfContact(clubID string, p
return nil, &errors.PointOfContactAlreadyExists
}

fileInfo, err := cpoc.Deps.File.UploadFile("point_of_contacts", fileHeader, []file.FileType{file.IMAGE})
fileInfo, err := cpoc.Integrations.File.UploadFile("point_of_contacts", fileHeader, []file.FileType{file.IMAGE})
if err != nil {
return nil, err
}
Expand All @@ -92,7 +92,7 @@ func (cpoc *ClubPointOfContactService) CreateClubPointOfContact(clubID string, p
}

if err := tx.Commit().Error; err != nil {
if err := cpoc.Deps.File.DeleteFile(fileInfo.FileURL); err != nil {
if err := cpoc.Integrations.File.DeleteFile(fileInfo.FileURL); err != nil {
return nil, err
}

Expand Down Expand Up @@ -120,11 +120,11 @@ func (cpoc *ClubPointOfContactService) UpdateClubPointOfContactPhoto(clubID, poc
return nil, err
}

if err := cpoc.Deps.File.DeleteFile(pointOfContact.PhotoFile.FileURL); err != nil {
if err := cpoc.Integrations.File.DeleteFile(pointOfContact.PhotoFile.FileURL); err != nil {
return nil, err
}

fileInfo, err := cpoc.Deps.File.UploadFile("point_of_contacts", fileHeader, []file.FileType{file.IMAGE})
fileInfo, err := cpoc.Integrations.File.UploadFile("point_of_contacts", fileHeader, []file.FileType{file.IMAGE})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (cpoc *ClubPointOfContactService) DeleteClubPointOfContact(clubID, pocID st
return err
}

if err := cpoc.Deps.File.DeleteFile(pointOfContact.PhotoFile.FileURL); err != nil {
if err := cpoc.Integrations.File.DeleteFile(pointOfContact.PhotoFile.FileURL); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions backend/entities/files/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/file"
"github.com/GenerateNU/sac/backend/integrations/file"
"github.com/GenerateNU/sac/backend/types"
"github.com/GenerateNU/sac/backend/utilities"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func (f *FileService) CreateFile(fileBody *models.CreateFileRequestBody, fileHea
return nil, &errors.FailedToValidateFile
}

fileInfo, err := f.Deps.File.UploadFile(fileBody.OwnerType, fileHeader, []file.FileType{file.ALL})
fileInfo, err := f.Integrations.File.UploadFile(fileBody.OwnerType, fileHeader, []file.FileType{file.ALL})
if err != nil {
return nil, &errors.FailedToUploadFile
}
Expand Down
2 changes: 1 addition & 1 deletion backend/entities/users/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (u *UserService) CreateUser(userBody models.CreateUserRequestBody) (*models
user.Email = strings.ToLower(userBody.Email)
user.PasswordHash = *passwordHash

emailErr := u.Deps.Email.SendWelcomeEmail(fmt.Sprintf("%s %s", user.FirstName, user.LastName), user.Email)
emailErr := u.Integrations.Email.SendWelcomeEmail(fmt.Sprintf("%s %s", user.FirstName, user.LastName), user.Email)
if emailErr != nil {
return nil, nil, &errors.FailedToSendEmail
}
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions backend/integrations/expose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package integrations

import (
"github.com/GenerateNU/sac/backend/integrations/email"
"github.com/GenerateNU/sac/backend/integrations/file"
"github.com/GenerateNU/sac/backend/integrations/search"
)

type Integrations struct {
Search search.SearchClientInterface
AI search.AIClientInterface
Email email.EmailClientInterface
File file.FileClientInterface
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 9 additions & 9 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/GenerateNU/sac/backend/config"
"github.com/GenerateNU/sac/backend/database"
_ "github.com/GenerateNU/sac/backend/docs"
"github.com/GenerateNU/sac/backend/email"
"github.com/GenerateNU/sac/backend/file"
"github.com/GenerateNU/sac/backend/search"
"github.com/GenerateNU/sac/backend/integrations"
"github.com/GenerateNU/sac/backend/integrations/email"
"github.com/GenerateNU/sac/backend/integrations/file"
"github.com/GenerateNU/sac/backend/integrations/search"
"github.com/GenerateNU/sac/backend/server"
"github.com/GenerateNU/sac/backend/tests/api/mocks"
t "github.com/GenerateNU/sac/backend/types"
)

func CheckServerRunning(host string, port uint16) error {
Expand All @@ -33,7 +33,7 @@ func Exit(format string, a ...interface{}) {
os.Exit(0)
}

func configureDependencies(config *config.Settings, connectToPinecone bool) t.Dependencies {
func configureIntegrations(config *config.Settings, connectToPinecone bool) integrations.Integrations {
openAi := search.NewOpenAIClient(config.OpenAISettings)
var pinecone search.SearchClientInterface

Expand All @@ -43,13 +43,13 @@ func configureDependencies(config *config.Settings, connectToPinecone bool) t.De
pinecone = mocks.NewPineconeMockClient()
}

dependencies := t.Dependencies{
integrations := integrations.Integrations{
File: file.NewAWSProvider(config.AWS),
AI: openAi,
Email: email.NewResendClient(config.ResendSettings, true),
Search: pinecone,
}
return dependencies
return integrations
}

func main() {
Expand Down Expand Up @@ -96,9 +96,9 @@ func main() {
Exit("Error with connection pooling: %s", err.Error())
}

dependencies := configureDependencies(config, *connectToPinecone)
integrations := configureIntegrations(config, *connectToPinecone)

app := server.Init(db, dependencies, *config)
app := server.Init(db, integrations, *config)

err = app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
pocs "github.com/GenerateNU/sac/backend/entities/pocs/base"
tags "github.com/GenerateNU/sac/backend/entities/tags/base"
users "github.com/GenerateNU/sac/backend/entities/users/base"
"github.com/GenerateNU/sac/backend/integrations"
"github.com/GenerateNU/sac/backend/middleware"

"github.com/GenerateNU/sac/backend/types"
Expand All @@ -37,7 +38,7 @@ import (
// @host 127.0.0.1:8080
// @BasePath /
// @schemes http
func Init(db *gorm.DB, dependencies types.Dependencies, settings config.Settings) *fiber.App {
func Init(db *gorm.DB, integrations integrations.Integrations, settings config.Settings) *fiber.App {
app := newFiberApp(settings.Application)

validate, err := utilities.RegisterCustomValidators()
Expand All @@ -59,7 +60,7 @@ func Init(db *gorm.DB, dependencies types.Dependencies, settings config.Settings
Validate: validate,
AuthSettings: &settings.Auth,
JWT: jwt,
Deps: dependencies,
Integrations: integrations,
},
}

Expand Down
6 changes: 3 additions & 3 deletions backend/tests/api/helpers/dependencies.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package helpers

import (
"github.com/GenerateNU/sac/backend/integrations"
"github.com/GenerateNU/sac/backend/tests/api/mocks"
"github.com/GenerateNU/sac/backend/types"
)

func NewMockDependencies() *types.Dependencies {
return &types.Dependencies{
func NewMockDependencies() *integrations.Integrations {
return &integrations.Integrations{
Search: mocks.NewPineconeMockClient(),
AI: mocks.NewOpenAIMockClient(),
Email: mocks.NewResendMockClient(),
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/mocks/aws_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/file"
"github.com/GenerateNU/sac/backend/integrations/file"
)

type AWSMockClient struct{}
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/mocks/openai_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mocks

import (
"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/search"
"github.com/GenerateNU/sac/backend/integrations/search"
)

type OpenAIMockClient struct{}
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/mocks/pinecone_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package mocks

import (
"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/search"
"github.com/GenerateNU/sac/backend/integrations/search"
"gorm.io/gorm"
)

Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/mocks/resend_mock.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package mocks

import (
"github.com/GenerateNU/sac/backend/email"
"github.com/GenerateNU/sac/backend/errors"
"github.com/GenerateNU/sac/backend/integrations/email"
)

type ResendMockClient struct{}
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/GenerateNU/sac/backend/config"
"github.com/GenerateNU/sac/backend/constants"
"github.com/GenerateNU/sac/backend/search"
"github.com/GenerateNU/sac/backend/integrations/search"
m "github.com/garrettladley/mattress"
"github.com/h2non/gock"
"github.com/huandu/go-assert"
Expand Down
13 changes: 2 additions & 11 deletions backend/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package types
import (
"github.com/GenerateNU/sac/backend/auth"
"github.com/GenerateNU/sac/backend/config"
"github.com/GenerateNU/sac/backend/email"
"github.com/GenerateNU/sac/backend/file"
"github.com/GenerateNU/sac/backend/integrations"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/search"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
Expand All @@ -23,12 +21,5 @@ type ServiceParams struct {
Validate *validator.Validate
AuthSettings *config.AuthSettings
JWT auth.JWTClientInterface
Deps Dependencies
}

type Dependencies struct {
Search search.SearchClientInterface
AI search.AIClientInterface
Email email.EmailClientInterface
File file.FileClientInterface
Integrations integrations.Integrations
}

0 comments on commit 3522a3f

Please sign in to comment.