Skip to content

Commit 1fc7d31

Browse files
committed
fix: fixes and improved trainee dashboard
1 parent 6fd2a27 commit 1fc7d31

File tree

5 files changed

+212
-151
lines changed

5 files changed

+212
-151
lines changed

src/resolvers/2fa.resolvers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const resolvers = {
110110
const geoData = await logGeoActivity(user, clientIpAdress)
111111
const organizationName = user.organizations[0];
112112
if (organizationName) {
113-
const location = geoData.city && geoData.country_name ? `${geoData.city}-${geoData.country_name}` : null;
113+
const location = geoData && geoData.city && geoData.country_name ? `${geoData.city}-${geoData.country_name}` : null;
114114
await loginsCount(organizationName, location);
115115
}
116116

src/resolvers/attendance.resolvers.ts

Lines changed: 122 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -349,142 +349,146 @@ const returnAttendanceData = async (teamData: any) => {
349349
return formatAttendanceData(sanitizedAttendance, teamData)
350350
}
351351

352-
const attendanceResolver = {
353-
Query: {
354-
355-
async getTraineeAttendance(_: any, { traineeId }: { traineeId: string }, context: Context) {
356-
const { userId: id, role } = (await checkUserLoggedIn(context))([
357-
RoleOfUser.TRAINEE, RoleOfUser.COORDINATOR, RoleOfUser.ADMIN, RoleOfUser.TTL
358-
])
359-
const userId = role === RoleOfUser.TRAINEE ? id : traineeId;
360-
361-
const user = await User.findById(userId);
362-
if (!user) {
363-
throw new GraphQLError('User with provided id do not exist')
364-
}
365-
const userTeamId = user.team;
366-
const teamData = await Team.findById(userTeamId);
367-
const attendance = await Attendance.find();
368-
const phases: TraineeAttendancePhase[] = [];
352+
export const fetchTraineeAttendance = async (userId: string | undefined) => {
353+
const user = await User.findById(userId);
354+
if (!user) {
355+
throw new GraphQLError('User with provided id do not exist')
356+
}
357+
const userTeamId = user.team;
358+
const teamData = await Team.findById(userTeamId);
359+
const attendance = await Attendance.find();
360+
const phases: TraineeAttendancePhase[] = [];
369361

370-
if (!teamData) {
371-
throw new Error('Team provided doesn\'t exist')
372-
}
362+
if (!teamData) {
363+
throw new Error('Team provided doesn\'t exist')
364+
}
373365

374-
let totalAllPhasesScore = 0;
375-
let totalAllPhasesDays = 0;
366+
let totalAllPhasesScore = 0;
367+
let totalAllPhasesDays = 0;
376368

377-
const phasesAverage: PhasesAverage[] = []
369+
const phasesAverage: PhasesAverage[] = []
378370

379-
if (attendance.length) {
371+
if (attendance.length) {
380372

381-
for (const attendanceRecord of attendance) {
382-
const phaseData = await Phase.findById(attendanceRecord.phase)
373+
for (const attendanceRecord of attendance) {
374+
const phaseData = await Phase.findById(attendanceRecord.phase)
383375

384-
if (!phaseData) {
385-
throw new Error('Phase provided doesn\'t exist')
386-
}
387-
const phaseObj = phaseData.toObject() as PhaseInterface
388-
389-
let existingPhaseAverageIndex = phasesAverage.findIndex(phase => phase.id === phaseData.id)
390-
if (existingPhaseAverageIndex === -1) {
391-
phasesAverage.push({
392-
id: phaseData.id,
393-
totalPhaseScore: 0,
394-
totalPhaseDays: 0
395-
});
396-
existingPhaseAverageIndex = phasesAverage.length - 1;
397-
}
376+
if (!phaseData) {
377+
throw new Error('Phase provided doesn\'t exist')
378+
}
379+
const phaseObj = phaseData.toObject() as PhaseInterface
380+
381+
let existingPhaseAverageIndex = phasesAverage.findIndex(phase => phase.id === phaseData.id)
382+
if (existingPhaseAverageIndex === -1) {
383+
phasesAverage.push({
384+
id: phaseData.id,
385+
totalPhaseScore: 0,
386+
totalPhaseDays: 0
387+
});
388+
existingPhaseAverageIndex = phasesAverage.length - 1;
389+
}
398390

399-
attendanceRecord.teams.forEach((traineeAttendanceData) => {
391+
attendanceRecord.teams.forEach((traineeAttendanceData) => {
392+
if (
393+
traineeAttendanceData.team.equals(
394+
(userTeamId as string).toString()
395+
)
396+
) {
397+
traineeAttendanceData.trainees.forEach((traineeData) => {
400398
if (
401-
traineeAttendanceData.team.equals(
402-
(userTeamId as string).toString()
403-
)
399+
(traineeData.trainee as string).toString() === userId &&
400+
traineeAttendanceData.date
404401
) {
405-
traineeAttendanceData.trainees.forEach((traineeData) => {
406-
if (
407-
(traineeData.trainee as string).toString() === userId &&
408-
traineeAttendanceData.date
409-
) {
410-
let totalWeekScore = 0;
411-
let totalWeekDays = 0;
412-
const weekDays: WeekdaysInterface = getDateForDays(
413-
new Date(traineeAttendanceData.date).getTime().toString()
414-
)
415-
416-
traineeData.status.forEach((status) => {
417-
if (weekDays[status.day]) {
418-
if (!('score' in weekDays[status.day])) {
419-
weekDays[status.day].score = status.score.toString();
420-
if (status.score && Number(status.score) >= 0) {
421-
// sum for a week
422-
totalWeekScore += Number(status.score);
423-
totalWeekDays++;
424-
425-
// sum for a phase
426-
phasesAverage[existingPhaseAverageIndex].totalPhaseScore += Number(status.score)
427-
phasesAverage[existingPhaseAverageIndex].totalPhaseDays++
428-
429-
// sum for all phases
430-
totalAllPhasesScore += Number(status.score);
431-
totalAllPhasesDays++;
432-
}
433-
}
434-
}
435-
})
402+
let totalWeekScore = 0;
403+
let totalWeekDays = 0;
404+
const weekDays: WeekdaysInterface = getDateForDays(
405+
new Date(traineeAttendanceData.date).getTime().toString()
406+
)
436407

437-
Object.keys(weekDays).forEach((day) => {
438-
const dayKey = day as keyof WeekdaysInterface
439-
if (!('score' in weekDays[dayKey])) {
440-
weekDays[dayKey].score = null
441-
}
442-
})
443-
444-
let phaseExists = false
445-
446-
phases.forEach((phase) => {
447-
if (
448-
phase.phase._id.equals(
449-
(attendanceRecord.phase as string).toString()
450-
)
451-
) {
452-
phase.phaseAverage = (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2);
453-
phase.weeks.push({
454-
week: attendanceRecord.week,
455-
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
456-
daysStatus: weekDays,
457-
});
458-
phaseExists = true;
408+
traineeData.status.forEach((status) => {
409+
if (weekDays[status.day]) {
410+
if (!('score' in weekDays[status.day])) {
411+
weekDays[status.day].score = status.score.toString();
412+
if (status.score && Number(status.score) >= 0) {
413+
// sum for a week
414+
totalWeekScore += Number(status.score);
415+
totalWeekDays++;
416+
417+
// sum for a phase
418+
phasesAverage[existingPhaseAverageIndex].totalPhaseScore += Number(status.score)
419+
phasesAverage[existingPhaseAverageIndex].totalPhaseDays++
420+
421+
// sum for all phases
422+
totalAllPhasesScore += Number(status.score);
423+
totalAllPhasesDays++;
459424
}
460-
})
461-
462-
if (!phaseExists) {
463-
phases.push({
464-
phase: phaseObj,
465-
phaseAverage: (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2),
466-
weeks: [
467-
{
468-
week: attendanceRecord.week,
469-
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
470-
daysStatus: weekDays,
471-
},
472-
],
473-
})
474425
}
475426
}
476427
})
428+
429+
Object.keys(weekDays).forEach((day) => {
430+
const dayKey = day as keyof WeekdaysInterface
431+
if (!('score' in weekDays[dayKey])) {
432+
weekDays[dayKey].score = null
433+
}
434+
})
435+
436+
let phaseExists = false
437+
438+
phases.forEach((phase) => {
439+
if (
440+
phase.phase._id.equals(
441+
(attendanceRecord.phase as string).toString()
442+
)
443+
) {
444+
phase.phaseAverage = (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2);
445+
phase.weeks.push({
446+
week: attendanceRecord.week,
447+
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
448+
daysStatus: weekDays,
449+
});
450+
phaseExists = true;
451+
}
452+
})
453+
454+
if (!phaseExists) {
455+
phases.push({
456+
phase: phaseObj,
457+
phaseAverage: (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2),
458+
weeks: [
459+
{
460+
week: attendanceRecord.week,
461+
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
462+
daysStatus: weekDays,
463+
},
464+
],
465+
})
466+
}
477467
}
478468
})
479469
}
480-
}
470+
})
471+
}
472+
}
481473

482-
return {
483-
traineeId: userId,
484-
allPhasesAverage: (totalAllPhasesScore / totalAllPhasesDays).toPrecision(2),
485-
teamName: teamData.name,
486-
phases,
487-
}
474+
return {
475+
traineeId: userId,
476+
allPhasesAverage: (totalAllPhasesScore / totalAllPhasesDays).toPrecision(2),
477+
teamName: teamData.name,
478+
phases,
479+
}
480+
}
481+
482+
const attendanceResolver = {
483+
Query: {
484+
485+
async getTraineeAttendance(_: any, { traineeId }: { traineeId: string }, context: Context) {
486+
const { userId: id, role } = (await checkUserLoggedIn(context))([
487+
RoleOfUser.TRAINEE, RoleOfUser.COORDINATOR, RoleOfUser.ADMIN, RoleOfUser.TTL
488+
])
489+
const userId = role === RoleOfUser.TRAINEE ? id : traineeId;
490+
491+
return await fetchTraineeAttendance(userId);
488492
},
489493

490494
async getTeamAttendance(

0 commit comments

Comments
 (0)