-
Notifications
You must be signed in to change notification settings - Fork 1
enhancement/organization-apis #77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a92706a
fix: extract url while deleting the org logo
mdawoud27 9373d82
refactor: get all organizations function
mdawoud27 bd41bc0
Merge branch 'main' of github.com:TaskTrial/server into enhancement/2…
mdawoud27 a85bd2f
fix: get a specific organization function
mdawoud27 c2b98bd
fix: get a specific organization route
mdawoud27 7e67206
fix: `updateOrganization` route
mdawoud27 e657382
docs: add swagger docs for upload/delete organization logo
mdawoud27 e1cc435
refactor: remove comment code
mdawoud27 a51f62b
fix: verify user permission middleware
mdawoud27 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -184,7 +184,7 @@ export const verifyOrganization = async (req, res, next) => { | |||||||||
| */ | ||||||||||
| export const getAllOrganizations = async (req, res, next) => { | ||||||||||
| try { | ||||||||||
| // Destructure and parse query params with defaults | ||||||||||
| // Parse query parameters | ||||||||||
| const { | ||||||||||
| page = 1, | ||||||||||
| limit = 10, | ||||||||||
|
|
@@ -197,32 +197,27 @@ export const getAllOrganizations = async (req, res, next) => { | |||||||||
| const limitNum = parseInt(limit, 10); | ||||||||||
| const skip = (pageNum - 1) * limitNum; | ||||||||||
|
|
||||||||||
| // Robust boolean conversion utility | ||||||||||
| const parseBoolean = (value) => { | ||||||||||
| if (value === 'true' || value === true || value === '1') { | ||||||||||
| return true; | ||||||||||
| } | ||||||||||
| if (value === 'false' || value === false || value === '0') { | ||||||||||
| return false; | ||||||||||
| // Build filters | ||||||||||
| const where = { deletedAt: null }; | ||||||||||
|
|
||||||||||
| // Apply text search | ||||||||||
| if (filters.name) { | ||||||||||
| where.name = { contains: filters.name, mode: 'insensitive' }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Apply direct filters | ||||||||||
| ['industry', 'sizeRange', 'status'].forEach((field) => { | ||||||||||
| if (filters[field]) { | ||||||||||
| where[field] = filters[field]; | ||||||||||
| } | ||||||||||
| return undefined; // or throw error if you prefer strict validation | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| // Build base where clause | ||||||||||
| const where = { | ||||||||||
| deletedAt: null, | ||||||||||
| ...(filters.name && { | ||||||||||
| name: { contains: filters.name, mode: 'insensitive' }, | ||||||||||
| }), | ||||||||||
| ...(filters.industry && { industry: filters.industry }), | ||||||||||
| ...(filters.sizeRange && { sizeRange: filters.sizeRange }), | ||||||||||
| ...(filters.status && { status: filters.status }), | ||||||||||
| ...(filters.isVerified !== undefined && { | ||||||||||
| isVerified: parseBoolean(filters.isVerified), | ||||||||||
| }), | ||||||||||
| }; | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| // Rest of the function remains the same... | ||||||||||
| // Handle boolean filter | ||||||||||
| if (filters.isVerified !== undefined) { | ||||||||||
| where.isVerified = ['true', '1', true].includes(filters.isVerified); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Add permission filters for non-admin users | ||||||||||
| if (req.user.role !== 'ADMIN') { | ||||||||||
| where.OR = [ | ||||||||||
| { createdBy: req.user.id }, | ||||||||||
|
|
@@ -231,6 +226,7 @@ export const getAllOrganizations = async (req, res, next) => { | |||||||||
| ]; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Fetch data and count in parallel | ||||||||||
| const [totalCount, organizations] = await Promise.all([ | ||||||||||
| prisma.organization.count({ where }), | ||||||||||
| prisma.organization.findMany({ | ||||||||||
|
|
@@ -263,7 +259,8 @@ export const getAllOrganizations = async (req, res, next) => { | |||||||||
| }), | ||||||||||
| ]); | ||||||||||
|
|
||||||||||
| const response = { | ||||||||||
| // Format response | ||||||||||
| return res.status(200).json({ | ||||||||||
| success: true, | ||||||||||
| message: 'Organizations retrieved successfully', | ||||||||||
| data: { | ||||||||||
|
|
@@ -289,9 +286,7 @@ export const getAllOrganizations = async (req, res, next) => { | |||||||||
| pages: Math.ceil(totalCount / limitNum), | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| return res.status(200).json(response); | ||||||||||
| }); | ||||||||||
| } catch (error) { | ||||||||||
| next(error); | ||||||||||
| } | ||||||||||
|
|
@@ -314,7 +309,7 @@ export const getSpecificOrganization = async (req, res, next) => { | |||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Check if the organization exists and is not deleted | ||||||||||
| // Find organization with all related data | ||||||||||
| const organization = await prisma.organization.findFirst({ | ||||||||||
| where: { | ||||||||||
| id: organizationId, | ||||||||||
|
|
@@ -353,7 +348,7 @@ export const getSpecificOrganization = async (req, res, next) => { | |||||||||
| name: true, | ||||||||||
| description: true, | ||||||||||
| _count: { | ||||||||||
| select: { members: true, projects: true }, // Changed from users to members | ||||||||||
| select: { members: true, projects: true }, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| take: 5, | ||||||||||
|
|
@@ -379,6 +374,12 @@ export const getSpecificOrganization = async (req, res, next) => { | |||||||||
| templates: true, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| users: { | ||||||||||
| where: { | ||||||||||
| id: req.user.id, | ||||||||||
| }, | ||||||||||
| take: 1, | ||||||||||
| }, | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
|
|
@@ -389,73 +390,55 @@ export const getSpecificOrganization = async (req, res, next) => { | |||||||||
| }); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Check if user has permission to view this organization | ||||||||||
| // Check permissions for non-admin users | ||||||||||
| if (req.user.role !== 'ADMIN') { | ||||||||||
| const hasAccess = await prisma.user.findFirst({ | ||||||||||
| where: { | ||||||||||
| id: req.user.id, | ||||||||||
| OR: [ | ||||||||||
| { createdOrganizations: { some: { id: organizationId } } }, | ||||||||||
| { organizations: { some: { id: organizationId } } }, | ||||||||||
| { ownedOrganizations: { some: { organizationId } } }, | ||||||||||
| ], | ||||||||||
| }, | ||||||||||
| }); | ||||||||||
| // Check if user is an owner | ||||||||||
| const isOwner = organization.owners.some( | ||||||||||
| (owner) => owner.user.id === req.user.id, | ||||||||||
| ); | ||||||||||
|
|
||||||||||
| // Check if user is a member | ||||||||||
| const isMember = organization.users.length > 0; | ||||||||||
|
|
||||||||||
|
Comment on lines
+401
to
402
|
||||||||||
| const isMember = organization.users.length > 0; | |
| const isMember = organization.users.some( | |
| (user) => user.id === req.user.id, | |
| ); |
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.
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.
Potential bug: The boolean conversion for filters.isVerified does not explicitly handle false-equivalent values (e.g., 'false', '0'). Consider expanding the logic to clearly determine both true and false conditions.