Skip to content

Instantly share code, notes, and snippets.

@adamthewan
Created March 15, 2021 14:13
Show Gist options
  • Save adamthewan/e54f24fe5f7cc49def30ef402fbf5290 to your computer and use it in GitHub Desktop.
Save adamthewan/e54f24fe5f7cc49def30ef402fbf5290 to your computer and use it in GitHub Desktop.
async function syncDailySession(startingAfter) {
/* first we check if the room exists */
const response = await axios(`${config.dailyCoApiUrl}/meetings`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${config.dailyCoApiKey}`,
},
params: {
ongoing: false,
starting_after: startingAfter,
limit,
},
}).catch(err => {
return err;
});
if (response) {
const dailySessions = response.data.data;
const totalCount = response.data.total_count;
dailySessions.forEach(async (dailySession, i) => {
try {
const roomHashid = dailySession.room.split('-')[0].toUpperCase();
const roomId = RoomHashid.decode(roomHashid);
const participantSeconds = dailySession.participants.reduce(
(t, x) => t + x.duration,
0,
);
if (roomId[0]) {
var uniqueParticipants = [];
const participants = dailySession.participants;
participants.forEach(function (participant) {
var i = uniqueParticipants.findIndex(
x => x == participant.user_id,
);
if (i <= -1) {
uniqueParticipants.push(participant.user_id);
}
});
const room = await models.Room.findOne({
where: {
id: roomId,
},
});
if (room) {
sendTrack({
userId: room.hostId,
event: 'Sync Participant Minutes',
properties: {
dailyMeetingSessionId: dailySession.id,
roomId: roomHashid,
meetingDuration: dailySession.duration,
participantSeconds,
maxParticipants: uniqueParticipants.length,
endedAt: moment(
dailySession.start_time + dailySession.duration,
'X',
).toDate(),
},
});
}
models.MeetingHistory.create({
id: dailySession.id,
dailyMeetingSessionId: dailySession.id,
dailyRoomName: dailySession.room,
roomId,
meetingDuration: dailySession.duration,
dailyParticipantsMetadata: participants,
participantSeconds,
maxParticipants: uniqueParticipants.length,
createdAt: moment(dailySession.start_time, 'X').toDate(),
endedAt: moment(
dailySession.start_time + dailySession.duration,
'X',
).toDate(),
updatedAt: new Date(),
});
}
const isLast = i + 1 === dailySessions.length;
if (isLast) {
if (totalCount > limit) {
syncDailySession(dailySession.id);
}
}
} catch (e) {
Sentry.captureException(e);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment