From 7d33d03dc8823c2ba6c4976a19d6d0d8a6e3b3b8 Mon Sep 17 00:00:00 2001 From: Karl Fredrik Haugland Date: Wed, 10 Jul 2024 15:45:22 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=94=20fix:=20Add=20parent=20orgs=20to?= =?UTF-8?q?=20Company?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/financial_management/types.py | 2 ++ .../service/financial_management/utils.py | 9 +++++++++ .../service/resource_management/_base_types.py | 17 +++++++++++++++++ .../service/resource_management/types.py | 12 ++---------- .../service/resource_management/utils.py | 9 ++++++--- 5 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 oda_wd_client/service/resource_management/_base_types.py diff --git a/oda_wd_client/service/financial_management/types.py b/oda_wd_client/service/financial_management/types.py index ecb3a3e..b747cd6 100644 --- a/oda_wd_client/service/financial_management/types.py +++ b/oda_wd_client/service/financial_management/types.py @@ -8,6 +8,7 @@ WorkdayCurrencyHighAccuracy, WorkdayReferenceBaseModel, ) +from oda_wd_client.service.resource_management._base_types import Organization # All public imports should be done through oda_wd_client.types.financial_management __all__: list = [] @@ -69,6 +70,7 @@ class Company(WorkdayReferenceBaseModel): name: str | None = None currency: Currency | None = None country_code: str | None = Field(max_length=2, default=None) + parents: list[Organization] = [] class JournalSource(WorkdayReferenceBaseModel): diff --git a/oda_wd_client/service/financial_management/utils.py b/oda_wd_client/service/financial_management/utils.py index 37d9d4e..e8cdfe3 100644 --- a/oda_wd_client/service/financial_management/utils.py +++ b/oda_wd_client/service/financial_management/utils.py @@ -13,6 +13,7 @@ ProjectWorktag, SpendCategory, ) +from oda_wd_client.service.resource_management._base_types import Organization from oda_wd_client.service.resource_management.types import TaxApplicability @@ -89,11 +90,19 @@ def workday_company_to_pydantic(data: dict) -> Company: else: country_code = None + parent_org_refs = cdata.get("Organization_Container_Reference", []) + parent_orgs = [ + _org + for _org in [Organization.from_id_list(item["ID"]) for item in parent_org_refs] + if _org is not None + ] + return Company( workday_id=cdata["Organization_Data"]["ID"], name=cdata["Organization_Data"]["Organization_Name"], country_code=country_code, currency=Currency(currency_code=currency_code) if currency_code else None, + parents=parent_orgs, ) diff --git a/oda_wd_client/service/resource_management/_base_types.py b/oda_wd_client/service/resource_management/_base_types.py new file mode 100644 index 0000000..f64fd49 --- /dev/null +++ b/oda_wd_client/service/resource_management/_base_types.py @@ -0,0 +1,17 @@ +""" +Some models are used both inside `resource_management` as well as other scopes. To avoid circular imports, +we put these in _base_types.py to allow them to be imported from inside other services. +""" +from typing import Literal + +from oda_wd_client.base.types import WorkdayReferenceBaseModel + + +class Organization(WorkdayReferenceBaseModel): + """ + Reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v42.1/Get_Suppliers.html#OrganizationObjectType # noqa + """ + + _class_name = "OrganizationObject" + workday_id: str + workday_id_type: Literal["Organization_Reference_ID"] = "Organization_Reference_ID" diff --git a/oda_wd_client/service/resource_management/types.py b/oda_wd_client/service/resource_management/types.py index ac3c14a..b30126f 100644 --- a/oda_wd_client/service/resource_management/types.py +++ b/oda_wd_client/service/resource_management/types.py @@ -20,6 +20,8 @@ SpendCategory, ) +from ._base_types import Organization + # All public imports should be done through oda_wd_client.types.resource_management __all__: list = [] @@ -73,16 +75,6 @@ class WorkdayID(str, Enum): ] = "Business_Entity_Status_Value_ID" -class Organization(WorkdayReferenceBaseModel): - """ - Reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v42.1/Get_Suppliers.html#OrganizationObjectType # noqa - """ - - _class_name = "OrganizationObject" - workday_id: str - workday_id_type: Literal["Organization_Reference_ID"] = "Organization_Reference_ID" - - class Supplier(WorkdayReferenceBaseModel): """ Reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v40.2/Get_Suppliers.html#SupplierType # noqa diff --git a/oda_wd_client/service/resource_management/utils.py b/oda_wd_client/service/resource_management/utils.py index bfd9320..efde3b2 100644 --- a/oda_wd_client/service/resource_management/utils.py +++ b/oda_wd_client/service/resource_management/utils.py @@ -10,9 +10,9 @@ Currency, SpendCategory, ) +from oda_wd_client.service.resource_management._base_types import Organization from oda_wd_client.service.resource_management.exceptions import NoSupplierID from oda_wd_client.service.resource_management.types import ( - Organization, PrepaidAmortizationType, Supplier, SupplierInvoice, @@ -108,9 +108,12 @@ def workday_supplier_to_pydantic(data: dict) -> Supplier: status_ref = _ref restrict_org_refs = sup_data.get("Restricted_To_Companies_Reference", list()) - _orgs = [Organization.from_id_list(item["ID"]) for item in restrict_org_refs] + restrict_orgs = [ + Organization.from_id_list(item["ID"]) for item in restrict_org_refs + ] + # Type narrowing to remove falsy values - restricted_orgs = [org for org in _orgs if org] + restricted_orgs = [org for org in restrict_orgs if org] return Supplier( workday_id=sup_id,