File size: 4,096 Bytes
5ae7c8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# N8N Setup Guide for Food Waste Assistant

This guide helps you set up the backend logic using n8n. You will need to create a new workflow (or multiple) to handle the 4 main API endpoints.

## Prerequisites
- An active [n8n](https://n8n.io/) instance (Cloud or Self-hosted).
- A database (Google Sheets, Airtable, or Postgres) to store inventory. This guide assumes **Google Sheets** for simplicity.

## Quick Start (Import Workflow)
I have updated `workflow.json` to be much simpler. It now uses a **Single Webhook URL** that handles everything (`add_item`, `get_items`, etc.).

1. Download the updated `workflow.json` from this project.
2. Go to your n8n dashboard.
3. Click **Workflows** > **Import from File**.
4. Select `workflow.json`.
5. **Activate** the workflow.
6. Open the "Webhook (Router)" node:
   - Copy the **Production URL**.
   - **CRITICAL**: Set **Respond** to **"Using 'Respond to Webhook' Node"**.
   - **If you forget this, you will get "Unexpected End of JSON Input" error!**
7. Paste the URL into your app's Settings.

## 🚀 Making It Real (Google Sheets)

To save data permanently, use `workflow_real.json` instead.

### 1. Prepare Google Sheet
Create a new Sheet driven by these columns in Row 1:
`id` | `name` | `quantity` | `expiryDate` | `category`

### 2. Import Real Workflow
1. Import `workflow_real.json` into N8N.
2. Double-click **Google Sheets (Add)** and **Google Sheets (Read)** nodes.
3. Authenticate with your Google Account ("Sign in with Google").
4. Select your Spreadsheet and Sheet Name.
   - For **Add Node**: Map the fields (`name` -> `{{$json.body.name}}`, etc).
5. Activate and copy the new Webhook URL to your app settings.

## Workflow Details
Below are the details if you want to build it manually or understand how it works.

Create a workflow with a **Webhook** node.
- **Method**: `POST` (and `GET` if you want a single entry point, but easier to use distinct hooks).
- **Authentication**: None (for this demo) or Header Auth.

### Endpoint 1: Add Item
1. **Webhook Node**: Listen for `POST` on `/add-item`.
2. **AI Node (Optional)**: Use an OpenAI node to predict expiry date based on the "Name" if "ExpiryDate" is empty.
3. **Google Sheets Node**: "Append" row to Sheet "Inventory".
   - Map: `Name`, `Quantity`, `ExpiryDate`, `Category`.
4. **Respond to Webhook Node**: Return `{ "success": true }`.

### Endpoint 2: Get Inventory
1. **Webhook Node**: Listen for `GET` on `/get-items`.
2. **Google Sheets Node**: "Read" all rows from Sheet "Inventory".
3. **Code Node**: Calculate `daysRemaining` for each item.
4. **Respond to Webhook Node**: Return JSON `{ "items": [...] }`.

### Endpoint 3: Dashboard Stats
1. **Webhook Node**: Listen for `GET` on `/dashboard-stats`.
2. **Google Sheets Node**: Read all rows.
3. **Code Node**:
   - Count total items.
   - Filter items where `expiryDate` is within 3 days.
   - Calculate potential waste savings.
4. **Respond to Webhook Node**: Return Stats JSON.

### Endpoint 4: Suggest Recipes
1. **Webhook Node**: Listen for `POST` on `/suggest-recipes`.
2. **Google Sheets Node**: Read "Inventory" (find items expiring soon).
3. **OpenAI Node** (or other LLM):
   - **System Prompt**: "You are a chef. Suggest 3 recipes based on these ingredients: [List of ingredients]. Return valid JSON."
4. **Respond to Webhook Node**: Return the JSON list of recipes.

## Connecting to Frontend
1. After activating your workflow, copy the **Production URL** of your Webhook nodes.
2. If you used different URLs for each endpoint, you might need to adjust `app.js` or use a single Router workflow (recommended).
   - **Router Approach**: Have one Webhook URL and route based on `body.action` or query param?
   - **Current App Support**: The `app.js` assumes a base URL structure:
     - `BASE_URL/add-item`
     - `BASE_URL/get-items`
     - etc.
   - **Tip**: In n8n, you can set the path for the Webhook node. Ensure you set 4 separate Webhook nodes with these specific suffixes, or use a Reverse Proxy to route them.