Skip to content

Add a personal setting

Rijnland’s accountants want an email for each invoice that passes its due date; the warehouse would rather get none. That is a choice each person makes for themselves, so it belongs on their account page, and the fields the page shows come from a resource like any other. This guide adds it to the ERP example (tests/apps/erp) in four steps: a table, a resource, roles and a tab.

The table is the app’s own, keyed by the person’s user id. With Better Auth the id is text, and a reference to auth."user" removes the row with its person. Each choice is a column with its default:

db/migrations/021_notification_settings.sql
-- Each person's choice of notification emails, one row per person, keyed by their Better Auth id. Someone who never
-- saved a choice has no row: read the default for them.
create table core.notification_settings (
user_id text primary key references auth."user" (id) on delete cascade,
overdue_invoices text not null default 'each' check (overdue_invoices in ('each', 'daily', 'never'))
);

Apply it with bun run db:migrate. A person has no row until they first save; the default stands for them until then.

The resource names the table, its fields and their defaults, and makes the key its owner with .owner(...), so each person has one record of their own. The view gives the field its label, help and the words for its values, and keeps the resource out of the sidebar:

config/notification_settings/data.ts
import { f, resource } from '@protobase/schema'
import { erpAccess } from '../roles'
/** core.notification_settings: each person's own, shown by the account page's Notifications tab. */
export const notificationSettings = resource('notification_settings')
.table('core.notification_settings')
.fields({
user_id: f.text(),
overdue_invoices: f.enum(['each', 'daily', 'never']).default('each'),
})
.primaryKey((r) => r.user_id)
.owner((r) => r.user_id)
.access(erpAccess('notification_settings'))
config/notification_settings/ui.ts
import { view } from '@protobase/schema'
import type { notificationSettings } from './data'
export const notificationSettingsView = view<typeof notificationSettings>('notification_settings')
.names({ singular: 'Notification setting', plural: 'Notification settings' })
// Each person changes their own on the account page.
.nav({ hidden: true })
.fields((r) => ({
overdue_invoices: r.overdue_invoices
.label('Overdue invoices')
.help('An email when an invoice passes its due date unpaid.')
.valueLabels({ each: 'For each invoice', daily: 'Once a day', never: 'Never' }),
}))

Export both from config/index.ts, as every resource is.

Give each role that should have the setting .read.own, .create.own and .update.own on the resource, in config/roles.ts:

const ownNotificationSettings = ['notification_settings.read.own', 'notification_settings.create.own', 'notification_settings.update.own'] as const
export const roles = defineRoles<ErpName>({
admin: ['*'],
auditor: ['*.read', ...ownNotificationSettings],
sales: [...ownNotificationSettings, 'orders.read.own', ...],
// the other roles alike: manager holds them already through '*.read', '*.create' and '*.update', and the
// integration role, which is no person, has none
})

A role that may read but not write sees the choice without a control to change it; a role that may not read has no Notifications tab. Once a Settings block shows the resource, every caller reads and writes only their own record, an admin holding * included, in the block and in the API alike.

Export a page named account to replace the standard one: the standard tabs from accountTabs, in the order to show them, and a tab of your own with a Settings block on the resource:

config/account.tsx
/** @jsxImportSource @protobase/layout */
import { Page, Settings, Tab, Tabs, accountTabs, page } from '@protobase/layout'
/** The standard account page with a Notifications tab, where each person picks the emails they get. */
export const account = page(
'account',
<Page title="Account">
<Tabs>
{accountTabs.profile}
{accountTabs.security}
<Tab name="notifications" label="Notifications">
<Settings resource="notification_settings" title="Emails" description="What the ERP emails you about." />
</Tab>
{accountTabs.devices}
{accountTabs.apps}
</Tabs>
</Page>,
)

Export it from config/index.ts too. Open Account in the user menu and then Notifications, at /-/account/notifications: the choice shows as a select with its label and help, set to For each invoice until it changes. Picking another value saves it at once; the first save creates the person’s row.

Code of your own, such as a job that sends the emails, reads the choice with SQL. Someone who never saved has no row, so read the default for them:

select u.email
from auth."user" u
left join core.notification_settings s on s.user_id = u.id
where coalesce(s.overdue_invoices, 'each') = 'each'

The Settings block has its props, the field types it draws and the checks it runs at startup.