Skip to content

Commit

Permalink
fix: fixes and improved trainee dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
aimedivin committed Nov 21, 2024
1 parent 6fd2a27 commit c0c09e3
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/resolvers/2fa.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const resolvers = {
const geoData = await logGeoActivity(user, clientIpAdress)
const organizationName = user.organizations[0];
if (organizationName) {
const location = geoData.city && geoData.country_name ? `${geoData.city}-${geoData.country_name}` : null;
const location = geoData && geoData.city && geoData.country_name ? `${geoData.city}-${geoData.country_name}` : null;
await loginsCount(organizationName, location);
}

Expand Down
240 changes: 122 additions & 118 deletions src/resolvers/attendance.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,142 +349,146 @@ const returnAttendanceData = async (teamData: any) => {
return formatAttendanceData(sanitizedAttendance, teamData)
}

const attendanceResolver = {
Query: {

async getTraineeAttendance(_: any, { traineeId }: { traineeId: string }, context: Context) {
const { userId: id, role } = (await checkUserLoggedIn(context))([
RoleOfUser.TRAINEE, RoleOfUser.COORDINATOR, RoleOfUser.ADMIN, RoleOfUser.TTL
])
const userId = role === RoleOfUser.TRAINEE ? id : traineeId;

const user = await User.findById(userId);
if (!user) {
throw new GraphQLError('User with provided id do not exist')
}
const userTeamId = user.team;
const teamData = await Team.findById(userTeamId);
const attendance = await Attendance.find();
const phases: TraineeAttendancePhase[] = [];
export const fetchTraineeAttendance = async (userId: string | undefined) => {
const user = await User.findById(userId);
if (!user) {
throw new GraphQLError('User with provided id do not exist')
}
const userTeamId = user.team;
const teamData = await Team.findById(userTeamId);
const attendance = await Attendance.find();
const phases: TraineeAttendancePhase[] = [];

if (!teamData) {
throw new Error('Team provided doesn\'t exist')
}
if (!teamData) {
throw new Error('Team provided doesn\'t exist')
}

let totalAllPhasesScore = 0;
let totalAllPhasesDays = 0;
let totalAllPhasesScore = 0;
let totalAllPhasesDays = 0;

const phasesAverage: PhasesAverage[] = []
const phasesAverage: PhasesAverage[] = []

if (attendance.length) {
if (attendance.length) {

for (const attendanceRecord of attendance) {
const phaseData = await Phase.findById(attendanceRecord.phase)
for (const attendanceRecord of attendance) {
const phaseData = await Phase.findById(attendanceRecord.phase)

if (!phaseData) {
throw new Error('Phase provided doesn\'t exist')
}
const phaseObj = phaseData.toObject() as PhaseInterface

let existingPhaseAverageIndex = phasesAverage.findIndex(phase => phase.id === phaseData.id)
if (existingPhaseAverageIndex === -1) {
phasesAverage.push({
id: phaseData.id,
totalPhaseScore: 0,
totalPhaseDays: 0
});
existingPhaseAverageIndex = phasesAverage.length - 1;
}
if (!phaseData) {
throw new Error('Phase provided doesn\'t exist')
}
const phaseObj = phaseData.toObject() as PhaseInterface

let existingPhaseAverageIndex = phasesAverage.findIndex(phase => phase.id === phaseData.id)
if (existingPhaseAverageIndex === -1) {
phasesAverage.push({
id: phaseData.id,
totalPhaseScore: 0,
totalPhaseDays: 0
});
existingPhaseAverageIndex = phasesAverage.length - 1;
}

attendanceRecord.teams.forEach((traineeAttendanceData) => {
attendanceRecord.teams.forEach((traineeAttendanceData) => {
if (
traineeAttendanceData.team.equals(
(userTeamId as string).toString()
)
) {
traineeAttendanceData.trainees.forEach((traineeData) => {
if (
traineeAttendanceData.team.equals(
(userTeamId as string).toString()
)
(traineeData.trainee as string).toString() === userId &&
traineeAttendanceData.date
) {
traineeAttendanceData.trainees.forEach((traineeData) => {
if (
(traineeData.trainee as string).toString() === userId &&
traineeAttendanceData.date
) {
let totalWeekScore = 0;
let totalWeekDays = 0;
const weekDays: WeekdaysInterface = getDateForDays(
new Date(traineeAttendanceData.date).getTime().toString()
)

traineeData.status.forEach((status) => {
if (weekDays[status.day]) {
if (!('score' in weekDays[status.day])) {
weekDays[status.day].score = status.score.toString();
if (status.score && Number(status.score) >= 0) {
// sum for a week
totalWeekScore += Number(status.score);
totalWeekDays++;

// sum for a phase
phasesAverage[existingPhaseAverageIndex].totalPhaseScore += Number(status.score)
phasesAverage[existingPhaseAverageIndex].totalPhaseDays++

// sum for all phases
totalAllPhasesScore += Number(status.score);
totalAllPhasesDays++;
}
}
}
})
let totalWeekScore = 0;
let totalWeekDays = 0;
const weekDays: WeekdaysInterface = getDateForDays(
new Date(traineeAttendanceData.date).getTime().toString()
)

Object.keys(weekDays).forEach((day) => {
const dayKey = day as keyof WeekdaysInterface
if (!('score' in weekDays[dayKey])) {
weekDays[dayKey].score = null
}
})

let phaseExists = false

phases.forEach((phase) => {
if (
phase.phase._id.equals(
(attendanceRecord.phase as string).toString()
)
) {
phase.phaseAverage = (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2);
phase.weeks.push({
week: attendanceRecord.week,
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
daysStatus: weekDays,
});
phaseExists = true;
traineeData.status.forEach((status) => {
if (weekDays[status.day]) {
if (!('score' in weekDays[status.day])) {
weekDays[status.day].score = status.score.toString();
if (status.score && Number(status.score) >= 0) {
// sum for a week
totalWeekScore += Number(status.score);
totalWeekDays++;

// sum for a phase
phasesAverage[existingPhaseAverageIndex].totalPhaseScore += Number(status.score)
phasesAverage[existingPhaseAverageIndex].totalPhaseDays++

// sum for all phases
totalAllPhasesScore += Number(status.score);
totalAllPhasesDays++;
}
})

if (!phaseExists) {
phases.push({
phase: phaseObj,
phaseAverage: (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2),
weeks: [
{
week: attendanceRecord.week,
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
daysStatus: weekDays,
},
],
})
}
}
})

Object.keys(weekDays).forEach((day) => {
const dayKey = day as keyof WeekdaysInterface
if (!('score' in weekDays[dayKey])) {
weekDays[dayKey].score = null
}
})

let phaseExists = false

phases.forEach((phase) => {
if (
phase.phase._id.equals(
(attendanceRecord.phase as string).toString()
)
) {
phase.phaseAverage = (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2);
phase.weeks.push({
week: attendanceRecord.week,
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
daysStatus: weekDays,
});
phaseExists = true;
}
})

if (!phaseExists) {
phases.push({
phase: phaseObj,
phaseAverage: (phasesAverage[existingPhaseAverageIndex].totalPhaseScore / phasesAverage[existingPhaseAverageIndex].totalPhaseDays).toPrecision(2),
weeks: [
{
week: attendanceRecord.week,
weekAverage: (totalWeekScore / totalWeekDays).toPrecision(2),
daysStatus: weekDays,
},
],
})
}
}
})
}
}
})
}
}

return {
traineeId: userId,
allPhasesAverage: (totalAllPhasesScore / totalAllPhasesDays).toPrecision(2),
teamName: teamData.name,
phases,
}
return {
traineeId: userId,
allPhasesAverage: (totalAllPhasesScore / totalAllPhasesDays).toPrecision(2),
teamName: teamData.name,
phases,
}
}

const attendanceResolver = {
Query: {

async getTraineeAttendance(_: any, { traineeId }: { traineeId: string }, context: Context) {
const { userId: id, role } = (await checkUserLoggedIn(context))([
RoleOfUser.TRAINEE, RoleOfUser.COORDINATOR, RoleOfUser.ADMIN, RoleOfUser.TTL
])
const userId = role === RoleOfUser.TRAINEE ? id : traineeId;

return await fetchTraineeAttendance(userId);
},

async getTeamAttendance(
Expand Down
Loading

0 comments on commit c0c09e3

Please sign in to comment.