# Preview a change of template for many records

`GET /api/v1/vaults/{vault}/record-kinds/{recordKind}/template-change-preview` and `QUERY /api/v1/vaults/{vault}/record-kinds/{recordKind}/template-change-preview`

Say what moving every non-archived record of one template onto another would do, without changing anything.

Call this before POST /vaults/{vault}/record-kinds/{recordKind}/template-changes and show the result to the person. A value follows the matching field of the new version. A value without one is set aside, never deleted, and comes back if a later template has the field.

A sensitive field is named only to a key holding the sensitive fields scope. Otherwise it is counted in hidden_count.

This endpoint answers GET and QUERY alike, and both return the same answer. GET sends the parameters in the address, QUERY in the request body. [How QUERY works](https://my.monicahq.com/docs/api/query-method).

## Parameters

- `vault` (uuid, required): The id of the vault.
- `recordKind` (string, required): contacts for people, or the id of an entity type, as GET /entity-types gives it.
- `from` (uuid, required): The id of the template the records leave, as the statistics give it.
- `version` (integer, optional): Take only the records on this version of the source template. Leave it out to take every version.
- `to` (uuid, required): The id of the template the records move onto, at the version it publishes now. Send the same template as from to update records on older versions to the published one.

## Returns

What the change would do. Nothing is written. A vault, an entity type or a template of another account answers 404. A destination that describes another kind of record, is turned off or has no published version answers 422 with the reason on to.

- `data` (object): What the change would do.
- `data.type` (string): Always template_change_preview.
- `data.attributes` (object): Everything the preview says.
- `data.attributes.from` (uuid): The id of the template the records leave.
- `data.attributes.to` (uuid): The id of the template they would move onto.
- `data.attributes.to_version` (integer): The number of the version they would move onto.
- `data.attributes.counts` (object): How many records each effect touches.
- `data.attributes.counts.ready` (integer): How many records can move. This is the number the change takes.
- `data.attributes.counts.unchanged` (integer): How many would have no value set aside or restored.
- `data.attributes.counts.set_aside` (integer): How many would have values set aside.
- `data.attributes.counts.restored` (integer): How many would have values set aside earlier brought back.
- `data.attributes.counts.reminders_paused` (integer): How many reminders would pause.
- `data.attributes.counts.reminders_resumed` (integer): How many reminders would run again.
- `data.attributes.set_aside` (array): The fields whose values would be set aside, most records first.
- `data.attributes.set_aside[]` (object): One field.
- `data.attributes.set_aside[].label` (string): What the field is called.
- `data.attributes.set_aside[].section` (string): The section it belongs to.
- `data.attributes.set_aside[].records` (integer): How many records it concerns.
- `data.attributes.restored` (array): The fields whose values would come back. Same shape as set_aside.
- `data.attributes.restored[]` (object): One field.
- `data.attributes.restored[].label` (string): What the field is called.
- `data.attributes.restored[].section` (string): The section it belongs to.
- `data.attributes.restored[].records` (integer): How many records it concerns.
- `data.attributes.reminders_paused` (array): The reminders that would pause, grouped by name.
- `data.attributes.reminders_paused[]` (object): One reminder name.
- `data.attributes.reminders_paused[].name` (string): What the reminder is called.
- `data.attributes.reminders_paused[].records` (integer): How many reminders of that name.
- `data.attributes.reminders_resumed` (array): The reminders that would run again. Same shape as reminders_paused.
- `data.attributes.reminders_resumed[]` (object): One reminder name.
- `data.attributes.reminders_resumed[].name` (string): What the reminder is called.
- `data.attributes.reminders_resumed[].records` (integer): How many reminders of that name.
- `data.attributes.blocked` (array): The ids of the records another running change already holds. They are left out of every count.
- `data.attributes.blocked[]` (uuid): The id of one such record.
- `data.attributes.hidden_count` (integer): How many sensitive fields the lists leave out, because the key does not hold the sensitive fields scope.

## Status codes

| Code | When | Body |
| --- | --- | --- |
| `422` Unprocessable Content | With QUERY, a parameter was sent both in the address and in the body. | A message, and an errors object naming each parameter sent twice. |

## Rate limit

Sixty requests per minute, per user, shared by every key and device. Every answer carries the budget left in `X-RateLimit-Remaining`.

## Request

```sh
curl https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e \
  -H "Authorization: Bearer $MONICA_TOKEN"
```

```javascript
const response = await fetch('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', {
  headers: { Authorization: `Bearer ${token}` },
})
const data = await response.json()
```

```php
$response = Http::withToken($token)->get('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e');
```

## Request with QUERY

```sh
curl https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e \
  -X QUERY \
  -H "Authorization: Bearer $MONICA_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
      "from": "3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b",
      "to": "8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e"
  }'
```

```javascript
const response = await fetch('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', {
  method: 'QUERY',
  headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
  body: JSON.stringify({
    from: '3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b',
    to: '8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e',
  }),
})
const data = await response.json()
```

```php
$response = Http::withToken($token)->query('https://my.monicahq.com/api/v1/vaults/9f1c2b7a-4e3d-4a6b-8c5d-2e7f1a9b3c4d/record-kinds/5b8e2f1a-7c3d-4e9a-b6f0-1d2c3e4f5a6b/template-change-preview?from=3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b&to=8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e', [
    'from' => '3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b',
    'to' => '8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e',
]);
```

## Response

`200`

```json
{
    "data": {
        "type": "template_change_preview",
        "attributes": {
            "from": "3e7a1c9b-2d4f-4a6e-8b0c-5f9d1e3a7c2b",
            "to": "8b3c2d1e-4f5a-4b6c-9d7e-0f1a2b3c4d5e",
            "to_version": 3,
            "counts": {
                "ready": 3,
                "unchanged": 1,
                "set_aside": 2,
                "restored": 1,
                "reminders_paused": 1,
                "reminders_resumed": 1
            },
            "set_aside": [
                {
                    "label": "Stable name",
                    "section": "Stable",
                    "records": 2
                }
            ],
            "restored": [
                {
                    "label": "Microchip number",
                    "section": "Identification",
                    "records": 1
                }
            ],
            "reminders_paused": [
                {
                    "name": "Farrier visit",
                    "records": 1
                }
            ],
            "reminders_resumed": [
                {
                    "name": "Microchip renewal",
                    "records": 1
                }
            ],
            "blocked": [
                "2f9c4b7e-1a3d-4e6f-8b0c-7d5a9e1f3c2b"
            ],
            "hidden_count": 0
        }
    }
}
```
