Skip to content

Commit

Permalink
Get the assessments with students submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
sukanyamohanty143 committed Sep 20, 2024
1 parent cb5ea2a commit ef234a2
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/controller/adminAssessment/adminAssessment.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Controller, Get, Post, Put, Patch, Delete, Body, Param, ValidationPipe, UsePipes, Optional, Query, BadRequestException, Req } from '@nestjs/common';
import { Controller, Get, Post, Put, Patch, Delete, Body, Param, ValidationPipe, UsePipes, Optional, Query, BadRequestException, Req, Res } from '@nestjs/common';
import { AdminAssessmentService } from './adminAssessment.service';
import { ApiTags, ApiBody, ApiOperation, ApiCookieAuth, ApiQuery } from '@nestjs/swagger';
import { ApiBearerAuth } from '@nestjs/swagger';
import { Request } from '@nestjs/common';
import { query } from 'express';
import { STATUS_CODES } from 'src/helpers';
import { ErrorResponse, SuccessResponse } from 'src/errorHandler/handler';


@Controller('admin')
Expand Down Expand Up @@ -50,4 +52,23 @@ export class AdminAssessmentController {
return this.adminAssessmentService.getUserAssessmentSubmission(req,submissionID, userID);
}

@Get(':bootcamp_id/assessments')
@ApiOperation({ summary: 'Get all assessments for a bootcamp' })
@ApiBearerAuth()
async AssessmentsAndStudents(
@Param('bootcamp_id') bootcampID: number,
@Res() res
) {
try {
const result = await this.adminAssessmentService.getAssessmentsAndStudents(bootcampID);

if (result.statusCode === STATUS_CODES.NOT_FOUND) {
return ErrorResponse.BadRequestException(result.message).send(res);
}

return new SuccessResponse('Assessments retrieved successfully', STATUS_CODES.OK, result).send(res);
} catch (error) {
return ErrorResponse.BadRequestException(error.message).send(res);
}
}
}
112 changes: 112 additions & 0 deletions src/controller/adminAssessment/adminAssessment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { db } from '../../db/index';
import { sql } from 'drizzle-orm';
import * as _ from 'lodash';
import { zuvyBatchEnrollments, zuvyOutsourseAssessments } from '../../../drizzle/schema';
import { STATUS_CODES } from 'src/helpers';

const { ZUVY_CONTENT_URL } = process.env; // INPORTING env VALUSE ZUVY_CONTENT

Expand Down Expand Up @@ -360,4 +361,115 @@ export class AdminAssessmentService {
throw error;
}
}

async getAssessmentsAndStudents(bootcampID: number): Promise<any> {
try {
const assessments = await db.query.zuvyOutsourseAssessments.findMany({
where: (zuvyOutsourseAssessments, { eq }) =>
eq(zuvyOutsourseAssessments.bootcampId, bootcampID),
columns: {
id: true,
bootcampId: true,
passPercentage: true,
order: true,
},
with: {
submitedOutsourseAssessments: {
columns: {
id: true,
userId: true,
marks: true,
startedAt: true,
submitedAt: true,
isPassed: true,
percentage: true,
},
where: (submitedOutsourseAssessments, { sql }) => sql`
${submitedOutsourseAssessments.submitedAt} IS NOT NULL
AND EXISTS (
SELECT 1
FROM main.zuvy_batch_enrollments
WHERE main.zuvy_batch_enrollments.user_id = ${submitedOutsourseAssessments.userId}
AND main.zuvy_batch_enrollments.bootcamp_id = ${bootcampID}
AND main.zuvy_batch_enrollments.batch_id IS NOT NULL
)
`,
with: {
user: {
columns: {
name: true,
email: true,
},
},
},
},
ModuleAssessment: {
columns: {
title: true,
description: true,
},
},
Module: {
columns: {
name: true,
description: true,
timeAlloted: true,
order: true,
},
},
Quizzes: true,
OpenEndedQuestions: true,
CodingQuestions: true,
},
});

if (!assessments || assessments.length === 0) {
return [{ statusCode: STATUS_CODES.NOT_FOUND, message: 'No assessments found.' }];
}

const assessmentsByModule = assessments.reduce((acc, assessment) => {
const moduleName = assessment.Module?.name;

const assessmentData = {
id: assessment.id,
order: assessment.order || 0,
title: assessment.ModuleAssessment?.title || null,
description: assessment.ModuleAssessment?.description || null,
totalCodingQuestions: assessment.CodingQuestions?.length || 0,
totalOpenEndedQuestions: assessment.OpenEndedQuestions?.length || 0,
totalQuizzes: assessment.Quizzes?.length || 0,
totalSubmitedAssessments: assessment.submitedOutsourseAssessments?.length || 0,
qualifiedStudents: assessment.submitedOutsourseAssessments?.filter(sub => sub.isPassed).length || 0,
passPercentage: assessment.passPercentage,
submitedOutsourseAssessments: assessment.submitedOutsourseAssessments.map(submission => ({
id: submission.id,
userId: submission.userId,
marks: submission.marks,
startedAt: submission.startedAt,
submitedAt: submission.submitedAt,
isPassed: submission.isPassed,
percentage: submission.percentage,
name: submission['user'].name || null,
email: submission['user'].email || null,
})),
};
if (!acc[moduleName]) {
acc[moduleName] = [];
}
acc[moduleName].push(assessmentData);

return acc;
}, {});

const studentsEnrolled = await this.getTotalStudentsEnrolled(bootcampID);

return {
statusCode: STATUS_CODES.OK,
...assessmentsByModule,
totalStudents: studentsEnrolled.length,
};
} catch (err) {
return [{message: err.message}]
}
}
}

0 comments on commit ef234a2

Please sign in to comment.