Skip to content

Commit

Permalink
remove need for survey id in interview stream
Browse files Browse the repository at this point in the history
  • Loading branch information
kaligrafy committed Jan 19, 2024
1 parent edf15e4 commit e4ff21c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
27 changes: 6 additions & 21 deletions packages/evolution-backend/src/models/interviews.db.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ const getInterviewByUuid = async <CustomSurvey, CustomHousehold, CustomHome, Cus
// specify which fields, it will depend on the calling context. Maybe an
// array of strings keyof InterviewAttributes which will map to sql
// fields
const surveyId = await getSurveyId();
const interviews = await knex
.select('i.*')
.from(`${tableName} as i`)
.where('survey_id', surveyId)
.andWhere('uuid', interviewUuid);
const interviews = await knex.select('i.*').from(`${tableName} as i`).andWhere('uuid', interviewUuid);
if (interviews.length !== 1) {
return undefined;
}
Expand All @@ -169,12 +164,7 @@ const getInterviewIdByUuid = async <CustomSurvey, CustomHousehold, CustomHome, C
interviewUuid: string
): Promise<number | undefined> => {
try {
const surveyId = await getSurveyId();
const interviews = await knex
.select('id')
.from(tableName)
.where('survey_id', surveyId)
.andWhere('uuid', interviewUuid);
const interviews = await knex.select('id').from(tableName).andWhere('uuid', interviewUuid);
return interviews.length === 1 ? interviews[0].id : undefined;
} catch (error) {
console.error(error);
Expand Down Expand Up @@ -472,7 +462,6 @@ const getRawWhereClause = (
* and the second element is the array of bindings
*/
const updateRawWhereClause = (
surveyId: number,
filters: { [key: string]: { value: string | boolean | number | null; op?: keyof OperatorSigns } },
baseFilter: string
): [string, (string | boolean | number)[]] => {
Expand All @@ -487,7 +476,6 @@ const updateRawWhereClause = (
bindings.push(whereClause[1]);
}
});
rawFilter += ` AND i.survey_id = ${surveyId}`;
return [rawFilter, bindings];
};

Expand All @@ -514,10 +502,9 @@ const getList = async <CustomSurvey, CustomHousehold, CustomHome, CustomPerson>(
totalCount: number;
}> => {
try {
const surveyId = await getSurveyId();
const baseRawFilter =
'i.is_active IS TRUE AND participant.is_valid IS TRUE AND participant.is_test IS NOT TRUE';
const [rawFilter, bindings] = await updateRawWhereClause(surveyId, params.filters, baseRawFilter);
const [rawFilter, bindings] = updateRawWhereClause(params.filters, baseRawFilter);
// Get the total count for that query and filter
const countResult = await knex
.count('i.id')
Expand Down Expand Up @@ -595,10 +582,9 @@ const getValidationErrors = async (params: {
filters: { [key: string]: { value: string | boolean | number | null; op?: keyof OperatorSigns } };
}): Promise<{ errors: { key: string; cnt: string }[] }> => {
try {
const surveyId = await getSurveyId();
const baseRawFilter =
'i.is_active IS TRUE AND participant.is_valid IS TRUE AND participant.is_test IS NOT TRUE';
const [rawFilter, bindings] = updateRawWhereClause(surveyId, params.filters, baseRawFilter);
const [rawFilter, bindings] = updateRawWhereClause(params.filters, baseRawFilter);

const validationErrorsQuery = knex
.select('key', knex.raw('sum(value::numeric) cnt'))
Expand Down Expand Up @@ -640,17 +626,16 @@ const getValidationErrors = async (params: {
* which field to sort the interviews by
* @returns An interview stream
*/
const getInterviewsStream = async function (params: {
const getInterviewsStream = function (params: {
filters: { [key: string]: { value: string | boolean | number | null; op?: keyof OperatorSigns } };
select?: {
includeAudits?: boolean;
responses?: 'none' | 'participant' | 'validated' | 'both' | 'validatedIfAvailable';
};
sort?: (string | { field: string; order: 'asc' | 'desc' })[];
}) {
const surveyId = await getSurveyId();
const baseRawFilter = 'participant.is_valid IS TRUE AND participant.is_test IS NOT TRUE';
const [rawFilter, bindings] = updateRawWhereClause(surveyId, params.filters, baseRawFilter);
const [rawFilter, bindings] = updateRawWhereClause(params.filters, baseRawFilter);
const sortFields = params.sort || [];
const selectFields = params.select || { includeAudits: true, responses: 'both' };
const responseType = selectFields.responses || 'both';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const getRenamedPaths = async (): Promise<{ attributes: string[]; objectPaths: s
objectPaths: [],
arrayPaths: []
};
const queryStream = await interviewsDbQueries.getInterviewsStream({
const queryStream = interviewsDbQueries.getInterviewsStream({
filters: {},
select: { includeAudits: false, responses: 'validatedIfAvailable' }
});
Expand Down Expand Up @@ -249,7 +249,7 @@ const exportAllToCsvByObject = async function () {

console.log('reading interview data...');

const queryStream = await interviewsDbQueries.getInterviewsStream({
const queryStream = interviewsDbQueries.getInterviewsStream({
filters: {},
select: { includeAudits: false, responses: 'validatedIfAvailable' }
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class Interviews {
};

static async auditInterviews(validations, surveyProjectHelper, parsers): Promise<void> {
const queryStream = await interviewsDbQueries.getInterviewsStream({ filters: {} });
const queryStream = interviewsDbQueries.getInterviewsStream({ filters: {} });
let i = 0;
return new Promise((resolve, reject) => {
queryStream
Expand Down Expand Up @@ -222,7 +222,7 @@ export default class Interviews {
reject('The confirm string should be \'I WANT TO DELETE ALL VALIDATION WORK\'');
});
}
const queryStream = await interviewsDbQueries.getInterviewsStream({ filters: {} });
const queryStream = interviewsDbQueries.getInterviewsStream({ filters: {} });
let i = 0;
return new Promise((resolve, reject) => {
queryStream
Expand Down

0 comments on commit e4ff21c

Please sign in to comment.