-
Notifications
You must be signed in to change notification settings - Fork 5
Expand Salesforce sync to SchoolClass, ClassTeacher, and Lesson #851
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ce12b7
Generalize ensure_parent_synced! onto SalesforceSyncJob base
fspeirs 1fd4780
Add Heroku Connect mirror models for SchoolClass, Lesson, ClassTeacher
fspeirs a9f5669
Add SchoolClass, ClassTeacher, Lesson sync jobs + backfill rake tasks
fspeirs 3963d79
Wire Salesforce sync callbacks across classroom models
fspeirs ef85761
Count Experience CS Scratch completions toward lesson completion
fspeirs f57968b
Document Salesforce / Heroku Connect sync workflow in AGENTS.md
fspeirs 63d1a36
Address PR #851 review: tighten sync triggers and trim model comments
fspeirs aca52ff
Redefine Lesson__c.numberofassignedprojects__c as student-visibility …
fspeirs a0bbbd0
Simplify a comment on ClassStudent.
fspeirs 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class ClassTeacherSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::ClassTeacher | ||
|
|
||
| FIELD_MAPPINGS = { | ||
| contactclassroomaffiliationuuid__c: :id, | ||
| classroom__r__classroomuuid__c: :school_class_id, | ||
| contact_teacher__r__pi_accounts_unique_id__c: :teacher_id, | ||
| createdat__c: :created_at, | ||
| updatedat__c: :updated_at | ||
| }.freeze | ||
|
|
||
| def perform(class_teacher_id:) | ||
| class_teacher = ::ClassTeacher.find(class_teacher_id) | ||
|
|
||
| ensure_parent_synced!(Salesforce::SchoolClass, :classroomuuid__c, class_teacher.school_class_id, 'Classroom__c') | ||
| ensure_parent_synced!(Salesforce::Contact, :pi_accounts_unique_id__c, class_teacher.teacher_id, 'Contact') | ||
|
|
||
| sf_class_teacher = Salesforce::ClassTeacher.find_or_initialize_by( | ||
| contactclassroomaffiliationuuid__c: class_teacher_id | ||
| ) | ||
| sf_class_teacher.attributes = sf_class_teacher_attributes(class_teacher:) | ||
|
|
||
| sf_class_teacher.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sf_class_teacher_attributes(class_teacher:) | ||
| mapped_attributes(class_teacher:).to_h do |sf_field, value| | ||
| value = truncate_value(sf_field:, value:) if value.is_a?(String) | ||
|
|
||
| [sf_field, value] | ||
| end | ||
| end | ||
|
|
||
| def mapped_attributes(class_teacher:) | ||
| FIELD_MAPPINGS.transform_values do |class_teacher_field| | ||
| class_teacher.send(class_teacher_field) | ||
| end | ||
| end | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:class_teacher_id] | ||
| end | ||
| end |
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,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class LessonSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::Lesson | ||
|
|
||
| FIELD_MAPPINGS = { | ||
| lesson_uuid__c: :id, | ||
| classroom__r__classroomuuid__c: :school_class_id, | ||
| lessontitle__c: :name, | ||
| createdat__c: :created_at, | ||
| updatedat__c: :updated_at | ||
| }.freeze | ||
|
|
||
| def perform(lesson_id:) | ||
| lesson = ::Lesson.find(lesson_id) | ||
| return if lesson.school_class_id.blank? | ||
|
|
||
| ensure_parent_synced!(Salesforce::SchoolClass, :classroomuuid__c, lesson.school_class_id, 'Classroom__c') | ||
|
|
||
| sf_lesson = Salesforce::Lesson.find_or_initialize_by(lesson_uuid__c: lesson_id) | ||
| sf_lesson.attributes = sf_lesson_attributes(lesson:) | ||
|
|
||
| sf_lesson.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sf_lesson_attributes(lesson:) | ||
| mapped_attributes(lesson:).merge( | ||
| teacherprojecttitle__c: lesson.project&.name, | ||
| teacherprojecttype__c: lesson.project&.project_type, | ||
| numberofassignedprojects__c: assigned_projects_count(lesson), | ||
| # Sum of the two completion paths: state-machine `:submitted` (Code Editor flow) | ||
| # and `school_projects.finished` (Experience CS flow). They are mutually exclusive | ||
| # per project, so the sum is safe. | ||
| numberofcompletedprojects__c: lesson.submitted_projects_count + lesson.finished_projects_count, | ||
| lastsyncdate__c: Time.current | ||
| ).to_h do |sf_field, value| | ||
| value = truncate_value(sf_field:, value:) if value.is_a?(String) | ||
|
|
||
| [sf_field, value] | ||
| end | ||
| end | ||
|
|
||
| def mapped_attributes(lesson:) | ||
| FIELD_MAPPINGS.transform_values do |lesson_field| | ||
| lesson.send(lesson_field) | ||
| end | ||
| end | ||
|
|
||
| # A lesson is "assigned" to every student in its class iff it's visible to them | ||
| # (visibility == 'students'). Other visibilities aren't assigned to students at all. | ||
| def assigned_projects_count(lesson) | ||
| return 0 unless lesson.visibility == 'students' | ||
|
|
||
| lesson.school_class.students.count | ||
| end | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:lesson_id] | ||
| end | ||
| end |
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,47 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class SchoolClassSyncJob < SalesforceSyncJob | ||
| MODEL_CLASS = Salesforce::SchoolClass | ||
|
|
||
| FIELD_MAPPINGS = { | ||
| classroomuuid__c: :id, | ||
| editor__r__editoruuid__c: :school_id, | ||
| classroomtitle__c: :name, | ||
| createdat__c: :created_at, | ||
| updatedat__c: :updated_at | ||
| }.freeze | ||
|
|
||
| def perform(school_class_id:) | ||
| school_class = ::SchoolClass.find(school_class_id) | ||
|
|
||
| ensure_parent_synced!(Salesforce::School, :editoruuid__c, school_class.school_id, 'Editor__c') | ||
|
|
||
| sf_school_class = Salesforce::SchoolClass.find_or_initialize_by(classroomuuid__c: school_class_id) | ||
| sf_school_class.attributes = sf_school_class_attributes(school_class:) | ||
|
|
||
| sf_school_class.save! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def sf_school_class_attributes(school_class:) | ||
| mapped_attributes(school_class:).merge( | ||
| numberofmembers__c: school_class.students.count, | ||
| lastsyncdate__c: Time.current | ||
| ).to_h do |sf_field, value| | ||
| value = truncate_value(sf_field:, value:) if value.is_a?(String) | ||
|
|
||
| [sf_field, value] | ||
| end | ||
| end | ||
|
|
||
| def mapped_attributes(school_class:) | ||
| FIELD_MAPPINGS.transform_values do |school_class_field| | ||
| school_class.send(school_class_field) | ||
| end | ||
| end | ||
|
|
||
| def concurrency_key_id = arguments.first.with_indifferent_access[:school_class_id] | ||
| end | ||
| end |
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,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class ClassTeacher < Salesforce::Base | ||
| self.table_name = 'salesforce.contact_classroom_affiliation__c' | ||
| self.primary_key = :contactclassroomaffiliationuuid__c | ||
| end | ||
| end |
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,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class Lesson < Salesforce::Base | ||
| self.table_name = 'salesforce.lesson__c' | ||
| self.primary_key = :lesson_uuid__c | ||
| end | ||
| end |
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,8 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Salesforce | ||
| class SchoolClass < Salesforce::Base | ||
| self.table_name = 'salesforce.classroom__c' | ||
| self.primary_key = :classroomuuid__c | ||
| end | ||
| end |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.