Skip to content

Commit

Permalink
✨ [#4344] Configurable objects API group in ZGW registration
Browse files Browse the repository at this point in the history
previously this was not configurable and always used the ObjectsAPIGroup with the lowest pk to create the client when registering the submission
  • Loading branch information
stevenbal committed Oct 15, 2024
1 parent bfc679d commit 59eab2f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ const ZGWOptionsForm = ({index, name, label, schema, formData, onChange}) => {
const [modalOpen, setModalOpen] = useState(false);
const validationErrors = useContext(ValidationErrorContext);

const {zgwApiGroup, zaakVertrouwelijkheidaanduiding} = schema.properties;
const {zgwApiGroup, zaakVertrouwelijkheidaanduiding, objectsApiGroup} = schema.properties;
const apiGroupChoices = getChoicesFromSchema(zgwApiGroup.enum, zgwApiGroup.enumNames);
const objectsApiGroupChoices = getChoicesFromSchema(
objectsApiGroup.enum,
objectsApiGroup.enumNames
);
const confidentialityLevelChoices = getChoicesFromSchema(
zaakVertrouwelijkheidaanduiding.enum,
zaakVertrouwelijkheidaanduiding.enumNames
);

const numErrors = filterErrors(name, validationErrors).length;
const defaultGroup = apiGroupChoices.length === 1 ? apiGroupChoices[0][0] : undefined;
const defaultObjectsApiGroup =
objectsApiGroupChoices.length === 1 ? objectsApiGroupChoices[0][0] : undefined;
return (
<Field name={name} label={label}>
<>
Expand Down Expand Up @@ -92,6 +98,7 @@ const ZGWOptionsForm = ({index, name, label, schema, formData, onChange}) => {
...formData,
// Ensure that if there's only one option, it is automatically selected.
zgwApiGroup: formData.zgwApiGroup ?? defaultGroup,
objectsApiGroup: formData.objectsApiGroup ?? defaultObjectsApiGroup,
}}
onSubmit={(values, actions) => {
onChange({formData: values});
Expand All @@ -104,6 +111,7 @@ const ZGWOptionsForm = ({index, name, label, schema, formData, onChange}) => {
<ZGWFormFields
name={name}
apiGroupChoices={apiGroupChoices}
objectsApiGroupChoices={objectsApiGroupChoices}
confidentialityLevelChoices={confidentialityLevelChoices}
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ValidationErrorContext,
ValidationErrorsProvider,
} from 'components/admin/forms/ValidationErrors';
import {ObjectsAPIGroup} from 'components/admin/forms/objects_api';

import BasicOptionsFieldset from './BasicOptionsFieldset';
import ManageVariableToPropertyMappings from './ManageVariableToPropertyMappings';
Expand All @@ -25,7 +26,21 @@ import {
} from './fields';
import {filterErrors} from './utils';

const ZGWFormFields = ({name, apiGroupChoices, confidentialityLevelChoices}) => {
/**
* Callback to invoke when the API group changes - used to reset the dependent fields.
*/
const onApiGroupChange = prevValues => ({
...prevValues,
objecttype: '',
objecttypeVersion: undefined,
});

const ZGWFormFields = ({
name,
apiGroupChoices,
objectsApiGroupChoices,
confidentialityLevelChoices,
}) => {
const {
values: {propertyMappings = []},
} = useFormikContext();
Expand Down Expand Up @@ -112,6 +127,10 @@ const ZGWFormFields = ({name, apiGroupChoices, confidentialityLevelChoices}) =>
collapsible
fieldNames={['objecttype', 'objecttypeVersion', 'contentJson']}
>
<ObjectsAPIGroup
apiGroupChoices={objectsApiGroupChoices}
onApiGroupChange={onApiGroupChange}
/>
<ObjectType />
<ObjectTypeVersion />
<ContentJSON />
Expand All @@ -137,6 +156,14 @@ ZGWFormFields.propTypes = {
])
)
).isRequired,
objectsApiGroupChoices: PropTypes.arrayOf(
PropTypes.arrayOf(
PropTypes.oneOfType([
PropTypes.number, // value
PropTypes.string, // label
])
)
),
confidentialityLevelChoices: PropTypes.arrayOf(
PropTypes.arrayOf(PropTypes.string) // value & label are both string
).isRequired,
Expand Down
9 changes: 9 additions & 0 deletions src/openforms/registrations/contrib/zgw_apis/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from zgw_consumers.api_models.constants import VertrouwelijkheidsAanduidingen

from openforms.api.fields import PrimaryKeyRelatedAsChoicesField
from openforms.contrib.objects_api.models import ObjectsAPIGroupConfig
from openforms.contrib.zgw.clients.catalogi import (
CaseType,
CatalogiClient,
Expand Down Expand Up @@ -104,6 +105,14 @@ class ZaakOptionsSerializer(JsonSchemaSerializerMixin, serializers.Serializer):
)

# Objects API
objects_api_group = PrimaryKeyRelatedAsChoicesField(
queryset=ObjectsAPIGroupConfig.objects.exclude(
Q(objects_service=None) | Q(objecttypes_service=None)
),
help_text=_("Which Objects API set to use."),
label=_("Objects API set"),
required=False,
)
objecttype = serializers.URLField(
label=_("objects API - objecttype"),
help_text=_(
Expand Down
5 changes: 4 additions & 1 deletion src/openforms/registrations/contrib/zgw_apis/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,10 @@ def register_submission_to_objects_api(
)

# In a follow up PR: the group will be configurable:
api_group = ObjectsAPIGroupConfig.objects.order_by("pk").first()
# import pdb; pdb.set_trace()
api_group = options.get(
"objects_api_group", ObjectsAPIGroupConfig.objects.order_by("pk").first()
)
if not api_group: # pragma: no cover
raise RegistrationFailed("No API group available at all")
with get_objects_client(api_group) as objects_client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,12 @@ def test_submission_with_zgw_and_objects_api_backends(self, m, obj_api_config):
),
)

# unused group
ObjectsAPIGroupConfigFactory.create(
objects_service__api_root="https://objecten2.nl/api/v1/",
organisatie_rsin="111222333",
)
objects_api_group = ObjectsAPIGroupConfigFactory.create(
objects_service__api_root="https://objecten.nl/api/v1/",
organisatie_rsin="000000000",
)
Expand Down Expand Up @@ -1723,6 +1728,7 @@ def get_create_json_for_zaakobject(req, ctx):
"organisatie_rsin": "000000000",
"zaak_vertrouwelijkheidaanduiding": "openbaar",
"doc_vertrouwelijkheidaanduiding": "openbaar",
"objects_api_group": objects_api_group,
"objecttype": "https://objecttypen.nl/api/v1/objecttypes/2",
"objecttype_version": 1,
}
Expand Down
3 changes: 3 additions & 0 deletions src/openforms/registrations/contrib/zgw_apis/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from typing import TYPE_CHECKING, Literal, NotRequired, TypedDict

if TYPE_CHECKING:
from openforms.contrib.objects_api.models import ObjectsAPIGroupConfig

from .models import ZGWApiGroupConfig


Expand Down Expand Up @@ -37,6 +39,7 @@ class RegistrationOptions(TypedDict):
organisatie_rsin: NotRequired[str]
zaak_vertrouwelijkheidaanduiding: NotRequired[VertrouwelijkheidAanduiding]
medewerker_roltype: NotRequired[str]
objects_api_group: NotRequired[ObjectsAPIGroupConfig]
objecttype: NotRequired[str]
objecttype_version: NotRequired[int]
content_json: NotRequired[str]
Expand Down

0 comments on commit 59eab2f

Please sign in to comment.