Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Walter/card patch delete #59

Merged
merged 19 commits into from
Sep 2, 2021
Merged

Walter/card patch delete #59

merged 19 commits into from
Sep 2, 2021

Conversation

waltervan00
Copy link
Collaborator

@waltervan00 waltervan00 commented Aug 31, 2021

Hi all. I have finished writing unit tests for PATCH and DELETE endpoints.

Please confirm that the tests are valid.
For PATCH, please confirm that the image (optional field for cards) can be removed by specifying them as null.

Closes #25.
Closes #26.

@waltervan00
Copy link
Collaborator Author

Appears to be a problem with the linter resolving the path to the shared folder. Linting locally passes.

@chomosuke chomosuke self-requested a review August 31, 2021 04:01
Copy link
Owner

@chomosuke chomosuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall pretty good, some minor changes needed.

server/src/api/authenticated/cardPatch.ts Outdated Show resolved Hide resolved
server/src/api/authenticated/router.ts Outdated Show resolved Hide resolved
server/src/api/authenticated/cardDelete.ts Show resolved Hide resolved
server/src/api/authenticated/cardPatch.ts Show resolved Hide resolved
server/src/api/authenticated/cardPatch.ts Outdated Show resolved Hide resolved
@chomosuke chomosuke added the enhancement New feature or request label Aug 31, 2021
@chomosuke chomosuke added this to the Sprint 1 milestone Aug 31, 2021
Copy link
Owner

@chomosuke chomosuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg

@chomosuke chomosuke self-requested a review August 31, 2021 13:38
Copy link
Owner

@chomosuke chomosuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last bit of stuff

server/src/api/authenticated/router.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are better ways to do some of these.

try {
await dbs.withTransaction(async () => {
// removes card in collection belonging to user
const cardToDelete = await this.parent.Cards.findById(id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the ObjectId object constructed earlier instead of giving the string id here.

const cardToDelete = await this.parent.Cards.findById(id);
if (!cardToDelete) {
throw new HttpStatusError(410);
} else if (user.id !== cardToDelete.user.toString()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly compare the user ObjectIds with ObjectId.prototype.equals(other).

Suggested change
} else if (user.id !== cardToDelete.user.toString()) {
} else if (!user._id.equals(cardToDelete.user)) {

Reference: https://github.com/mongodb/js-bson/blob/6dcd46e677c40c4409d6f601aaf57ce15d4d05d1/src/objectid.ts#L212

Copy link
Collaborator Author

@waltervan00 waltervan00 Sep 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throws an error with linter. Error is dangling underscore. Note that user.id gives a string type of id.

} else if (user.id !== cardToDelete.user.toString()) {
throw new HttpStatusError(401);
} else {
await this.parent.Cards.findByIdAndDelete(id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Model.prototype.remove() to delete a document that you had already found instead of finding it again.

Suggested change
await this.parent.Cards.findByIdAndDelete(id);
await cardToDelete.remove();

Reference: https://mongoosejs.com/docs/api/model.html#model_Model-remove

// if card is not found
if (!updatedCard) {
throw new HttpStatusError(404);
} else if (updatedCard.user.toString() !== user.id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly compare ObjectIds with equals()

Suggested change
} else if (updatedCard.user.toString() !== user.id) {
} else if (!updatedCard.user.equals(user._id)) {

const tag = await this.parent.Tags.findById(tagId);
if (tag == null) {
throw new HttpStatusError(400);
} else if (tag.user.toString() !== user.id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly compare ObjectIds with equals()

Suggested change
} else if (tag.user.toString() !== user.id) {
} else if (!tag.user.equals(user._id)) {

Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite right yet.

if (!cardToDelete) {
throw new HttpStatusError(410);
} else if (user.id !== cardToDelete.user.toString()) {
} else if (!user.id.equals(cardToDelete.user)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong property. id is the string representation of the document id. _id is the actual mongoose.Types.ObjectId.

Suggested change
} else if (!user.id.equals(cardToDelete.user)) {
} else if (!user._id.equals(cardToDelete.user)) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Accessing the _id property is valid. However the linter will produce an error and prevent this code from passing.

const tag = await this.parent.Tags.findById(tagId);
if (tag == null) {
throw new HttpStatusError(400);
} else if (tag.user.toString() !== user.id) {
} else if (!user.id.equals(tag.user)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, wrong property.

Suggested change
} else if (!user.id.equals(tag.user)) {
} else if (!user._id.equals(tag.user)) {

// if card is not found
if (!updatedCard) {
throw new HttpStatusError(404);
} else if (updatedCard.user.toString() !== user.id) {
} else if (!user.id.equals(updatedCard.user)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, wrong property.

Suggested change
} else if (!user.id.equals(updatedCard.user)) {
} else if (!user._id.equals(updatedCard.user)) {

throw new HttpStatusError(401);
} else {
updatedCard.set(updateDetails);
await updatedCard.save();
}

const response: CardPatchResponse = {
id: updatedCard.id,
id: updatedCard.id.toString(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change unnecessary, id is already a string.

Copy link
Collaborator

@shangzhel shangzhel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@waltervan00 waltervan00 merged commit 2dda106 into master Sep 2, 2021
@waltervan00 waltervan00 deleted the walter/cardPatchDelete branch September 2, 2021 08:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement card deletion endpoint -- DELETE api/card Implement edit card endpoint -- PATCH api/card
3 participants