From 451d985f02d510878ad7d825918b0d857080410c Mon Sep 17 00:00:00 2001 From: Chege Gitau Date: Fri, 5 Jul 2024 12:12:11 -0700 Subject: [PATCH 1/2] [Metadata] Save metadata after modification --- src/models/MetadataMongoDB.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/models/MetadataMongoDB.ts b/src/models/MetadataMongoDB.ts index 91c85be4..67154891 100644 --- a/src/models/MetadataMongoDB.ts +++ b/src/models/MetadataMongoDB.ts @@ -182,7 +182,7 @@ export async function updatePublicUserMetadata( metadataDoc.markModified("stats"); metadataDoc.markModified("node_information"); - // await metadataDoc.save(); + await metadataDoc.save(); return metadataDoc; } @@ -262,7 +262,7 @@ export async function sendCardToTrash( metadataDoc.markModified("node_information"); metadataDoc.markModified("trashed_cards"); - // await metadataDoc.save(); + await metadataDoc.save(); return `Card moved to the trash. Undo Action`; } @@ -294,7 +294,7 @@ export async function restoreCardFromTrash( card.tags, "urgency", ); - // await metadataDoc.save(); + await metadataDoc.save(); return `${card.title} has been restored!`; } @@ -320,7 +320,7 @@ export async function deleteCardFromTrash( } const metadataDoc = await removeCardFromMetadataTrash(card); - // await metadataDoc.save(); + await metadataDoc.save(); return `${card.title} has been permanently deleted!`; } @@ -350,7 +350,7 @@ async function removeCardFromMetadataTrash( if (cardIdentifier._id in metadataDoc.trashed_cards[0]) { delete metadataDoc.trashed_cards[0][cardIdentifier._id]; metadataDoc.markModified("trashed_cards"); - // await metadataDoc.save(); + await metadataDoc.save(); return metadataDoc; } else { return Promise.reject("Card wasn't found in the trash"); @@ -510,7 +510,7 @@ export async function updateUserSettings( } metadataDoc.streak.dailyTarget = newUserSettings.dailyTarget; metadataDoc.markModified("streak"); - // await metadataDoc.save(); + await metadataDoc.save(); } return user.save(); From 539736f4f5ae91ed0424f278229d455d214ba995 Mon Sep 17 00:00:00 2001 From: Chege Gitau Date: Fri, 5 Jul 2024 12:12:49 -0700 Subject: [PATCH 2/2] [Metadata] Add tests for deleting and restoring a trashed card --- src/routes/InAppRouter.test.ts | 42 +++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/routes/InAppRouter.test.ts b/src/routes/InAppRouter.test.ts index a8e52eec..3ada077f 100644 --- a/src/routes/InAppRouter.test.ts +++ b/src/routes/InAppRouter.test.ts @@ -249,10 +249,13 @@ describe("/trpc/updateCard", function() { }); describe("/trpc/trashCard", function() { + let cardID: string; + this.beforeEach(async () => { const gUser = await authenticateUser(authDetails); gCaller = createCaller({ user: gUser }); - return await createPublicAndPrivateCards(gUser); + ({ samplePublicCardId: cardID } = await createPublicAndPrivateCards(gUser)); + return Promise.resolve(); }); it("should use an input parser/validator", function(done) { @@ -264,6 +267,43 @@ describe("/trpc/trashCard", function() { passIfValidationError(e, done); }); }); + + describe("when a card is trashed", function() { + this.beforeEach(async () => { + { + const trashedCards = (await gCaller.metadata())[0].trashed_cards; + expect(trashedCards.length).equal(0); + } + + await gCaller.trashCard({ _id: cardID }); + + { + const trashedCards = (await gCaller.metadata())[0].trashed_cards; + expect(trashedCards.length).equal(1); + expect(Object.keys(trashedCards[0])).contains(cardID); + } + }); + + it("should delete the card via /trpc/deleteCard", async () => { + await gCaller.deleteCard({ _id: cardID }); + + const trashedCards = (await gCaller.metadata())[0].trashed_cards; + expect(trashedCards).deep.equal([{}]); + + const card = await gCaller.fetchCard({ cardID: cardID }); + expect(card).to.be.null; + }); + + it("should restore the card via /trpc/restoreCardFromTrash", async () => { + await gCaller.restoreCardFromTrash({ _id: cardID }); + + const trashedCards = (await gCaller.metadata())[0].trashed_cards; + expect(trashedCards).deep.equal([{}]); + + const card = await gCaller.fetchCard({ cardID: cardID }); + expect(card?._id.toString()).to.equal(cardID); + }); + }); }); describe("/trpc/deleteCard", function() {