Skip to main content

Delete Implement

Remove an implement from the active catalog. This endpoint performs a soft delete by setting the implement status to inactive rather than permanently removing it from the database.
Authentication Required: This endpoint requires a valid JWT Bearer token with administrator privileges (role_id: 1).

Endpoint

DELETE /api/implements/{id}

Path Parameters

id
integer
required
The unique identifier of the implement to delete

Authentication

Include your JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>

Response

success
boolean
Indicates if the deletion was successful
message
string
Success or error message
data
object
The updated implement object with status: inactive

Examples

curl -X DELETE http://localhost:4000/api/implements/5 \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Response Example (200 OK)

{
  "success": true,
  "message": "Implemento eliminado exitosamente",
  "data": {
    "implement_id": 5,
    "implement_name": "5-row seeder",
    "brand": "Semeato",
    "power_requirement_hp": 40,
    "working_width_m": 1.5,
    "soil_type": "Loam",
    "working_depth_cm": 10,
    "weight_kg": 450,
    "implement_type": "seeder",
    "status": "inactive",
    "registration_date": "2026-02-01T09:00:00.000Z"
  }
}
Soft Delete: Notice that the implement’s status is changed to inactive. The record remains in the database.

Error Responses

Invalid ID (400)

{
  "success": false,
  "message": "ID de implemento inválido"
}

Unauthorized - No Token (401)

{
  "success": false,
  "message": "Token no proporcionado"
}

Unauthorized - Invalid Token (401)

{
  "success": false,
  "message": "Token inválido o expirado"
}

Forbidden - Not Admin (403)

{
  "success": false,
  "message": "Acceso denegado: se requiere rol de administrador"
}

Not Found (404)

{
  "success": false,
  "message": "Implemento no encontrado"
}

Internal Server Error (500)

{
  "success": false,
  "message": "Error al eliminar implemento"
}

Soft Delete Implementation

This endpoint implements a soft delete strategy:

What Happens

  1. The implement is located in the database by ID
  2. If found, its status field is updated to inactive
  3. The implement remains in the database with all data intact
  4. The updated implement object (with status: inactive) is returned

Controller Implementation

From src/controllers/implementController.js:269:
export const deleteImplement = asyncHandler(async (req, res) => {
  const id = parseInt(req.params.id, 10);

  if (Number.isNaN(id) || id <= 0) {
    return res.status(400).json({
      success: false,
      message: 'ID de implemento inválido',
    });
  }

  const existing = await Implement.findById(id);

  if (!existing) {
    return res.status(404).json({
      success: false,
      message: 'Implemento no encontrado',
    });
  }

  // Soft delete - cambiar status a 'inactive'
  const updated = await Implement.update(id, { status: 'inactive' });

  return res.json({
    success: true,
    message: 'Implemento eliminado exitosamente',
    data: updated,
  });
});

Why Soft Delete?

Soft deletion is used instead of hard deletion because:
  1. Data Preservation: Maintains historical records and referential integrity
  2. Audit Trail: Allows tracking of when and why equipment was removed
  3. Reversibility: Implements can be reactivated by updating status back to available
  4. Reference Safety: Existing calculations or recommendations referencing the implement remain valid

Reactivating a Deleted Implement

To reactivate a soft-deleted implement, use the Update Implement endpoint:
curl -X PUT http://localhost:4000/api/implements/5 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer {token}" \
  -d '{"status": "available"}'
This changes the status from inactive back to available, making the implement visible in catalog queries again.

Impact on Other Endpoints

List All Implements

Inactive implements are included in results from GET /api/implements (unless filtered).

Get Available Implements

Inactive implements are excluded from GET /api/implements/available results, which only returns implements with status: available.

Search Implements

The search endpoint (GET /api/implements/search) includes inactive implements unless you filter by status.

Power Calculations

Inactive implements may still appear in historical calculation results but should not be used for new calculations.

Implementation Notes

  • Source: src/routes/implement.routes.js:492 - Route definition
  • Controller: src/controllers/implementController.js:269 - deleteImplement function
  • Actual Operation: Updates status field via Implement.update(id, { status: 'inactive' })
  • SQL: No DELETE statement is executed; uses UPDATE instead
  • Middleware Chain:
    1. verifyTokenMiddleware - Validates JWT token
    2. isAdmin - Checks for admin role (role_id: 1)
    3. deleteImplement - Sets status to inactive
  • Cache Invalidation: Invalidates both *implements* and *recommendations* cache patterns
Deleting implements will invalidate recommendation caches, as existing recommendations may reference the deleted implement.

Permanent Deletion

If you need to permanently remove an implement from the database (hard delete), you would need to:
  1. Connect directly to the PostgreSQL database
  2. Execute: DELETE FROM implement WHERE implement_id = {id}
  3. Be aware that this may cause foreign key constraint violations if the implement is referenced elsewhere
Hard deletion is not exposed via the API for data safety reasons.

Alternative: Maintenance Status

Instead of deleting an implement, consider setting it to maintenance status:
curl -X PUT http://localhost:4000/api/implements/{id} \
  -H "Authorization: Bearer {token}" \
  -d '{"status": "maintenance"}'
This indicates the implement is temporarily unavailable but will return to service.

Status Lifecycle

available → maintenance → available

 inactive → available (via update)
  • available: Active and ready for use
  • maintenance: Temporarily unavailable
  • inactive: Soft-deleted (can be reactivated)

List Implements

View all implements (including inactive)

Get Implement

Retrieve implement details

Create Implement

Add a new implement (Admin only)

Update Implement

Reactivate or modify an implement (Admin only)
Deleted (inactive) implements can be filtered out in list queries by using the search endpoint with appropriate status filters or by using the /api/implements/available endpoint.

Build docs developers (and LLMs) love