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

Fix create and delete organisation #1867

Merged
merged 8 commits into from
Nov 15, 2024
Merged

Conversation

Sujanadh
Copy link
Contributor

What type of PR is this? (check all applicable)

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Documentation
  • πŸ§‘β€πŸ’» Refactor
  • βœ… Test
  • πŸ€– Build or CI
  • ❓ Other (please specify)

Related Issue

Describe this PR

This PR fixes the issue related to creating and deleting organizations awaiting the async functions that were missing. It also fixes the get_my_organisations using proper parameter injection and row_factory to define the db cursor to use Pydantic model OrganisationOut for the response.

frontend
There is a payload mismatch with the body and query in frontend (yet to be fixed), @NSUWAL123

Screenshots

N/A

Alternative Approaches Considered

Did you attempt any other approaches that are not documented in code?

Review Guide

Notes for the reviewer. How to test this change?

Checklist before requesting a review

[optional] What gif best describes this PR or how it makes you feel?

@Sujanadh Sujanadh requested a review from spwoodcock November 11, 2024 09:36
@Sujanadh Sujanadh self-assigned this Nov 11, 2024
@github-actions github-actions bot added bug Something isn't working backend Related to backend code labels Nov 11, 2024
@Sujanadh
Copy link
Contributor Author

@spwoodcock , here I haven't updated the status of NO_CONTENT, which would be the best to use? Use it as it is or update it to return a success message?

Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

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

Nice!

I actually neglected to look at the org router properly / test endpoints thoroughly after the big migration, so thanks for this!

@@ -58,6 +58,8 @@ async def get_organisation(
pass
db_org = await DbOrganisation.one(db, id)

if db_org is None:
raise KeyError(f"Organisation ({id}) not found.")
Copy link
Member

Choose a reason for hiding this comment

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

Was moving this related to the 'my organisations' endpoint?

The logic is a bit duplicated now though, as you could probably just raise a HTTPException without needing the KeyError.

Is it possible to keep the KeyError in the model .one method though, and instead catch the error wherever you needed it? Using try/except blocks actually has very little cost in the latest versions of Python, so it's good to keep the KeyError at the lowest level, then catch exceptions higher up (if no organisation exists with that name/id)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see, I got your point. But the reason why I removed it from the .one function is that if you see on the create function of the organisation , there is a logic to check if the organisation already exists or not to allow the creation of a new organisation where .one function is called, so if there isn't any organisation with the same name, it should have allowed creating new organisation but the .one function raises a KeyError as it won't find any db record which blocks .create function to create a new organisation.

Copy link
Member

@spwoodcock spwoodcock Nov 11, 2024

Choose a reason for hiding this comment

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

Ah that makes sense πŸ‘

I'm not on my laptop to really dig into the logic.

But it work using the ignore_conflict param on DbOrganisation.create?

We just need to ideally check if the name already exists on creation, but don't need to get the org with .one. Not sure what the best solution is, so I'll let you decide what you think is best 😁

(just be sure that removing the keyerror doesn't break logic elsewhere)

Copy link
Contributor Author

@Sujanadh Sujanadh Nov 12, 2024

Choose a reason for hiding this comment

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

Yes, we need to check if that name already exists, so we need to write SQL again to check it in the create function. Or, we might create a new function to check if the organisation exists or not and use it in other endpoints too. We already have the org_exists function in organisation_deps we can replace it with a new class function by defining it in DbOrganisation?


if not deleted_org_id:
return Response(
org_deleted = await DbOrganisation.delete(db, org_user_dict.id)
Copy link
Member

Choose a reason for hiding this comment

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

I believe org_user_dict actually has keys org and user so the org id is nested one level more

org_user_id.get("org").id

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yeah actually, I tested and overdid it while refactoring without noticing. Thanks for pointing out.

@spwoodcock
Copy link
Member

@spwoodcock , here I haven't updated the status of NO_CONTENT, which would be the best to use? Use it as it is or update it to return a success message?

Good question as it's a bit counter intuitive, but NO_CONTENT is a pretty common response code for delete endpoints (often recommended). So the code looks good as it is πŸ‘

Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

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

Merge if you are happy with it!

@Sujanadh Sujanadh marked this pull request as draft November 13, 2024 05:59
@Sujanadh
Copy link
Contributor Author

I have made some changes keeping KeyError as it is in .create function. I also created separate parser function to generate form fields for the organisation input. @spwoodcock

@Sujanadh Sujanadh marked this pull request as ready for review November 14, 2024 06:46
Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

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

Nice work!

@@ -397,11 +397,6 @@ async def create(
# Set requesting user to the org owner
org_in.created_by = user_id

if not ignore_conflict and cls.one(db, org_in.name):
Copy link
Member

Choose a reason for hiding this comment

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

Is ignore_conflict used anywhere now?

I forgot why I added it in the first place.
Perhaps it can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it is used in the init default HOTOSM organisation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is still there in SQL to create an organization.

@spwoodcock spwoodcock merged commit 81556cb into development Nov 15, 2024
5 checks passed
@spwoodcock spwoodcock deleted the fix/create-delete-org branch November 15, 2024 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants