Skip to content

Commit

Permalink
feat(projects): send notification message to new project manager upon…
Browse files Browse the repository at this point in the history
… assignment and refactor add manager endpoint
  • Loading branch information
Anuj-Gupta4 committed Dec 23, 2024
1 parent 7f012c6 commit 0391b9e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/manuals/field-mapping-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This page provides some links to user-generated guides of FMTM over time.
[Link](https://docs.google.com/document/d/1i6LPj3Ah860BaSQCLvmxqbLmcQB3fM0_W8n15NEeZPo/edit?tab=t.0)
to project document, including detail on the mapping workflow.

## OSM Rwanda Remote Mapping
## OSM Rwanda Remote Mapping

[Link](https://docs.google.com/document/d/12BvzLPhILTYhK_8b2TY_oAByNJILuoaomDqd3hJmvUc/edit?tab=t.0)
OSM Rwanda user experience report
39 changes: 37 additions & 2 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@
import uuid
from io import BytesIO
from pathlib import Path
from textwrap import dedent
from traceback import format_exc
from typing import Optional, Union

import geojson
import geojson_pydantic
import requests
from asgiref.sync import async_to_sync
from fastapi import HTTPException
from fastapi import HTTPException, Request
from loguru import logger as log
from osm_fieldwork.basemapper import create_basemap_file
from osm_login_python.core import Auth
from osm_rawdata.postgres import PostgresClient
from psycopg import Connection
from psycopg.rows import class_row

from app.auth.providers.osm import get_osm_token, send_osm_message
from app.central import central_crud, central_schemas
from app.config import settings
from app.db.enums import BackgroundTaskStatus, HTTPStatus, XLSFormType
from app.db.models import DbBackgroundTask, DbBasemap, DbProject, DbTask
from app.db.models import DbBackgroundTask, DbBasemap, DbProject, DbTask, DbUser
from app.db.postgis_utils import (
check_crs,
featcol_keep_single_geom_type,
Expand Down Expand Up @@ -932,3 +935,35 @@ async def get_project_users_plus_contributions(db: Connection, project_id: int):
) as cur:
await cur.execute(query, {"project_id": project_id})
return await cur.fetchall()


async def send_project_manager_message(
request: Request,
project: DbProject,
new_manager: DbUser,
osm_auth: Auth,
):
"""Send message to the new project manager after assigned."""
log.info(f"Sending message to new project manager ({new_manager.username}).")

osm_token = get_osm_token(request, osm_auth)
project_url = f"{settings.FMTM_DOMAIN}/project/{project.id}"
if not project_url.startswith("http"):
project_url = f"http://{project_url}"

message_content = dedent(f"""
You have been assigned to the project **{project.name}** as a
manager. You can now manage the project and its tasks.
[Click here to view the project]({project_url})
Thank you for being a part of our platform!
""")

send_osm_message(
osm_token=osm_token,
osm_id=new_manager.id,
title=f"You have been assigned to project {project.name} as a manager",
body=message_content,
)
log.info(f"Message sent to new project manager ({new_manager.username}).")
24 changes: 20 additions & 4 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
Form,
HTTPException,
Query,
Request,
Response,
UploadFile,
)
Expand All @@ -47,6 +48,7 @@

from app.auth.auth_deps import login_required, mapper_login_required
from app.auth.auth_schemas import AuthUser, OrgUserDict, ProjectUserDict
from app.auth.providers.osm import init_osm_auth
from app.auth.roles import mapper, org_admin, project_manager
from app.central import central_crud, central_deps, central_schemas
from app.config import settings
Expand All @@ -62,6 +64,7 @@
DbOdkEntities,
DbProject,
DbTask,
DbUser,
DbUserRole,
)
from app.db.postgis_utils import (
Expand All @@ -75,6 +78,7 @@
from app.organisations import organisation_deps
from app.projects import project_crud, project_deps, project_schemas
from app.s3 import delete_all_objs_under_prefix
from app.users.user_deps import get_user

router = APIRouter(
prefix="/projects",
Expand Down Expand Up @@ -727,19 +731,31 @@ async def upload_custom_extract(

@router.post("/add-manager")
async def add_new_project_manager(
request: Request,
db: Annotated[Connection, Depends(db_conn)],
project_user_dict: Annotated[ProjectUserDict, Depends(project_manager)],
background_tasks: BackgroundTasks,
new_manager: Annotated[DbUser, Depends(get_user)],
org_user_dict: Annotated[OrgUserDict, Depends(org_admin)],
osm_auth=Depends(init_osm_auth),
):
"""Add a new project manager.
The logged in user must be either the admin of the organisation or a super admin.
The logged in user must be the admin of the organisation.
"""
await DbUserRole.create(
db,
project_user_dict["project"].id,
project_user_dict["user"].id,
org_user_dict["project"].id,
new_manager.id,
ProjectRole.PROJECT_MANAGER,
)

background_tasks.add_task(
project_crud.send_project_manager_message,
request=request,
project=org_user_dict["project"],
new_manager=new_manager,
osm_auth=osm_auth,
)
return Response(status_code=HTTPStatus.OK)


Expand Down

0 comments on commit 0391b9e

Please sign in to comment.