-
Notifications
You must be signed in to change notification settings - Fork 46
feat(enterprise) send and manage org recommendations email #4219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
St0rmz1
wants to merge
5
commits into
main
Choose a base branch
from
feat/org-email-preferences-adoption-digest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,109
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bcae219
feat(org): add email preferences section with adoption digest opt-in
St0rmz1 2331274
feat(org): send weekly recommendations digest to enterprise owners
St0rmz1 7101d32
fix local review bugs: atomic digest toggle + SQL opt-in filter + ded…
St0rmz1 d2f3879
fixes
St0rmz1 4e93644
fix Opt-out checks still run against the replica
St0rmz1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
apps/web/src/app/api/cron/enterprise-recommendations-digest/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { NextResponse } from 'next/server'; | ||
|
|
||
| import { CRON_SECRET } from '@/lib/config.server'; | ||
| import { dispatchEnterpriseRecommendationsDigests } from '@/lib/organizations/recommendations-digest'; | ||
| import { sentryLogger } from '@/lib/utils.server'; | ||
|
|
||
| if (!CRON_SECRET) { | ||
| throw new Error('CRON_SECRET is not configured in environment variables'); | ||
| } | ||
|
|
||
| export async function GET(request: Request) { | ||
| const authHeader = request.headers.get('authorization'); | ||
| const expectedAuth = `Bearer ${CRON_SECRET}`; | ||
| if (authHeader !== expectedAuth) { | ||
| sentryLogger( | ||
| 'cron', | ||
| 'warning' | ||
| )( | ||
| 'SECURITY: Invalid CRON job authorization attempt: ' + | ||
| (authHeader ? 'Invalid authorization header' : 'Missing authorization header') | ||
| ); | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); | ||
| } | ||
|
|
||
| const summary = await dispatchEnterpriseRecommendationsDigests(); | ||
|
|
||
| return NextResponse.json( | ||
| { | ||
| success: true, | ||
| summary, | ||
| timestamp: new Date().toISOString(), | ||
| }, | ||
| { status: 200 } | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
apps/web/src/components/organizations/OrganizationEmailPreferencesCard.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState } from 'react'; | ||
| import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Switch } from '@/components/ui/switch'; | ||
| import { Bell, ChartLine, Mail } from 'lucide-react'; | ||
| import type { LucideIcon } from 'lucide-react'; | ||
| import { toast } from 'sonner'; | ||
| import { | ||
| useOrganizationWithMembers, | ||
| useUpdateRecommendationsDigest, | ||
| } from '@/app/api/organizations/hooks'; | ||
| import { SpendingAlertsModal } from './SpendingAlertsModal'; | ||
|
|
||
| type Props = { | ||
| organizationId: string; | ||
| }; | ||
|
|
||
| function recipientStateLabel(recipientCount: number): string { | ||
| if (recipientCount === 0) { | ||
| return 'Off'; | ||
| } | ||
| return `On · ${recipientCount} recipient${recipientCount === 1 ? '' : 's'}`; | ||
| } | ||
|
|
||
| function PreferenceRow({ | ||
| icon: Icon, | ||
| title, | ||
| description, | ||
| stateLabel, | ||
| isOn, | ||
| control, | ||
| }: { | ||
| icon: LucideIcon; | ||
| title: string; | ||
| description: string; | ||
| stateLabel?: string; | ||
| isOn?: boolean; | ||
| control: React.ReactNode; | ||
| }) { | ||
| return ( | ||
| <div className="flex items-start justify-between gap-4 py-4 first:pt-0 last:pb-0"> | ||
| <div className="flex items-start gap-3"> | ||
| <Icon className="text-muted-foreground mt-0.5 h-4 w-4 shrink-0" /> | ||
| <div className="space-y-1"> | ||
| <p className="text-sm font-medium">{title}</p> | ||
| <p className="text-muted-foreground text-xs">{description}</p> | ||
| {stateLabel && ( | ||
| <p className="text-muted-foreground text-xs tabular-nums"> | ||
| <span className={isOn ? 'text-foreground font-medium' : undefined}>{stateLabel}</span> | ||
| </p> | ||
| )} | ||
| </div> | ||
| </div> | ||
| <div className="shrink-0">{control}</div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export function OrganizationEmailPreferencesCard({ organizationId }: Props) { | ||
| const { data } = useOrganizationWithMembers(organizationId); | ||
| const [isSpendingAlertsOpen, setIsSpendingAlertsOpen] = useState(false); | ||
| const updateRecommendationsDigest = useUpdateRecommendationsDigest(); | ||
|
|
||
| if (!data) { | ||
| return null; | ||
| } | ||
|
|
||
| const settings = data.settings; | ||
| const isEnterprise = data.plan === 'enterprise'; | ||
|
|
||
| // Low-balance alerts are "on" only when both a threshold and at least one | ||
| // recipient are configured (matches SpendingAlertsModal's enabled check). | ||
| const spendingRecipientCount = | ||
| settings?.minimum_balance !== undefined | ||
| ? (settings?.minimum_balance_alert_email?.length ?? 0) | ||
| : 0; | ||
| const digestEnabled = settings?.recommendations_digest_enabled === true; | ||
|
|
||
| const handleDigestToggle = (next: boolean) => { | ||
| updateRecommendationsDigest.mutate( | ||
| { organizationId, enabled: next }, | ||
| { | ||
| onSuccess: () => { | ||
| toast.success( | ||
| next | ||
| ? 'Weekly recommendations email enabled. Organization owners will receive it.' | ||
| : 'Weekly recommendations email disabled.' | ||
| ); | ||
| }, | ||
| onError: (error: unknown) => { | ||
| toast.error( | ||
| error instanceof Error | ||
| ? error.message | ||
| : 'Failed to update the recommendations digest setting' | ||
| ); | ||
| }, | ||
| } | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <Card> | ||
| <CardHeader> | ||
| <CardTitle> | ||
| <Mail className="mr-2 inline h-5 w-5" /> | ||
| Email preferences | ||
| </CardTitle> | ||
| <CardDescription>Choose which emails this organization receives.</CardDescription> | ||
| </CardHeader> | ||
| <CardContent> | ||
| <div className="divide-border divide-y"> | ||
| <PreferenceRow | ||
| icon={Bell} | ||
| title="Low balance alerts" | ||
| description="Notify recipients when the organization balance falls below a threshold." | ||
| stateLabel={recipientStateLabel(spendingRecipientCount)} | ||
| isOn={spendingRecipientCount > 0} | ||
| control={ | ||
| <Button | ||
| variant="outline" | ||
| size="sm" | ||
| className="h-8" | ||
| onClick={() => setIsSpendingAlertsOpen(true)} | ||
| > | ||
| Configure | ||
| </Button> | ||
| } | ||
| /> | ||
| {isEnterprise && ( | ||
| <PreferenceRow | ||
| icon={ChartLine} | ||
| title="Weekly recommendations email" | ||
| description="Email the organization's owners a weekly summary of open recommendations and feature setup." | ||
| control={ | ||
| <Switch | ||
| checked={digestEnabled} | ||
| disabled={updateRecommendationsDigest.isPending} | ||
| onCheckedChange={handleDigestToggle} | ||
| aria-label="Weekly recommendations email" | ||
| /> | ||
| } | ||
| /> | ||
| )} | ||
| </div> | ||
| </CardContent> | ||
|
|
||
| <SpendingAlertsModal | ||
| open={isSpendingAlertsOpen} | ||
| onOpenChange={setIsSpendingAlertsOpen} | ||
| organizationId={organizationId} | ||
| settings={settings} | ||
| /> | ||
| </Card> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Your weekly Kilo recommendations</title> | ||
| </head> | ||
| <body | ||
| style=" | ||
| margin: 0; | ||
| padding: 0; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; | ||
| background-color: #ffffff; | ||
| color: #1a1a1a; | ||
| " | ||
| > | ||
| <table width="100%" cellpadding="0" cellspacing="0" style="background-color: #ffffff"> | ||
| <tr> | ||
| <td align="center" style="padding: 40px 20px"> | ||
| <table width="520" cellpadding="0" cellspacing="0"> | ||
| <!-- Header --> | ||
| <tr> | ||
| <td style="padding: 40px 40px 20px"> | ||
| <h1 | ||
| style=" | ||
| margin: 0; | ||
| font-size: 24px; | ||
| font-weight: 700; | ||
| color: #1a1a1a; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| Your weekly Kilo recommendations | ||
| </h1> | ||
| </td> | ||
| </tr> | ||
| <!-- Body --> | ||
| <tr> | ||
| <td style="padding: 0 40px 30px"> | ||
| <p | ||
| style=" | ||
| margin: 0 0 16px; | ||
| font-size: 14px; | ||
| line-height: 20px; | ||
| color: #333; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| Here's how to get more from Kilo for | ||
| <strong style="color: #1a1a1a">{{ organization_name }}</strong> this week. You | ||
| have <strong style="color: #1a1a1a">{{ open_count }}</strong> open | ||
| recommendations, and your organization has set up | ||
| <strong style="color: #1a1a1a">{{ adopted_summary }}</strong> features. | ||
| </p> | ||
|
|
||
| <!-- Recommendations --> | ||
| {{ recommendations_section }} | ||
|
|
||
| <!-- CTA Button --> | ||
| <table width="100%" cellpadding="0" cellspacing="0"> | ||
| <tr> | ||
| <td align="center" style="padding: 10px 0 20px"> | ||
| <a | ||
| href="{{ dashboard_url }}" | ||
| style=" | ||
| display: inline-block; | ||
| padding: 10px 20px; | ||
| background-color: #1a1a1a; | ||
| color: #ffffff; | ||
| text-decoration: none; | ||
| border-radius: 7px; | ||
| font-size: 13px; | ||
| font-weight: 600; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| View recommendations | ||
| </a> | ||
| </td> | ||
| </tr> | ||
| </table> | ||
|
|
||
| <p | ||
| style=" | ||
| margin: 0; | ||
| font-size: 14px; | ||
| line-height: 20px; | ||
| color: #333; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| You're receiving this because you're an owner of this organization. You can turn | ||
| the weekly recommendations email off from the organization's Email preferences. | ||
| </p> | ||
| </td> | ||
| </tr> | ||
| <!-- Sign-off --> | ||
| <tr> | ||
| <td style="padding: 0 40px 40px; border-top: 1px solid #ebebea"> | ||
| <p | ||
| style=" | ||
| margin: 20px 0 0; | ||
| font-size: 14px; | ||
| line-height: 20px; | ||
| color: #333; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| — The Kilo Team | ||
| </p> | ||
| </td> | ||
| </tr> | ||
| </table> | ||
| <!-- Branding Footer --> | ||
| <table width="520" cellpadding="0" cellspacing="0"> | ||
| <tr> | ||
| <td align="center" style="padding: 30px 20px; border-top: 1px solid #eee"> | ||
| <p | ||
| style=" | ||
| margin: 0; | ||
| font-size: 11px; | ||
| color: #ccc; | ||
| font-family: | ||
| -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, | ||
| sans-serif; | ||
| " | ||
| > | ||
| © {{ year }} Kilo Code, Inc<br />455 Market St, Ste 1940 PMB 993504<br />San | ||
| Francisco, CA 94105, USA | ||
| </p> | ||
| </td> | ||
| </tr> | ||
| </table> | ||
| </td> | ||
| </tr> | ||
| </table> | ||
| </body> | ||
| </html> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be a
crypto.timingSafeEqual?