-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Remove ReadySetCyber (RSC) from TypeScript Backend. (#723)"
This reverts commit fac878c.
- Loading branch information
Showing
15 changed files
with
652 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { validateBody, wrapHandler, NotFound, Unauthorized } from './helpers'; | ||
import { Assessment, connectToDatabase } from '../models'; | ||
import { isUUID } from 'class-validator'; | ||
|
||
/** | ||
* @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 = 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 list = 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 get = 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', | ||
'responses.question.resources' | ||
] | ||
}); | ||
|
||
if (!assessment) { | ||
return NotFound; | ||
} | ||
|
||
// Sort responses by question.number and then by category.number | ||
assessment.responses.sort((a, b) => { | ||
const questionNumberComparison = a.question.number.localeCompare( | ||
b.question.number | ||
); | ||
if (questionNumberComparison !== 0) { | ||
return questionNumberComparison; | ||
} else { | ||
return a.question.category.number.localeCompare( | ||
b.question.category.number | ||
); | ||
} | ||
}); | ||
|
||
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) | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
OneToMany, | ||
PrimaryGeneratedColumn | ||
} from 'typeorm'; | ||
import { Response } from './response'; | ||
import { User } from './user'; | ||
|
||
@Entity() | ||
export class Assessment extends BaseEntity { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column() | ||
createdAt: Date; | ||
|
||
@Column() | ||
updatedAt: Date; | ||
|
||
@Column({ unique: true }) | ||
rscId: string; | ||
|
||
@Column() | ||
type: string; | ||
|
||
@ManyToOne(() => User, (user) => user.assessments) | ||
user: User; | ||
|
||
@OneToMany(() => Response, (response) => response.assessment) | ||
responses: Response[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
OneToMany, | ||
PrimaryGeneratedColumn | ||
} from 'typeorm'; | ||
import { Question } from './question'; | ||
|
||
@Entity() | ||
export class Category extends BaseEntity { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column({ unique: true }) | ||
number: string; | ||
|
||
@Column({ nullable: true }) | ||
shortName: string; | ||
|
||
@OneToMany(() => Question, (question) => question.category) | ||
questions: Question[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.