# Prevent record update

This example shows you how to prevent updating records based on specific criteria.\
Here, shipped orders should not be editable. We will notify the user with a customized error message.

![](http://g.recordit.co/3PB5z1WoeQ.gif)

## Requirements

* An admin backend running on forest-express-sequelize

## How it works

### Directory: /models

This directory contains the `orders.js` file where the model is declared.

{% code title="/models/order.js" %}

```javascript
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  const Orders = sequelize.define('orders', {
    shippingStatus: {
      type: DataTypes.STRING,
    },
    ...
  }, {
    tableName: 'orders',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA,
  });

  Orders.associate = (models) => {
    ...
  };

  return Orders;
};

```

{% endcode %}

### Directory: /routes

This directory contains the `orders.js` file where the routes are declared.

We override the update route so it sends an error as a response when the `shippingStatus` is either `Shipped` or `In transit`. Otherwise, it triggers the default logic with `next()`.

{% code title="/routes/orders.js" %}

```javascript
const express = require('express');
const { PermissionMiddlewareCreator, RecordsGetter } = require('forest-express-sequelize');
const { orders } = require('../models');

const router = express.Router();
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('orders');

...

// Update a Order
router.put('/orders/:recordId', permissionMiddlewareCreator.update(), (request, response, next) => {
  return new RecordGetter(orders, request.user, request.query).get(request.params.recordId)
    .then((order) => {
      if (order.shippingStatus === 'Shipped' || order.shippingStatus === 'In transit') {
        response.status(403).send('Sorry, an order cannot be modified once shipped!');
      } else {
        next();
      }
    });
});

...

module.exports = router;
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/woodshop/how-tos/prevent-record-update-based-on-specific-criterias.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
