Resource Category
ResourceCategory is a facility-scoped, self-nesting category tree that classifies resources — product_knowledge, activity_definition, and charge_item_definition — and carries charge-item monetary components that inherit down the tree.
The Django model is just storage. The behaviour you code against lives in the Pydantic resource specs in care/emr/resources/resource_category/: the resource_type enum, the MonetaryComponent shape behind the *_monetary_components JSON fields, slug validation, and the split between read and write schemas. Several model fields are opaque JSONFields whose real structure exists only in the specs — see Resource specs (API schema).
Source:
- Model:
care/emr/models/resource_category.py - Spec:
care/emr/resources/resource_category/spec.py - Viewset:
care/emr/api/viewsets/resource_category.py - Shared:
base.py·common/monetary_component.py·common/coding.py
Models
| Model | Purpose |
|---|---|
ResourceCategory | A facility-scoped, self-nesting category tree node that classifies resources (product_knowledge, activity_definition, charge_item_definition) and carries inheritable charge-item monetary components |
ResourceCategory extends SlugBaseModel with FACILITY_SCOPED = True, the facility-scoped slug variant of the base. It inherits the EMR base fields — external_id, audit fields, soft-delete, history/meta JSON — and adds slug handling on top.
ResourceCategory fields
Core
| Field | Type | Required | Notes |
|---|---|---|---|
facility | FK → facility.Facility (PROTECT) | yes | Owning facility; PROTECT blocks deletion while categories reference it. Set server-side and excluded from spec write/read payloads |
resource_type | CharField(255) | yes | Top-level classification. The spec constrains it to the ResourceCategoryResourceTypeOptions enum — see values |
resource_sub_type | CharField(255) | yes | Secondary classification; a free string in the spec (resource_sub_type: str) |
title | CharField(255) | yes | Display name (title: str) |
slug | CharField(255) | yes | Stored encoded as f-<facility_external_id>-<slug_value>. Clients send the unencoded slug_value (validated SlugType); the viewset's perform_create / perform_update encode it with calculate_slug_from_facility() |
description | TextField | no | Nullable, blank-able (description: str | None = None) |
Meta declares a composite index on (slug, facility) for slug lookups within a facility.
Tree structure
| Field | Type | Notes |
|---|---|---|
parent | FK → emr.ResourceCategory (CASCADE) | Self-reference to the parent node; nullable. related_name="children". __exclude__d from spec serialization — written on create by the parent's stored (encoded) slug, read via get_parent_json() |
is_child | BooleanField | Default False. Exposed on create (ResourceCategoryWriteSpec) and read. A category with is_child = True cannot be a parent: create rejects it as parent with "Parent category is a child" |
has_children | BooleanField | Default False. Flips to True on the parent when a child is created (see save behaviour), and back to False when the delete endpoint removes the last non-deleted child. Read-only in specs |
root_org | FK → emr.ResourceCategory (CASCADE) | Self-reference to the root of this node's tree; nullable. related_name="root". Not exposed in specs |
Tree caches
set_organization_cache() and get_parent_json() denormalize the ancestor chain into these fields, so reads resolve it without recursive joins. They are platform-maintained; clients never write them.
| Field | Type | Rebuilt by |
|---|---|---|
parent_cache | ArrayField[int] | set_organization_cache() — the parent's parent_cache plus the parent's id |
level_cache | IntegerField | set_organization_cache() — parent's level_cache + 1 (default 0 for roots). Exposed read-only (level_cache: int = 0) |
cached_parent_json | JSONField | get_parent_json() — nested snapshot (shape below) with a cache_expiry timestamp; refreshed after cache_expiry_days (15) |
get_parent_json() builds cached_parent_json in this shape, which is also the shape of the parent field in ResourceCategoryReadSpec:
{
"id": str, # parent.external_id (UUID)
"slug": str, # parent.slug (encoded)
"title": str,
"description": str | null,
"parent": { ... }, # recursively the grandparent's cached_parent_json ({} at root)
"cache_expiry": str # ISO datetime; now + 15 days
}
Root nodes (no parent_id) serialize parent as {}.
Charge item monetary components
Both are JSONField(default=list) in the model; the spec types each element as a MonetaryComponent (see shape). ResourceCategoryReadSpec returns both fields for every category, because they are model fields that serialize() maps directly. Only a charge_item_definition category can hold values other than [], because only that type accepts set_monetary_components.
| Field | Type | Notes |
|---|---|---|
configured_monetary_components | JSONField → list[MonetaryComponent] | None | Components configured directly on this category. Not in the create or update spec; written only through the set_monetary_components action |
calculated_monetary_components | JSONField → list[MonetaryComponent] | None | Effective components after merging the parent's calculated components with this node's configured components (see Methods). Server-maintained, read-only |
Enums
ResourceCategoryResourceTypeOptions values
care/emr/resources/resource_category/spec.py — bound to resource_type.
| Value | Meaning |
|---|---|
product_knowledge | Category classifies Product Knowledge entries |
activity_definition | Category classifies Activity Definitions |
charge_item_definition | Category classifies Charge Item Definitions; the only type that accepts set_monetary_components |
MonetaryComponentType values
care/emr/resources/common/monetary_component.py — bound to MonetaryComponent.monetary_component_type.
| Value |
|---|
base |
surcharge |
discount |
tax |
informational |
Resource specs (API schema)
All specs extend EMRResource (serialize/de_serialize; reads run perform_extra_serialization, writes run perform_extra_deserialization). With __model__ = ResourceCategory and __exclude__ = ["parent"], the parent FK is never auto-mapped — every spec resolves it explicitly.
| Spec class | Role | Exposes / behaviour |
|---|---|---|
ResourceCategoryBaseSpec | shared base | id (UUID, read), title, description, resource_type (enum), resource_sub_type |
ResourceCategoryWriteSpec | write · create | Base + parent: str | None (the parent's stored, encoded slug), is_child: bool = False, slug_value: SlugType. When parent is set, perform_extra_deserialization resolves obj.parent = ResourceCategory.objects.get(slug=self.parent), and sets obj.slug = self.slug_value |
ResourceCategoryUpdateSpec | write · update | Base + slug_value: SlugType. perform_extra_deserialization sets obj.slug = self.slug_value; update never reassigns parent |
ResourceCategoryReadSpec | read · list & detail | Base + parent: dict, has_children: bool, level_cache: int = 0, is_child: bool, slug: str, slug_config: dict, calculated_monetary_components, configured_monetary_components. See serialization below |
Docstrings label these "ChargeItemDefinition Category", but the model is
ResourceCategory. There is no separate*ListSpec/*RetrieveSpec—ResourceCategoryReadSpecserves both list and detail.
ResourceCategoryReadSpec serialization (perform_extra_serialization)
id = obj.external_idparent = obj.get_parent_json()— the nested ancestor snapshot (shape above)slug_config = obj.parse_slug(obj.slug)— a facility-scoped slug returns{"facility": <uuid str>, "slug_value": <str>}; an instance slug returns{"slug_value": <str>}calculated_monetary_components/configured_monetary_components—serialize()already maps both from the model for every category. The extra step re-sets them to the same values whenresource_type == "charge_item_definition", so the condition does not hide them for other types (they come back as[])
SlugType validation
slug_value is Annotated[str, Field(min_length=5, max_length=50), AfterValidator(slug_validator)]:
- length 5–50
- regex
^[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]$— URL-safe (letters, digits,-,_), starting and ending alphanumeric
The stored slug is the encoded form f-<facility_external_id>-<slug_value>. The viewset encodes it with ResourceCategory.calculate_slug_from_facility() on create and update; slug_config round-trips it back to {facility, slug_value}.
MonetaryComponent (nested shape)
Element type of configured_monetary_components / calculated_monetary_components (common/monetary_component.py).
| Field | Type | Default | Notes |
|---|---|---|---|
monetary_component_type | MonetaryComponentType enum | — | required; see values |
code | Coding | None | None | Coding { system?: str, version?: str, code: str, display?: str } (extra="forbid") |
factor | Decimal | None | None | max_digits=20, decimal_places=6 |
amount | Decimal | None | None | max_digits=20, decimal_places=6 |
tax_included_amount | Decimal | None | None | max_digits=20, decimal_places=6; allowed only on base |
global_component | bool | False | |
conditions | list[EvaluatorConditionSpec] | [] | each { metric: str, operation: str, value: dict | str }, validated against the evaluator-metrics registry |
MonetaryComponent model validators:
tax_included_amountonly allowed when type isbasebasemust have noconditionsbasemust have anamountamountandfactorare mutually exclusive (not both)- either
amountorfactormust be present (unlessglobal_component and code)
The read spec types each element as
MonetaryComponent. Theset_monetary_componentsaction validates the request body asMonetaryComponentsWithoutBase(no duplicatecode.code, tax-sum balance when a base hastax_included_amount). It then rejects anybasecomponent with"Base component is not allowed in configured monetary components".
Methods & save behaviour
ResourceCategory overrides save() to populate the tree caches on create. A handful of helper methods plus a Celery task maintain those caches and the inherited monetary components.
save()
- On create (
self.idnot yet set): runssuper().save(), thenset_organization_cache(), then callssummarise_monetary_components(self)directly (synchronously, not through.delay()). - On update: a plain
super().save(*args, **kwargs), no cache rebuild.
set_organization_cache()
Runs once on creation. When a parent is set, it:
- Sets
parent_cache = [*parent.parent_cache, parent.id]andlevel_cache = parent.level_cache + 1. - Sets
root_orgto the parent'sroot_org, or to the parent itself if the parent is a root. - If the parent's
has_childrenwasFalse, flips it toTrueand persists it withsave(update_fields=["has_children"]).
It always persists the node via super().save(), with or without a parent.
get_parent_json()
Returns cached_parent_json, rebuilding it lazily. A snapshot still within its cache_expiry is returned as-is. Past expiry, the method walks the parent chain, builds a fresh nested snapshot with a new expiry (now + cache_expiry_days), persists it via save(update_fields=["cached_parent_json"]), and returns it. Root nodes (no parent_id) return {}.
summarise_monetary_components(category) — Celery task
A @shared_task that recomputes calculated_monetary_components down the subtree. save() (on create) and the set_monetary_components action call it directly for the node itself:
- For a root (no
parent),calculated_monetary_components = configured_monetary_components. - Otherwise it merges the parent's
calculated_monetary_componentswith this node'sconfigured_monetary_componentsviamerge_monetary_components(), then persists withsave(update_fields=["calculated_monetary_components"]). - It re-dispatches itself (
.delay(component.id)) for every child, propagating changes through the tree.
merge_monetary_components(parent, child) keys components by code.system + code.code; child components override parent components sharing a key, and components without a code are appended unmerged.
API integration notes
- Categories are facility-scoped. The
SlugBaseModelencoding (f-<facility>-<slug>) puts the facility in every slug, andvalidate_datarejects a duplicate encoded slug. The(slug, facility)index is not unique, so no database rule enforces this. Clients sendslug_value(5–50, URL-safe); the server encodes it andslug_configdecodes it. - Write
parentby the parent's stored, encoded slug (ResourceCategoryWriteSpec.parent→ResourceCategory.objects.get(slug=...)), not by id. On read it comes back as the nestedget_parent_json()snapshot. parent_cache,level_cache,root_org,has_children,cached_parent_json, andcalculated_monetary_componentsare platform-maintained — don't set them. Writeparenton create andconfigured_monetary_componentsthroughset_monetary_components;save()and the Celery task derive the rest.set_organization_cacheruns only on create.ResourceCategoryUpdateSpechas noparentfield, so the API cannot move a node after create.- Monetary-component summarisation runs on create and after every
set_monetary_componentscall. The node itself updates synchronously; descendants update through Celery (.delay()), so theircalculated_monetary_componentsis eventually consistent. - Monetary-component fields come back for every
resource_type(as[]for types other thancharge_item_definition). During merges, component identity rests on the nestedcode.system+code.codepair.
Endpoints
ResourceCategoryViewSet is nested under the facility router at facility/{facility_external_id}/resource_category/. lookup_field = "slug", so detail routes take the stored, encoded slug.
| Action | Method · route | Behaviour |
|---|---|---|
| list | GET resource_category/ | Scoped to the URL facility; requires can_list_facility_resource_category. Filters: parent (parent slug, iexact; an empty ?parent= returns only roots), title (icontains), resource_type, resource_sub_type, level_cache. Ordering: created_date, modified_date |
| create | POST resource_category/ | ResourceCategoryWriteSpec; requires can_write_facility_resource_category on the URL facility |
| retrieve | GET resource_category/{slug}/ | ResourceCategoryReadSpec. get_queryset applies the same facility scope and can_list_facility_resource_category check as list |
| update | PUT resource_category/{slug}/ | ResourceCategoryUpdateSpec; requires can_write_facility_resource_category on the category's facility |
| delete | DELETE resource_category/{slug}/ | Soft delete. Same permission as update |
upsert | POST resource_category/upsert/ | Batch create or update (EMRUpsertMixin) |
set_monetary_components | POST resource_category/{slug}/set_monetary_components/ | Body: a list of MonetaryComponent. Only for charge_item_definition categories ("Resource category is not a charge item definition"). Requires can_write_facility_resource_category. Saves configured_monetary_components, runs summarise_monetary_components, and returns the refreshed ResourceCategoryReadSpec |
validate_data runs on create and update:
- Rejects a slug that already exists (case-insensitive match on the encoded slug) with
"Resource category with this slug already exists.". Update excludes the category itself from this check. - On create with a
parent: the parent must exist in the same facility, have the sameresource_type("Parent category does not belong to same resource type"), and haveis_child = False("Parent category is a child").
The delete endpoint (validate_destroy and perform_destroy):
- Rejects a category that has children (
"Cannot delete a resource category with children"). - Rejects a category that a resource of its
resource_typestill references throughcategory(ActivityDefinition,ChargeItemDefinition, orProductKnowledge), with"Cannot delete a resource category associated with a resource". - In one transaction, sets
deleted = Trueandupdated_by. If the parent has no other non-deleted children, it sets the parent'shas_childrentoFalse.
Related
- Reference: Charge Item Definition
- Reference: Charge Item
- Reference: Activity Definition
- Reference: Product Knowledge
- Reference: Facility
- Reference: Base Model
- Source: resource_category.py (model) · spec.py · monetary_component.py