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

Vps 25/resource api definitions are implemented #216

Merged
merged 18 commits into from
Aug 19, 2024

Conversation

jacobmathew105
Copy link
Contributor

@jacobmathew105 jacobmathew105 commented Aug 12, 2024

Describe the issue

Resources is a new feature that allows users to access various medical resources. Without API endpoints, users are unable to create, retrieve, update, or delete resources, limiting the functionality of the feature. Once the API endpoints are implemented, users can start using them when designing the resources feature.

Describe the solution

This feature enables users to interact with various medical resources by adding the following API endpoints

  • Create a New Resource: POST /resources
  • Retrieve a Specific Resource: GET /resources/:resourceId
  • Update a Resource: PUT /resources/:resourceId
  • Delete a Resource: DELETE /resources/:resourceId
  • Retrieve All Visible Resources: GET /group/:groupId/resources/
  • Add a Flag to a Group: POST /group/:groupId/flag

Risk

A potential risk is the new feature may introduce new bugs.

Definition of Done

  • Code peer-reviewed
  • Wiki Documentation is written and up to date
  • Unit tests written and passing
  • Integration tests written and passing
  • Continuous Integration build passing
  • Acceptance criteria met
  • Deployed to production environment

Reviewed By

Who reviewed your PR - for commit history once merged

Copy link
Contributor

@codecreator127 codecreator127 left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Member

@wjin-lee wjin-lee left a comment

Choose a reason for hiding this comment

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

Great job - looking sweet.
Few nits, after should be good to merge.

backend/src/routes/api/resources.js Outdated Show resolved Hide resolved
backend/src/routes/api/resources.js Outdated Show resolved Hide resolved
Comment on lines 112 to 114
} catch (error) {
return res.status(HTTP_FORBIDDEN).send("Forbidden");
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: this is not actually true?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403

indicates that the server understood the request but refused to process it

(4xx suggests that there was something that went wrong because of the request made)

We should have a list of 5xx codes as well - which is the correct response when something goes wrong on "our" side.

I'd say that this is a 5xx error if we catch

Comment on lines +34 to +77
const getResourceById = async (resourceId) => {
const resource = await Resource.findById(resourceId);
return resource;
};

const deleteResourceById = async (resourceId) => {
const resource = await Resource.findById(resourceId);
await Resource.findByIdAndRemove(resourceId);
return resource;
};

const getAllVisibleResources = async (groupId) => {
const group = await Group.findById(groupId);
const allResources = await Resource.find({});
const flags = group.currentFlags;

// Filter resources based on the comparison
const visibleResources = allResources.filter((resource) =>
resource.requiredFlags.every((flag) => flags.includes(flag))
);

return visibleResources;
};

const addFlag = async (groupId, flag) => {
const group = await Group.findById(groupId);
if (!group.currentFlags.includes(flag)) {
group.currentFlags.push(flag);
await group.save();
}
return group.currentFlags;
};

const removeFlag = async (groupId, flag) => {
const group = await Group.findById(groupId);

if (group.currentFlags.includes(flag)) {
const index = group.currentFlags.indexOf(flag);
group.currentFlags.splice(index, 1);
await group.save();
}

return group.currentFlags;
};
Copy link
Member

@choden-dev choden-dev Aug 15, 2024

Choose a reason for hiding this comment

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

its just me, but do we agree using no typescript without at least documenting functions with JSDoc leads to many problems down the line? After talking to @wjin-lee code quality is now whatever in VPS but I'd imagine doing so would assist in meeting the required milestone.

At least a @param and @returns would help immensely for whoever next consumes these functions

Copy link
Member

Choose a reason for hiding this comment

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

something simple could be:

/**
 * @param {string} groupId - The unique identifier of the group.
 * @param {string} flag - The flag to be removed from the group's current flags.
 * @returns {Promise<string[]>} A promise that resolves to the updated list of current flags for the group.
 *
 * @example
 * const updatedFlags = await removeFlag('12345', 'active');
 * console.log(updatedFlags); // Output: ['inactive', 'pending']
 */

this way someone can hover over the function to see what its doing rather needing to go to the definition

Copy link
Member

Choose a reason for hiding this comment

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

obviously a nit tho 💀

Copy link
Member

Choose a reason for hiding this comment

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

mmm - I might start enforcing this from now - we can discuss more in the next sprint meeting!

@@ -24,7 +24,15 @@ const HTTP_INTERNAL_SERVER_ERROR = 500;
// Apply auth middleware to all routes below this point
router.use(auth);

// Create a New Resource
/**
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

Choose a reason for hiding this comment

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

nice catch on the 5xx

Copy link
Member

@wjin-lee wjin-lee left a comment

Choose a reason for hiding this comment

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

lgtm!

Comment on lines +34 to +77
const getResourceById = async (resourceId) => {
const resource = await Resource.findById(resourceId);
return resource;
};

const deleteResourceById = async (resourceId) => {
const resource = await Resource.findById(resourceId);
await Resource.findByIdAndRemove(resourceId);
return resource;
};

const getAllVisibleResources = async (groupId) => {
const group = await Group.findById(groupId);
const allResources = await Resource.find({});
const flags = group.currentFlags;

// Filter resources based on the comparison
const visibleResources = allResources.filter((resource) =>
resource.requiredFlags.every((flag) => flags.includes(flag))
);

return visibleResources;
};

const addFlag = async (groupId, flag) => {
const group = await Group.findById(groupId);
if (!group.currentFlags.includes(flag)) {
group.currentFlags.push(flag);
await group.save();
}
return group.currentFlags;
};

const removeFlag = async (groupId, flag) => {
const group = await Group.findById(groupId);

if (group.currentFlags.includes(flag)) {
const index = group.currentFlags.indexOf(flag);
group.currentFlags.splice(index, 1);
await group.save();
}

return group.currentFlags;
};
Copy link
Member

Choose a reason for hiding this comment

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

mmm - I might start enforcing this from now - we can discuss more in the next sprint meeting!

@@ -24,7 +24,15 @@ const HTTP_INTERNAL_SERVER_ERROR = 500;
// Apply auth middleware to all routes below this point
router.use(auth);

// Create a New Resource
/**
Copy link
Member

Choose a reason for hiding this comment

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

nice catch on the 5xx

@codecreator127 codecreator127 merged commit 8a6af67 into master Aug 19, 2024
6 checks passed
@JordanBlenn JordanBlenn deleted the VPS-25/Resource-API-Definitions-are-implemented branch August 30, 2024 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants