* 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 <[email protected]> * 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 <[email protected]> * fix(notifications): use Exercise instead of Quiz in toggle copy Co-authored-by: Rotimi Best <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * feat(notifications): add email preferences to LMS settings Students manage personal email opt-outs from /lms/settings/notifications, matching the org settings page but scoped to the LMS navigation students use. Co-authored-by: Rotimi Best <[email protected]> * feat(notifications): store per-org personal prefs in organizationmember_email_notifications - Add organizationmember_email_notifications table keyed by organizationmember - Expose GET/PUT /organization/member/email-notifications for current org member - Resolve personal email prefs by profileId + organizationId at send time - Migrate legacy profile.settings.emailNotifications into per-membership rows - Update org and LMS settings UI to load/save org-scoped member preferences Co-authored-by: Rotimi Best <[email protected]> * fix(notifications): address PR review on locales and backfill script Move profile email-notification backfill out of the migration SQL into a dedicated db script with dry-run support. Translate personal preference description strings in all non-English locale files. Co-authored-by: Rotimi Best <[email protected]> * fix(i18n): translate notification preference strings in all locales Complete settings.notifications and settings.tabs.notifications_tab translations for da, de, es, fr, hi, pl, pt, ru, and vi. Co-authored-by: Rotimi Best <[email protected]> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Rotimi Best <[email protected]>
121 lines
3.9 KiB
TypeScript
121 lines
3.9 KiB
TypeScript
import {
|
|
DEFAULT_MEMBER_EMAIL_NOTIFICATION_ROW,
|
|
memberEmailNotificationRowToApiResponse,
|
|
memberEmailNotificationRowToPersonalSettings,
|
|
personalSettingsToMemberEmailNotificationRow,
|
|
type MemberEmailNotificationRow
|
|
} from '@cio/utils/notifications';
|
|
import type { PersonalEmailNotificationSettings } from '@cio/utils/notifications';
|
|
import type { TMemberEmailNotificationPreferencesUpdate } from '@cio/utils/validation/notifications';
|
|
|
|
import { getOrganizationMemberIdByOrgAndProfile } from '../organization/organization';
|
|
import { db } from '@db/drizzle';
|
|
import * as schema from '@db/schema';
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
function rowFromRecord(
|
|
record: typeof schema.organizationmemberEmailNotifications.$inferSelect | undefined
|
|
): MemberEmailNotificationRow | null {
|
|
if (!record) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
newStudent: record.newStudent,
|
|
newSubmission: record.newSubmission,
|
|
gradingResult: record.gradingResult,
|
|
newsfeed: record.newsfeed,
|
|
quizAssigned: record.quizAssigned,
|
|
cohortReminder: record.cohortReminder,
|
|
session: record.session,
|
|
courseCompletion: record.courseCompletion
|
|
};
|
|
}
|
|
|
|
export async function getOrganizationMemberEmailNotifications(
|
|
profileId: string,
|
|
organizationId: string
|
|
): Promise<MemberEmailNotificationRow | null> {
|
|
try {
|
|
const organizationMemberId = await getOrganizationMemberIdByOrgAndProfile(organizationId, profileId);
|
|
|
|
if (!organizationMemberId) {
|
|
return null;
|
|
}
|
|
|
|
const [record] = await db
|
|
.select()
|
|
.from(schema.organizationmemberEmailNotifications)
|
|
.where(eq(schema.organizationmemberEmailNotifications.organizationMemberId, organizationMemberId))
|
|
.limit(1);
|
|
|
|
return rowFromRecord(record);
|
|
} catch (error) {
|
|
console.error('getOrganizationMemberEmailNotifications error:', error);
|
|
throw new Error('Failed to get organization member email notification preferences');
|
|
}
|
|
}
|
|
|
|
export async function getOrganizationMemberEmailNotificationsForApi(profileId: string, organizationId: string) {
|
|
const row = await getOrganizationMemberEmailNotifications(profileId, organizationId);
|
|
|
|
return memberEmailNotificationRowToApiResponse(row);
|
|
}
|
|
|
|
export async function getPersonalEmailNotificationSettingsForOrg(
|
|
profileId: string,
|
|
organizationId: string
|
|
): Promise<PersonalEmailNotificationSettings | undefined> {
|
|
const row = await getOrganizationMemberEmailNotifications(profileId, organizationId);
|
|
|
|
return memberEmailNotificationRowToPersonalSettings(row);
|
|
}
|
|
|
|
export async function upsertOrganizationMemberEmailNotifications(
|
|
profileId: string,
|
|
organizationId: string,
|
|
preferences: TMemberEmailNotificationPreferencesUpdate
|
|
): Promise<MemberEmailNotificationRow> {
|
|
try {
|
|
const organizationMemberId = await getOrganizationMemberIdByOrgAndProfile(organizationId, profileId);
|
|
|
|
if (!organizationMemberId) {
|
|
throw new Error('Organization member not found');
|
|
}
|
|
|
|
const existing = await getOrganizationMemberEmailNotifications(profileId, organizationId);
|
|
const merged = personalSettingsToMemberEmailNotificationRow({
|
|
...(existing ?? DEFAULT_MEMBER_EMAIL_NOTIFICATION_ROW),
|
|
...preferences
|
|
});
|
|
const updatedAt = new Date().toISOString();
|
|
|
|
const [record] = await db
|
|
.insert(schema.organizationmemberEmailNotifications)
|
|
.values({
|
|
organizationMemberId,
|
|
...merged,
|
|
updatedAt
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: schema.organizationmemberEmailNotifications.organizationMemberId,
|
|
set: {
|
|
...merged,
|
|
updatedAt
|
|
}
|
|
})
|
|
.returning();
|
|
|
|
const row = rowFromRecord(record);
|
|
|
|
if (!row) {
|
|
throw new Error('Failed to upsert organization member email notification preferences');
|
|
}
|
|
|
|
return row;
|
|
} catch (error) {
|
|
console.error('upsertOrganizationMemberEmailNotifications error:', error);
|
|
throw new Error('Failed to update organization member email notification preferences');
|
|
}
|
|
}
|