# Conditions format

Conditions are used for several things in Iteras: To decide whether something happens or to filter a set of objects, e.g. customers, subscriptions, etc.

The format is an array of objects, each being a condition. Each object has a `condition_type` that identifies the condition, plus the fields that type needs. Some conditions work on related objects and have nested sub-conditions. All the listed conditions must be met.

The following conditions match Danish customers whose email is filled in:

```json
[
   {
     "condition_type": "customer:field",
     "field": "country",
     "operator": "in",
     "options": ["DK"]
   },
   {
     "condition_type": "customer:field",
     "field": "email",
     "operator": "filledin"
   }
]
```

The context the conditions are evaluated in determines which conditions are available. For instance, when filtering customers `customer:field` conditions are immediately available, but you need to go through a `subscription:exists` condition to be able to add constraints on subscriptions the customers must have.

## Combining conditions

Simple combinations is already possible - just put multiple conditions in the same list where a list must be supplied and they all have apply.

### any

To match when just one of the conditions is met, put them in an `any` condition. The following conditions match customers with an address in Denmark **or** whose email is filled in:

```json
[
   {
     "condition_type": "any",
     "conditions": [
       {
         "condition_type": "customer:field",
         "field": "country",
         "operator": "in",
         "options": ["DK"]
       },
       {
         "condition_type": "customer:field",
         "field": "email",
         "operator": "filledin"
       }
     ]
   }
]
```

Each condition in `any` is checked on its own, so e.g. two `subscription:exists` conditions in it may match the same subscription.

In a top-level `any` you can also put the conditions that are otherwise only allowed at the top level, like `customer:satisfies`, once in each alternative.

### all

Inside an `any` condition you may want to have several conditions that all have to apply. This is what `all` condition is for. It can only be used directly inside `any`. - everywhere else, conditions next to each other already all have to apply. The following conditions match customers with an address in Denmark **and** whose email is filled in, **or** who have an active subscription:

```json
[
   {
     "condition_type": "any",
     "conditions": [
       {
         "condition_type": "all",
         "conditions": [
           {
             "condition_type": "customer:field",
             "field": "country",
             "operator": "in",
             "options": ["DK"]
           },
           {
             "condition_type": "customer:field",
             "field": "email",
             "operator": "filledin"
           }
         ]
       },
       {
         "condition_type": "subscription:exists",
         "states": ["active"]
       }
     ]
   }
]
```

## Customer conditions

### customer:field

Evaluation context: customer

Matches when a field on the customer satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the customer fields set up for your account.

```json
{
  "condition_type": "customer:field",
  "field": "country",
  "operator": "in",
  "options": ["DK", "SE"]
}
```

### customer:freetextsearch

Evaluation context: customer

Matches when a free-text search for `search_string` finds the customer, like the search in the customer list in Iteras. Each word must be found in one of the fields searched: the customer ID, names and companies, addresses, phone numbers, email addresses, the note and the custom fields set up to be searched. Names, companies and addresses are also searched on overrides: a temporary delivery address that is current, coming up or recently ended, and an address change that hasn't taken effect yet (one that has is the customer's address).

- `search_string` (string, required, may change): The text to search for, e.g. `"rikke hansen"`.
- `search_type` (string, Defaults to `all`, may change): Which fields to search:
  - `all` - all of them
  - `customerid` - the customer ID
  - `name` - names and companies, also those of a payer and on the overrides above
  - `address` - addresses, also those on the overrides above
  - `phone` - phone numbers
  - `email` - email addresses
  - `note` - the note on the customer

  A custom field set up to be searched is given by its field ID. An unknown value is answered with the ones the account has.

```json
{
  "condition_type": "customer:freetextsearch",
  "search_string": "rikke hansen"
}
```

### customer:segment / customer:notsegment

Evaluation context: customer

Matches when the customer is in one of the segments (or not in one of the segments). Segments are defined inside Iteras as any customer matching a set of self-chosen conditions.

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the segments set up for your account.

```json
{
  "condition_type": "customer:segment",
  "segments": [
    { "external_id": "foreign" },
    { "external_id": "b2b" }
  ]
}
```

- `segments` (array, required): The segments to check, e.g. `[{ "external_id": "foreign" }]`. Each object has an `external_id` matching a segment.

### customer:subscriptioncount

Evaluation context: customer

Matches when the number of active subscriptions for the customer is between `from_count` and `to_count`, both inclusive.

- `from_count` (integer): The minimum number of active subscriptions. Defaults to `0`.
- `to_count` (integer): The maximum number of active subscriptions. Must not be smaller than `from_count`. Omit for no upper limit.
- `count_recipients` (bool): Whether to also count receivers on group subscriptions the customer controls. Defaults to `false`.

