Skip to content

Commit

Permalink
feat: survey settings & configurable multi-pass (V4-1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashroch committed Aug 15, 2024
1 parent 1865954 commit f64b3f6
Show file tree
Hide file tree
Showing 25 changed files with 282 additions and 126 deletions.
2 changes: 1 addition & 1 deletion apps/admin/src/views/survey-schemes/browse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineComponent({
value: 'name',
},
{
text: this.$t('survey-schemes.types._'),
text: this.$t('survey-schemes.settings.types._'),
sortable: true,
value: 'type',
},
Expand Down
74 changes: 52 additions & 22 deletions apps/admin/src/views/survey-schemes/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
/>
</template>
<v-form @keydown.native="clearError" @submit.prevent="submit">
<v-container fluid>
<v-card-text>
<v-card-text>
<v-container fluid>
<v-row>
<v-col cols="12" md="6">
<v-text-field
Expand All @@ -21,18 +21,6 @@
outlined
/>
</v-col>
<v-col cols="12" md="6">
<v-select
v-model="form.type"
:error-messages="form.errors.get('type')"
hide-details="auto"
:items="schemeTypeItems"
:label="$t('survey-schemes.types._')"
name="type"
outlined
@change="form.errors.clear('type')"
/>
</v-col>
<v-col cols="12" md="6">
<v-select
v-model="form.visibility"
Expand Down Expand Up @@ -60,8 +48,37 @@
</v-select>
</v-col>
</v-row>
</v-card-text>
</v-container>
<div class="text-subtitle-1 font-weight-medium py-4">
{{ $t('survey-schemes.settings._') }}
</div>
<v-row>
<v-col cols="12" md="6">
<v-select
v-model="form.settings.type"
:error-messages="form.errors.get('settings.type')"
hide-details="auto"
:items="schemeTypeItems"
:label="$t('survey-schemes.settings.types._')"
name="settings.type"
outlined
@change="form.errors.clear('settings.type')"
/>
</v-col>
<v-col cols="12" md="6">
<v-select
v-model="form.settings.flow"
:error-messages="form.errors.get('settings.flow')"
hide-details="auto"
:items="recallPassItems"
:label="$t('survey-schemes.settings.flows._')"
name="settings.flow"
outlined
@change="form.errors.clear('settings.flow')"
/>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-divider />
<meal-list v-model="form.meals" :scheme-id="id" />
<v-card-text>
Expand All @@ -75,19 +92,19 @@
import { defineComponent, ref } from 'vue';
import type { RecordVisibility } from '@intake24/common/security';
import type { ExportSection, Meal, RecallPrompts, SchemeType } from '@intake24/common/surveys';
import type { ExportSection, Meal, RecallPrompts, SchemeSettings } from '@intake24/common/surveys';
import type { SurveySchemeEntry, SurveySchemeRefs } from '@intake24/common/types/http/admin';
import { formMixin } from '@intake24/admin/components/entry';
import { MealList } from '@intake24/admin/components/lists';
import { CopySchemeDialog } from '@intake24/admin/components/schemes';
import { useEntry, useEntryFetch, useEntryForm, useSelects } from '@intake24/admin/composables';
import { defaultMeals, schemeTypes } from '@intake24/common/surveys';
import { defaultMeals, defaultSchemeSettings, recallFlows, schemeTypes } from '@intake24/common/surveys';
import { useI18n } from '@intake24/i18n';
export type SurveySchemeForm = {
id: string | null;
name: string | null;
type: SchemeType;
settings: SchemeSettings;
version: number;
prompts: RecallPrompts;
meals: Meal[];
Expand All @@ -97,7 +114,7 @@ export type SurveySchemeForm = {
export type PatchSurveySchemeForm = Pick<
SurveySchemeForm,
'name' | 'version' | 'type' | 'meals' | 'visibility'
'name' | 'settings' | 'meals' | 'visibility'
>;
export default defineComponent({
Expand All @@ -114,7 +131,14 @@ export default defineComponent({
const schemeTypeItems = ref(
schemeTypes.map(value => ({
value,
text: i18n.t(`survey-schemes.types.${value}`),
text: i18n.t(`survey-schemes.settings.types.${value}`),
})),
);
const recallPassItems = ref(
recallFlows.map(value => ({
value,
text: i18n.t(`survey-schemes.settings.flows.${value}`),
})),
);
Expand All @@ -127,7 +151,12 @@ export default defineComponent({
PatchSurveySchemeForm,
SurveySchemeEntry
>(props, {
data: { name: null, version: 2, type: 'default', meals: defaultMeals, visibility: 'public' },
data: {
name: null,
settings: defaultSchemeSettings,
meals: defaultMeals,
visibility: 'public',
},
editMethod: 'patch',
});
Expand All @@ -140,6 +169,7 @@ export default defineComponent({
refsLoaded,
clearError,
form,
recallPassItems,
routeLeave,
submit,
visibilityList,
Expand Down
8 changes: 6 additions & 2 deletions apps/admin/src/views/survey-schemes/read.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
<td>{{ entry.name }}</td>
</tr>
<tr>
<th>{{ $t('survey-schemes.types._') }}</th>
<td>{{ entry.type }}</td>
<th>{{ $t('survey-schemes.settings.types._') }}</th>
<td>{{ entry.settings.type }}</td>
</tr>
<tr>
<th>{{ $t('survey-schemes.settings.flows._') }}</th>
<td>{{ entry.settings.flow }}</td>
</tr>
<tr>
<th>{{ $t('securables.visibility._') }}</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export default () => {
await suite.sharedTests.assertInvalidInput(
'patch',
url,
['name', 'type', 'visibility', 'meals', 'prompts', 'dataExport'],
['name', 'settings.type', 'settings.flow', 'visibility', 'meals', 'prompts', 'dataExport'],
{
input: {
name: [],
type: {},
settings: { type: false, flow: '10-pass' },
visibility: { one: 'two' },
meals: 10,
prompts: 'invalidPrompts',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default () => {
await suite.sharedTests.assertInvalidInput(
'post',
url,
['name', 'type', 'visibility', 'meals', 'prompts', 'dataExport'],
['name', 'settings.type', 'settings.flow', 'visibility', 'meals', 'prompts', 'dataExport'],
{
input: {
name: [],
type: 'invalidType',
settings: { type: false, flow: '10-pass' },
visibility: [],
meals: 5,
prompts: [],
Expand Down
4 changes: 2 additions & 2 deletions apps/api/__tests__/integration/helpers/mocker/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
defaultExport,
defaultMeals,
defaultPrompts,
defaultSchemeSettings,
searchSortingAlgorithms,
spellingCorrectionPreferences,
} from '@intake24/common/surveys';
Expand Down Expand Up @@ -223,12 +224,11 @@ function personalAccessToken() {

function surveyScheme(): SurveySchemeCreationAttributes {
const name = faker.word.words(3);
const type = 'default';
const visibility = recordVisibilities[faker.number.int({ min: 0, max: 1 })];

return {
name,
type,
settings: defaultSchemeSettings,
visibility,
prompts: defaultPrompts,
meals: defaultMeals,
Expand Down
4 changes: 2 additions & 2 deletions apps/api/__tests__/integration/helpers/setup/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
defaultExport,
defaultMeals,
defaultPrompts,
defaultSchemeSettings,
} from '@intake24/common/surveys';
import { SystemLocaleAttributes } from '@intake24/common/types/http/admin';
import { permissions } from '@intake24/common-backend';
Expand Down Expand Up @@ -165,9 +166,8 @@ export async function initDatabase(): Promise<MockData> {
}),
SurveyScheme.create({
name: 'Default',
type: 'default',
settings: defaultSchemeSettings,
visibility: 'public',
version: 2,
prompts: defaultPrompts,
meals: [...defaultMeals],
dataExport: defaultExport,
Expand Down
5 changes: 2 additions & 3 deletions apps/api/src/http/routers/admin/survey-scheme.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,11 @@ export function surveyScheme() {
});

const { name } = body;
const { type, version, visibility, prompts, meals, dataExport } = surveyScheme;
const { settings, visibility, prompts, meals, dataExport } = surveyScheme;

const surveySchemeCopy = await SurveyScheme.create({
name,
type,
version,
settings,
visibility,
prompts,
meals,
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/http/routers/survey-respondent.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function surveyRespondent() {

const survey = await Survey.findBySlug(slug, {
include: [
{ association: 'surveyScheme', attributes: ['id', 'type', 'meals', 'prompts'] },
{ association: 'surveyScheme', attributes: ['id', 'settings', 'meals', 'prompts'] },
{
association: 'feedbackScheme',
attributes: [
Expand Down Expand Up @@ -86,6 +86,7 @@ export function surveyRespondent() {
surveyScheme: {
meals,
prompts: surveySchemePrompts,
settings,
},
} = survey;

Expand Down Expand Up @@ -142,7 +143,7 @@ export function surveyRespondent() {
name,
state,
locale,
surveyScheme: { id: surveyScheme.id, type: surveyScheme.type, meals, prompts },
surveyScheme: { id: surveyScheme.id, meals, prompts, settings },
feedbackScheme,
numberOfSubmissionsForFeedback,
session,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ function migrateSinglePrompt(prompt: SinglePromptV1, section: PromptSection): Si
barcode: { type: 'none' },
};
}
case 'date-picker-prompt': {
const { futureDates, ...rest } = prompt;
return {
...rest,
version: 2,
useGraph: false,
conditions,
current: null,
min: null,
max: null,
};
}
default:
return {
...prompt,
Expand Down
7 changes: 4 additions & 3 deletions apps/api/src/jobs/survey-schemes/survey-schemes-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { Job } from 'bullmq';
import { literal, where } from 'sequelize';

import type { IoC } from '@intake24/api/ioc';
import type { PromptSection, RecallPrompts } from '@intake24/common/surveys';
import { Condition, customPrompts, defaultAction, getConditionDefaults, portionSizePrompts, SinglePrompt, standardPrompts } from '@intake24/common/prompts';
import { defaultSchemeSettings, type PromptSection, type RecallPrompts } from '@intake24/common/surveys';
import { merge } from '@intake24/common/util';
import { Survey, SurveyScheme } from '@intake24/db';

Expand Down Expand Up @@ -121,7 +121,7 @@ export default class SurveySchemesSync extends BaseJob<'SurveySchemesSync'> {
};

const schemes = await this.models.system.SurveyScheme.findAll({
attributes: ['id', 'name', 'prompts'],
attributes: ['id', 'settings', 'prompts'],
order: [['id', 'ASC']],
});

Expand All @@ -138,8 +138,9 @@ export default class SurveySchemesSync extends BaseJob<'SurveySchemesSync'> {
postMeals: scheme.prompts.postMeals.map(mergeCallback),
submission: scheme.prompts.submission.map(mergeCallback),
};
const settings = merge(defaultSchemeSettings, scheme.settings);

await scheme.update({ prompts });
await scheme.update({ prompts, settings });
}

const surveys = await this.models.system.Survey.findAll({
Expand Down
Loading

0 comments on commit f64b3f6

Please sign in to comment.