feat(notifications): email notification preferences (#747)

* feat(notifications): add email notification preferences

Implement the email notification preferences PRD with org-level defaults
and personal opt-outs enforced at the email enqueue chokepoint.

- Add toggle map, profile.settings jsonb, and shouldSendEmail resolver
- Extend profile and organization update APIs with emailNotifications
- Pass preference context through all toggleable email send call sites
- Add Settings > Notifications page with personal and org admin sections

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* feat(notifications): group email toggles into course, cohort, and enrollment sections

Organize personal and org notification settings into scannable sections
so users can find course- or cohort-related emails quickly.

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(notifications): use Exercise instead of Quiz in toggle copy

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(notifications): only mark certification email sent when enqueued

Skip setMemberCertificationEmailSent when notification preferences or
missing Redis prevent the completion email from being queued, so the
email can still send if preferences change later.

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(notifications): address PR review feedback

- Add EmailPreferenceLookupCache to avoid N+1 settings lookups in batch
  processors and multi-recipient email enqueue paths
- Replace $effect store hydration in notifications settings with onMount
  and explicit sync after save
- Remove thin api email-preferences re-export; import from db queries
- Hide tutor-only personal toggles (newStudent, newSubmission) for students

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

* fix(notifications): hydrate toggles after store load and add profile settings migration

- One-time store hydration via onMount plus guarded effect when profile/org
  load after mount, without clobbering in-progress edits
- Add drizzle migration for profile.settings jsonb column

Co-authored-by: Rotimi Best <rotimi-best@users.noreply.github.com>

---------
This commit is contained in:
Rotimi Best
2026-07-13 10:33:19 +01:00
committed by GitHub
co-authored by Rotimi Best
parent 14db5363e7
commit fa060f5f6d
47 changed files with 14622 additions and 46 deletions
@@ -0,0 +1,118 @@
import {
resolveEmailDelivery,
type EmailNotificationSettings,
type PersonalEmailNotificationSettings
} from '@cio/utils/notifications';
import { getProfileByEmail, getProfileById } from '../auth/profile';
import { db } from '@db/drizzle';
import * as schema from '@db/schema';
import { eq } from 'drizzle-orm';
import type { EmailId } from '@cio/email';
export async function getOrganizationEmailNotificationSettings(
orgId: string
): Promise<EmailNotificationSettings | undefined> {
try {
const [organization] = await db
.select({ settings: schema.organization.settings })
.from(schema.organization)
.where(eq(schema.organization.id, orgId))
.limit(1);
return organization?.settings?.emailNotifications;
} catch (error) {
console.error('getOrganizationEmailNotificationSettings error:', error);
throw new Error('Failed to get organization email notification settings');
}
}
export async function getProfileEmailNotificationSettingsById(
profileId: string
): Promise<PersonalEmailNotificationSettings | undefined> {
try {
const profile = await getProfileById(profileId);
return profile?.settings?.emailNotifications;
} catch (error) {
console.error('getProfileEmailNotificationSettingsById error:', error);
throw new Error('Failed to get profile email notification settings');
}
}
export async function getProfileEmailNotificationSettingsByEmail(
email: string
): Promise<PersonalEmailNotificationSettings | undefined> {
try {
const profile = await getProfileByEmail(email);
return profile?.settings?.emailNotifications;
} catch (error) {
console.error('getProfileEmailNotificationSettingsByEmail error:', error);
throw new Error('Failed to get profile email notification settings');
}
}
export async function shouldSendEmail(input: {
emailId: EmailId;
organizationId?: string;
recipientEmail?: string;
recipientProfileId?: string;
}): Promise<boolean> {
return new EmailPreferenceLookupCache().shouldSend(input);
}
/**
* Reuses org/profile settings lookups across many recipients in batch processors.
*/
export class EmailPreferenceLookupCache {
private orgSettingsById = new Map<string, EmailNotificationSettings | undefined>();
private profileSettingsById = new Map<string, PersonalEmailNotificationSettings | undefined>();
private profileIdByEmail = new Map<string, string | undefined>();
async shouldSend(input: {
emailId: EmailId;
organizationId?: string;
recipientEmail?: string;
recipientProfileId?: string;
}): Promise<boolean> {
const orgSettings = input.organizationId ? await this.getOrganizationSettings(input.organizationId) : undefined;
let profileId = input.recipientProfileId;
if (!profileId && input.recipientEmail) {
profileId = await this.getProfileIdByEmail(input.recipientEmail);
}
const personalSettings = profileId ? await this.getProfileSettings(profileId) : undefined;
return resolveEmailDelivery(input.emailId, orgSettings, personalSettings);
}
private async getOrganizationSettings(orgId: string): Promise<EmailNotificationSettings | undefined> {
if (!this.orgSettingsById.has(orgId)) {
const settings = await getOrganizationEmailNotificationSettings(orgId);
this.orgSettingsById.set(orgId, settings);
}
return this.orgSettingsById.get(orgId);
}
private async getProfileIdByEmail(email: string): Promise<string | undefined> {
if (!this.profileIdByEmail.has(email)) {
const profile = await getProfileByEmail(email);
this.profileIdByEmail.set(email, profile?.id);
}
return this.profileIdByEmail.get(email);
}
private async getProfileSettings(profileId: string): Promise<PersonalEmailNotificationSettings | undefined> {
if (!this.profileSettingsById.has(profileId)) {
const settings = await getProfileEmailNotificationSettingsById(profileId);
this.profileSettingsById.set(profileId, settings);
}
return this.profileSettingsById.get(profileId);
}
}
@@ -0,0 +1 @@
export * from './email-preferences';