Supabase Edge Functions handle server-side logic in CHRIS.
Overview
Edge Functions are located in supabase/functions/ and run on Deno Deploy.
| Function | Purpose |
|---|---|
| create-employee | Create new employee with auth user |
| send-notification-email | Send emails via SMTP |
| reset-employee-password | Password reset functionality |
create-employee
Creates a new employee by setting up both the auth user and profile.
Endpoint
POST /functions/v1/create-employee
Headers
Authorization: Bearer <ADMIN_JWT_TOKEN>
Content-Type: application/json
Request Body
{
"email": "john.doe@company.com",
"full_name": "John Doe",
"role": "employee",
"team_id": "optional-team-uuid",
"employment_start_date": "2025-02-01",
"annual_entitlement_days": 20
}
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
| string | Yes | Unique email address | |
| full_name | string | Yes | Employee's full name |
| role | string | Yes | admin, hr_manager, employee |
| team_id | uuid | No | Team assignment |
| employment_start_date | date | No | Start date |
| annual_entitlement_days | integer | No | Leave days (default: 20) |
Response
Success (201):
{
"success": true,
"user_id": "new-user-uuid",
"message": "Employee created successfully"
}
Error (400):
{
"success": false,
"error": "Email already exists"
}
Behavior
- Validates admin JWT token
- Creates user in Supabase Auth
- Creates profile record
- Assigns role in user_roles
- Sends welcome email (if SMTP configured)
send-notification-email
Sends emails using SMTP settings from company_settings.
Endpoint
POST /functions/v1/send-notification-email
Headers
Authorization: Bearer <JWT_TOKEN>
Content-Type: application/json
Request Body
{
"to": ["recipient@company.com"],
"subject": "Leave Request Approved",
"html": "<h1>Your leave has been approved</h1>",
"text": "Your leave has been approved"
}
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
| to | string[] | Yes | Recipient email addresses |
| subject | string | Yes | Email subject line |
| html | string | Yes | HTML email body |
| text | string | No | Plain text fallback |
Response
Success (200):
{
"success": true,
"message": "Email sent successfully"
}
Error (500):
{
"success": false,
"error": "SMTP connection failed"
}
Behavior
- Validates JWT token
- Retrieves SMTP settings from company_settings
- Checks if email rerouting is enabled
- Sends email via configured SMTP server
- Returns success/failure status
Email Rerouting
When email_reroute_enabled is true in company_settings:
- All emails go to
email_reroute_addressesinstead - Original recipients are ignored
- Useful for testing
reset-employee-password
Initiates password reset for an employee.
Endpoint
POST /functions/v1/reset-employee-password
Headers
Authorization: Bearer <ADMIN_JWT_TOKEN>
Content-Type: application/json
Request Body
{
"user_id": "employee-user-uuid"
}
Parameters
| Field | Type | Required | Description |
|---|---|---|---|
| user_id | uuid | Yes | Employee's user ID |
Response
Success (200):
{
"success": true,
"message": "Password reset email sent"
}
Error (403):
{
"success": false,
"error": "Admin access required"
}
Behavior
- Validates admin JWT token
- Looks up employee email
- Generates password reset link
- Sends reset email to employee
Calling from Frontend
Use the Supabase client to invoke Edge Functions:
import { supabase } from '@/integrations/supabase/client';
// Create employee
const { data, error } = await supabase.functions.invoke('create-employee', {
body: {
email: 'new.employee@company.com',
full_name: 'New Employee',
role: 'employee'
}
});
// Send notification
const { data, error } = await supabase.functions.invoke('send-notification-email', {
body: {
to: ['manager@company.com'],
subject: 'New Leave Request',
html: '<p>A new leave request requires your approval.</p>'
}
});
// Reset password
const { data, error } = await supabase.functions.invoke('reset-employee-password', {
body: {
user_id: 'employee-uuid'
}
});
Development
Local Development
# Start Supabase locally
npx supabase start
# Serve functions locally
npx supabase functions serve
Deploy Functions
# Deploy all functions
npx supabase functions deploy
# Deploy specific function
npx supabase functions deploy send-notification-email
View Logs
In Supabase Dashboard:
- Go to Edge Functions
- Select the function
- Click Logs tab
Or via CLI:
npx supabase functions logs send-notification-email
Environment Variables
Edge Functions can access these secrets:
| Variable | Description |
|---|---|
| SUPABASE_URL | Project URL |
| SUPABASE_ANON_KEY | Anonymous key |
| SUPABASE_SERVICE_ROLE_KEY | Service role key |
Set secrets via dashboard or CLI:
npx supabase secrets set MY_SECRET=value
Error Handling
All functions follow consistent error responses:
{
"success": false,
"error": "Error message here",
"details": "Optional additional details"
}
HTTP status codes:
| Code | Meaning |
|---|---|
| 200 | Success |
| 201 | Created |
| 400 | Bad request (invalid input) |
| 401 | Unauthorized (invalid token) |
| 403 | Forbidden (insufficient permissions) |
| 500 | Server error |