Skip to content

Commit

Permalink
Merge pull request #252 from PThorpe92/dockerenv
Browse files Browse the repository at this point in the history
fix: add missing user dashboard field, dont return resource
  • Loading branch information
PThorpe92 authored May 28, 2024
2 parents 730655d + 00068ca commit 01b4b34
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
74 changes: 41 additions & 33 deletions backend/src/database/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,44 @@ func (db *DB) DeleteActivity(activityID int) error {
return db.Conn.Delete(&models.Activity{}, activityID).Error
}

type result struct {
ProgramID uint
AltName string
Name string
ProviderPlatformName string
ExternalURL string
Date string
TimeDelta uint
}

func dashboardHelper(results []result) []models.CurrentEnrollment {
enrollmentsMap := make(map[uint]*models.CurrentEnrollment)
for _, result := range results {
if _, exists := enrollmentsMap[result.ProgramID]; !exists {
enrollmentsMap[result.ProgramID] = &models.CurrentEnrollment{
AltName: result.AltName,
Name: result.Name,
ProviderPlatformName: result.ProviderPlatformName,
ExternalURL: result.ExternalURL,
TotalActivityTime: []models.RecentActivity{},
}
}
date, _ := time.Parse("2006-01-02", result.Date)
index := int(time.Since(date).Hours() / 24)
if index >= 0 && index < 7 {
enrollmentsMap[result.ProgramID].TotalActivityTime[index] = models.RecentActivity{
Date: result.Date,
Delta: result.TimeDelta,
}
}
}
var enrollments []models.CurrentEnrollment
for _, enrollment := range enrollmentsMap {
enrollments = append(enrollments, *enrollment)
}
return enrollments
}

func (db *DB) GetUserDashboardInfo(userID int) (models.UserDashboardJoin, error) {
var recentPrograms [3]models.RecentProgram
// first get the users 3 most recently interacted with programs
Expand All @@ -42,6 +80,7 @@ func (db *DB) GetUserDashboardInfo(userID int) (models.UserDashboardJoin, error)
Select(`p.name as program_name,
p.alt_name,
p.thumbnail_url,
p.external_url,
pp.name as provider_platform_name,
COUNT(sub_milestones.id) * 100.0 / p.total_progress_milestones as course_progress`).
Joins(`LEFT JOIN (
Expand All @@ -60,15 +99,7 @@ func (db *DB) GetUserDashboardInfo(userID int) (models.UserDashboardJoin, error)
// then get the users current enrollments
// which is the program.alt_name, program.name, provider_platform.name as provider_platform_name (join on provider_platform_id = provider_platform.id), program.external_url,
// and acitvity.total_activity_time for the last 7 days of activity where activity.user_id = userID and activity.program_id = program.id
var results []struct {
ProgramID uint
AltName string
Name string
ProviderPlatformName string
ExternalURL string
Date string
TimeDelta uint
}
results := []result{}

err = db.Conn.Table("programs p").
Select(`p.id as program_id,
Expand All @@ -86,30 +117,7 @@ func (db *DB) GetUserDashboardInfo(userID int) (models.UserDashboardJoin, error)
if err != nil {
log.Fatalf("Query failed: %v", err)
}
enrollmentsMap := make(map[uint]*models.CurrentEnrollment)
for _, result := range results {
if _, exists := enrollmentsMap[result.ProgramID]; !exists {
enrollmentsMap[result.ProgramID] = &models.CurrentEnrollment{
AltName: result.AltName,
Name: result.Name,
ProviderPlatformName: result.ProviderPlatformName,
ExternalURL: result.ExternalURL,
TotalActivityTime: []models.RecentActivity{},
}
}
date, _ := time.Parse("2006-01-02", result.Date)
index := int(time.Since(date).Hours() / 24)
if index >= 0 && index < 7 {
enrollmentsMap[result.ProgramID].TotalActivityTime[index] = models.RecentActivity{
Date: result.Date,
Delta: result.TimeDelta,
}
}
}
var enrollments []models.CurrentEnrollment
for _, enrollment := range enrollmentsMap {
enrollments = append(enrollments, *enrollment)
}
enrollments := dashboardHelper(results)
if len(enrollments) == 0 {
log.Println("No enrollments found")
var newEnrollments []struct {
Expand Down
9 changes: 2 additions & 7 deletions backend/src/handlers/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package handlers

import (
"UnlockEdv2/src/database"
"UnlockEdv2/src/models"
"net/http"
"strconv"

Expand All @@ -27,9 +25,7 @@ func (srv *Server) HandleUserDashboard(w http.ResponseWriter, r *http.Request) {
srv.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
response := models.Resource[models.UserDashboardJoin]{Message: "successfully retrieved users dashboard info"}
response.Data = append(response.Data, userDashboard)
if err := srv.WriteResponse(w, http.StatusOK, response); err != nil {
if err := srv.WriteResponse(w, http.StatusOK, userDashboard); err != nil {
log.Errorf("user dashboard endpoint: error writing response: %v", err)
srv.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down Expand Up @@ -57,8 +53,7 @@ func (srv *Server) HandleUserCatalogue(w http.ResponseWriter, r *http.Request) {
srv.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
response := models.Resource[database.UserCatalogueJoin]{Message: "successfully retrieved users catalogue info", Data: userCatalogue}
if err := srv.WriteResponse(w, http.StatusOK, response); err != nil {
if err := srv.WriteResponse(w, http.StatusOK, userCatalogue); err != nil {
log.Errorf("user catalogue endpoint: error writing response: %v", err)
srv.ErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down

0 comments on commit 01b4b34

Please sign in to comment.