Skip to content

Commit

Permalink
fix: cleanup git merge mistakes, broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Sep 28, 2024
1 parent 3ddff90 commit b6e02d3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 100 deletions.
6 changes: 4 additions & 2 deletions backend/src/database/courses.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (db *DB) GetCourse(page, perPage int, search string) (int64, []models.Cours
}
}
} else {
_ = db.Model(&models.Course{}).Count(&total)
if err := db.Model(&models.Course{}).Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "courses")
}
if err := db.Limit(perPage).Offset((page - 1) * perPage).Find(&content).Error; err != nil {
return 0, nil, err
}
Expand Down Expand Up @@ -77,7 +79,7 @@ func (db *DB) DeleteCourse(id int) error {

func (db *DB) GetCourseByProviderPlatformID(id int) ([]models.Course, error) {
content := []models.Course{}
if err := db.Where("provider_platform_id = ?", id).Find(&content).Error; err != nil {
if err := db.Model(&models.Course{}).Find(&content, "provider_platform_id = ?", id).Error; err != nil {
return nil, err
}
return content, nil
Expand Down
44 changes: 16 additions & 28 deletions backend/src/database/provider_user_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package database

import (
"UnlockEdv2/src/models"
"errors"
"fmt"
"strings"

Expand All @@ -18,7 +17,9 @@ func (db *DB) CreateProviderUserMapping(providerUserMapping *models.ProviderUser

func (db *DB) GetProviderUserMapping(userID, providerID int) (*models.ProviderUserMapping, error) {
var providerUserMapping models.ProviderUserMapping
if err := db.Where("user_id = ? AND provider_platform_id = ?", userID, providerID).First(&providerUserMapping).Error; err != nil {
if err := db.Model(&models.ProviderUserMapping{}).
First(&providerUserMapping, "user_id = ? AND provider_platform_id = ?", userID, providerID).
Error; err != nil {
return nil, newNotFoundDBError(err, "provider_user_mappings")
}
return &providerUserMapping, nil
Expand All @@ -35,14 +36,10 @@ func (db *DB) UpdateProviderUserMapping(providerUserMapping *models.ProviderUser
return nil
}

func (db *DB) GetUnmappedUsers(page, perPage int, providerID string, userSearch []string, facilityId uint) (int64, []models.User, error) {
func (db *DB) GetUnmappedUsers(page, perPage int, providerID int, userSearch []string, facilityId uint) (int64, []models.User, error) {
var users []models.User
var total int64

if providerID == "" {
return 0, nil, NewDBError(errors.New("no provider id provided to search unmapped users"), "error getting unmapped users")
}

if len(userSearch) != 0 {
fmt.Println("getting unmapped users, searching for ", userSearch)
users, err := db.getUnmappedProviderUsersWithSearch(providerID, userSearch, facilityId)
Expand All @@ -51,50 +48,41 @@ func (db *DB) GetUnmappedUsers(page, perPage int, providerID string, userSearch
}
return int64(len(users)), users, nil
}
if err := db.Debug().Table("users").Select("*").
Where("users.role = ?", "student").
Where("users.id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", providerID).
Where("facility_id = ?", fmt.Sprintf("%d", facilityId)).
Where("users.deleted_at IS NULL ").
Offset((page - 1) * perPage).
Limit(perPage).Count(&total).Error; err != nil {
if err := db.Model(&models.User{}).
Where("facility_id = ? AND role = ? AND id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", facilityId, "student", providerID).
Count(&total).Error; err != nil {
return 0, nil, NewDBError(err, "error counting unmapped users")
}
if err := db.Debug().Table("users").Select("*").
Where("users.role = ?", "student").
Where("users.id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", providerID).
Where("facility_id = ?", fmt.Sprintf("%d", facilityId)).
if err := db.Model(&models.User{}).
Find(&users, "facility_id = ? AND role = ? AND id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", facilityId, "student", providerID).
Offset((page - 1) * perPage).
Limit(perPage).
Find(&users).Error; err != nil {
return 0, nil, NewDBError(err, "error getting unmapped users")
}

return total, users, nil
}

func (db *DB) getUnmappedProviderUsersWithSearch(providerID string, userSearch []string, facilityId uint) ([]models.User, error) {
func (db *DB) getUnmappedProviderUsersWithSearch(providerID int, userSearch []string, facilityId uint) ([]models.User, error) {
var users []models.User
tx := db.Table("users u").Select("u.*").
Where("u.role = ?", "student").
Where("u.id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", providerID).
Where("facility_id = ?", fmt.Sprintf("%d", facilityId))
tx := db.Model(&models.User{}).
Where("facility_id = ? AND role = ? AND id NOT IN (SELECT user_id FROM provider_user_mappings WHERE provider_platform_id = ?)", facilityId, "student", providerID)

searchCondition := db.DB
for _, search := range userSearch {
split := strings.Split(search, " ")
if len(split) > 1 {
first := "%" + strings.TrimSpace(strings.ToLower(split[0])) + "%"
last := "%" + strings.TrimSpace(strings.ToLower(split[1])) + "%"
searchCondition = searchCondition.Or(db.Where("u.name_first ILIKE ? OR u.name_last ILIKE ?", first, first).Or("u.name_first ILIKE ? OR u.name_last ILIKE ?", last, last))
searchCondition = searchCondition.Or(db.Where("name_first LIKE ? OR name_last LIKE ?", first, first).Or("name_first LIKE ? OR name_last LIKE ?", last, last))
continue
}
search = "%" + strings.TrimSpace(strings.ToLower(search)) + "%"
if strings.Contains(search, "@") {
searchCondition = searchCondition.Or("u.email ILIKE ?", search)
searchCondition = searchCondition.Or("email LIKE ?", search)
continue
}
searchCondition = searchCondition.Or("u.name_first ILIKE ?", search).Or("u.name_last ILIKE ?", search).Or("u.username ILIKE ?", search)
searchCondition = searchCondition.Or("name_first LIKE ?", search).Or("name_last LIKE ?", search).Or("username LIKE ?", search)
}
tx = tx.Where(searchCondition)

Expand All @@ -108,7 +96,7 @@ func (db *DB) getUnmappedProviderUsersWithSearch(providerID string, userSearch [

func (db *DB) GetAllProviderMappingsForUser(userID int) ([]models.ProviderUserMapping, error) {
var providerUserMappings []models.ProviderUserMapping
if err := db.Where("user_id = ?", userID).Find(&providerUserMappings).Error; err != nil {
if err := db.Model(&models.ProviderUserMapping{}).Find(&providerUserMappings, "user_id = ?", userID).Error; err != nil {
return nil, newGetRecordsDBError(err, "provider_user_mappings")
}
return providerUserMappings, nil
Expand Down
68 changes: 30 additions & 38 deletions backend/src/database/section_enrollments.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import "UnlockEdv2/src/models"
func (db *DB) GetProgramSectionEnrollmentsForUser(userID, page, perPage int) (int64, []models.ProgramSectionEnrollment, error) {
content := []models.ProgramSectionEnrollment{}
var total int64
if err := db.Find(&content, "user_id = ?", userID).Count(&total).Error; err != nil {
tx := db.Model(&models.ProgramSectionEnrollment{}).Where("user_id = ?", userID)

if err := tx.Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
if err := tx.Find(&content).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
return total, content, nil
Expand All @@ -22,7 +27,11 @@ func (db *DB) GetProgramSectionEnrollmentsByID(id int) (*models.ProgramSectionEn
func (db *DB) GetEnrollmentsForSection(page, perPage, sectionId int) (int64, []models.ProgramSectionEnrollment, error) {
content := []models.ProgramSectionEnrollment{}
var total int64
if err := db.Find(&content, "section_id = ?", sectionId).Count(&total).Limit(page).Offset((page - 1) * perPage).Error; err != nil {
tx := db.Model(&models.ProgramSectionEnrollment{}).Where("section_id = ?", sectionId)
if err := tx.Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
if err := tx.Find(&content).Limit(page).Offset((page - 1) * perPage).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
return total, content, nil
Expand All @@ -31,19 +40,13 @@ func (db *DB) GetEnrollmentsForSection(page, perPage, sectionId int) (int64, []m
func (db *DB) GetProgramSectionEnrollmentsForFacility(page, perPage int, facilityID uint) (int64, []models.ProgramSectionEnrollment, error) {
content := []models.ProgramSectionEnrollment{}
var total int64 //count
if err := db.Table("program_section_enrollments pse").
Select("*").
Joins("JOIN program_sections ps ON pse.section_id = ps.id and ps.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityID).
Where("pse.deleted_at IS NULL").
Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
if err := db.Table("program_section_enrollments pse").
Select("pse.*").
Joins("JOIN program_sections ps ON pse.section_id = ps.id and ps.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityID).
Limit(perPage).
tx := db.Model(&models.ProgramSectionEnrollment{}).
Joins("JOIN program_sections ps ON program_section_enrollments.section_id = ps.id and ps.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityID)

_ = tx.Count(&total)

if err := tx.Limit(perPage).
Offset((page - 1) * perPage).
Find(&content).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
Expand Down Expand Up @@ -84,21 +87,15 @@ func (db *DB) UpdateProgramSectionEnrollments(content *models.ProgramSectionEnro
func (db *DB) GetProgramSectionEnrollmentssForProgram(page, perPage, facilityID, programID int) (int64, []models.ProgramSectionEnrollment, error) {
content := []models.ProgramSectionEnrollment{}
var total int64
if err := db.Table("program_section_enrollments pse").
Select("*").
Joins("JOIN program_sections ps ON pse.section_id = ps.id and ps.deleted_at IS NULL").
tx := db.Model(&models.ProgramSectionEnrollment{}).
Joins("JOIN program_sections ps ON program_section_enrollments.section_id = ps.id and ps.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityID).
Where("ps.program_id = ?", programID).
Where("pse.deleted_at IS NULL").
Count(&total).Error; err != nil {
Where("ps.program_id = ?", programID)

if err := tx.Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
}
if err := db.Table("program_section_enrollments pse").
Select("pse.*").
Joins("JOIN program_sections ps ON pse.section_id = ps.id and ps.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityID).
Where("ps.program_id = ?", programID).
Limit(perPage).
if err := tx.Limit(perPage).
Offset((page - 1) * perPage).
Find(&content).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "program section enrollments")
Expand All @@ -109,23 +106,18 @@ func (db *DB) GetProgramSectionEnrollmentssForProgram(page, perPage, facilityID,
func (db *DB) GetProgramSectionEnrollmentsAttendance(page, perPage, id int) (int64, []models.ProgramSectionEventAttendance, error) {
content := []models.ProgramSectionEventAttendance{}
var total int64
if err := db.Table("program_section_event_attendance att").
tx := db.Table("program_section_event_attendance att").
Select("*").
Joins("JOIN program_section_events evt ON att.event_id = evt.id and evt.deleted_at IS NULL").
Joins("JOIN program_sections ps ON evt.section_id = ps.id and ps.deleted_at IS NULL").
Joins("JOIN program_section_enrollments pse ON ps.id = pse.section_id and pse.deleted_at IS NULL").
Where("pse.id = ?", id).
Where("att.deleted_at IS NULL").
Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "section event attendance")
Where("att.deleted_at IS NULL")

if err := tx.Count(&total).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "section event")
}
if err := db.Table("program_section_event_attendance att").
Select("att.*").
Joins("JOIN program_section_events evt ON att.event_id = evt.id and evt.deleted_at IS NULL").
Joins("JOIN program_sections ps ON evt.section_id = ps.id and ps.deleted_at IS NULL").
Joins("JOIN program_section_enrollments pse ON ps.id = pse.section_id and pse.deleted_at IS NULL").
Where("pse.id = ?", id).
Limit(perPage).
if err := tx.Limit(perPage).
Offset((page - 1) * perPage).
Find(&content).Error; err != nil {
return 0, nil, newNotFoundDBError(err, "section event attendance")
Expand Down
36 changes: 18 additions & 18 deletions backend/src/database/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package database
import (
"UnlockEdv2/src/models"
"errors"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -17,24 +16,24 @@ func (db *DB) GetCurrentUsers(page, itemsPerPage int, facilityId uint, order str
if search != "" {
return db.SearchCurrentUsers(page, itemsPerPage, facilityId, order, search, role)
}

offset := (page - 1) * itemsPerPage
var count int64
var users []models.User
tx := db.Model(&models.User{}).
Where("facility_id = ?", facilityId)
if err := db.Model(&models.User{}).Where("facility_id = ?", facilityId).Count(&count).Error; err != nil {
return 0, nil, newGetRecordsDBError(err, "users")
}
tx := db.Model(&models.User{})
switch role {
case "admin":
tx = tx.Where("role = 'admin'")
case "student":
tx = tx.Where("role = 'student'")
}
if err := tx.
Count(&count).
if err := tx.Find(&users, "facility_id = ?", facilityId).
Order(order).
Offset(offset).
Limit(itemsPerPage).
Order(order).
Find(&users).Error; err != nil {
Error; err != nil {
log.Printf("Error fetching users: %v", err)
return 0, nil, newGetRecordsDBError(err, "users")
}
Expand All @@ -47,21 +46,23 @@ func (db *DB) SearchCurrentUsers(page, itemsPerPage int, facilityId uint, order,
var count int64
offset := (page - 1) * itemsPerPage
search = strings.TrimSpace(search)
likeSearch := "%" + search + "%"
tx := db.Model(&models.User{}).
Where("facility_id = ?", fmt.Sprintf("%d", facilityId)).
Where("name_first ILIKE ? OR username ILIKE ? OR name_last ILIKE ?", likeSearch, likeSearch, likeSearch)
likeSearch := "%" + strings.ToLower(search) + "%"
tx := db.Model(&models.User{})
switch role {
case "admin":
tx = tx.Where("role = 'admin'")
case "student":
tx = tx.Where("role = 'student'")
}
if err := tx.Count(&count).Error; err != nil {
return 0, nil, newGetRecordsDBError(err, "users")
}
if err := tx.
Find(&users, "facility_id = ? AND (name_first LIKE ? OR username LIKE ? OR name_last LIKE ?)", facilityId, likeSearch, likeSearch, likeSearch).
Order(order).
Offset(offset).
Limit(itemsPerPage).
Find(&users).Count(&count).Error; err != nil {
Error; err != nil {
log.Printf("Error fetching users: %v", err)
return 0, nil, newGetRecordsDBError(err, "users")
}
Expand All @@ -71,18 +72,17 @@ func (db *DB) SearchCurrentUsers(page, itemsPerPage int, facilityId uint, order,
first := "%" + split[0] + "%"
last := "%" + split[1] + "%"
if err := db.Model(&models.User{}).
Where("facility_id = ?", fmt.Sprintf("%d", facilityId)).
Where("(name_first ILIKE ? AND name_last ILIKE ?) OR (name_first ILIKE ? AND name_last ILIKE ?)", first, last, last, first).
Where("facility_id = ?", facilityId).
Where("(name_first LIKE ? AND name_last LIKE ?) OR (name_first LIKE ? AND name_last LIKE ?)", first, last, last, first).
Order(order).
Offset(offset).
Limit(itemsPerPage).
Find(&users).Count(&count).Error; err != nil {
Find(&users).Error; err != nil {
log.Printf("Error fetching users: %v", err)
return 0, nil, newGetRecordsDBError(err, "users")
}
}
}

log.Printf("found %d users", count)
return count, users, nil
}
Expand All @@ -104,7 +104,7 @@ func (db *DB) GetUsersWithLogins(page, per_page int, facilityId uint) (int64, []
var users []models.User
var count int64
if err := db.Model(&models.User{}).
Offset((page-1)*per_page).Limit(per_page).Count(&count).Find(&users, "facility_id = ?", fmt.Sprintf("%d", facilityId)).Error; err != nil {
Offset((page-1)*per_page).Limit(per_page).Count(&count).Find(&users, "facility_id = ?", facilityId).Error; err != nil {
return 0, nil, newGetRecordsDBError(err, "users")
}
var userWithLogins []UserWithLogins
Expand Down
4 changes: 2 additions & 2 deletions backend/src/handlers/section_enrollments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ func getProgramSectionEnrollmentWithAttendance(facilityId uint) map[string]any {
if err := server.Db.Table("program_section_enrollments pse").
Select("pse.*").
Joins("JOIN program_sections ps ON pse.section_id = ps.id and ps.deleted_at IS NULL").
Joins("join program_section_events evt ON ps.id = evt.section_id and evt.deleted_at IS NULL").
Joins("join program_section_event_attendance att ON evt.id = att.event_id and att.deleted_at IS NULL").
Joins("JOIN program_section_events evt ON ps.id = evt.section_id and evt.deleted_at IS NULL").
Joins("JOIN program_section_event_attendance att ON evt.id = att.event_id and att.deleted_at IS NULL").
Where("ps.facility_id = ?", facilityId).
Find(&programSectionEnrollments).Error; err != nil {
form["err"] = err
Expand Down
Loading

0 comments on commit b6e02d3

Please sign in to comment.