Commit 1f96b90
authored
Implement API Endpoints for bulk-import of Schools (#622)
## Status
- Related to
RaspberryPiFoundation/experience-cs#1309
## Points for consideration:
- 🧠 Review - Reviewers may find it easier to review commit-by-commit
- ❓ Exclude - do we want to keep the wrapper scripts and test data?
- 📖 Documentation - should we migrate the documentation for CSMs out of
here?
- 🔒 Security - calling these endpoints should only be possible for users
with `editor-admin` or `experience-cs-admin` roles. All other roles and
none should return `401`.
- 🐌 Performance - The schools are created asynchronously through
GoodJob, so immediate performance of the endpoints should not be an
issue, however this code does make an N+1 query to `userinfo` to look up
school owners by email - there is no bulk option for this.
## What's changed?
- Introduction of two new endpoints to allow import and validation of
schools via CSV files:
- `/api/schools/import` - for POSTing CSV
- `/api/school_import_jobs` - for tracking job status
- Both endpoints require a user to have the `editor-admin` or
`experience-cs-admin` roles.
- An `ImportSchoolsJob` GoodJob job type that will execute the import
asynchronously
- A `SchoolImportResult` record type that tracks the results of
`ImportSchoolsJob`s.
## Steps to perform after deploying to production
There is a database migration involved in this to add the table that
tracks `SchoolImportResult`.
## Tasks Outstanding
- [x] Improve consistency of naming (I have used `import_schools` in
some places and `school_import` in others).
- [x] Check that we validate all of the data correctly
- [x] Stress-test with some very large imports to get a sense of upper
bounds.
- [x] Write a tool to make it easy to get an `editor-api` OAuth token
- [x] Merge the branch that adds UI in the `/admin` path
- [x] Remove the wrapper scripts in favour of web-based UI
- [x] Remove test data commit.
- [x] Rewrite the CSM guide to refer to the new Admin UI.
## Follow-On Work
~This is not the final form of this feature. In a follow-up task in the
next sprint we intend to add a UI under the `/admin` route to make it
easier for non-developers to access this endpoint. The goal in the first
instance is to develop and test the endpoints separately from the UI
that accesses them.~
This PR now contains all the work necessary to make a complete feature,
including web-based UI.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Adds CSV-based bulk school import with admin UI, async GoodJob
processing, job status API, and persisted results (with CSV export),
restricted to editor/experience CS admins.
>
> - **API/Services**:
> - `POST /api/schools/import` to start imports; `GET
/api/school_import_jobs/:id` to track status.
> - CSV parsing/validation via `School::ImportBatch` with structured
`SchoolImportError` codes.
> - **Background Processing**:
> - `SchoolImportJob` (queue: `import_schools_job`) creates/verifies
schools, assigns owner role, and saves results to `SchoolImportResult`.
> - GoodJob queues updated in `config/initializers/good_job.rb`.
> - **Admin UI**:
> - New `admin/school_import_results` pages (index/show/new) for upload,
status, results, and CSV download.
> - Custom Administrate fields: `StatusField`, `ResultsSummaryField`,
`UserInfoField` with batched user lookups.
> - **Data Model**:
> - New `SchoolImportResult` model/table (+ migration) and dashboard.
> - **Authorization**:
> - CanCan abilities grant `School#import` and `:school_import_job` read
to editor/experience CS admins.
> - **Utilities/Config**:
> - `UserInfoApiClient` adds `find_user_by_email` and improved
transforms.
> - Locale for missing CSV error; routes wired for admin/API.
> - **Docs/Tests**:
> - CSM guide and CSV template added under `docs/import/`.
> - Specs for import service, job, API endpoints, and admin pages.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
6eacf9a. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 2a6ab27 commit 1f96b90
File tree
38 files changed
+2018
-22
lines changed- app
- controllers
- admin
- api
- dashboards
- fields
- jobs
- models
- views
- admin/school_import_results
- api
- school_import_jobs
- schools
- fields
- results_summary_field
- status_field
- user_info_field
- config
- initializers
- locales
- db
- migrate
- docs/import
- lib
- concepts/school/operations
- public/docs/import
- spec
- concepts/school
- features/school
- jobs
- requests/admin
- support
38 files changed
+2018
-22
lines changedLines changed: 151 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
50 | 74 | | |
51 | 75 | | |
52 | 76 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments