Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interview: Make sure _startedAt is initialized at interview creation #433

Merged
merged 1 commit into from
Apr 9, 2024
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
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
Loading