Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DeepPartial } from './ProposalDataSource';
import {
DependenciesLogicOperator,
EvaluatorOperator,
Expand Down Expand Up @@ -29,7 +30,6 @@ import {
TextInputConfig,
} from '../../resolvers/types/FieldConfig';
import { QuestionaryDataSource } from '../QuestionaryDataSource';
import { DeepPartial } from './ProposalDataSource';

export let dummyQuestionarySteps: QuestionaryStep[];
export let dummyQuestionary: Questionary;
Expand Down Expand Up @@ -74,6 +74,7 @@ export const dummyQuestionTemplateRelationFactory = (
dummyQuestionFactory(values?.question),
values?.sortOrder || Math.round(Math.random() * 100),
values?.topicId || Math.round(Math.random() * 10),
values?.templateId || Math.round(Math.random() * 100),
values?.config || { ...new BooleanConfig(), readPermissions: [] },
values?.dependencies as FieldDependency[],
values?.dependenciesOperator as DependenciesLogicOperator
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/datasources/postgres/records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,7 @@ export const createQuestionTemplateRelationObject = async <T extends DataType>(
),
record.topic_id,
record.sort_order,
record.template_id,
createConfig<any>(record.data_type as DataType, transformedConfig),
dependencies,
record.dependencies_operator
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/models/Questionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Answer extends QuestionTemplateRelation {
questionTemplateRelation.question,
questionTemplateRelation.topicId,
questionTemplateRelation.sortOrder,
questionTemplateRelation.templateId,
questionTemplateRelation.config,
questionTemplateRelation.dependencies,
questionTemplateRelation.dependenciesOperator
Expand Down
1 change: 1 addition & 0 deletions apps/backend/src/models/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class QuestionTemplateRelation {
public question: Question,
public topicId: number,
public sortOrder: number,
public templateId: number,
public config: typeof FieldConfigType,
public dependencies: FieldDependency[],
public dependenciesOperator?: DependenciesLogicOperator
Expand Down
33 changes: 33 additions & 0 deletions apps/backend/src/queries/TemplateQueries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,37 @@ describe('getDynamicMultipleChoiceOptions', () => {

expect(options).toEqual(['option1', 'option2']);
});

it('should use the question template relation if a template ID is supplied', async () => {
const templateDataSource = container.resolve<TemplateDataSourceMock>(
Tokens.TemplateDataSource
);
const getQuestionSpy = jest.spyOn(templateDataSource, 'getQuestion');
const getQuestionTemplateRelationSpy = jest.spyOn(
templateDataSource,
'getQuestionTemplateRelation'
);
jest.spyOn(global, 'fetch').mockImplementation(() =>
Promise.resolve({
json: () => Promise.resolve(['option1', 'option2']),
ok: true,
} as Response)
);

const options = await templateQueries.getDynamicMultipleChoiceOptions(
dummyUserWithRole,
'dmcQuestionEmptyJsonPath',
1
);

expect(options).toEqual(['option1', 'option2']);
expect(getQuestionTemplateRelationSpy).toHaveBeenCalledWith(
'dmcQuestionEmptyJsonPath',
1
);
expect(getQuestionSpy).not.toHaveBeenCalled();

getQuestionSpy.mockRestore();
getQuestionTemplateRelationSpy.mockRestore();
});
});
30 changes: 28 additions & 2 deletions apps/backend/src/queries/TemplateQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ export default class TemplateQueries {
@Authorized()
async getDynamicMultipleChoiceOptions(
user: UserWithRole | null,
questionId: string
questionId: string,
templateId?: number | null
) {
const question = await this.dataSource.getQuestion(questionId);
const question = await this.getDynamicMultipleChoiceQuestion(
questionId,
templateId
);
if (!question) return [];

const config = question.config as DynamicMultipleChoiceConfig;
Expand Down Expand Up @@ -139,4 +143,26 @@ export default class TemplateQueries {

return [];
}

private async getDynamicMultipleChoiceQuestion(
questionId: string,
templateId?: number | null
): Promise<Question | null> {
if (templateId !== null && templateId !== undefined) {
const questionTemplateRelation =
await this.dataSource.getQuestionTemplateRelation(
questionId,
templateId
);

if (!questionTemplateRelation) return null;

return {
...questionTemplateRelation.question,
config: questionTemplateRelation.config,
};
}

return this.dataSource.getQuestion(questionId);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Query, Ctx, Resolver, Arg } from 'type-graphql';
import { Query, Ctx, Resolver, Arg, Int } from 'type-graphql';

import { ResolverContext } from '../../context';

Expand All @@ -7,11 +7,14 @@ export class DynamicMultipleChoiceQuery {
@Query(() => [String], { nullable: true })
getDynamicMultipleChoiceOptions(
@Arg('questionId', () => String) questionId: string,
@Arg('templateId', () => Int, { nullable: true })
templateId: number | null,
@Ctx() context: ResolverContext
) {
return context.queries.template.getDynamicMultipleChoiceOptions(
context.user,
questionId
questionId,
templateId
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class QuestionTemplateRelation
@Field(() => Int)
public sortOrder: number;

@Field(() => Int)
public templateId: number;

@Field(() => Int)
public topicId: number;

Expand Down
24 changes: 24 additions & 0 deletions apps/e2e/cypress/fixtures/template_export.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"topicId": 1,
"sortOrder": 0,
"templateId": 1,
"config": {
"tooltip": "",
"required": false,
Expand Down Expand Up @@ -78,6 +79,7 @@
},
"topicId": 8,
"sortOrder": 0,
"templateId": 1,
"config": {
"addEntryButtonLabel": "Add",
"templateCategory": "SAMPLE_DECLARATION",
Expand Down Expand Up @@ -129,6 +131,7 @@
},
"topicId": 8,
"sortOrder": 2,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -175,6 +178,7 @@
},
"topicId": 8,
"sortOrder": 3,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -209,6 +213,7 @@
},
"topicId": 8,
"sortOrder": 4,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -244,6 +249,7 @@
},
"topicId": 8,
"sortOrder": 5,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -277,6 +283,7 @@
},
"topicId": 8,
"sortOrder": 6,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -316,6 +323,7 @@
},
"topicId": 8,
"sortOrder": 7,
"templateId": 1,
"config": {
"required": false,
"small_label": "",
Expand Down Expand Up @@ -350,6 +358,7 @@
},
"topicId": 8,
"sortOrder": 8,
"templateId": 1,
"config": {
"required": false,
"small_label": "",
Expand Down Expand Up @@ -385,6 +394,7 @@
},
"topicId": 8,
"sortOrder": 8,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -421,6 +431,7 @@
},
"topicId": 8,
"sortOrder": 9,
"templateId": 1,
"config": {
"html": "",
"plain": "",
Expand Down Expand Up @@ -454,6 +465,7 @@
},
"topicId": 8,
"sortOrder": 10,
"templateId": 1,
"config": {
"required": false,
"small_label": "",
Expand Down Expand Up @@ -492,6 +504,7 @@
},
"topicId": 8,
"sortOrder": 11,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -532,6 +545,7 @@
},
"topicId": 8,
"sortOrder": 12,
"templateId": 1,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -854,6 +868,7 @@
},
"topicId": 7,
"sortOrder": 0,
"templateId": 7,
"config": {
"titlePlaceholder": "Title",
"tooltip": "",
Expand All @@ -880,6 +895,7 @@
},
"topicId": 7,
"sortOrder": 3,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand All @@ -906,6 +922,7 @@
},
"topicId": 7,
"sortOrder": 4,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -940,6 +957,7 @@
},
"topicId": 7,
"sortOrder": 5,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -982,6 +1000,7 @@
},
"topicId": 7,
"sortOrder": 6,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -1037,6 +1056,7 @@
},
"topicId": 7,
"sortOrder": 7,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -1085,6 +1105,7 @@
},
"topicId": 7,
"sortOrder": 8,
"templateId": 7,
"config": {
"required": false,
"small_label": "",
Expand Down Expand Up @@ -1119,6 +1140,7 @@
},
"topicId": 7,
"sortOrder": 9,
"templateId": 7,
"config": {
"small_label": "",
"required": false,
Expand Down Expand Up @@ -1156,6 +1178,7 @@
},
"topicId": 7,
"sortOrder": 10,
"templateId": 7,
"config": {
"required": false,
"small_label": "",
Expand Down Expand Up @@ -1193,6 +1216,7 @@
},
"topicId": 7,
"sortOrder": 11,
"templateId": 7,
"config": {
"required": false,
"small_label": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function QuestionaryFilter({ onSubmit, callId }: QuestionaryFilterProps) {
}}
questionTemplateRelation={selectedQuestion}
callId={callId}
templateId={selectedQuestion.templateId}
/>
)}
</Collapse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export interface SearchCriteriaInputProps {
) => unknown;
questionTemplateRelation: QuestionTemplateRelationFragment;
callId?: InputMaybe<Call['id']>;
templateId?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ function DynamicMultipleChoiceSearchCriteriaComponent({
onChange,
questionTemplateRelation,
searchCriteria,
templateId,
}: SearchCriteriaInputProps) {
const [value, setValue] = useState(searchCriteria?.value ?? '');
const { options, loadingOptions } = useGetDynamicMultipleChoiceOptions(
questionTemplateRelation.question.id
questionTemplateRelation.question.id,
templateId
);

return (
Expand Down
Loading
Loading