Skip to content

Commit

Permalink
More fields for ServiceRequest CSV export (medplum#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
codyebberson committed Feb 7, 2022
1 parent acdce41 commit 3f79fef
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
46 changes: 45 additions & 1 deletion packages/server/src/fhir/csv.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ServiceRequest } from '@medplum/fhirtypes';
import { randomUUID } from 'crypto';
import express from 'express';
import request from 'supertest';
import { initApp } from '../app';
Expand All @@ -24,7 +26,7 @@ describe('CSV Export', () => {
await closeDatabase();
});

test('Success', async () => {
test('Export Patient', async () => {
// Create a patient that we want to export
const res1 = await request(app)
.post(`/fhir/R4/Patient`)
Expand Down Expand Up @@ -81,6 +83,48 @@ describe('CSV Export', () => {
expect(res3.text).toContain('alice@example.com');
});

test('Export ServiceRequest', async () => {
const serviceRequest: ServiceRequest = {
resourceType: 'ServiceRequest',
status: 'active',
subject: {
reference: 'Patient/' + randomUUID(),
display: 'Alice Smith',
},
code: {
coding: [
{
system: 'https://example.com',
code: 'test1',
display: 'test1',
},
],
},
orderDetail: [
{
text: 'Shipped',
},
],
};

// Create a service requeset that we want to export
const res1 = await request(app)
.post(`/fhir/R4/ServiceRequest`)
.set('Authorization', 'Bearer ' + accessToken)
.set('Content-Type', 'application/fhir+json')
.send(serviceRequest);
expect(res1.status).toBe(201);

const res3 = await request(app)
.get(`/fhir/R4/ServiceRequest/$csv`)
.set('Authorization', 'Bearer ' + accessToken);
expect(res3.status).toBe(200);
expect(res3.text).toContain(res1.body.id);
expect(res3.text).toContain('Alice Smith');
expect(res3.text).toContain('test1');
expect(res3.text).toContain('Shipped');
});

test('Invalid resource type', async () => {
const res = await request(app)
.get(`/fhir/R4/Patientx/$csv`)
Expand Down
18 changes: 14 additions & 4 deletions packages/server/src/fhir/csv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assertOk, badRequest, formatAddress, formatHumanName } from '@medplum/core';
import { evalFhirPath } from '@medplum/fhirpath';
import { Address, BundleEntry, ContactPoint, HumanName, Resource } from '@medplum/fhirtypes';
import { Address, BundleEntry, CodeableConcept, ContactPoint, HumanName, Reference } from '@medplum/fhirtypes';
import { Request, Response } from 'express';
import { sendOutcome } from './outcomes';
import { Repository } from './repo';
Expand All @@ -20,7 +20,10 @@ const resourceTypeColumns: Record<string, Record<string, string>> = {
ServiceRequest: {
ID: 'id',
'Last Updated': 'meta.lastUpdated',
Patient: 'subject.display',
Patient: 'subject',
Code: 'code.coding',
Status: 'status',
'Order Detail': 'orderDetail',
},
};

Expand Down Expand Up @@ -51,12 +54,11 @@ export async function csvHandler(req: Request, res: Response): Promise<void> {

// For each resource...
for (const entry of bundle.entry as BundleEntry[]) {
const resource = entry.resource as Resource;
const row: string[] = [];

// For each column...
for (const [_, column] of columnEntries) {
const values = evalFhirPath(column, resource);
const values = evalFhirPath(column, entry.resource);
if (values.length > 0) {
row.push(csvEscape(values[0]));
} else {
Expand Down Expand Up @@ -99,6 +101,14 @@ function csvEscape(input: unknown): string {
// ContactPoint
return csvEscapeString((input as ContactPoint).value as string);
}
if ('display' in input) {
// Reference
return csvEscapeString((input as Reference).display as string);
}
if ('text' in input) {
// CodeableConcept
return csvEscapeString((input as CodeableConcept).text as string);
}
}

// ???
Expand Down

0 comments on commit 3f79fef

Please sign in to comment.