Skip to main content

buildDynaRequest

@nmi-agro/fdm-source


@nmi-agro/fdm-source / fdm-calculator/src / buildDynaRequest

Function: buildDynaRequest()

buildDynaRequest(field, soilData, cultivations, fertilizers, farmSector, timeframe, cropProperties?, harvestsByBlu?): Record<string, unknown>

Defined in: fdm-calculator/src/mineralization/builders.ts:268

Builds the JSON request body for POST /bemestingsplan/dyna.

Constructs the nested field / farm / crop_properties / fertilizer_properties structure required by the DYNA endpoint.

Rotation building rules:

  1. Cultivations are grouped by b_lu_start calendar year.
  2. The main crop per year is selected using the May 15th rule (see getMainCultivation).
  3. Catchcrops (b_lu_croprotation === "catchcrop") are converted to b_lu_green + b_date_green_incorporation on the same year's entry — they do not become a separate rotation entry.
  4. Fertilizer amendments are only attached to the current calendar year (i.e. timeframe.start.getFullYear()). Preceding years have empty amendment arrays.
  5. If no cultivations are found at all, a minimal placeholder entry is generated for the requested year so the API call can still proceed.

Harvests array: Each rotation entry includes a harvests array with one element containing the harvest date and optionally the yield (looked up from cropProperties).

Fertilizer properties deduplication: If the same p_id appears in multiple applications, only the first occurrence is included in fertilizer_properties.

Parameters

field

Field geometry and identifier.

b_area?

number | null

b_centroid?

[number, number] | null

b_id?

string | null

soilData

Record<string, number | string | null | undefined>

Flat map of soil parameter values from getCurrentSoilData.

cultivations

object[]

All cultivations for the field across all years (no timeframe filter). Preceding-year entries are required for the rotation history.

fertilizers

object[]

Fertilizer applications for the field (current year only, with application date, dose, method, and nutrient content).

farmSector

string

Farm sector string sent to the API (e.g. "arable", "dairy"). Defaults to "arable" if empty.

timeframe

Timeframe

Calendar year timeframe; the start year determines the requested calculation year.

cropProperties?

object[]

Optional catalogue entries for the cultivations on the field, used to populate b_lu_yield in harvests and crop_properties.

harvestsByBlu?

Map<string, object[]>

Returns

Record<string, unknown>

A plain object suitable for JSON.stringify and sending to the API.

Example

const body = buildDynaRequest(
{ b_id: "field_1", b_centroid: [5.585, 53.288] },
{ a_som_loi: 1.5, a_clay_mi: 10, a_silt_mi: 25, a_depth_lower: 0.3 },
cultivations,
fertilizerApplications,
"arable",
{ start: new Date("2026-01-01"), end: new Date("2026-12-31") },
cropProperties,
)