async function assignParticipantsToRoom()

in projects/deliberation_at_scale/packages/orchestrator/src/tasks/handleQueuedParticipants.ts [279:319]


async function assignParticipantsToRoom(options: AssignParticipantsToRoomOptions) {
    const { participantIds, topicIds, helpers } = options;
    const roomEndAt = dayjs().add(MAX_ROOM_DURATION_MS, 'ms');
    const externalRoom = await createExternalRoom(roomEndAt);
    const selectedTopicId = draw(topicIds);

    if (!externalRoom) {
        return Promise.reject(`Could not create an external room, participants: ${JSON.stringify(participantIds)}`);
    }

    if (!selectedTopicId) {
        return Promise.reject(`Could not find a topic candidate for a new room, topic IDs: ${JSON.stringify(topicIds)}`);
    }

    const insertRoomResult = await supabaseClient.from('rooms').insert({
        active: true,
        topic_id: selectedTopicId,
        starts_at: dayjs().toISOString(),
        external_room_id: externalRoom.roomUrl,
    }).select();
    const roomId = insertRoomResult?.data?.[0].id;

    if (!roomId) {
        return Promise.reject(`Could not create a new room for topic ${selectedTopicId} and external room: ${JSON.stringify(externalRoom)}`);
    }

    helpers.logger.info(`Successfully created a new room with ID ${roomId}, with external room: ${JSON.stringify(externalRoom)}`);

    // TODO: consider checking whether everyone is still queued
    const assignParticipantsResult = await supabaseClient.from('participants').update({
        room_id: roomId,
        status: 'waiting_for_confirmation',
    }).in('id', [participantIds]);

    if (assignParticipantsResult.error) {
        return Promise.reject(`Could not assign participants to room ${roomId}, participants: ${JSON.stringify(participantIds)}`);
    }

    helpers.logger.info(`Successfully assigned participants to room ${roomId}, participants: ${JSON.stringify(participantIds)}`);
    return true;
}