Skip to content

Commit

Permalink
Merge branch 'main' into dashboard-form
Browse files Browse the repository at this point in the history
  • Loading branch information
in-mai-space committed May 17, 2024
2 parents 8fd955a + cf1e04e commit fdffec7
Show file tree
Hide file tree
Showing 72 changed files with 1,901 additions and 1,094 deletions.
24 changes: 24 additions & 0 deletions backend/config/calendar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"time"
)

type CalendarSettings struct {
MaxTerminationDate time.Time
}

type intermediateCalendarSettings struct {
MaxTerminationDate string `yaml:"maxterminationdate"`
}

func (int *intermediateCalendarSettings) into() (*CalendarSettings, error) {
t, err := time.Parse("01-02-2006", int.MaxTerminationDate)
if err != nil {
return nil, err
}

return &CalendarSettings{
MaxTerminationDate: t,
}, nil
}
24 changes: 16 additions & 8 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import (
)

type Settings struct {
Application ApplicationSettings
Database DatabaseSettings
SuperUser SuperUserSettings
Auth AuthSettings
AWS AWSSettings
PineconeSettings PineconeSettings
OpenAISettings OpenAISettings
ResendSettings ResendSettings
Application ApplicationSettings
Database DatabaseSettings
SuperUser SuperUserSettings
Auth AuthSettings
AWS AWSSettings
Pinecone PineconeSettings
OpenAI OpenAISettings
Resend ResendSettings
Calendar CalendarSettings
}

type intermediateSettings struct {
Application ApplicationSettings `yaml:"application"`
Database intermediateDatabaseSettings `yaml:"database"`
SuperUser intermediateSuperUserSettings `yaml:"superuser"`
Auth intermediateAuthSettings `yaml:"authsecret"`
Calendar intermediateCalendarSettings `yaml:"calendar"`
}

func (int *intermediateSettings) into() (*Settings, error) {
Expand All @@ -41,11 +43,17 @@ func (int *intermediateSettings) into() (*Settings, error) {
return nil, err
}

calendarSettings, err := int.Calendar.into()
if err != nil {
return nil, err
}

return &Settings{
Application: int.Application,
Database: *databaseSettings,
SuperUser: *superUserSettings,
Auth: *authSettings,
Calendar: *calendarSettings,
}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions backend/config/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func readLocal(v *viper.Viper, path string, useDevDotEnv bool) (*Settings, error
return nil, fmt.Errorf("failed to read Pinecone settings: %w", err)
}

settings.PineconeSettings = *pineconeSettings
settings.Pinecone = *pineconeSettings

openAISettings, err := readOpenAISettings()
if err != nil {
return nil, fmt.Errorf("failed to read OpenAI settings: %w", err)
}

settings.OpenAISettings = *openAISettings
settings.OpenAI = *openAISettings

awsSettings, err := readAWSSettings()
if err != nil {
Expand All @@ -63,7 +63,7 @@ func readLocal(v *viper.Viper, path string, useDevDotEnv bool) (*Settings, error
return nil, fmt.Errorf("failed to read Resend settings: %w", err)
}

settings.ResendSettings = *resendSettings
settings.Resend = *resendSettings

return settings, nil
}
10 changes: 6 additions & 4 deletions backend/config/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type ProductionSettings struct {
Database ProductionDatabaseSettings `yaml:"database"`
Application ProductionApplicationSettings `yaml:"application"`
Calendar CalendarSettings `yaml:"calendar"`
}

type ProductionDatabaseSettings struct {
Expand Down Expand Up @@ -118,9 +119,10 @@ func readProd(v *viper.Viper) (*Settings, error) {
AccessKey: authAccessKey,
RefreshKey: authRefreshKey,
},
PineconeSettings: *pineconeSettings,
OpenAISettings: *openAISettings,
AWS: *awsSettings,
ResendSettings: *resendSettings,
Pinecone: *pineconeSettings,
OpenAI: *openAISettings,
AWS: *awsSettings,
Resend: *resendSettings,
Calendar: prodSettings.Calendar,
}, nil
}
34 changes: 34 additions & 0 deletions backend/entities/auth/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ func (a *AuthController) Login(c *fiber.Ctx) error {
return c.Status(http.StatusOK).JSON(user)
}

