Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions backend/internal/app/auth/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ type GithubUserResponse struct {
IsAdmin bool `json:"isAdmin"`
}

type AdminLoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}

type Admin struct {
User
JwtToken string `json:"jwtToken"`
}
24 changes: 24 additions & 0 deletions backend/internal/app/auth/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
Expand All @@ -20,6 +21,7 @@ type Handler interface {
GithubOAuthLoginUrl(w http.ResponseWriter, r *http.Request)
GithubOAuthLoginCallback(w http.ResponseWriter, r *http.Request)
GetLoggedInUser(w http.ResponseWriter, r *http.Request)
LoginAdmin(w http.ResponseWriter, r *http.Request)
}

func NewHandler(authService Service, appConfig config.AppConfig) Handler {
Expand Down Expand Up @@ -82,3 +84,25 @@ func (h *handler) GetLoggedInUser(w http.ResponseWriter, r *http.Request) {

response.WriteJson(w, http.StatusOK, "logged in user fetched successfully", userInfo)
}

func (h *handler) LoginAdmin(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

var requestBody AdminLoginRequest
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
slog.Error(apperrors.ErrFailedMarshal.Error(), "error", err)
response.WriteJson(w, http.StatusBadRequest, apperrors.ErrInvalidRequestBody.Error(), nil)
return
}

adminInfo, err := h.authService.VerifyAdminCredentials(ctx, requestBody)
if err != nil {
slog.Error("failed to verify admin credentials", "error", err)
status, errorMessage := apperrors.MapError(apperrors.ErrContextValue)
response.WriteJson(w, status, errorMessage, nil)
return
}

response.WriteJson(w, http.StatusOK, "admin logged in successfully", adminInfo)
}
29 changes: 29 additions & 0 deletions backend/internal/app/auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/joshsoftware/code-curiosity-2025/internal/config"
"github.com/joshsoftware/code-curiosity-2025/internal/pkg/apperrors"
"github.com/joshsoftware/code-curiosity-2025/internal/pkg/jwt"
"golang.org/x/crypto/bcrypt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
)
Expand All @@ -23,6 +24,7 @@ type Service interface {
GithubOAuthLoginUrl(ctx context.Context) string
GithubOAuthLoginCallback(ctx context.Context, code string) (string, error)
GetLoggedInUser(ctx context.Context, userId int) (User, error)
VerifyAdminCredentials(ctx context.Context, adminCredentials AdminLoginRequest) (Admin, error)
}

func NewService(userService user.Service, appCfg config.AppConfig) Service {
Expand Down Expand Up @@ -102,3 +104,30 @@ func (s *service) GetLoggedInUser(ctx context.Context, userId int) (User, error)

return User(user), nil
}

func (s *service) VerifyAdminCredentials(ctx context.Context, adminCredentials AdminLoginRequest) (Admin, error) {
adminInfo, err := s.userService.GetLoggedInAdmin(ctx, user.AdminLoginRequest(adminCredentials))
if err != nil {
slog.Error("failed to verify admin", "error", err)
return Admin{}, err
}

err = bcrypt.CompareHashAndPassword([]byte(adminInfo.Password), []byte(adminCredentials.Password))
if err != nil {
slog.Error("failed to verify admin, invalid password", "error", err)
return Admin{}, apperrors.ErrInvalidCredentials
}

jwtToken, err := jwt.GenerateJWT(adminInfo.Id, adminInfo.IsAdmin, s.appCfg)
if err != nil {
slog.Error("failed to generate jwt token", "error", err)
return Admin{}, apperrors.ErrInternalServer
}

admin := Admin{
User: User(adminInfo),
JwtToken: jwtToken,
}

return admin, nil
}
5 changes: 5 additions & 0 deletions backend/internal/app/contribution/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,8 @@ type FetchUserContributionsResponse struct {
Contribution
Repository
}

type ConfigureContributionTypeScore struct {
ContributionType string `json:"contributionType"`
Score int `json:"score"`
}
48 changes: 48 additions & 0 deletions backend/internal/app/contribution/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package contribution

