Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
Add contributos to homework
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Feb 26, 2024
1 parent 140d0f1 commit a55f380
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/database/homework/homework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ homeworkCollection.createIndex({ class: 1, from: 1 }, { unique: true });

export interface Homework {
creator: ObjectId;
contributors: ObjectId[];
class: ObjectId;

createdAt: number; // timestamp
Expand Down
12 changes: 12 additions & 0 deletions src/migrations/addContributors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { homeworkCollection } from '../database/homework/homework';

(async () => {
const allHomework = await homeworkCollection.find().toArray();

for (const hw of allHomework) {
homeworkCollection.updateOne(
{ _id: hw._id },
{ $set: { contributors: [hw.creator] } },
);
}
})();
1 change: 1 addition & 0 deletions src/routes/homework/createHomework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ router.post('/', authenticate, async (req, res) => {
buildHomework.class = classOfHomework._id;
buildHomework.creator = user._id;
buildHomework.createdAt = Date.now();
buildHomework.contributors = [];

let data: WithId<Homework>;

Expand Down
15 changes: 7 additions & 8 deletions src/routes/homework/updateHomework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,14 @@ router.put('/:id', authenticate, async (req, res) => {
const { assignments, from } = zodResult.data;
const newHomework: Homework = {
from: sortDate(from),
assignments: assignments.map((assignment) => {
return {
subject: assignment.subject,
description: assignment.description,
due: sortDate(assignment.due),
};
}),
assignments: assignments.map((assignment) => ({
subject: assignment.subject,
description: assignment.description,
due: sortDate(assignment.due),
})),
class: classId,
creator: userId,
creator: oldHomework.creator,
contributors: [...oldHomework.contributors, userId],
createdAt,
};

Expand Down

0 comments on commit a55f380

Please # to comment.