-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathsurvey.utils.ts
More file actions
47 lines (41 loc) · 1.44 KB
/
survey.utils.ts
File metadata and controls
47 lines (41 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
ParticipantProfileExtended,
SurveyStageParticipantAnswer,
SurveyStagePublicData,
SurveyStageConfig,
} from '@deliberation-lab/utils';
import {app} from '../app';
/** Update survey stage public data to include participant private data. */
export async function addParticipantAnswerToSurveyStagePublicData(
experimentId: string,
stage: SurveyStageConfig,
participant: ParticipantProfileExtended,
answer: SurveyStageParticipantAnswer,
) {
// Run document write as transaction to ensure consistency
await app.firestore().runTransaction(async (transaction) => {
const publicDocument = app
.firestore()
.collection('experiments')
.doc(experimentId)
.collection('cohorts')
.doc(participant.currentCohortId)
.collection('publicStageData')
.doc(stage.id);
// Update public stage data (current participant rankings, current winner)
const publicDoc = await transaction.get(publicDocument);
const publicStageData = publicDoc.data() as
| SurveyStagePublicData
| undefined;
if (!publicStageData) {
console.warn(
`Public stage data not found for stage ${stage.id} in cohort ${participant.currentCohortId}. This should have been initialized on cohort creation.`,
);
return;
}
publicStageData.participantAnswerMap[participant.publicId] =
answer.answerMap;
// Write public data
transaction.set(publicDocument, publicStageData);
});
}