Skip to content

Commit

Permalink
Add rough draft API endpoints for assessments; add shortName to categ…
Browse files Browse the repository at this point in the history
…ory table.
  • Loading branch information
Matthew-Grayson committed Mar 28, 2024
1 parent c8304a9 commit 0cc46ef
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
107 changes: 107 additions & 0 deletions backend/src/api/assessments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { validateBody, wrapHandler, NotFound, Unauthorized } from './helpers';
import { connectToDatabase } from '../models';
import { Assessment } from '../models/assessment';
import { isUUID } from 'class-validator';
import { Response } from '../models/response';
import { Question } from '../models/question';
import { Category } from '../models/category';

/**
* @swagger
*
* /assessments:
* post:
* description: Save an RSC assessment to the XFD database.
* tags:
* - Assessments
*/
export const createAssessment = wrapHandler(async (event) => {
const body = await validateBody(Assessment, event.body);

await connectToDatabase();

const assessment = await Assessment.create(body);
await Assessment.save(assessment);

return {
statusCode: 200,
body: JSON.stringify(assessment)
};
});

/**
* @swagger
*
* /assessments:
* get:
* description: Lists all assessments for the logged in user.
* tags:
* - Assessments
*/
export const listAssessments = wrapHandler(async (event) => {
const userId = event.requestContext.authorizer!.id;

if (!userId) {
return Unauthorized;
}

await connectToDatabase();

const assessments = await Assessment.find({
where: { user: userId }
});

return {
statusCode: 200,
body: JSON.stringify(assessments)
};
});

/**
* @swagger
*
* /assessments/{id}:
* get:
* description: Return user responses and questions organized by category for a specific assessment.
* parameters:
* - in: path
* name: id
* description: Assessment id
* tags:
* - Assessments
*/
export const getAssessment = wrapHandler(async (event) => {
const assessmentId = event.pathParameters?.id;

if (!assessmentId || !isUUID(assessmentId)) {
return NotFound;
}

await connectToDatabase();

const assessment = await Assessment.findOne(assessmentId, {
relations: [
'responses',
'responses.question',
'responses.question.category'
]
});

if (!assessment) {
return NotFound;
}

const responsesByCategory = assessment.responses.reduce((acc, response) => {
const categoryName = response.question.category.name;
if (!acc[categoryName]) {
acc[categoryName] = [];
}
acc[categoryName].push(response);
return acc;
}, {});

return {
statusCode: 200,
body: JSON.stringify(responsesByCategory)
};
});
3 changes: 3 additions & 0 deletions backend/src/models/category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class Category extends BaseEntity {
@Column()
name: string;

@Column({ nullable: true })
shortName: string;

@OneToMany(() => Question, (question) => question.category)
questions: Question[];
}

0 comments on commit 0cc46ef

Please sign in to comment.