Skip to content

Commit

Permalink
Merge pull request #149 from dibik96/17.0-develop
Browse files Browse the repository at this point in the history
OE-132,OE-134: Fayda API models
  • Loading branch information
shibu-narayanan authored Jul 8, 2024
2 parents 65b0f84 + 332ceea commit 0f3fc67
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 11 deletions.
3 changes: 1 addition & 2 deletions g2p_registry_rest_api/models/fastapi_endpoint_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class G2PRegistryEndpoint(models.Model):
def _get_fastapi_routers(self) -> list[APIRouter]:
routers = super()._get_fastapi_routers()
if self.app == "registry":
routers.append(group_router)
routers.append(individual_router)
routers.extend([group_router, individual_router])
return routers

@api.model
Expand Down
97 changes: 92 additions & 5 deletions g2p_registry_rest_api/routers/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

from ..exceptions.base_exception import G2PApiValidationError
from ..exceptions.error_codes import G2PErrorCodes
from ..schemas.individual import IndividualInfoRequest, IndividualInfoResponse
from ..schemas.individual import IndividualInfoRequest, IndividualInfoResponse, UpdateIndividualInfoRequest

individual_router = APIRouter(prefix="/individual", tags=["individual"])
individual_router = APIRouter(tags=["individual"])


@individual_router.get("/{_id}", responses={200: {"model": IndividualInfoResponse}})
@individual_router.get("/individual/{_id}", responses={200: {"model": IndividualInfoResponse}})
async def get_individual(_id, env: Annotated[Environment, Depends(odoo_env)]):
"""
Get partner's information by ID
Expand All @@ -27,7 +27,7 @@ async def get_individual(_id, env: Annotated[Environment, Depends(odoo_env)]):


@individual_router.get(
"",
"/individual",
responses={200: {"model": list[IndividualInfoResponse]}},
)
def search_individuals(
Expand Down Expand Up @@ -55,7 +55,7 @@ def search_individuals(


@individual_router.post(
"/",
"/individual/",
responses={200: {"model": IndividualInfoResponse}},
)
def create_individual(
Expand All @@ -75,6 +75,93 @@ def create_individual(
return IndividualInfoResponse.model_validate(partner)


@individual_router.get(
"/get_ids",
responses={200: {"model": list[str]}},
)
async def get_ids(
env: Annotated[Environment, Depends(odoo_env)],
include_id_type: str | None = "",
exclude_id_type: str | None = "",
):
"""
Get the IDs of an individual
"""

try:
if not include_id_type:
_handle_error(G2PErrorCodes.G2P_REQ_010, "Record is not present in the database.")

domain = [("is_registrant", "=", True), ("is_group", "=", False)]
if include_id_type:
domain.append(("reg_ids.id_type", "=", include_id_type))

registrant_rec = env["res.partner"].sudo().search(domain)

all_ids = set()

for partner in registrant_rec:
has_exclude_id_type = any(reg_id.id_type.name == exclude_id_type for reg_id in partner.reg_ids)
if has_exclude_id_type:
continue
for reg_id in partner.reg_ids:
if reg_id.id_type.name == include_id_type:
all_ids.add(reg_id.value)

return list(all_ids)

except Exception as e:
logging.error(f"Error while getting IDs: {str(e)}")
_handle_error(G2PErrorCodes.G2P_REQ_010, "An error occurred while getting IDs.")


@individual_router.put("/update_individual", responses={200: {"model": IndividualInfoResponse}})
async def update_individual(
requests: list[UpdateIndividualInfoRequest],
env: Annotated[Environment, Depends(odoo_env)],
id_type: str | None = "",
) -> list[IndividualInfoResponse]:
"""
Update an individual
"""
results = []

for request in requests:
try:
logging.info(f"!!!!!!!!Request data: {request}")
_id = request.updateId
if _id and id_type:
partner_rec = (
env["res.partner"]
.sudo()
.search([("reg_ids.value", "=", _id), ("reg_ids.id_type", "=", id_type)], limit=1)
)
if not partner_rec:
_handle_error(
G2PErrorCodes.G2P_REQ_010,
f"Individual with the given ID {_id} not found.",
)

# Update the individual
indv_rec = env["process_individual.rest.mixin"]._process_individual(request)

logging.info("Individual Api: Updating Individual Record", indv_rec)

partner_rec.write(indv_rec)

results.append(IndividualInfoResponse.model_validate(partner_rec))

else:
logging.error("ID & ID type is required for update individual")
_handle_error(G2PErrorCodes.G2P_REQ_010, "ID is required for update individual")

except Exception as e:
logging.error(f"Error occurred while updating the partner with ID {str(e)}")
results.append({"Id": request.updateId, "status": "Failure", "message": str(e)})

return results


def _get_individual(env: Environment, _id: int):
return (
env["res.partner"]
Expand Down
6 changes: 5 additions & 1 deletion g2p_registry_rest_api/schemas/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def validate_email(cls, v):
class IndividualInfoRequest(RegistrantInfoRequest):
given_name: str
addl_name: str | None = None
family_name: str
family_name: str | None = None
gender: str | None
birthdate: date | None
birth_place: str | None
is_group: bool = False


class UpdateIndividualInfoRequest(IndividualInfoRequest):
updateId: str
6 changes: 3 additions & 3 deletions g2p_registry_rest_api/schemas/registrant.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RegistrantIDResponse(NaiveOrmModel):
id: int
id_type: str = pydantic.Field(..., alias="id_type_as_str")
value: str | None
expiry_date: date | None = None
expiry_date: date | bool | None = None


class PhoneNumberResponse(NaiveOrmModel):
Expand Down Expand Up @@ -52,13 +52,13 @@ def validate_email(cls, v):
class RegistrantIDRequest(NaiveOrmModel):
id_type: str
value: str
expiry_date: date
expiry_date: date | None = None


class RegistrantInfoRequest(NaiveOrmModel):
name: str = Field(..., description="Mandatory field")
ids: list[RegistrantIDRequest]
registration_date: date = None
phone_numbers: list[PhoneNumberRequest]
phone_numbers: list[PhoneNumberRequest] | None = None
email: str | None = None
address: str | None = None

0 comments on commit 0f3fc67

Please sign in to comment.