-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add batch process for updating status of classes and enrolled_at per ticket 607 #1164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
carddev81
wants to merge
1
commit into
main
Choose a base branch
from
carddev81/ticket_id607_createbatchprocess
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+157
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "UnlockEdv2/src/models" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/nats-io/nats.go" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // handleActivateScheduledClasses is the entrypoint for the daily | ||
| // `tasks.activate_scheduled_classes` job. It flips any class whose scheduled | ||
| // start date has is the current day of the job run (in its facility's local timezone) from Scheduled to | ||
| // Active. The status update goes through the same map-based update the user interface uses | ||
| // so that ProgramClass.AfterUpdate fires and backfills enrolled_at on the | ||
| // class's enrollments. | ||
| func (sh *ServiceHandler) handleActivateScheduledClasses(ctx context.Context, msg *nats.Msg) { | ||
| var body map[string]any | ||
| if err := json.Unmarshal(msg.Data, &body); err != nil { | ||
| logger().Errorf("failed to unmarshal activate_scheduled_classes message: %v", err) | ||
| return | ||
| } | ||
| jobId, ok := body["job_id"].(string) | ||
| if !ok { | ||
| logger().Errorf("job_id not found in activate_scheduled_classes message: %v", body) | ||
| return | ||
| } | ||
| success := sh.activateScheduledClasses(ctx) == nil | ||
| sh.cleanupJob(ctx, nil, jobId, success) | ||
| } | ||
|
|
||
| func (sh *ServiceHandler) activateScheduledClasses(ctx context.Context) error { | ||
| batchUserID, err := sh.systemBatchUserID(ctx) | ||
| if err != nil { //batch id doesn't exist then fail | ||
| logger().Errorf("cannot activate scheduled classes: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| var classIDs []int | ||
| if err := sh.db.WithContext(ctx). | ||
| Model(&models.ProgramClass{}). | ||
| Joins("JOIN facilities f ON f.id = program_classes.facility_id"). | ||
| Where("program_classes.status = ?", models.Scheduled). | ||
| Where("program_classes.archived_at IS NULL"). | ||
| Where("program_classes.start_dt <= (now() AT TIME ZONE f.timezone)::date"). | ||
| Pluck("program_classes.id", &classIDs).Error; err != nil { | ||
| logger().Errorf("failed to query scheduled classes to activate: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| if len(classIDs) == 0 { | ||
| logger().Infoln("no scheduled classes are due for activation") | ||
| return nil | ||
| } | ||
| logger().Infof("activating %d scheduled class(es): %v", len(classIDs), classIDs) | ||
|
|
||
| batchCtx := context.WithValue(ctx, models.UserIDKey, batchUserID) | ||
| enrolledAt := time.Now().UTC() | ||
| if err := sh.db.WithContext(batchCtx).Transaction(func(tx *gorm.DB) error { | ||
| if err := tx. | ||
| Model(&models.ProgramClass{}). | ||
| Where("id IN ?", classIDs). | ||
| Set("class_ids", classIDs). | ||
| Updates(map[string]any{"status": models.Active}).Error; err != nil { | ||
| return err | ||
| } | ||
| return tx. | ||
| Model(&models.ProgramClassEnrollment{}). | ||
| Where("class_id IN ?", classIDs). | ||
| Where("enrollment_status = ?", models.Enrolled). | ||
| Where("enrolled_at IS NULL"). | ||
| Updates(map[string]any{ | ||
| "enrolled_at": enrolledAt, | ||
| "update_user_id": batchUserID, | ||
| }).Error | ||
| }); err != nil { | ||
| logger().Errorf("failed to activate scheduled classes %v: %v", classIDs, err) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (sh *ServiceHandler) systemBatchUserID(ctx context.Context) (uint, error) { | ||
| var user models.User | ||
| if err := sh.db.WithContext(ctx). | ||
| Where("username = ?", "system_batch"). | ||
| First(&user).Error; err != nil { | ||
| return 0, fmt.Errorf("system_batch user not found: %w", err) | ||
| } | ||
| return user.ID, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 860
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 331
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 10652
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 1107
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 9199
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 7052
🏁 Script executed:
Repository: UnlockedLabs/UnlockEdv2
Length of output: 7052
Clarify
Set("class_ids", classIDs)purpose and avoid overlappingenrolled_atbackfillSet("class_ids", classIDs)is required:models.ProgramClass.AfterUpdatecallstx.Get("class_ids")and uses it to updateProgramClassEnrollmentrows whenstatuschanges toActive.AfterUpdatehook already backfillsProgramClassEnrollment.enrolled_atforenrollment_status = Enrolledwhereenrolled_at IS NULL, so the subsequent explicitProgramClassEnrollmentupdate inprovider-middleware/program_classes.go(lines 70–78) is redundant forenrolled_atand may also skip settingupdate_user_iddue to theenrolled_at IS NULLpredicate running after the hook.🤖 Prompt for AI Agents