# `flag.tenant_read`

A read of a flag decided by tenant names no tenant, or a read of a flag decided by user names one.

Area 16, [Built-in services](/docs/built-in-services.md); not declinable; the next step is usually the agent's.

## Why Jadpo refuses it

A flag with `by: FlagKey.tenant` is turned on for companies rather than
for people, so everyone in a company sees the same thing. A request
carries no company of its own: a caller can belong to several, and a
subscriber or a schedule has no caller at all. So each read of such a
flag says which company it is for, and Jadpo can't guess it.

A flag decided by user, the default, is worked out for whoever is
signed in. A company named in its read would never be looked at, so
the read would say something the flag doesn't do.

## What it looks like

```jadpo
type Handle = Text {
    max_length: 40
}

type CompanyRole = Enum {
    owner
}

entity User {
    id: Uuid
    handle: Handle {
        unique: true
    }

    policy: {
        tenant: Tenancy.shared
    }
}

principal {
    user: {
        subject: User.handle
    }
}

entity Company {
    id: Uuid
}

entity CompanyMembership {
    id: Uuid
    company_id: Company.id {
        on_delete: OnDelete.cascade
    }
    user_id: User.id {
        on_delete: OnDelete.cascade
    }
    role: CompanyRole

    membership: {
        scope: company_id
        member: user_id
        role: role
    }
}

policy {
    tenant: Company
}

flags {
    new_invoices: Bool {
        default: false
        owner: "billing"
        expires: "2099-01-31"
        by: FlagKey.tenant
    }
}

type InvoiceLayout = Enum {
    classic
    banded
}

action layout_for(company_id: Company.id) -> InvoiceLayout {
    if (flags.new_invoices) {
        return InvoiceLayout.banded
    }
    return InvoiceLayout.classic
}
```

```text
error: `flags.new_invoices` is decided by tenant, so each read names the tenant
 --> main.jadpo:66:9
   |
66 |     if (flags.new_invoices) {
   |         ^^^^^^^^^^^^^^^^^^ names no tenant
  reason: `new_invoices` says `by: FlagKey.tenant`, so each `Company` gets its own value. A request carries no tenant of its own, so a read says which one it is for, as a `Company.id`.
  next: Write `flags.new_invoices(<Company.id>)`, naming the `Company` this is for (agent)
  rule: flag.tenant_read
  help: https://jadpo.dev/docs/errors/flag.tenant_read

check failed: 1 error, 1 file
```

`(agent)` at the end of the `next:` line means your coding agent can make
the fix itself.

The other way round, a flag decided by user given a company:

```jadpo
type CheckoutSteps = Int {
    min: 1
    max: 3
}

type ShopId = Uuid {}

flags {
    new_checkout: Bool {
        default: false
        owner: "payments"
        expires: "2099-01-31"
    }
}

action checkout_steps(shop: ShopId) -> CheckoutSteps {
    if (flags.new_checkout(shop)) {
        return 1
    }
    return 3
}
```

```text
error: `flags.new_checkout` is decided by user, so a read names no tenant
 --> main.jadpo:17:9
   |
17 |     if (flags.new_checkout(shop)) {
   |         ^^^^^^^^^^^^^^^^^^^^^^^^ names a tenant
  reason: `new_checkout` has no `by:`, so it is `FlagKey.user`: its value is worked out for the signed-in caller, and a tenant named here would never be read.
  next: Read it as `flags.new_checkout`, or add `by: FlagKey.tenant` to `new_checkout` in `flags.jadpo` (agent)
  fix:
    17 - if (flags.new_checkout(shop)) {
    17 + if (flags.new_checkout) {
  rule: flag.tenant_read
  help: https://jadpo.dev/docs/errors/flag.tenant_read

check failed: 1 error, 1 file

next: jadpo fix  fixes this error for you, then checks again
```

## How to fix it

**If the flag is decided by tenant**, name the company the read is for.
Here it is the company the action was given:

```jadpo
action layout_for(company_id: Company.id) -> InvoiceLayout {
    if (flags.new_invoices(company_id)) {
        return InvoiceLayout.banded
    }
    return InvoiceLayout.classic
}
```


A subscriber names it the same way, from its event, so the background
work done for a company gets that company's value. A route's `run:`
can't pass a flag decided by tenant at all, since a `run:` argument
names no company: the action the route runs reads it.

**If the flag is decided by user**, read it without a company, as
`jadpo fix` writes it:

```jadpo
action checkout_steps(shop: ShopId) -> CheckoutSteps {
    if (flags.new_checkout) {
        return 1
    }
    return 3
}
```


If it is meant to be turned on for whole companies instead, add `by:
FlagKey.tenant` to it in `flags.jadpo`, which needs a `tenant:` in root
policy, and name the company in every read.

## Related

- [A flag for each tenant](/docs/feature-flags.md#a-flag-for-each-tenant)
- [Tenancy](/docs/tenancy.md)