```json
{
  "condition_type": "customer:subscriptioncount",
  "from_count": 2,
  "to_count": 5
}
```

### customer:eventcount

Evaluation context: customer

Matches when the number of events of the given type for the customer in the given period is between `from_count` and `to_count`, both inclusive.

- `event_type` (string, required): The events to count, one of:
  - `customer:login@selfservice` - logins to the self-service
  - `customer:login@paywall` - logins through a paywall
  - `customer:regenerated-paywall-token` - renewals of the paywall access of a reader who stays logged in
  - `customer:attempted-login` - failed logins where the customer was recognized
  - `customer:onetime-password-requested`, `customer:password-reset-requested`, `customer:logout`
  - `customer:updated`, `error:email-delivery`, `error:invalid-email`
  - `order:approved` - orders approved, manually or automatically
  - `subscription:created`, `subscription:renewed`, `subscription:registered-stop`, `subscription:changed-campaign`, `subscription:changed-status`
  - `invoice:invoiced`, `payment:created`
  - `invoice:resent` - invoices sent again to the customer
  - `payment:rejected` - direct debit payments rejected by the customer
  - `distribution:created-complaint` - complaints about deliveries
  - `distribution:delay` - delays registered on deliveriesThe other event types are described in [Available event types](/api/docs/server/eventtypes.md).
- `from_count` (integer): The minimum number of events. Defaults to `0`.
- `to_count` (integer): The maximum number of events, e.g. `0` to find customers without any. Must not be smaller than `from_count`. Omit for no upper limit.
- `operator` (string): The period to count in, with `date_interval`, `number`, `unit` and `offset` as for `subscription:ends`. The choices are `ininterval`, `lessthanago`, `morethanago` and `incalendarperiod`, and `offset` is `-1` or `0` since events can't be in the future. Omit the period to count all events.

```json
{
  "condition_type": "customer:eventcount",
  "event_type": "customer:login@selfservice",
  "from_count": 3,
  "to_count": 7,
  "operator": "lessthanago",
  "number": 45,
  "unit": "day"
}
```

### customer:satisfies

Evaluation context: customer

Conditions applied to the customer, e.g. `[{ "condition_type": "customer:field", ... }]`. Evaluation context is the customer.

```json
{
  "condition_type": "customer:satisfies",
  "conditions": [
    {
      "condition_type": "customer:field",
      "field": "country",
      "operator": "in",
      "options": ["DK"]
    }
  ]
}
```

## Subscription conditions

### subscription:exists / subscription:notexists

Evaluation context: customer

Matches when the customer has, respectively, a subscription or no subscription in one of the `states`, where any extra `conditions` also apply to that subscription.

- `states` (array): The subscription states to match: `active`, `awaiting`, `suspended` or `ended`. Omit to match subscriptions in any state.
- `conditions` (array): Conditions applied to the subscription, e.g. `[{ "condition_type": "subscription:product", ... }]`. Evaluation context is the subscription.

```json
{
  "condition_type": "subscription:exists",
  "states": ["active"],
  "conditions": [
    {
      "condition_type": "subscription:product",
      "products": [1, 2]
    }
  ]
}
```

### subscription:field

Evaluation context: subscription

Matches when a field on the subscription satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the subscription fields set up for your account.

### subscription:type

Evaluation context: subscription

Matches when the subscription is one of the given types.

- `types` (array): The subscription types to match. Each is one of `standard`, `group`, `receiver`, `addon`, `proforma` or `not_proforma`.

```json
{
  "condition_type": "subscription:type",
  "types": ["group", "receiver"]
}
```

### subscription:groupsubscription

Evaluation context: subscription

Matches when the subscription is a receiver subscrition of a group subscription where the given subscription conditions apply. Inside `invoiceline:subscriptionperiod`, this lets a discount on a receiver's invoice lines depend on a field on the group subscription.

- `conditions` (array): Conditions applied to the group subscription, e.g. `[{ "condition_type": "subscription:field", ... }]`. Evaluation context is the group subscription.

```json
{
  "condition_type": "subscription:groupsubscription",
  "conditions": [
    {
      "condition_type": "subscription:field",
      "field": "discount_agreement",
      "operator": "equal",
      "value": "yes"
    }
  ]
}
```

### subscription:product

Evaluation context: subscription

Matches when the subscription is on one of the given products. Products can be referenced by their external ID as well as their internal ID.

- `products` (array, required): The products to match, as internal IDs or objects with an `external_id`, e.g. `[1, { "external_id": "magazine" }]`.

