Skip to content

Commit b4bc716

Browse files
implement goal summary in backend
1 parent bbc2b9d commit b4bc716

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

backend/internal/app/bigquery/domain.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ WHERE
2525
'PullRequestEvent',
2626
'PullRequestReviewEvent',
2727
'IssueCommentEvent',
28-
'PullRequestReviewCommentEvent'
28+
'PullRequestReviewCommentEvent',
29+
'PushEvent'
2930
)
3031
AND (
3132
actor.id IN (%s)

backend/internal/app/contribution/service.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package contribution
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log/slog"
78
"net/http"
89

@@ -76,6 +77,7 @@ type Service interface {
7677
ListMonthlyContributionSummary(ctx context.Context, year int, monthParam int, userId int) ([]MonthlyContributionSummary, error)
7778
ListAllContributionTypes(ctx context.Context) ([]ContributionScore, error)
7879
ConfigureContributionTypeScore(ctx context.Context, configureContributionTypeScore []ConfigureContributionTypeScore) ([]ContributionScore, error)
80+
HandleGoalSynchronization(ctx context.Context, userId int) error
7981
}
8082

8183
func NewService(bigqueryService bigquery.Service, contributionRepository repository.ContributionRepository, repositoryService repoService.Service, userService user.Service, transactionService transaction.Service, goalService goal.Service, httpClient *http.Client) Service {
@@ -153,6 +155,12 @@ func (s *service) ProcessEachContribution(ctx context.Context, contribution Cont
153155
return err
154156
}
155157

158+
err = s.HandleGoalSynchronization(ctx, obtainedContribution.UserId)
159+
if err != nil {
160+
slog.Error("error handling goal synchronization", "error", err)
161+
return err
162+
}
163+
156164
return nil
157165
}
158166

@@ -274,24 +282,6 @@ func (s *service) HandleContributionCreation(ctx context.Context, repositoryID i
274282
return Contribution{}, err
275283
}
276284

277-
err = s.goalService.SyncUserGoalProgressWithContributions(ctx, user.Id)
278-
if err != nil {
279-
slog.Error("error syncing goal progress with contibutions", "error", err)
280-
return obtainedContribution, err
281-
}
282-
283-
err = s.goalService.AllocateBadge(ctx, user.Id)
284-
if err != nil {
285-
slog.Error("error allocating badge", "error", err)
286-
return obtainedContribution, err
287-
}
288-
289-
_, err = s.goalService.CreateUserGoalSummary(ctx, user.Id)
290-
if err != nil {
291-
slog.Error("error creating goal summary for user", "error", err)
292-
return obtainedContribution, err
293-
}
294-
295285
return obtainedContribution, nil
296286
}
297287

@@ -387,3 +377,26 @@ func (s *service) ConfigureContributionTypeScore(ctx context.Context, configureC
387377

388378
return serviceContributionTypeScores, nil
389379
}
380+
381+
func (s *service) HandleGoalSynchronization(ctx context.Context, userId int) error {
382+
err := s.goalService.SyncUserGoalProgressWithContributions(ctx, userId)
383+
if err != nil {
384+
slog.Error("error syncing goal progress with contibutions", "error", err)
385+
return err
386+
}
387+
388+
err = s.goalService.AllocateBadge(ctx, userId)
389+
if err != nil {
390+
slog.Error("error allocating badge", "error", err)
391+
return err
392+
}
393+
394+
fmt.Println("before create user")
395+
_, err = s.goalService.CreateUserGoalSummary(ctx, userId)
396+
if err != nil {
397+
slog.Error("error creating goal summary for user", "error", err)
398+
return err
399+
}
400+
401+
return nil
402+
}

backend/internal/app/goal/service.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package goal
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"log/slog"
78
"time"
89

@@ -315,7 +316,7 @@ func (s *service) SyncUserGoalProgressWithContributions(ctx context.Context, use
315316
return err
316317
}
317318

