cloud-gov / pages-core

cloud.gov Pages is a publishing platform for modern 21st Century IDEA websites.
https://cloud.gov/pages
Other
276 stars 68 forks source link

Error creating an organization (staging) #3779

Closed svenaas closed 2 years ago

svenaas commented 2 years ago

Creating a new organization raises a 500 on staging on POST to /admin/organizations

Steps to reproduce:

  1. Go to https://admin.federalistapp-staging.18f.gov/organizations/
  2. Hit the + button to add a new organization.
  3. Fill out the required fields. I tried Name: "Test organization", Agency: "Test Agency", Organization Manager UAA Email: "sven.aas@gsa.gov"
  4. Hit the Create button

The console shows

api.js:34 
 POST https://federalistapp-staging.18f.gov/admin/organizations 500
He  @   api.js:34
Be  @   api.js:75
(anonymous) @   api.js:156
(anonymous) @   New.svelte:24
(anonymous) @   index.mjs:231

and

Uncaught (in promise) Error
    at api.js:47:13
    at async HTMLFormElement.<anonymous> (New.svelte:24:13)
(anonymous) @   api.js:47
await in (anonymous) (async)        
(anonymous) @   index.mjs:231
svenaas commented 2 years ago

Closing because Federalist staging isn't Pages staging.

svenaas commented 2 years ago

OK now that we have https://admin.pages-staging.cloud.gov/ I tried this again and got exactly the same error at the same point in the code, so it's not just a Pages vs. Federalist thing.

svenaas commented 2 years ago

I decided to take a look at this on local.

With a clean build of current staging I visit :3000/organizations and try and create a new org.

Attempt 1: didn't add GitHub username, entered my own GSA email address, checked "Self Authorized".

Result: a popup informing me "Invite sent to [my email] [URL]" and a new organization successfully added.

Attempt 2: didn't add GitHub username, entered my own GSA email address did not check "Self Authorized".

Result: no popup, but a new organization successfully added once again.

Cannot reproduce locally. 🤔

svenaas commented 2 years ago

Tried again on Pages staging. Same error. I will note, because I didn't up above, that the org does get created, in spite of the error that occurs when creating it. I'm not sure I got that far last time, and it's good news because it means this isn't a blocker to confirming #3728.

svenaas commented 2 years ago

The error shows up at /events:

Name: error | request-handler Created: 2022-03-11 12:28PM Model: Message: Cannot read properties of null (reading 'id')

svenaas commented 2 years ago

In the pages-staging logs I find

uncaughtException: Cannot read properties of null (reading 'id')
TypeError: Cannot read properties of null (reading 'id')
    at Object.createOrganization (/home/vcap/app/api/services/organization/Organization.js:207:62)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async create (/home/vcap/app/api/admin/controllers/organization.js:70:48)
svenaas commented 2 years ago

Where line 207 of the Organization service in the async createOrganization method is

await org.addUser(user, { through: { roleId: managerRole.id } });

where managerRole was supposed to have been initialized by

const managerRole = await Role.findOne({ where: { name: 'manager' } });
davemcorwin commented 2 years ago

Seeded user and manager roles in staging and was able to create an organization via admin.pages-staging.cloud.gov without an error.

As a follow up, we should create a file for seed data that should be run in all new environments, which is separate from the sample data we use when developing locally.

svenaas commented 2 years ago

In Slack, I noted that

In local dev create-dev-data seeds some manager users, one with and one without GitHub. Is it possible that no such manager users exist on staging? Role doesn't appear to be shown at admin /users and I have not yet tried spelunking in the database, though I'm close to that point now.

@davemcorwin deduced that it's the seeding of the role types themselves that's missing, and created #3811 to track the work of addressing this.

svenaas commented 2 years ago

Oh, sorry @davemcorwin. I didn't notice that you'd done the seeding manually. Moving this back.