// Register godoc
//
// @Summary Registers a user
// @Description Registers a user
// @ID register-user
// @Tags auth
// @Accept json
// @Produce json
// @Param userBody body models.CreateUserRequestBody true "User Body"
// @Success 201 {object} models.User
// @Failure 400 {object} error
// @Failure 404 {object} error
// @Failure 500 {object} error
// @Router /auth/register [post]
func (a *AuthController) Register(c *fiber.Ctx) error {
var userBody models.CreateUserRequestBody

if err := c.BodyParser(&userBody); err != nil {
return utilities.InvalidJSON()
}

user, tokens, err := a.authService.Register(userBody)
if err != nil {
return err
}

err = auth.SetResponseTokens(c, tokens)
if err != nil {
return err
}

return c.Status(http.StatusCreated).JSON(user)
}

// Refresh godoc
//
// @Summary Refreshes a user's access token
Expand Down
8 changes: 3 additions & 5 deletions backend/entities/auth/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"github.com/GenerateNU/sac/backend/types"
)

func Auth(params types.RouteParams) AuthServiceInterface {
authService := NewAuthService(params.ServiceParams)
authController := NewAuthController(authService)
func Auth(params types.RouteParams) {
authController := NewAuthController(NewAuthService(params.ServiceParams))

// api/v1/auth/*
auth := params.Router.Group("/auth")

auth.Post("/register", authController.Register)
auth.Post("/logout", authController.Logout)
auth.Post("/login", authController.Login)
auth.Post("/refresh", authController.Refresh)
Expand All @@ -22,6 +22,4 @@ func Auth(params types.RouteParams) AuthServiceInterface {
// TODO: rate limit
auth.Post("/forgot-password", authController.ForgotPassword)
auth.Post("/verify-reset", authController.VerifyPasswordResetToken)

return authService
}
44 changes: 44 additions & 0 deletions backend/entities/auth/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type AuthServiceInterface interface {
GetRole(id string) (*models.UserRole, error)
Register(userBody models.CreateUserRequestBody) (*models.User, *auth.Token, error)
Login(userBody models.LoginUserResponseBody) (*models.User, *auth.Token, error)
Refresh(refreshToken string) (*auth.Token, error)
Logout(c *fiber.Ctx) error
Expand Down Expand Up @@ -79,6 +80,49 @@ func (a *AuthService) Login(loginBody models.LoginUserResponseBody) (*models.Use
return user, tokens, nil
}

func (a *AuthService) Register(userBody models.CreateUserRequestBody) (*models.User, *auth.Token, error) {
if err := utilities.Validate(a.Validate, userBody); err != nil {
return nil, nil, err
}

if err := auth.ValidatePassword(userBody.Password); err != nil {
return nil, nil, err
}

user, err := utilities.MapJsonTags(userBody, &models.User{})
if err != nil {
return nil, nil, err
}

passwordHash, err := auth.ComputeHash(userBody.Password)
if err != nil {
return nil, nil, err
}

user.Email = utilities.NormalizeEmail(userBody.Email)
user.PasswordHash = *passwordHash

if err := a.Integrations.Email.SendWelcomeEmail(fmt.Sprintf("%s %s", user.FirstName, user.LastName), user.Email); err != nil {
return nil, nil, fmt.Errorf("failed to send welcome email: %w", err)
}

user, err = CreateUser(a.DB, user)
if err != nil {
return nil, nil, err
}

if err := a.SendCode(user.Email); err != nil {
return nil, nil, err
}

_, tokens, err := a.Login(models.LoginUserResponseBody{Email: user.Email, Password: userBody.Password})
if err != nil {
return nil, nil, err
}

return user, tokens, nil
}

func (a *AuthService) Refresh(refreshToken string) (*auth.Token, error) {
claims, err := a.JWT.ExtractClaims(refreshToken, auth.RefreshToken)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions backend/entities/auth/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"gorm.io/gorm"
)

func CreateUser(db *gorm.DB, user *models.User) (*models.User, error) {
if err := db.Create(user).Error; err != nil {
if errors.Is(err, gorm.ErrDuplicatedKey) {
return nil, utilities.ErrDuplicate
}
return nil, err
}

return user, nil
}

func GetToken(db *gorm.DB, token string, tokenType models.VerificationType) (*models.Verification, error) {
tokenModel := models.Verification{}
if err := db.Where("token = ? AND type = ?", token, tokenType).First(&tokenModel).Error; err != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/categories/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (c *CategoryService) CreateCategory(categoryBody models.CategoryRequestBody
return nil, err
}

category, err := utilities.MapRequestToModel(categoryBody, &models.Category{})
category, err := utilities.MapJsonTags(categoryBody, &models.Category{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (c *CategoryService) UpdateCategory(id string, categoryBody models.Category
return nil, err
}

category, err := utilities.MapRequestToModel(categoryBody, &models.Category{})
category, err := utilities.MapJsonTags(categoryBody, &models.Category{})
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions backend/entities/clubs/base/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ import (
"github.com/GenerateNU/sac/backend/entities/clubs/member"
"github.com/GenerateNU/sac/backend/entities/clubs/poc"
"github.com/GenerateNU/sac/backend/entities/clubs/tag"
"github.com/GenerateNU/sac/backend/middleware"
"github.com/garrettladley/fiberpaginate"

"github.com/GenerateNU/sac/backend/types"
"github.com/gofiber/fiber/v2"
)

func ClubRoutes(clubParams types.RouteParams) {
clubIDRouter := ClubRouter(clubParams)

// update the router in params
clubParams.Router = clubIDRouter
clubParams.Router = ClubRouter(clubParams)

tag.ClubTag(clubParams)
follower.ClubFollower(clubParams)
Expand All @@ -41,7 +39,11 @@ func ClubRouter(clubParams types.RouteParams) fiber.Router {
clubID := clubs.Group("/:clubID")

clubID.Get("/", clubController.GetClub)
clubID.Patch("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubController.UpdateClub)
clubID.Patch(
"/",
middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")),
clubController.UpdateClub,
)
clubID.Delete("/", clubParams.AuthMiddleware.Authorize(p.DeleteAll), clubController.DeleteClub)

return clubID
Expand Down
4 changes: 2 additions & 2 deletions backend/entities/clubs/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models
return nil, err
}

club, err := utilities.MapRequestToModel(clubBody, &models.Club{})
club, err := utilities.MapJsonTags(clubBody, &models.Club{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, err
}

club, err := utilities.MapRequestToModel(clubBody, &models.Club{})
club, err := utilities.MapJsonTags(clubBody, &models.Club{})
if err != nil {
return nil, err
}
Expand Down
7 changes: 6 additions & 1 deletion backend/entities/clubs/contact/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package contact

import (
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
)

Expand All @@ -11,5 +12,9 @@ func ClubContact(clubParams types.RouteParams) {

// api/v1/clubs/:clubID/contacts/*
clubContacts.Get("/", clubContactController.GetClubContacts)
clubContacts.Put("/", clubParams.AuthMiddleware.ClubAuthorizeById, clubContactController.PutContact)
clubContacts.Put(
"/",
middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")),
clubContactController.PutContact,
)
}
2 changes: 1 addition & 1 deletion backend/entities/clubs/contact/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *ClubContactService) PutClubContact(clubID string, contactBody models.Pu
return nil, err
}

contact, err := utilities.MapRequestToModel(contactBody, &models.Contact{})
contact, err := utilities.MapJsonTags(contactBody, &models.Contact{})
if err != nil {
return nil, err
}
Expand Down
13 changes: 11 additions & 2 deletions backend/entities/clubs/member/routes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package member

import (
"github.com/GenerateNU/sac/backend/middleware"
"github.com/GenerateNU/sac/backend/types"
"github.com/garrettladley/fiberpaginate"
)
Expand All @@ -12,6 +13,14 @@ func ClubMember(clubParams types.RouteParams) {

// api/v1/clubs/:clubID/members/*
clubMembers.Get("/", fiberpaginate.New(), clubMemberController.GetClubMembers)
clubMembers.Post("/:userID", clubParams.AuthMiddleware.ClubAuthorizeById, clubMemberController.CreateClubMember)
clubMembers.Delete("/:userID", clubParams.AuthMiddleware.ClubAuthorizeById, clubMemberController.DeleteClubMember)
clubMembers.Post(
"/:userID",
middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")),
clubMemberController.CreateClubMember,
)
clubMembers.Delete(
"/:userID",
middleware.AttachExtractor(clubParams.AuthMiddleware.ClubAuthorizeById, middleware.ExtractFromParams("clubID")),
clubMemberController.DeleteClubMember,
)
}
Loading

0 comments on commit fdffec7

Please sign in to comment.