318-
serviceUserCurrentGoalTargets := make([]UserGoalTarget, 0)
319+
serviceUserCurrentGoalTargets := make([]UserGoalTarget, len(userCurrentGoalTargets))
319320
for i, userCurrentGoalTarget := range userCurrentGoalTargets {
320321
serviceUserCurrentGoalTargets[i] = UserGoalTarget(userCurrentGoalTarget)
321322
}
@@ -390,12 +391,15 @@ func (s *service) UpdateUserGoalStatusMonthly(ctx context.Context) error {
390391
}
391392

392393
func (s *service) CreateUserGoalSummary(ctx context.Context, userId int) (GoalSummary, error) {
394+
fmt.Println("in create user goal summary ")
393395
userIncompleteGoalCount, err := s.goalRepository.CalculateUserIncompleteGoalsUntilDay(ctx, nil, userId)
394396
if err != nil {
395397
slog.Error("error calculating user incomplete goalstatus until day", "error", err)
396398
return GoalSummary{}, err
397399
}
398400

401+
fmt.Println("in create user goal summary 2", userIncompleteGoalCount)
402+
399403
userCurrentGoalStatus, err := s.GetUserCurrentGoalStatus(ctx, userId)
400404
if err != nil {
401405
slog.Error("error getting user current goal status", "error", err)
@@ -417,7 +421,13 @@ func (s *service) CreateUserGoalSummary(ctx context.Context, userId int) (GoalSu
417421
TargetCompleted: totalTargetCompleted,
418422
}
419423

420-
return userMonthlyGoalSummary, nil
424+
createdUserGoalSummary, err := s.goalRepository.CreateUserGoalSummary(ctx, nil, repository.GoalSummary(userMonthlyGoalSummary))
425+
if err != nil {
426+
slog.Error("error creating user goal summary", "error", err)
427+
return GoalSummary{}, err
428+
}
429+
430+
return GoalSummary(createdUserGoalSummary), nil
421431
}
422432

423433
func (s *service) FetchUserGoalSummary(ctx context.Context, userId int) ([]GoalSummary, error) {
@@ -427,7 +437,7 @@ func (s *service) FetchUserGoalSummary(ctx context.Context, userId int) ([]GoalS
427437
return nil, err
428438
}
429439

430-
serviceUserGoalSummary := make([]GoalSummary, 0, len(usersGoalSummary))
440+
serviceUserGoalSummary := make([]GoalSummary, len(usersGoalSummary))
431441
for i, userGoalSummary := range usersGoalSummary {
432442
serviceUserGoalSummary[i] = GoalSummary(userGoalSummary)
433443
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
DROP TABLE IF EXISTS "goal_contribution";
2-
DROP TABLE IF EXISTS "goal";
32
DROP TABLE IF EXISTS "summary";
43
DROP TABLE IF EXISTS "badges";
54
DROP TABLE IF EXISTS "leaderboard_hourly";
@@ -8,3 +7,4 @@ DROP TABLE IF EXISTS "contributions";
87
DROP TABLE IF EXISTS "repositories";
98
DROP TABLE IF EXISTS "contribution_score";
109
DROP TABLE IF EXISTS "users";
10+
DROP TABLE IF EXISTS "goal";

backend/internal/repository/goal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type GoalRepository interface {
3030
GetGoalLevelById(ctx context.Context, tx *sqlx.Tx, goalLevelId int) (GoalLevel, error)
3131
FetchInProgressUserGoalsOfPreviousMonth(ctx context.Context, tx *sqlx.Tx) ([]UserGoal, error)
3232
CalculateUserIncompleteGoalsUntilDay(ctx context.Context, tx *sqlx.Tx, userID int) (int, error)
33+
CreateUserGoalSummary(ctx context.Context, tx *sqlx.Tx, userGoalSummary GoalSummary) (GoalSummary, error)
3334
FetchUserGoalSummary(ctx context.Context, tx *sqlx.Tx, userId int) ([]GoalSummary, error)
3435
}
3536

0 commit comments

Comments
 (0)