-
Notifications
You must be signed in to change notification settings - Fork 6
Associated projects #332
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
Open
karthiek390
wants to merge
3
commits into
main
Choose a base branch
from
associated_projects
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Associated projects #332
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,60 @@ const isPermittedTo = accessControl('datasets'); | |
| const router = express.Router(); | ||
| const prisma = new PrismaClient(); | ||
|
|
||
| const build_include_object = ({ | ||
| include_users = true, | ||
| include_datasets = true, | ||
| include_contacts = true, | ||
| } = {}) => _.omitBy(_.isUndefined)({ | ||
| users: include_users ? { | ||
| select: { | ||
| user: true, | ||
| assigned_at: true, | ||
| assignor: { | ||
| select: { | ||
| id: true, | ||
| username: true, | ||
| name: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } : undefined, | ||
| datasets: include_datasets ? { | ||
| select: { | ||
| dataset: { | ||
| include: { | ||
| workflows: { | ||
| select: { | ||
| id: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| assigned_at: true, | ||
| assignor: { | ||
| select: { | ||
| id: true, | ||
| username: true, | ||
| name: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } : undefined, | ||
| contacts: include_contacts ? { | ||
| select: { | ||
| contact: true, | ||
| assigned_at: true, | ||
| assignor: { | ||
| select: { | ||
| id: true, | ||
| username: true, | ||
| name: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } : undefined, | ||
| }); | ||
|
|
||
| // stats - UI | ||
| router.get( | ||
| '/stats', | ||
|
|
@@ -62,17 +116,17 @@ router.get( | |
| }); | ||
| } else { | ||
| result = await prisma.$queryRaw` | ||
| select | ||
| count(*) as "count", | ||
| sum(du_size) as total_size, | ||
| select | ||
| count(*) as "count", | ||
| sum(du_size) as total_size, | ||
| SUM( | ||
| CASE | ||
| WHEN metadata -> 'num_genome_files' IS NOT NULL | ||
| THEN (metadata ->> 'num_genome_files')::int | ||
| ELSE 0 | ||
| END | ||
| ) AS total_num_genome_files | ||
| from dataset | ||
| from dataset | ||
| where is_deleted = false; | ||
| `; | ||
|
|
||
|
|
@@ -386,6 +440,105 @@ router.post( | |
| }), | ||
| ); | ||
|
|
||
| // Route to fetch projects linked to a specific dataset ID | ||
| router.get( | ||
| '/:id/projects', // Adding /:id to represent DatasetID | ||
| isPermittedTo('read'), | ||
| validate([ | ||
| query('take').default(25).isInt().toInt(), | ||
| query('skip').default(0).isInt().toInt(), | ||
| query('search').default(''), // Adding search query validation | ||
| query('sort_order').default('desc').isIn(['asc', 'desc']), | ||
| query('sort_by').default('updated_at').isIn(['name', 'created_at', 'updated_at']), | ||
| ]), | ||
| asyncHandler(async (req, res, next) => { | ||
| const { search, sort_order, sort_by } = req.query; | ||
| // const datasetID = req.params.id; // DatasetID is retrieved from the URL parameter | ||
| const datasetID = parseInt(req.params.id, 10); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this conversion is needed |
||
|
|
||
| // Check if the datasetID has any project association | ||
| const hasProjectDatasetAssociation = await datasetService.has_project_dataset_assoc({ | ||
| DatasetId: datasetID, | ||
| }); | ||
|
|
||
| if (!hasProjectDatasetAssociation) { | ||
| return res.status(404).json({ | ||
| message: 'No projects found for the specified dataset', | ||
| }); | ||
| } | ||
|
|
||
| // Building filters for project retrieval, including datasetID filter | ||
| const filters = { | ||
| datasets: { | ||
| some: { | ||
| dataset_id: datasetID, // Filter projects by the datasetID | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| if (search) { | ||
| filters.OR = [ | ||
| { | ||
| name: { | ||
| contains: search, | ||
| mode: 'insensitive', // Case-insensitive search for project name | ||
| }, | ||
| }, | ||
| { | ||
| users: { | ||
| some: { | ||
| user: { | ||
| username: { | ||
| contains: search, | ||
| mode: 'insensitive', // Case-insensitive search for user username | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| datasets: { | ||
| some: { | ||
| dataset: { | ||
| name: { | ||
| contains: search, | ||
| mode: 'insensitive', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| const sort_obj = { | ||
| [sort_by]: sort_order, | ||
| }; | ||
|
|
||
| // Retrieving projects linked to the specified datasetID and applying the filters | ||
| const [projects, totalCount] = await prisma.$transaction([ | ||
| prisma.project.findMany({ | ||
| skip: req.query.skip, | ||
| take: req.query.take, | ||
| orderBy: sort_obj, | ||
| where: filters, | ||
| include: build_include_object(), // Including related data as per existing logic | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename |
||
| }), | ||
| prisma.project.count({ | ||
| where: filters, // Counting the total number of projects with the dataset filter | ||
| }), | ||
| ]); | ||
|
|
||
| // Sending back the retrieved projects and total count in the response | ||
| res.json({ | ||
| metadata: { count: totalCount }, | ||
| projects, | ||
| }); | ||
| }), | ||
| ); | ||
|
|
||
| module.exports = router; | ||
|
|
||
| // modify - worker | ||
| router.patch( | ||
| '/:id', | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have two separate routes - one which gets all projects for a given dataset id, and another which only fetches the projects which the current user is allowed to read. These routes can have paths
/datasets/:id/projectsand/datasets/:id/:username/projects.For these two routes, determine whether the current user is permitted to only view their own projects or all projects, via:
Then, in the
/datasets/:id/:username/projectsroute, make sure that the logged-in user is assigned to the projects being retrieved. The other route can return all projects associated with the dataset.There is a standard pattern we use to tell whether entities retrieved from the API should be filtered by the logged-in user or not. For example, here is a method that retrieves projects either based on the logged-in user, or by ignoring the logged in user. You can see this at https://github.com/IUSCA/bioloop/blob/main/ui/src/services/projects.js#L7