Skip to content

Commit

Permalink
Add archiver field to items (#191)
Browse files Browse the repository at this point in the history
* Add archiver field to items

Co-authored-by: James <[email protected]>
  • Loading branch information
elouiestudent and james-ngai authored Nov 22, 2022
1 parent 928945d commit 21a9d29
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions api/models/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface IItem extends Document {
modified: string[];
approver: string;
returner: string;
archiver: string;
}

const ItemSchema = new Schema(
Expand Down Expand Up @@ -116,6 +117,9 @@ const ItemSchema = new Schema(
returner: {
type: String,
},
archiver: {
type: String,
},
},
{
toJSON: { virtuals: true },
Expand Down
24 changes: 22 additions & 2 deletions api/routes/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ router.post("/all", isUser, async (req: Request, res: Response) => {
else console.log(docs);
});
const onlyArchived = req.body.onlyArchived ?? false;
Item.updateMany({ archiver: { $exists: false } }, [
{ $set: { archiver: null } },
]).exec(function (err, docs) {
if (err) console.log(err);
else console.log(docs);
});
Item.find({ archived: onlyArchived })
.populate("whereToRetrieve")
.sort({ dateFound: -1, timeFound: -1 })
Expand Down Expand Up @@ -94,6 +100,7 @@ router.post("/add", isUser, async (req: Request, res: Response) => {
modified: [user.username],
approver: approved ? user.username : null,
returner: null,
archiver: null,
});
item.save((err) => {
if (err) {
Expand Down Expand Up @@ -161,7 +168,11 @@ router.post("/archive", isUser, async (req: Request, res: Response) => {
) {
const updatedItem = await Item.findByIdAndUpdate(
id,
{ archived: archived, dateArchived: new Date() },
{
archived: archived,
archiver: user.username,
dateArchived: new Date(),
},
{ runValidators: true, useFindAndModify: false }
);
updatedItems.push(updatedItem);
Expand All @@ -188,6 +199,7 @@ router.post("/archiveByDays", isAdmin, async (req: Request, res: Response) => {
const days = req.body.days;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const ObjectID = require("mongodb").ObjectID;
const user = req.body.user;
Item.updateMany(
{
$and: [
Expand All @@ -203,7 +215,15 @@ router.post("/archiveByDays", isAdmin, async (req: Request, res: Response) => {
},
],
},
[{ $set: { archived: true, dateArchived: new Date() } }]
[
{
$set: {
archived: true,
archiver: user.username,
dateArchived: new Date(),
},
},
]
).exec(function (err, docs) {
if (err) {
console.log(err);
Expand Down

0 comments on commit 21a9d29

Please sign in to comment.