Skip to content

Commit

Permalink
refactor: re-write controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed May 28, 2024
1 parent 6d4ec47 commit 3fba890
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 60 deletions.
65 changes: 8 additions & 57 deletions ts/server/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { Controller, Post, Body, HttpCode, UseFilters } from '@nestjs/common';
import { AppService } from './app.service';
import { Resource } from 'fhir/r4b';
import * as fhirpath_r4_model from 'fhirpath/fhir-context/r4';
import * as fhirpath from 'fhirpath';
import { FPMLValidationErrorFilter } from './app.filters';
import { FPOptions } from './utils/extract';
class Template {
context: Record<string, Resource> | Resource;
template: object;
Expand All @@ -27,39 +25,11 @@ export class AppController {
resolveTemplateR4(@Body() body: Template): object {
const { context, template, strict = false } = body;

const options: FPOptions = {
userInvocationTable: {
answers: {
fn: (inputs, linkId: string) => {
return fhirpath.evaluate(
inputs,
`repeat(item).where(linkId='${linkId}').answer.value`,
null,
fhirpath_r4_model,
null,
);
},
arity: { 0: [], 1: ['String'] },
},
},
};
if (containsQuestionnaireResponse(context)) {
return this.appService.resolveTemplate(
context.QuestionnaireResponse,
template,
context,
fhirpath_r4_model,
options,
strict,
);
}

return this.appService.resolveTemplate(
context,
containsQuestionnaireResponse(context) ? context.QuestionnaireResponse : context,
template,
context,
fhirpath_r4_model,
options,
strict,
);
}
Expand All @@ -69,31 +39,12 @@ export class AppController {
resolveTemplateAidbox(@Body() body: Template): object {
const { context, template, strict = false } = body;

const options: FPOptions = {
userInvocationTable: {
answers: {
fn: (inputs, linkId: string) => {
return fhirpath.evaluate(
inputs,
`repeat(item).where(linkId='${linkId}').answer.value.children()`,
null,
);
},
arity: { 0: [], 1: ['String'] },
},
},
};
if (containsQuestionnaireResponse(context)) {
return this.appService.resolveTemplate(
context.QuestionnaireResponse,
template,
context,
null,
options,
strict,
);
}

return this.appService.resolveTemplate(context, template, context, null, options, strict);
return this.appService.resolveTemplate(
containsQuestionnaireResponse(context) ? context.QuestionnaireResponse : context,
template,
context,
null,
strict,
);
}
}
36 changes: 33 additions & 3 deletions ts/server/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,39 @@ export class AppService {
template: object,
context: Context,
model?: Model,
options?: FPOptions,
strict?: boolean
strict?: boolean,
): object {
return resolveTemplate(resource, template, { root: resource, ...context }, model, options, strict);
const options: FPOptions = {
userInvocationTable: {
answers: {
fn: (inputs, linkId: string) => {
return fhirpath.evaluate(
inputs,
model
? `repeat(item).where(linkId='${linkId}').answer.value`
: `repeat(item).where(linkId='${linkId}').answer.value.children()`,
null,
model,
null,
);
},
arity: { 0: [], 1: ['String'] },
},
// Get rid of toString once it's fixed https://github.com/HL7/fhirpath.js/issues/156
toString: {
fn: (inputs) => fhirpath.evaluate({ x: inputs }, 'x.toString()'),
arity: { 0: [] },
},
},
};

return resolveTemplate(
resource,
template,
{ root: resource, ...context },
model,
options,
strict,
);
}
}

0 comments on commit 3fba890

Please sign in to comment.