Skip to content

Commit

Permalink
Deprecated using examiner collection and use the teachers collection …
Browse files Browse the repository at this point in the history
…in classrooms to check the permission instead & change the existedIDs to be existedScoreData
  • Loading branch information
sirateek committed Jul 9, 2021
1 parent 2bbc2ff commit e3384b5
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/API/v1/WorkManagementApi/submitScoreApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@ class SubmitScoreApi extends WorkManagementApi {
return this.checkworkIdExist(firestore, workId)
.then((workDoc: FirebaseFirestore.DocumentData) => {
classroomId = workDoc.classroomId;
return this.checkWorkAccessPermission(firestore, workId, userId);
return this.checkWorkAccessPermission(firestore, classroomId, userId);
})
.then(() => {
return this.checkWorkDraft(firestore, workId, req.body.workDraft);
})
.then(() => {
return this.checkIfDocAlreadyExists(firestore, workId, req);
})
.then((existedIDs) => {
.then((existedScoreData) => {
return this.writeScoresToDatabase(
firestore,
workId,
userId,
classroomId,
req,
existedIDs
existedScoreData
);
})
.then((apiResponse: APIResponse) => {
Expand All @@ -63,7 +63,7 @@ class SubmitScoreApi extends WorkManagementApi {
apiResponse.statusCode,
apiResponse.message,
{
existedIDs: apiResponse.existedIDs,
existedScoreData: apiResponse.existedScoreData,
studentIdsNotEnrolled: apiResponse.studentIdsNotEnrolled,
}
);
Expand All @@ -79,13 +79,13 @@ class SubmitScoreApi extends WorkManagementApi {
userId: string,
classroomId: string,
req: Request,
existedIDs: Array<string>
existedScoreData: Array<string>
) {
let promiseArray: Array<Promise<FirebaseFirestore.WriteResult>> = [];
let notFoundStudentId: Array<string> = [];
for (let elementIndex in req.body.scores) {
const element: ScoreElement = req.body.scores[elementIndex];
if (!existedIDs.includes(element.studentId)) {
if (!existedScoreData.includes(element.studentId)) {
if (
!(await this.checkStudentExists(
firestore,
Expand Down Expand Up @@ -128,8 +128,8 @@ class SubmitScoreApi extends WorkManagementApi {
message: ResponseMessage.success,
};
console.debug("Writed all scores to the database");
if (existedIDs.length !== 0) {
apiResponse["existedIDs"] = existedIDs;
if (existedScoreData.length !== 0) {
apiResponse["existedScoreData"] = existedScoreData;
}
if (notFoundStudentId.length !== 0) {
apiResponse["studentIdsNotEnrolled"] = notFoundStudentId;
Expand Down Expand Up @@ -167,17 +167,17 @@ class SubmitScoreApi extends WorkManagementApi {

async checkWorkAccessPermission(
firestore: FirebaseFirestore.Firestore,
workId: string,
classroomId: string,
userId: string
) {
let examinerDoc: FirebaseFirestore.DocumentSnapshot = await firestore
.collection("Works")
.doc(workId)
.collection("examiner")
let teacherDoc: FirebaseFirestore.DocumentSnapshot = await firestore
.collection("Classrooms")
.doc(classroomId)
.collection("teachers")
.doc(userId)
.get();

if (!examinerDoc.exists) {
if (!teacherDoc.exists) {
throw {
statusCode: ResponseStatusCode.forbidden,
message: WorkManagementApiResponseMessage.insufficientPermission,
Expand Down

0 comments on commit e3384b5

Please sign in to comment.