# Locations

Audience: Programmer

Locations, Devices, and User Management

## Overview

pyck provides a location and device management system within the Management Service. It maps the physical warehouse layout, tracks devices, lets users check in and out, and associates inventory repositories with specific locations.

## Core entities

### Location

Represents a physical area within the warehouse (e.g., workstation, zone, dock).

| Field | Type | Description |
|-------|------|-------------|
| `id` | UUID | Unique identifier |
| `name` | String | Location name (unique per tenant) |
| `tenantID` | UUID | Owning tenant |
| `dataTypeID` | UUID | Optional custom `DataType` |
| `data` | Map | Custom fields defined by `DataType` |

### Device

Represents a physical device (scanner, tablet, workstation terminal).

| Field | Type | Description |
|-------|------|-------------|
| `id` | UUID | Unique identifier |
| `name` | String | Device name (unique per tenant) |
| `tenantID` | UUID | Owning tenant |
| `dataTypeID` | UUID | Optional custom `DataType` |
| `data` | Map | Custom fields defined by `DataType` |

### `DeviceLocation`

Associates a device with a location. A device can only be assigned to one location at a time.

| Field | Type | Description |
|-------|------|-------------|
| `id` | UUID | Unique identifier |
| `deviceID` | UUID | Associated device |
| `locationID` | UUID | Associated location |

### `DeviceUser`

Tracks which user is currently checked-in to a device. Only one user can be checked-in to a device at a time.

| Field | Type | Description |
|-------|------|-------------|
| `id` | UUID | Unique identifier |
| `deviceID` | UUID | Associated device |
| `userID` | UUID | Checked-in user |

## Entity relationships

```text
Location ──────────────────────────────────────────┐
    │                                              │
    ▼                                              │
DeviceLocation ◄─── Device ◄─── DeviceUser ───► User
                        │
                        │
                        ▼
              InventoryRepository
                 (location_id)
```

Key relationships:

- **Location → DeviceLocation**: One-to-many (a location can have multiple devices)
- **Device → DeviceLocation**: One-to-one (a device is at one location)
- **Device → DeviceUser**: One-to-one (only one user per device)
- **User → DeviceUser**: One-to-many (a user can have check-in history)
- **Location → Repository**: One-to-many (repositories can reference a location)

## GraphQL operations

### Creating a location

```graphql
mutation CreateLocation($input: CreateLocationInput!) {
  createLocation(input: $input) {
    location {
      id
      name
      data
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "name": "Workstation-A1"
  }
}
```

### Creating a device

```graphql
mutation CreateDevice($input: CreateDeviceInput!) {
  createDevice(input: $input) {
    device {
      id
      name
      data
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "name": "Scanner-001"
  }
}
```

### Assigning a device to a location

```graphql
mutation SetDeviceLocation($input: CreateDeviceLocationInput!) {
  setDeviceLocation(input: $input) {
    DeviceLocation {
      id
      deviceID
      locationID
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "deviceID": "device-uuid-here",
    "locationID": "location-uuid-here"
  }
}
```

To remove a device from its location:

```graphql
mutation UnsetDeviceLocation($id: ID!) {
  unsetDeviceLocation(id: $id) {
    deletedID
  }
}
```

### User check-in

When a user starts working at a device:

```graphql
mutation CheckInUserDevice($input: CheckInUserDeviceInput!) {
  checkInUserDevice(input: $input) {
    deviceUser {
      id
      deviceID
      userID
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "deviceID": "device-uuid-here"
  }
}
```

The `userID` is automatically derived from the authenticated user's token.

### User check-out

When a user finishes working at a device:

```graphql
mutation CheckOutUserDevice($input: CheckOutUserDeviceInput!) {
  checkOutUserDevice(input: $input) {
    deviceUser {
      id
      deviceID
      userID
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "deviceID": "device-uuid-here"
  }
}
```

### Assigning a location to a repository

When creating or updating an inventory repository, you can associate it with a management location:

```graphql
mutation CreateInventoryRepository($input: CreateRepositoryInput!) {
  createInventoryRepository(input: $input) {
    inventoryRepository {
      id
      name
      locationID
    }
  }
}
```

**Variables:**

```json
{
  "input": {
    "name": "Shelf-A1-001",
    "type": "static",
    "locationID": "location-uuid-here"
  }
}
```
