Skip to content

Commit

Permalink
[Metadata] Persist metadata changes on the DB (#190)
Browse files Browse the repository at this point in the history
* [Metadata] Save metadata after modification
* [Metadata] Add tests for deleting and restoring a trashed card
  • Loading branch information
dchege711 authored Jul 5, 2024
1 parent 19088d7 commit afe5d2a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/models/MetadataMongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export async function updatePublicUserMetadata(

metadataDoc.markModified("stats");
metadataDoc.markModified("node_information");
// await metadataDoc.save();
await metadataDoc.save();

return metadataDoc;
}
Expand Down Expand Up @@ -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. <span class="underline_bold_text clickable" onclick="restoreCardFromTrash('${card._id}', '${card.urgency}')">Undo Action</span>`;
}
Expand Down Expand Up @@ -294,7 +294,7 @@ export async function restoreCardFromTrash(
card.tags,
"urgency",
);
// await metadataDoc.save();
await metadataDoc.save();

return `${card.title} has been restored!`;
}
Expand All @@ -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!`;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
42 changes: 41 additions & 1 deletion src/routes/InAppRouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit afe5d2a

Please sign in to comment.