> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bludia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Build a CRM

> End-to-end guide: build a sales CRM backend on Bludia with companies, contacts, deals, and activities collections, relations, a staff login guard, sales_rep and sales_manager roles, row-level security, a validation hook, and a pipeline report.

You'll build the backend for a small sales CRM where reps manage their own deals and managers see everything.

> [card] What you'll build
>
> ```text theme={null}
> companies ──< contacts
>     │
>     └──< deals >── users (owner rep)
>             │
>             └──< activities
> Guard: users (login: email)   Roles: sales_rep, sales_manager
> ```

**1. Create the project**

Create a project named **CRM**. (Or describe it to the AI builder: *"A CRM with companies, contacts, deals with stages, and activities"*, then adjust.)

**2. Create collections and fields**

> [tab] users
>
> | Field | Type | Rules |
> | :- | :- | :- |
> | `email` | Text | required, unique, format `email` |
> | `full_name` | Text | required |
> | `title` | Text | |

> [tab] companies
>
> | Field | Type | Rules |
> | :- | :- | :- |
> | `name` | Text | required, unique |
> | `industry` | Select | SaaS, Retail, Manufacturing, Other |
> | `website` | Text | format `url` |

> [tab] contacts
>
> | Field | Type | Rules |
> | :- | :- | :- |
> | `full_name` | Text | required |
> | `email` | Text | format `email` |
> | `phone` | Text | format `phone` |

> [tab] deals
>
> | Field | Type | Rules |
> | :- | :- | :- |
> | `title` | Text | required |
> | `value` | Number | min `0`, scale `2` |
> | `stage` | Select | lead, qualified, proposal, won, lost |
> | `expected_close` | Date | |
> | `closed_reason` | Long Text | `required_if` stage = lost |
> | `closed_on` | Date | set by a hook |

> [tab] activities
>
> | Field | Type | Rules |
> | :- | :- | :- |
> | `type` | Select | call, email, meeting, note |
> | `summary` | Long Text | required |
> | `happened_at` | DateTime | required |

**3. Add relations**

| Type | Source | Target | Label |
| :- | :- | :- | :- |
| `one_to_many` | companies | contacts | `name` |
| `one_to_many` | companies | deals | `name` |
| `one_to_many` | users | deals | `full_name` (deal owner) |
| `one_to_many` | deals | activities | `title` |

**4. Add a login guard**

Create a guard on `users` with login field `email`. Turn **registration off**: you'll add staff yourself in the dashboard. See [Guards](https://docs.bludia.com/authentication/guards.md).

**5. Create roles and permissions**

| Collection | `sales_rep` | `sales_manager` |
| :- | :- | :- |
| `companies`, `contacts` | read, create, update | read, create, update, delete |
| `deals` | read, create, update | read, create, update, delete, export |
| `activities` | read, create | read, create, update, delete |
| `users` | read | read, update |

**6. Add row-level security**

* **sales\_rep** on `deals` and `activities`: default owner behavior. Reps see records they created.
* **sales\_rep** on `companies`, `contacts`, `users`: allow `field_equals` on a field every record has, so reps see the shared directory.
* **sales\_manager**: allow policies covering every record, so managers see the whole pipeline.

See [Row-level security](https://docs.bludia.com/authorization/row-level-security.md).

**7. Add a hook**

`beforeUpdate` on `deals`: stamp the close date when a deal is won or lost. (Add a `closed_on` Date field first.)

```go theme={null}
package main

import (
	"time"

	"bludia"
)

func main() {
	ctx := bludia.GetContext()
	rec, _ := ctx.Record()

	stage, _ := rec["stage"].(string)
	if stage == "won" || stage == "lost" {
		rec["closed_on"] = time.Now().UTC().Format("2006-01-02")
		ctx.SetRecord(rec)
	}
}
```

**8. Build a pipeline report**

Report on `deals`: dimension `stage`, metrics `count` and `sum(value)`. Add a dashboard with stats *Open pipeline value* and *Deals won this month*.

**9. Connect the frontend**

Create an `end_users` API key with your app's origin. Reps log in with `POST /auth/users/login` and load their deals:

```javascript theme={null}
const { data } = await records.list("deals", {
  filters: { stage: ["lead", "qualified", "proposal"] },
  with: ["activities"],
  sort: "updated_at",
  order: "desc",
});
```

See the [JavaScript client](https://docs.bludia.com/frontend/javascript-client.md).

> [!TIP]
>
> You have a multi-user CRM backend with validation, access control, business rules, and reporting.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.