import (
"encoding/json"
"log/slog"
"net/http"

Expand All @@ -17,6 +18,8 @@ type handler struct {
type Handler interface {
FetchUserContributions(w http.ResponseWriter, r *http.Request)
ListMonthlyContributionSummary(w http.ResponseWriter, r *http.Request)
ListAllContributionTypes(w http.ResponseWriter, r *http.Request)
ConfigureContributionTypeScore(w http.ResponseWriter, r *http.Request)
}

func NewHandler(contributionService Service) Handler {
Expand Down Expand Up @@ -88,3 +91,48 @@ func (h *handler) ListMonthlyContributionSummary(w http.ResponseWriter, r *http.

response.WriteJson(w, http.StatusOK, "contribution type overview for month fetched successfully", monthlyContributionSummary)
}

func (h *handler) ListAllContributionTypes(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

contributionTypes, err := h.contributionService.ListAllContributionTypes(ctx)
if err != nil {
slog.Error("error fetching all contribution types", "error", err)
status, errorMessage := apperrors.MapError(err)
response.WriteJson(w, status, errorMessage, nil)
return
}

response.WriteJson(w, http.StatusOK, "all contribution types fetched successfully", contributionTypes)
}

func (h *handler) ConfigureContributionTypeScore(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

// isAdminValue := ctx.Value(middleware.IsAdminKey)
// isAdmin, ok := isAdminValue.(bool)
// if !ok {
// slog.Error("error verifying admin from context")
// status, errorMessage := apperrors.MapError(apperrors.ErrContextValue)
// response.WriteJson(w, status, errorMessage, nil)
// return
// }

var configureContributionTypeScores []ConfigureContributionTypeScore
err := json.NewDecoder(r.Body).Decode(&configureContributionTypeScores)
if err != nil {
slog.Error(apperrors.ErrFailedMarshal.Error(), "error", err)
response.WriteJson(w, http.StatusBadRequest, apperrors.ErrInvalidRequestBody.Error(), nil)
return
}

contributionTypeScores, err := h.contributionService.ConfigureContributionTypeScore(ctx, configureContributionTypeScores)
if err != nil {
slog.Error("error configuring contribution type scores", "error", err)
status, errorMessage := apperrors.MapError(err)
response.WriteJson(w, status, errorMessage, nil)
return
}

response.WriteJson(w, http.StatusOK, "contribution types fscores configured successfully", contributionTypeScores)
}
37 changes: 37 additions & 0 deletions backend/internal/app/contribution/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Service interface {
FetchUserContributions(ctx context.Context, userId int) ([]FetchUserContributionsResponse, error)
GetContributionByGithubEventId(ctx context.Context, githubEventId string) (Contribution, error)
ListMonthlyContributionSummary(ctx context.Context, year int, monthParam int, userId int) ([]MonthlyContributionSummary, error)
ListAllContributionTypes(ctx context.Context) ([]ContributionScore, error)
ConfigureContributionTypeScore(ctx context.Context, configureContributionTypeScore []ConfigureContributionTypeScore) ([]ContributionScore, error)
}

func NewService(bigqueryService bigquery.Service, contributionRepository repository.ContributionRepository, repositoryService repoService.Service, userService user.Service, transactionService transaction.Service, httpClient *http.Client) Service {
Expand Down Expand Up @@ -310,3 +312,38 @@ func (s *service) ListMonthlyContributionSummary(ctx context.Context, year int,

return serviceMonthlyContributionSummaries, nil
}

func (s *service) ListAllContributionTypes(ctx context.Context) ([]ContributionScore, error) {
contributionTypes, err := s.contributionRepository.GetAllContributionTypes(ctx, nil)
if err != nil {
slog.Error("error fetching all contribution types", "error", err)
return nil, err
}

serviceContributionTypes := make([]ContributionScore, len(contributionTypes))
for i, c := range contributionTypes {
serviceContributionTypes[i] = ContributionScore(c)
}

return serviceContributionTypes, nil
}

func (s *service) ConfigureContributionTypeScore(ctx context.Context, configureContributionTypeScore []ConfigureContributionTypeScore) ([]ContributionScore, error) {
repoConfigureContributionScore := make([]repository.ConfigureContributionTypeScore, len(configureContributionTypeScore))
for i, c := range configureContributionTypeScore {
repoConfigureContributionScore[i] = repository.ConfigureContributionTypeScore(c)
}

contributionTypeScores, err := s.contributionRepository.UpdateContributionTypeScore(ctx, nil, repoConfigureContributionScore)
if err != nil {
slog.Error("error updating contritbution types score", "error", err)
return nil, err
}

serviceContributionTypeScores := make([]ContributionScore, len(contributionTypeScores))
for i, c := range contributionTypeScores {
serviceContributionTypeScores[i] = ContributionScore(c)
}

return serviceContributionTypeScores, nil
}
6 changes: 6 additions & 0 deletions backend/internal/app/goal/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ type CustomGoalLevelTarget struct {
ContributionType string `json:"contributionType"`
Target int `json:"target"`
}

type UserGoalLevelProgress struct {
ContributionType string `json:"contributionType"`
TargetCount int `json:"targetCount"`
AchievedCount int `json:"achievedCount"`
}
21 changes: 11 additions & 10 deletions backend/internal/app/goal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type handler struct {

type Handler interface {
ListGoalLevels(w http.ResponseWriter, r *http.Request)
ListGoalLevelTargets(w http.ResponseWriter, r *http.Request)
GetUserActiveGoalLevel(w http.ResponseWriter, r *http.Request)
CreateCustomGoalLevelTarget(w http.ResponseWriter, r *http.Request)
ListGoalLevelAchievedTarget(w http.ResponseWriter, r *http.Request)
ListUserGoalLevelProgress(w http.ResponseWriter, r *http.Request)
}

func NewHandler(goalService Service) Handler {
Expand All @@ -41,7 +41,7 @@ func (h *handler) ListGoalLevels(w http.ResponseWriter, r *http.Request) {
response.WriteJson(w, http.StatusOK, "goal levels fetched successfully", gaols)
}

func (h *handler) ListGoalLevelTargets(w http.ResponseWriter, r *http.Request) {
func (h *handler) GetUserActiveGoalLevel(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

userIdCtxVal := ctx.Value(middleware.UserIdKey)
Expand All @@ -53,15 +53,15 @@ func (h *handler) ListGoalLevelTargets(w http.ResponseWriter, r *http.Request) {
return
}

goalLevelTargets, err := h.goalService.ListGoalLevelTargetDetail(ctx, userId)
userGoalLevel, err := h.goalService.GetUserActiveGoalLevel(ctx, userId)
if err != nil {
slog.Error("error fetching goal level targets", "error", err)
slog.Error("error fetching users active goal level", "error", err)
status, errorMessage := apperrors.MapError(err)
response.WriteJson(w, status, errorMessage, nil)
return
}

response.WriteJson(w, http.StatusOK, "goal level targets fetched successfully", goalLevelTargets)
response.WriteJson(w, http.StatusOK, "user active goal level fetched successfully", userGoalLevel)
}

func (h *handler) CreateCustomGoalLevelTarget(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -94,7 +94,7 @@ func (h *handler) CreateCustomGoalLevelTarget(w http.ResponseWriter, r *http.Req
response.WriteJson(w, http.StatusOK, "custom goal level targets created successfully", createdCustomGoalLevelTargets)
}

func (h *handler) ListGoalLevelAchievedTarget(w http.ResponseWriter, r *http.Request) {
func (h *handler) ListUserGoalLevelProgress(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

userIdCtxVal := ctx.Value(middleware.UserIdKey)
Expand All @@ -106,12 +106,13 @@ func (h *handler) ListGoalLevelAchievedTarget(w http.ResponseWriter, r *http.Req
return
}

goalLevelAchievedTarget, err := h.goalService.ListGoalLevelAchievedTarget(ctx, userId)
userGoalLevelProgress, err := h.goalService.ListUserGoalLevelProgress(ctx, userId)
if err != nil {
slog.Error("error failed to list goal level achieved targets", "error", err)
slog.Error("error failed to fetch user goal level progress", "error", err)
response.WriteJson(w, http.StatusBadRequest, err.Error(), nil)
return
}

response.WriteJson(w, http.StatusOK, "goal level achieved targets fetched successfully", goalLevelAchievedTarget)
response.WriteJson(w, http.StatusOK, "user goal level progress fetched successfully", userGoalLevelProgress)

}
Loading