Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized and handled some edge cases in some api #232

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions drizzle/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2831,6 +2831,7 @@ export const postsRelations = relations(zuvyModuleChapter, ({ one, many }) => ({
formTrackingDetails: many(zuvyFormTracking),
OutsourseAssessments: many(zuvyOutsourseAssessments),
ModuleAssessment: many(zuvyModuleAssessment),
assignmentTrackingDetails: many(zuvyAssignmentSubmission),
moduleTracked: one(zuvyModuleTracking, {
fields: [zuvyModuleChapter.moduleId],
references: [zuvyModuleTracking.moduleId],
Expand Down Expand Up @@ -3111,8 +3112,12 @@ export const userCodeRelations = relations(users, ({ one }) => ({
}),
}));

export const userAssignmentSubmissionRelations = relations(zuvyAssignmentSubmission, ({ many }) => ({
assignmentSubmission : many(users)
export const userAssignmentSubmissionRelations = relations(zuvyAssignmentSubmission, ({one, many }) => ({
assignmentSubmission : many(users),
chapterDetails:one(zuvyModuleChapter, {
fields: [zuvyAssignmentSubmission.chapterId],
references: [zuvyModuleChapter.id],
}),
}));
export const zuvyChapterRelations = relations(
zuvyChapterTracking,
Expand Down
42 changes: 36 additions & 6 deletions src/controller/progress/tracking.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,28 @@ export class TrackingController {
);
return res;
}

@Get('getQuizAndAssignmentWithStatus')
@ApiOperation({ summary: 'get Quiz And Assignment With Status' })
@ApiBearerAuth()
async getQuizAndAssignmentWithStatus(
@Req() req,
@Query('chapterId') chapterId: number,
@Res() res
) {
try {
let [err, success] = await this.TrackingService.getQuizAndAssignmentWithStatus(
req.user[0].id,
chapterId,
);
if (err) {
return ErrorResponse.BadRequestException(err.message, err.statusCode).send(res)
}
return new SuccessResponse(success.message, success.statusCode, success.data).send(res);
} catch (error) {
return ErrorResponse.BadRequestException(error.message).send(res);
}
}

@Post('updateProject/:projectId')
@ApiOperation({ summary: 'Update project for a user' })
Expand Down Expand Up @@ -490,13 +512,21 @@ export class TrackingController {
@Param('projectId') projectId: number,
@Param('moduleId') moduleId: number,
@Req() req,
@Res() res
) {
const res = await this.TrackingService.getProjectDetailsWithStatus(
projectId,
moduleId,
req.user[0].id
);
return res;
try {
let [err, success] = await this.TrackingService.getProjectDetailsWithStatus(
projectId,
moduleId,
req.user[0].id
);
if (err) {
return ErrorResponse.BadRequestException(err.message, err.statusCode).send(res)
}
return new SuccessResponse(success.message, success.statusCode, success.data).send(res);
} catch (error) {
return ErrorResponse.BadRequestException(error.message).send(res);
}
}

@Get('/allBootcampProgress')
Expand Down
84 changes: 77 additions & 7 deletions src/controller/progress/tracking.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,69 @@ export class TrackingService {
}
}

async getQuizAndAssignmentWithStatus(
userId: number,
chapterId: number,
):Promise<any>
{
try {
const chapter = await db.select().from(zuvyModuleChapter).where(eq(zuvyModuleChapter.id,chapterId));
if(chapter.length > 0)
{
if(chapter[0].topicId == 4)
{
if(chapter[0].quizQuestions !== null)
{
const trackedData = await db.query.zuvyModuleQuiz.findMany({
where: (moduleQuiz, { sql }) => sql`${inArray(moduleQuiz.id, Object.values(chapter[0].quizQuestions))}`,
orderBy: (moduleQuiz, { asc }) => asc(moduleQuiz.id),
with: {

quizTrackingData: {
columns: {
chosenOption: true,
status: true
},
where: (quizTracking, { sql }) => sql`${quizTracking.userId} = ${userId} and ${quizTracking.chapterId} = ${chapterId}`,
}
}
});
const quizQuestionsLength = Object.values(chapter[0].quizQuestions).length;
const quizQuestionsWithTrackingData = trackedData.filter(quiz => quiz['quizTrackingData'].length > 0).length;

const allQuestionsHaveTrackingData = quizQuestionsLength === quizQuestionsWithTrackingData;
const status = allQuestionsHaveTrackingData ? 'Completed' : 'Pending';
return [null,{message:'Chapter details fetched successfully',statusCode: STATUS_CODES.OK,data:{chapterTitle:chapter[0].title,chapterId:chapter[0].id,chapterOrder:chapter[0].order,quizDetails:trackedData,status}}]
}
else {
return [null,{message:'No quiz questions found in this quiz chapter',statusCode: STATUS_CODES.OK,data:{chapterTitle:chapter[0].title,chapterId:chapter[0].id,chapterOrder:chapter[0].order,quizDetails: [],status:'Pending'}}]
}
}
else if(chapter[0].topicId == 5)
{
const assignmentTracking = await db
.select()
.from(zuvyAssignmentSubmission)
.where(sql`${zuvyAssignmentSubmission.userId} = ${userId}
AND ${zuvyAssignmentSubmission.chapterId} = ${chapterId}`);

const status = assignmentTracking.length > 0 ? 'Completed' : 'Pending';

return [null,{message:'Assignment chapter fetched succesfully',statusCode: STATUS_CODES.OK,data:{chapterDetails:chapter[0],assignmentTracking,status}}]
}
else {
return [null,{message:'It is not a quiz or assignment chapter',statusCode: STATUS_CODES.OK,data:null}]
}
}
else {
return [null,{message:'No chapter found',statusCode: STATUS_CODES.OK,data:null}]
}
}catch(error)
{

}
}

async submitProjectForAUser(
userId: number,
bootcampId: number,
Expand Down Expand Up @@ -1413,7 +1476,7 @@ export class TrackingService {
} catch (err) { }
}

async getProjectDetailsWithStatus(projectId: number, moduleId: number, userId: number) {
async getProjectDetailsWithStatus(projectId: number, moduleId: number, userId: number):Promise<any> {
try {
const data = await db.query.zuvyCourseModules.findFirst({
where: (courseModules, { eq }) =>
Expand All @@ -1422,6 +1485,17 @@ export class TrackingService {
projectData: {
where: (projectDetails, { eq }) =>
eq(projectDetails.id, projectId),
with: {
projectTrackingData: {
columns: {
projectLink:true,
isChecked:true,
grades:true,
createdAt:true
},
where: (projectTrack, { eq }) => eq(projectTrack.userId, userId)
}
}
},
moduleTracking: {
columns: {
Expand All @@ -1438,13 +1512,9 @@ export class TrackingService {
projectData: data['projectData'],
status: data['moduleTracking'].length > 0 ? 'Completed' : 'Pending'
}
return {
status: 'success',
code: 200,
projectDetails
}
return [null,{message:'Project details successfully fetched',statusCode: STATUS_CODES.OK,data:projectDetails}]
} catch (err) {
throw err;
return [{message:err.message,statusCode: STATUS_CODES.BAD_REQUEST}]
}
}

Expand Down