# qualifyingRestPairs

Industry: Workforce

## Signature

```ts
qualifyingRestPairs(log: DutyEntry[], spec: { statuses: string[]; minimum: string; pair: PairSpec; }): RestPair[]
```

Return every paired rest in a duty log: two separate rests that together count as one, because they meet all three floors of `spec.pair`.

- A pair is three constraints, not two integers: with `{ eachAtLeast: "PT2H", oneAtLeast: "PT7H", combinedAtLeast: "PT10H" }`, 7.5 + 2.5 hours and 8 + 2 qualify, and 6 + 4 does not.
- Rests are runs of touching entries in `spec.statuses`. Two rests in a row pair; either may be the longer.
- A rest of `spec.minimum` or more is a full rest on its own: it pairs with nothing, and no pair spans it. A rest shorter than `eachAtLeast` is neither half of a pair nor a break between two.
- Pairs chain: rests of 7, 3 and 7 hours give two pairs, (7, 3) and (3, 7).
- Intervals are UTC instants ending in "Z", in log order.
- `[]` is both a legitimate result and the sentinel: returned when the log fails `isValidDutyLog`, when `spec` is not an object, when `statuses` is not an array of strings, and when `minimum` or a floor of `pair` is not a positive ISO 8601 duration of exact time.

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| `log` | `DutyEntry[]` | the duty log, ordered and non-overlapping |

## Options

**spec**

the statuses a rest is made of, the length of a full rest, and the pair's floors

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `statuses` | `string[]` | Required | The statuses a rest is made of. Touching entries in any of them are one rest. |
| `minimum` | `string` | Required | The length of a full rest, a positive ISO 8601 duration of exact time (`"PT10H"`). A rest this long or longer is not half of a pair. |
| `pair` | [`PairSpec`](/gmt/reference/types/PairSpec/) | Required | The three floors the two rests must meet. |

## Returns

the pairs as `{ first, second }` records, or [] on invalid input

## Types

### RestPair

Two rest periods that satisfy a `PairSpec`, in the order they were taken.

```ts
import type { RestPair } from "@northguild/gmt";
```

| Member | Type | Description |
| --- | --- | --- |
| `first` | [`Interval`](/gmt/reference/types/Interval/) | The earlier period, as UTC instants. |
| `second` | [`Interval`](/gmt/reference/types/Interval/) | The later period, as UTC instants. |

## Related types

- [`DutyEntry`](/gmt/reference/types/DutyEntry/)
- [`PairSpec`](/gmt/reference/types/PairSpec/)
- [`Interval`](/gmt/reference/types/Interval/)

## Examples

```ts
import { qualifyingRestPairs } from "@northguild/gmt/workforce/calculate";

qualifyingRestPairs([{ status: "sleeper", start: "2024-01-01T00:00:00Z", end: "2024-01-01T07:30:00Z" }, { status: "work", start: "2024-01-01T07:30:00Z", end: "2024-01-01T11:30:00Z" }, { status: "off", start: "2024-01-01T11:30:00Z", end: "2024-01-01T14:00:00Z" }], { statuses: ["off", "sleeper"], minimum: "PT10H", pair: { eachAtLeast: "PT2H", oneAtLeast: "PT7H", combinedAtLeast: "PT10H" } }) // [{ first: { start: "2024-01-01T00:00:00Z", end: "2024-01-01T07:30:00Z" }, second: { start: "2024-01-01T11:30:00Z", end: "2024-01-01T14:00:00Z" } }]
```

## Source

[workforce/calculate/qualifyingRestPairs.ts](https://github.com/northguild/gmt/blob/main/packages/gmt/src/workforce/calculate/qualifyingRestPairs.ts)
