From f8daff4abab5c8dd3f47f7b88908f455e29462f0 Mon Sep 17 00:00:00 2001 From: Francis Li Date: Wed, 7 Aug 2024 12:11:08 -0700 Subject: [PATCH] [Closes #374] Fix bug creating/saving healthcare Organization records due to empty state unique id field (#375) * [Closes #374] Fix bug creating/saving healthcare Organization records due to empty state unique id field * Fix test --- server/routes/api/organizations.js | 18 ++++++-- server/test/fixtures/organizations.json | 22 +++++++++ server/test/integration/api/organizations.js | 47 +++++++++++++++++++- server/test/integration/api/users.js | 4 +- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/server/routes/api/organizations.js b/server/routes/api/organizations.js index 168b62f3..e6c547c3 100644 --- a/server/routes/api/organizations.js +++ b/server/routes/api/organizations.js @@ -20,11 +20,15 @@ router.post( '/', middleware.isSuperUser, wrapper(async (req, res) => { - const record = await models.Organization.create({ + const data = { ..._.pick(req.body, ['name', 'type', 'state', 'stateUniqueId', 'timeZone', 'isMfaEnabled', 'isActive']), CreatedById: req.user.id, UpdatedById: req.user.id, - }); + }; + if (data.type === 'HEALTHCARE' && !data.stateUniqueId) { + data.stateUniqueId = null; + } + const record = await models.Organization.create(data); res.status(HttpStatus.CREATED).json(record.toJSON()); }) ); @@ -33,7 +37,6 @@ router.get('/:id', middleware.isAdminUser, async (req, res) => { const organization = await models.Organization.findByPk(req.params.id, { include: [models.Hospital], }); - if (organization) { res.json(organization.toJSON()); } else { @@ -49,7 +52,14 @@ router.patch( await models.sequelize.transaction(async (transaction) => { organization = await models.Organization.findByPk(req.params.id, { transaction }); if (organization) { - await organization.update(req.body, { transaction }); + const data = { + ..._.pick(req.body, ['name', 'type', 'state', 'stateUniqueId', 'timeZone', 'isMfaEnabled', 'isActive']), + UpdatedById: req.user.id, + }; + if ((data.type === 'HEALTHCARE' || (!data.type && organization.type === 'HEALTHCARE')) && !data.stateUniqueId) { + data.stateUniqueId = null; + } + await organization.update(data, { transaction }); } }); if (organization) { diff --git a/server/test/fixtures/organizations.json b/server/test/fixtures/organizations.json index dec10155..fd321b7b 100644 --- a/server/test/fixtures/organizations.json +++ b/server/test/fixtures/organizations.json @@ -5,6 +5,7 @@ "id": "aac13870-f6f3-11ea-adc1-0242ac120002", "name": "Code for San Francisco", "type": "C4SF", + "state": "06", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "isMfaEnabled": true @@ -16,6 +17,19 @@ "id": "25ffdd7c-b4cf-4ebb-9750-1e628370e13b", "name": "Sutter Health", "type": "HEALTHCARE", + "state": "06", + "stateUniqueId": "", + "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", + "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" + } + }, + { + "model": "Organization", + "data": { + "id": "0b01d3a3-3a8c-40a9-b07a-b360f256d5fc", + "name": "UCSF Health", + "type": "HEALTHCARE", + "state": "06", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" } @@ -26,6 +40,8 @@ "id": "1dd0dfd7-562e-48db-ae78-31b9136d3e15", "name": "San Francisco Fire Department", "type": "EMS", + "state": "06", + "stateUniqueId": "S38-50827", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" } @@ -36,6 +52,8 @@ "id": "7c9023ff-dd16-4e87-823a-80567a7b834a", "name": "American Medical Response (AMR)", "type": "EMS", + "state": "06", + "stateUniqueId": "S38-50088", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" } @@ -46,6 +64,8 @@ "id": "0a276fb6-6766-4e2c-b735-cabc4da684ff", "name": "King American", "type": "EMS", + "state": "06", + "stateUniqueId": "S38-50502", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" } @@ -56,6 +76,8 @@ "id": "49f916bb-bfb9-40cf-965c-6e9a8ab7e970", "name": "NORCal Ambulance", "type": "EMS", + "state": "06", + "stateUniqueId": "S38-50672", "CreatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", "UpdatedById": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11" } diff --git a/server/test/integration/api/organizations.js b/server/test/integration/api/organizations.js index c794c6fb..bab7bece 100644 --- a/server/test/integration/api/organizations.js +++ b/server/test/integration/api/organizations.js @@ -22,7 +22,52 @@ describe('/api/organizations', () => { await helper.twoFactorAuthSession(testSession); }); - describe('PATCH/', () => { + describe('POST /', () => { + it('creates a new Organization', async () => { + const response = await testSession + .post('/api/organizations') + .set('Accept', 'application/json') + .send({ + name: 'Kaiser Permanente', + type: 'HEALTHCARE', + state: '06', + stateUniqueId: '', + timeZone: 'America/Los_Angeles', + isActive: true, + }) + .expect(HttpStatus.CREATED); + + const data = response.body; + assert.ok(data.id); + assert.deepStrictEqual(data.name, 'Kaiser Permanente'); + assert.deepStrictEqual(data.type, 'HEALTHCARE'); + assert.deepStrictEqual(data.state, '06'); + assert.deepStrictEqual(data.stateUniqueId, null); + assert.deepStrictEqual(data.timeZone, 'America/Los_Angeles'); + assert.deepStrictEqual(data.isActive, true); + + const record = await models.Organization.findByPk(data.id); + assert.deepStrictEqual(record.name, 'Kaiser Permanente'); + assert.deepStrictEqual(record.type, 'HEALTHCARE'); + assert.deepStrictEqual(record.state, '06'); + assert.deepStrictEqual(record.stateUniqueId, null); + assert.deepStrictEqual(record.timeZone, 'America/Los_Angeles'); + assert.deepStrictEqual(record.isActive, true); + }); + }); + + describe('PATCH /:id', () => { + it('updates an existing Organization record', async () => { + const response = await testSession + .patch('/api/organizations/0b01d3a3-3a8c-40a9-b07a-b360f256d5fc') + .set('Accept', 'application/json') + .send({ name: 'UC San Francisco Health', stateUniqueId: '' }) + .expect(HttpStatus.OK); + const { body: data } = response; + assert.deepStrictEqual(data.name, 'UC San Francisco Health'); + assert.deepStrictEqual(data.stateUniqueId, null); + }); + it('turns on mfa for an organization', async () => { const orgId = '25ffdd7c-b4cf-4ebb-9750-1e628370e13b'; diff --git a/server/test/integration/api/users.js b/server/test/integration/api/users.js index 804b6cf6..1779959c 100644 --- a/server/test/integration/api/users.js +++ b/server/test/integration/api/users.js @@ -87,8 +87,8 @@ describe('/api/users', () => { id: '25ffdd7c-b4cf-4ebb-9750-1e628370e13b', name: 'Sutter Health', type: 'HEALTHCARE', - state: null, - stateUniqueId: null, + state: '06', + stateUniqueId: '', timeZone: 'America/Los_Angeles', isActive: true, isMfaEnabled: false,