Skip to content
Open
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
9 changes: 6 additions & 3 deletions frontend/src/components/stages/ranking_reveal_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ export class RankingReveal extends MobxLitElement {
participantMap = {[currentId]: participantMap[currentId]};
}

const maxOptions = Math.max(
...Object.values(participantMap).map((lst) => lst.length),
);
const participantAnswers = Object.values(participantMap);
const maxOptions =
participantAnswers.length > 0
? Math.max(...participantAnswers.map((lst) => lst.length))
: 0;

return html`
<div>${headerText}</div>
Expand Down Expand Up @@ -183,6 +185,7 @@ export class RankingReveal extends MobxLitElement {
</h2>
${this.renderWinner()}
<div class="divider"></div>
${this.renderResultsTable()}
`;
}
}
Expand Down
12 changes: 6 additions & 6 deletions functions/src/stages/asset_allocation.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
AssetAllocationStageParticipantAnswer,
AssetAllocationStagePublicData,
ParticipantProfileExtended,
createAssetAllocationStagePublicData,
} from '@deliberation-lab/utils';

import * as admin from 'firebase-admin';
Expand All @@ -27,15 +26,16 @@ export async function addParticipantAnswerToAssetAllocationStagePublicData(

// Read current public data first (all reads must come before writes)
const publicDoc = await transaction.get(publicDocument);
let publicData = publicDoc.data() as
const publicData = publicDoc.data() as
| AssetAllocationStagePublicData
| undefined;

// Create initial public data if it doesn't exist
// Public stage data should be initialized on cohort creation
if (!publicData) {
publicData = createAssetAllocationStagePublicData({
id: stage.id,
});
console.warn(
`Public stage data not found for stage ${stage.id} in cohort ${participant.currentCohortId}. This should have been initialized on cohort creation.`,
);
return;
}

// Update public data with participant's allocation
Expand Down
43 changes: 25 additions & 18 deletions functions/src/stages/flipcard.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,31 @@ export async function addParticipantAnswerToFlipCardStagePublicData(
const publicDoc = await transaction.get(publicDocument);
const publicData = publicDoc.data() as FlipCardStagePublicData | undefined;

if (publicData) {
// Update public data with participant's flip history and selections
const updatedPublicData: FlipCardStagePublicData = {
...publicData,
participantFlipHistory: {
...publicData.participantFlipHistory,
[participant.publicId]: answer.flipHistory,
},
participantSelections: {
...publicData.participantSelections,
[participant.publicId]: answer.selectedCardIds,
},
};

transaction.set(publicDocument, {
...updatedPublicData,
timestamp: admin.firestore.FieldValue.serverTimestamp(),
});
if (!publicData) {
console.warn(
`Public stage data not found for stage ${stage.id} in cohort ${participant.currentCohortId}. This should have been initialized on cohort creation.`,
);
return;
}

const currentPublicData = publicData;

// Update public data with participant's flip history and selections
const updatedPublicData: FlipCardStagePublicData = {
...currentPublicData,
participantFlipHistory: {
...currentPublicData.participantFlipHistory,
[participant.publicId]: answer.flipHistory,
},
participantSelections: {
...currentPublicData.participantSelections,
[participant.publicId]: answer.selectedCardIds,
},
};

transaction.set(publicDocument, {
...updatedPublicData,
timestamp: admin.firestore.FieldValue.serverTimestamp(),
});
});
}
12 changes: 7 additions & 5 deletions functions/src/stages/multi_asset_allocation.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MultiAssetAllocationStagePublicData,
ParticipantProfile,
} from '@deliberation-lab/utils';
import * as admin from 'firebase-admin';

import {app} from '../app';

/**
Expand Down Expand Up @@ -42,16 +42,18 @@ export async function addParticipantAnswerToMultiAssetAllocationStagePublicData(
| undefined;

if (!publicData) {
console.error(
`Public data for stage ${stage.id} does not exist. Cannot update.`,
console.warn(
`Public stage data not found for stage ${stage.id} in cohort ${participant.currentCohortId}. This should have been initialized on cohort creation.`,
);
return;
}

const currentPublicData = publicData;

const updatedPublicData: MultiAssetAllocationStagePublicData = {
...publicData,
...currentPublicData,
participantAnswerMap: {
...publicData.participantAnswerMap,
...currentPublicData.participantAnswerMap,
[participant.publicId]: answer,
},
};
Expand Down
15 changes: 12 additions & 3 deletions functions/src/stages/ranking.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,18 @@ export async function addParticipantAnswerToRankingStagePublicData(
.collection('publicStageData')
.doc(LAS_WTL_STAGE_ID);
// Update public stage data (current participant rankings, current winner)
const publicStageData = (
await publicDocument.get()
).data() as RankingStagePublicData;
const publicDoc = await publicDocument.get();
const publicStageData = publicDoc.data() as
| RankingStagePublicData
| 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.rankingList;

Expand Down
16 changes: 13 additions & 3 deletions functions/src/stages/survey.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
ParticipantProfileExtended,
SurveyStageParticipantAnswer,
SurveyStagePublicData,
SurveyStageConfig,
} from '@deliberation-lab/utils';

import {app} from '../app';
Expand All @@ -25,9 +26,18 @@ export async function addParticipantAnswerToSurveyStagePublicData(
.doc(stage.id);

// Update public stage data (current participant rankings, current winner)
const publicStageData = (
await publicDocument.get()
).data() as SurveyStagePublicData;
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;

Expand Down
2 changes: 1 addition & 1 deletion utils/src/utils/algebraic.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function getRankingCandidatesFromWTL(
* filter rankings so that they only include those candidates. */
export function filterRankingsByCandidates(
participantRankings: Record<string, string[]>,
candidateList = [],
candidateList: string[] = [],
) {
Object.keys(participantRankings).forEach((id) => {
participantRankings[id] = participantRankings[id].filter(
Expand Down