```json
{
  "condition_type": "subscription:product",
  "products": [
    { "external_id": "magazine" },
    { "external_id": "newspaper" }
  ]
}
```

### subscription:campaign / subscription:notcampaign

Evaluation context: subscription

Matches when the subscription is, respectively, on one of the given campaigns or not on any of them. Campaigns can be referenced by their campaign ID as well as their internal ID.

- `campaigns` (array, required): The campaigns to match, as internal IDs or objects with a `campaign_id`, e.g. `[12, { "campaign_id": "3months" }]`.

```json
{
  "condition_type": "subscription:campaign",
  "campaigns": [
    { "campaign_id": "3months" },
    { "campaign_id": "halfyear" }
  ]
}
```

### subscription:campaignfield

Evaluation context: subscription

Matches when a field on the subscription's campaign satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the campaign fields set up for your account.

### subscription:policy

Evaluation context: subscription

Matches when the subscription is on one of the given business policies. Policies can be referenced by their external ID as well as their internal ID.

- `policies` (array, required): The business policies to match, as internal IDs or objects with an `external_id`, e.g. `[5, { "external_id": "schools" }]`.

```json
{
  "condition_type": "subscription:policy",
  "policies": [
    { "external_id": "schools" },
    { "external_id": "libraries" }
  ]
}
```

### subscription:paid

Evaluation context: subscription

Matches when the subscription is paid or free.

- `paid` (bool): `true` to match paid subscriptions, `false` to match free ones.

```json
{
  "condition_type": "subscription:paid",
  "paid": true
}
```

### subscription:state

Evaluation context: subscription

Matches when the subscription is in one of the `states`.

- `states` (array, required): The subscription states to match: `active`, `awaiting`, `suspended` or `ended`.

```json
{
  "condition_type": "subscription:state",
  "states": ["active", "suspended"]
}
```

### subscription:ends / subscription:renewal / subscription:suspended

Evaluation context: subscription

`subscription:ends` matches when the subscription has a stop date, `subscription:renewal` when it has a renewal date, and `subscription:suspended` when it was suspended. All three take a date operator that describes the relevant date:

- `operator` (string, required): How the date is expressed. `ininterval` is the default. The choices are:
  - `ininterval` — the date is between the two dates in `date_interval`, e.g. the dates 1 Jan and 31 Jan.
  - `morethanago` — the date is more than `number` `unit` ago, e.g. more than 3 months ago.
  - `lessthanago` — the date is less than `number` `unit` ago, e.g. less than 1 year ago.
  - `lessthanfromnow` — the date is within the next `number` `unit`, e.g. within the next 2 weeks (not supported by `subscription:suspended`).
  - `morethanfromnow` — the date is more than `number` `unit` from now, e.g. more than 6 months from now (not supported by `subscription:suspended`).
  - `incalendarperiod` — the date is in the given calendar period, described by `unit` and `offset`, e.g. this month or previous month.
- `date_interval` (array): The two dates, start and end, as ["YYYY-MM-DD", "YYYY-MM-DD"] for `ininterval`, e.g. `["2024-01-01", "2024-01-31"]`.
- `number` (integer): The amount of time for the ago and from-now operators, e.g. `3`.
- `unit` (string): The unit of `number` and `offset`: `year`, `month` or `day` for the ago and from-now operators; `day`, `month` or `year` for `incalendarperiod`.
- `offset` (integer): Offset to apply to `incalendarperiod`: `-1` gives the previous period, `0` the current one, `1` the next.

```json
{
  "condition_type": "subscription:ends",
  "operator": "ininterval",
  "date_interval": ["2024-01-01", "2024-01-31"]
}
```

```json
{
  "condition_type": "subscription:renewal",
  "operator": "incalendarperiod",
  "offset": 1,
  "unit": "month"
}
```

## Subscription period conditions

These conditions evaluate against a subscription period, not the subscription itself. A subscription period is the span of time a subscription is on a given product, so a subscription's product can change from period to period.

### subscriptionperiod:product

Evaluation context: subscription period

Matches when the subscription period is on one of the given products. Products can be referenced by their external ID as well as their internal ID.

- `products` (array, required): The products to match, as internal IDs or objects with an `external_id`, e.g. `[1, { "external_id": "magazine" }]`.

```json
{
  "condition_type": "subscriptionperiod:product",
  "products": [
    { "external_id": "magazine" },
    { "external_id": "newspaper" }
  ]
}
```

## Invoice conditions

### invoice:exists / invoice:notexists

Evaluation context: customer

Matches when the customer has, respectively, an invoice or no invoice, where any extra `conditions` also apply to that invoice.

