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

Update API to use events and task states + fixes to backend refactor #1838

Merged
merged 43 commits into from
Oct 29, 2024

Conversation

spwoodcock
Copy link
Member

@spwoodcock spwoodcock commented Oct 26, 2024

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

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

Related Issue

Fixes #1610

Describe this PR

Redesign of how the task locking works:

  • Events are called via the API to trigger certain states on the tasks.
  • We have a TaskEvent enum listing all the possible events.
  • Then a MappingStatus enum to list all the possible statuses for mapping currently.
  • The backend is mostly implemented, but I just need to update the naming of a few things, and pass in a JSON to the new event endpoint to task_id, user_id, event.
  • The frontend however is a little bodged now after all these changes πŸ˜… I really ran out of time to dive into the frontend code properly, so just did what I could for now. I would really love your help getting this to work @NSUWAL123 πŸ™

This includes a pretty major migration to the task_history table, changing the fields and table name to task_events.
The indexes are also all rebuilt.

Also includes fixes to various endpoints and frontend usage of endpoints after refactor in #1834

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?

@spwoodcock spwoodcock requested a review from NSUWAL123 October 26, 2024 02:34
@spwoodcock spwoodcock self-assigned this Oct 26, 2024
@github-actions github-actions bot added enhancement New feature or request frontend Related to frontend code backend Related to backend code migration Contains a DB migration tests Related to automated code tests labels Oct 26, 2024
@NSUWAL123
Copy link
Contributor

Would you merge this PR to dev first or should I make a branch from your branch?

@NSUWAL123
Copy link
Contributor

NSUWAL123 commented Oct 28, 2024

@spwoodcock One thing you missed in the previous PR I guess to update the fmtm-db --> build --> context version update from 3.4 to 3.5. Realized just now when trying to build.
image

@spwoodcock
Copy link
Member Author

Would you merge this PR to dev first or should I make a branch from your branch?

If you could branch from branch for today, I'll also continue to tweak the backend today & hopefully get that part done.

Last thing I need to fix is updating the task and entity status properly from the frontend.

For better clarity over what has changed, I think this code from enums.py helps to illustrate it:

class TaskEvent(StrEnum, Enum):
    """Task events via API.

    `MAP` -- Set to *locked for mapping*, i.e. mapping in progress.
    `FINISH` -- Set to *unlocked to validate*, i.e. is mapped.
    `FINISH` -- Request recent task ready to be validate.
    `GOOD` -- Set the state to *unlocked done*.
    `BAD` -- Set the state *unlocked to map* again, to be mapped once again.
    `SPLIT` -- Set the state *unlocked done* then generate additional
        subdivided task areas.
    `MERGE` -- Set the state *unlocked done* then generate additional
        merged task area.
    `ASSIGN` -- For a requester user to assign a task to another user.
        Set the state *locked for mapping* passing in the required user id.
        Also notify the user they should map the area.
    `COMMENT` -- Keep the state the same, but simply add a comment.
    """

    MAP = "MAP"
    FINISH = "FINISH"
    VALIDATE = "VALIDATE"
    GOOD = "GOOD"
    BAD = "BAD"
    SPLIT = "SPLIT"
    MERGE = "MERGE"
    ASSIGN = "ASSIGN"
    COMMENT = "COMMENT"


class MappingState(StrEnum, Enum):
    """State options for tasks in FMTM.

    NOTE We no longer have states invalidated / bad, and instead rely on the
    EntityState.MARKED_BAD buildings to display red on the map.
    """

    UNLOCKED_TO_MAP = "UNLOCKED_TO_MAP"
    LOCKED_FOR_MAPPING = "LOCKED_FOR_MAPPING"
    UNLOCKED_TO_VALIDATE = "UNLOCKED_TO_VALIDATE"
    LOCKED_FOR_VALIDATION = "LOCKED_FOR_VALIDATION"
    UNLOCKED_DONE = "UNLOCKED_DONE"


class EntityState(IntEnum, Enum):
    """State options for Entities in ODK.

    NOTE here we started with int enums and it's hard to migrate.
    NOTE we will continue to use int values in the form.
    NOTE we keep BAD=6 for legacy reasons too.
    """

    READY = 0
    OPEN_IN_ODK = 1
    SURVEY_SUBMITTED = 2
    MARKED_BAD = 6

So with these updates the whole idea is to post TaskEvent to the API, which creates an entry in the events table.

The MappingStatus will be automatically set from that.

We can also post the EntityState to update it, but I need to work on that part a bit more πŸ‘

@NSUWAL123
Copy link
Contributor

Sure, I'll make a branch and continue. I'll look through it and make the frontend fixes.

@github-actions github-actions bot added the devops Related to deployment or configuration label Oct 28, 2024
@spwoodcock spwoodcock marked this pull request as ready for review October 28, 2024 23:52
@spwoodcock spwoodcock changed the title Update API to use events and task states Update API to use events and task states + fixes to backend refactor Oct 29, 2024
@spwoodcock
Copy link
Member Author

Fixed all the remaining issues.

Going to merge this, but the Playwright tests are still a bit broken, so this can't deploy to dev.

Could you take a look into the tests please @NSUWAL123 ? πŸ™

@spwoodcock spwoodcock merged commit b8887b3 into development Oct 29, 2024
7 of 8 checks passed
@spwoodcock spwoodcock deleted the feat/event-driven-tasks branch October 29, 2024 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code devops Related to deployment or configuration enhancement New feature or request frontend Related to frontend code migration Contains a DB migration tests Related to automated code tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Redesign project/task/history schemas and relationships to better align with event-driven programming
2 participants