Skip to content

Commit

Permalink
[Closes #374] Fix bug creating/saving healthcare Organization records…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
francisli authored Aug 7, 2024
1 parent 1f3e045 commit f8daff4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
18 changes: 14 additions & 4 deletions server/routes/api/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
})
);
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions server/test/fixtures/organizations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -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"
}
Expand Down
47 changes: 46 additions & 1 deletion server/test/integration/api/organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions server/test/integration/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f8daff4

Please sign in to comment.