- `conditions` (array): Conditions applied to the invoice, e.g. `[{ "condition_type": "invoiceline:exists", ... }]`. Evaluation context is the invoice.

```json
{
  "condition_type": "invoice:exists"
}
```

### invoice:field

Evaluation context: invoice

Matches when a field on the invoice satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the invoice fields set up for your account.

## Invoice line conditions

### invoiceline:exists

Evaluation context: invoice

Matches when the invoice has an invoice line, where any extra `conditions` also apply to that line.

- `conditions` (array): Conditions applied to the invoice line, e.g. `[{ "condition_type": "invoiceline:subscriptionperiod", ... }]`. Evaluation context is the invoice line.

```json
{
  "condition_type": "invoiceline:exists"
}
```

### invoiceline:field

Evaluation context: invoice line

Matches when a field on the invoice line satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the invoice line fields set up for your account.

### invoiceline:subscriptionperiod

Evaluation context: invoice line

Matches when the line is for a subscription period where the given subscription period conditions apply, e.g. `subscriptionperiod:product`.

- `conditions` (array): Conditions applied to the subscription period, e.g. `[{ "condition_type": "subscriptionperiod:product", ... }]`. Evaluation context is the subscription period.

```json
{
  "condition_type": "invoiceline:subscriptionperiod",
  "conditions": [
    {
      "condition_type": "subscriptionperiod:product",
      "products": [1, 2]
    }
  ]
}
```

### invoiceline:productassignment

Evaluation context: invoice line

Matches when the line is for a product assignment where the given product assignment conditions apply, e.g. `productassignment:field`.

- `conditions` (array): Conditions applied to the product assignment, e.g. `[{ "condition_type": "productassignment:field", ... }]`. Evaluation context is the product assignment.

```json
{
  "condition_type": "invoiceline:productassignment",
  "conditions": [
    {
      "condition_type": "productassignment:field",
      "field": "type",
      "operator": "equal",
      "value": "sample"
    }
  ]
}
```

## Campaign conditions

### campaign:field

Evaluation context: campaign

