Skip to content

Commit

Permalink
interview: Make sure _startedAt is initialized at interview creation
Browse files Browse the repository at this point in the history
It should not depend on the frontend's section preload to have this
important data in the interview.
  • Loading branch information
tahini committed Apr 9, 2024
1 parent a17733b commit e6ed254
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import interviewsQueries from '../../../models/interviews.db.queries';
import interviewsAccessesQueries from '../../../models/interviewsAccesses.db.queries';
import { registerAccessCodeValidationFunction } from '../../accessCode';
import { updateInterview } from '../interview';
import moment from 'moment';

jest.mock('../../../models/interviews.db.queries', () => ({
findByResponse: jest.fn(),
Expand Down Expand Up @@ -181,11 +182,12 @@ describe('Create interviews', () => {
});

test('Create with empty responses', async() => {

const newInterview = await Interviews.createInterviewForUser(participantId, {});
expect(mockDbCreate).toHaveBeenCalledTimes(1);
expect(mockDbCreate).toHaveBeenCalledWith({
participant_id: participantId,
responses: {},
responses: { _startedAt: expect.anything() },
is_active: true,
validations: {},
logs: []
Expand All @@ -207,7 +209,7 @@ describe('Create interviews', () => {
expect(mockDbCreate).toHaveBeenCalledTimes(1);
expect(mockDbCreate).toHaveBeenCalledWith({
participant_id: participantId,
responses,
responses: { ...responses, _startedAt: expect.anything() },
is_active: true,
validations: {},
logs: []
Expand All @@ -226,7 +228,7 @@ describe('Create interviews', () => {
expect(mockDbCreate).toHaveBeenCalledTimes(1);
expect(mockDbCreate).toHaveBeenCalledWith({
participant_id: participantId,
responses: {},
responses: { _startedAt: expect.anything() },
is_active: true,
validations: {},
logs: []
Expand All @@ -237,19 +239,23 @@ describe('Create interviews', () => {
});

test('Create and return many other field', async() => {
const initialTimeStamp = moment().unix();
const returningFields = ['participant_id', 'responses', 'uuid'];
const newInterview = await Interviews.createInterviewForUser(participantId, {}, returningFields);
expect(mockDbCreate).toHaveBeenCalledTimes(1);
expect(mockDbCreate).toHaveBeenCalledWith({
participant_id: participantId,
responses: {},
responses: { _startedAt: expect.anything() },
is_active: true,
validations: {},
logs: []
}, returningFields);
expect(newInterview).toEqual({ participant_id: participantId, uuid: expect.anything(), responses: {} });
expect(newInterview).toEqual({ participant_id: participantId, uuid: expect.anything(), responses: { _startedAt: expect.anything() } });
expect(mockDbGetByUuid).not.toHaveBeenCalled();
expect(mockInterviewUpdate).not.toHaveBeenCalled();

// Make sure timestamp in response is higher than the one at the beginning of the test
expect((newInterview.responses as any)._startedAt).toBeGreaterThanOrEqual(initialTimeStamp);
});

});
Expand Down
15 changes: 12 additions & 3 deletions packages/evolution-backend/src/services/interviews/interviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* This file is licensed under the MIT License.
* License text available at https://opensource.org/licenses/MIT
*/
import _cloneDeep from 'lodash/cloneDeep';
import moment from 'moment';
import { auditInterview, getChangesAfterCleaningInterview } from 'evolution-common/lib/services/interviews/interview';
import { updateInterview, setInterviewFields, copyResponsesToValidatedData } from './interview';
import { mapResponsesToValidatedData } from './interviewUtils';
Expand Down Expand Up @@ -105,9 +107,13 @@ export default class Interviews {
): Promise<Partial<InterviewAttributes<CustomSurvey, CustomHousehold, CustomHome, CustomPerson>>> => {
// TODO Make sure there is no active interview for this user already?

// Create the interview for this user
// Create the interview for this user, make sure the start time is set
const responses = _cloneDeep(initialResponses);
if (responses._startedAt === undefined) {
responses._startedAt = moment().unix();
}
const interview = await interviewsDbQueries.create(
{ participant_id: participantId, responses: initialResponses, is_active: true, validations: {}, logs: [] },
{ participant_id: participantId, responses, is_active: true, validations: {}, logs: [] },
returning
);
if (!interview.uuid || Object.keys(initialResponses).length === 0) {
Expand Down Expand Up @@ -160,7 +166,10 @@ export default class Interviews {
static getValidationAuditStats = async (
params: {
filter?: { is_valid?: 'valid' | 'invalid' | 'notInvalid' | 'notValidated' | 'all' } & {
[key: string]: string | string[] | { value: string | string[] | boolean | number | null; op?: keyof OperatorSigns };
[key: string]:
| string
| string[]
| { value: string | string[] | boolean | number | null; op?: keyof OperatorSigns };
};
} = {}
): Promise<{ auditStats: AuditsByLevelAndObjectType }> => {
Expand Down

0 comments on commit e6ed254

Please sign in to comment.