> For the complete documentation index, see [llms.txt](https://docs.pyck.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pyck.cloud/graphql-api/mutations/rebuildinventorystock.md).

# Rebuild Inventory Stock

Rebuild Inventory Stock Mutation

Audience: Programmer

Rebuilds the entire inventory stock table for the current tenant. This administrative operation recalculates all stock figures from the ground truth of executed movements, correcting any drift caused by bugs, out-of-order movement processing, or data migrations.

## Arguments

This mutation takes no arguments. It operates on the tenant determined by the request context.

## Return Type

[`RebuildStockOutput`](/graphql-api/types/rebuildstockoutput.md) - Indicates whether the stock rebuild completed successfully.

| Field     | Type       | Description                                      |
| --------- | ---------- | ------------------------------------------------ |
| `success` | `Boolean!` | Whether the stock table was successfully rebuilt |

## Authorization

{% hint style="warning" %}
This mutation requires the **admin** role. Calling it without admin privileges will return an authorization error.
{% endhint %}

## Usage Example

```graphql
mutation {
  rebuildInventoryStock {
    success
  }
}
```

### Expected Response

```json
{
  "data": {
    "rebuildInventoryStock": {
      "success": true
    }
  }
}
```

## JavaScript Example

```javascript
const REBUILD_INVENTORY_STOCK = gql`
  mutation RebuildInventoryStock {
    rebuildInventoryStock {
      success
    }
  }
`;

const [rebuildStock, { loading }] = useMutation(REBUILD_INVENTORY_STOCK);

const handleRebuildStock = async () => {
  try {
    const { data } = await rebuildStock();

    if (data.rebuildInventoryStock.success) {
      console.log('Stock table rebuilt successfully');
    }
  } catch (error) {
    console.error('Error rebuilding stock:', error);
  }
};
```

## When to Use

This mutation should be used in the following scenarios:

* **Stock drift**: When stock figures no longer match the expected totals derived from executed movements
* **Bug recovery**: After a bug has caused incorrect stock calculations that need correction
* **Out-of-order movements**: When movements were processed in an unexpected order, leading to inconsistent stock levels
* **Data migration**: After importing or migrating movement data from an external system
* **Periodic reconciliation**: As part of a scheduled maintenance routine to ensure stock accuracy

## What Happens During Execution

1. **Authorization check**: Verifies the caller holds the admin role for the current tenant
2. **Transaction**: Opens a database transaction to ensure atomicity
3. **Rebuild**: Calls the stock service to recalculate all stock records from executed movements
4. **Commit**: If successful, commits the rebuilt stock table

The operation is atomic -- it either fully completes or rolls back entirely.

## Error Cases

* **Unauthorized**: The caller does not have the admin role for the tenant
* **Internal error**: A failure occurred during the stock rebuild (the transaction is rolled back)

## Notes

* This is an admin-only operation and is not intended for regular application use
* The mutation operates within a single database transaction for consistency
* No arguments are needed because it rebuilds the entire stock table for the tenant
* The operation may take longer on tenants with large numbers of movements
* Stock records are recalculated from the source of truth (executed movements and transactions)
* Consider running this during low-traffic periods to minimize impact on concurrent operations


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.pyck.cloud/graphql-api/mutations/rebuildinventorystock.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