Matches when a field on the campaign satisfies the expression, see [field conditions](#field-conditions).

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the campaign fields set up for your account.

### campaign:campaign / campaign:notcampaign

Evaluation context: campaign

Matches when the campaign is, respectively, one of or not one of the given campaigns. Campaigns can be referenced by their campaign ID as well as their internal ID.

- `campaigns` (array, required): The campaigns to match, as internal IDs or objects with a `campaign_id`, e.g. `[12, { "campaign_id": "3months" }]`.

```json
{
  "condition_type": "campaign:campaign",
  "campaigns": [
    { "campaign_id": "3months" },
    { "campaign_id": "halfyear" }
  ]
}
```

### campaign:timeduration

Evaluation context: campaign

Matches when the campaign runs for the given amount of time.

- `campaign_time_duration` (string, required): How long the campaign runs, e.g. `"3m"` for three months. The unit is `y`, `m`, `w` or `d`.

```json
{
  "condition_type": "campaign:timeduration",
  "campaign_time_duration": "3m"
}
```

## Product and business policy conditions

Each of these matches when a field on the object satisfies the expression, see [field conditions](#field-conditions) for the operators.

### product:field

Evaluation context: product

Matches when a field on the product satisfies the expression.

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the product fields set up for your account.

### productassignment:field

Evaluation context: product assignment

Matches when a field on the product assignment satisfies the expression.

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the product assignment fields set up for your account.

### businesspolicy:field

Evaluation context: business policy

Matches when a field on the business policy satisfies the expression.

[Log in](/login/?next=/api/docs/server/conditionsformat.md), or send your API key as a bearer token, to see the business policy fields set up for your account.

## Field conditions

The `*:field` conditions, such as `customer:field` and `subscription:field`, describe an expression. Which operators are available depends on the field's type.

- `field` (string, required): The ID of a field on the object the condition applies to.
- `operator` (string, required): The operator for the expression, see below.

### filledin / notfilledin

The field is, respectively, filled in or not filled in.

A missing field, `null`, an empty string and an empty list are not filled in, and neither is 0 in an amount field. On a yes/no field both `true` and `false` are filled in, so use `in` to match one of them.

Field types: all.

### in / notin

The field is, respectively, one of or not one of the given values.

- `options` (array, required): The allowed values, e.g. `["DK", "SE"]`.

Field types: `choice`, `multiple choice`, `boolean`.

For a yes/no (`boolean`) field the values are `true` and `false`.

```json
{
  "condition_type": "customer:field",
  "field": "country",
  "operator": "in",
  "options": ["DK", "SE"]
}
```

### in / notin on relation fields

The field is, respectively, related or not related to one of the given customers or users. A field related to several matches `in` when one of them is given.

- `customers` (array): The customer IDs, for a relation to customers, e.g. `["41325", "41326"]`.
- `users` (array): The emails of the users, for a relation to users, e.g. `["jane@example.com"]`. Upper and lower case don't matter.

Field types: `relation to customer`, `relation to one or more customers`, `relation to user`, `relation to one or more users`.

Conditions on relation fields, including `filledin` and `notfilledin`, work when filtering, e.g. when [retrieving customers](/api/docs/server/retrievingcustomers.md). They don't work in conditions that are checked against a single object, such as [invoice line](/api/docs/server/invoicelinesformat.md) conditions, where they never match.

```json
{
  "condition_type": "customer:field",
  "field": "contact_person",
  "operator": "in",
  "customers": ["41325"]
}
```

### equal / notequal

The field is, respectively, the same or not the same as the given value.

- `value` (string, required): The value to compare against, e.g. `"København"`.

Field types: `text`, `email`, `phone number`, `VAT number`, `e-invoice recipient`, `number`, `monetary`.

```json
{
  "condition_type": "customer:field",
  "field": "group",
  "operator": "equal",
  "value": "B2B"
}
```

### contains / notcontains

The field does, respectively, contain or not contain the given text.

- `text` (string, required): The text to look for, e.g. `"A/S"`.

Field types: `text`, `email`, `phone number`, `VAT number`, `e-invoice recipient`.

```json
{
  "condition_type": "customer:field",
  "field": "company",
  "operator": "contains",
  "text": "A/S"
}
```

### startswith / notstartswith / endswith / notendswith

The field does, respectively, start with or not start with, or end with or not end with, the given text. The comparison is case-insensitive.

- `text` (string, required): The text to compare against, e.g. `"firma.dk"`.

Field types: `text`, `email`.

```json
{
  "condition_type": "customer:field",
  "field": "email",
  "operator": "endswith",
  "text": "@example.com"
}
```

### oneof / notoneof

The field is, respectively, one of or not one of the given values.

- `values` (array, required): The allowed values, e.g. `["personal", "business"]`.

Field types: `text`, `email`, `VAT number`, `number`.

```json
{
  "condition_type": "customer:field",
  "field": "customer_type",
  "operator": "oneof",
  "values": ["personal", "business"]
}
```

### lessthan / greaterthan / lessthanequal / greaterthanequal

The field is less than, greater than, less than or equal to, or greater than or equal to the given value.

- `value` (number, required): The value to compare against, e.g. `70`. Monetary values should be written in cents, e.g. `9095` for 90.95.

Field types: `number`, `monetary`.

```json
{
  "condition_type": "customer:field",
  "field": "price",
  "operator": "greaterthanequal",
  "value": 99.95
}
```

### lessthanago / morethanago

The field is a date less than or more than the given amount of time ago.

- `number` (integer, required): How long ago, e.g. `3`.
- `unit` (string, required): The unit of `number`: `year`, `month` or `day`.

Field types: `date`, `date-time`.

```json
{
  "condition_type": "customer:field",
  "field": "start_date",
  "operator": "morethanago",
  "number": 3,
  "unit": "month"
}
```

### ininterval

The field is a date between the given two dates.

- `date_interval` (array, required): Two dates, the start and the end, as YYYY-MM-DD, e.g. `["2024-01-01", "2024-01-31"]`.

Field types: `date`, `date-time`.

```json
{
  "condition_type": "customer:field",
  "field": "start_date",
  "operator": "ininterval",
  "date_interval": ["2024-01-01", "2024-01-31"]
}
```

### incalendarperiod

The field is a date in the given calendar period.

- `offset` (integer, required): `-1` for the last period, `0` for the current one, `1` for the next.
- `unit` (string, required): `day`, `week`, `month` or `year`.

Field types: `date`, `date-time`.

```json
{
  "condition_type": "customer:field",
  "field": "start_date",
  "operator": "incalendarperiod",
  "offset": -1,
  "unit": "month"
}
```

### match / notmatch

The field is a postcode in, respectively, one of or none of the given postcodes or intervals.

- `postcode` (array, required): Postcodes or intervals, e.g. `["2412", "4000-4999"]`. A single postcode such as `2412` or an interval such as `4000-4999`.

Field types: `postcode`.

```json
{
  "condition_type": "customer:field",
  "field": "postcode",
  "operator": "match",
  "postcode": ["2412", "4000-4999"]
}
```