Repository navigation
feat(admin): add provider/model discount management #1606
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
56049a4
feat(admin): add provider/model discount management
steebchen cc79953
fix: use db instead of cdb in API discount routes
steebchen 0ba6c4e
Merge remote-tracking branch 'origin/main' into add-provider-model-di…
steebchen 10bf4b4
feat: add provider icons and search to discount selectors
steebchen 7fd67b7
fix: fix scrolling in command list component
steebchen 58bbc54
fix: remove overflow-hidden from Command wrapper
steebchen 82672ec
fix: fix command list scrolling in discount form
steebchen c77f7dd
fix: add CSS overrides for cmdk scroll behavior
steebchen 32b5167
refactor: replace Command components with Select for provider and mod…
smakosh cad0333
Merge remote-tracking branch 'origin/main' into add-provider-model-di…
smakosh 06109a6
chore: resolve migration conflict, accept main and remove old discoun…
steebchen cc56333
feat: add discount table migration
steebchen 67e5dfc
feat: use provider/model mappings for discounts
steebchen edf1ca1
chore(autofix): apply diff
steebchen 4666c78
Merge remote-tracking branch 'origin/main' into add-provider-model-di…
steebchen 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
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,229 @@ | ||
| import { Globe, Tag } from "lucide-react"; | ||
| import Link from "next/link"; | ||
|
|
||
| import { DeleteDiscountButton, DiscountForm } from "@/components/discount-form"; | ||
| import { Badge } from "@/components/ui/badge"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| Table, | ||
| TableBody, | ||
| TableCell, | ||
| TableHead, | ||
| TableHeader, | ||
| TableRow, | ||
| } from "@/components/ui/table"; | ||
| import { | ||
| createGlobalDiscount, | ||
| deleteGlobalDiscount, | ||
| getDiscountOptions, | ||
| getGlobalDiscounts, | ||
| } from "@/lib/admin-discounts"; | ||
|
|
||
| function formatDate(dateString: string) { | ||
| return new Date(dateString).toLocaleDateString("en-US", { | ||
| year: "numeric", | ||
| month: "short", | ||
| day: "numeric", | ||
| }); | ||
| } | ||
|
|
||
| function formatDiscount(decimalString: string): string { | ||
| const decimal = parseFloat(decimalString); | ||
| return `${(decimal * 100).toFixed(1)}%`; | ||
| } | ||
|
|
||
| function SignInPrompt() { | ||
| return ( | ||
| <div className="flex min-h-screen items-center justify-center px-4"> | ||
| <div className="w-full max-w-md text-center"> | ||
| <div className="mb-8"> | ||
| <h1 className="text-3xl font-semibold tracking-tight"> | ||
| Admin Dashboard | ||
| </h1> | ||
| <p className="mt-2 text-sm text-muted-foreground"> | ||
| Sign in to access the admin dashboard | ||
| </p> | ||
| </div> | ||
| <Button asChild size="lg" className="w-full"> | ||
| <Link href="/login">Sign In</Link> | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default async function GlobalDiscountsPage() { | ||
| const [discountsData, options] = await Promise.all([ | ||
| getGlobalDiscounts(), | ||
| getDiscountOptions(), | ||
| ]); | ||
|
|
||
| if (discountsData === null) { | ||
| return <SignInPrompt />; | ||
| } | ||
|
|
||
| const discounts = discountsData?.discounts ?? []; | ||
|
|
||
| // Server action to create discount | ||
| async function handleCreateDiscount(data: { | ||
| provider: string | null; | ||
| model: string | null; | ||
| discountPercent: number; | ||
| reason: string | null; | ||
| }): Promise<{ success: boolean; error?: string }> { | ||
| "use server"; | ||
|
|
||
| try { | ||
| const result = await createGlobalDiscount({ | ||
| provider: data.provider, | ||
| model: data.model, | ||
| discountPercent: data.discountPercent, | ||
| reason: data.reason, | ||
| }); | ||
|
|
||
| if (!result) { | ||
| return { | ||
| success: false, | ||
| error: "Failed to create discount. It may already exist.", | ||
| }; | ||
| } | ||
|
|
||
| return { success: true }; | ||
| } catch (error) { | ||
| console.error("Error creating discount:", error); | ||
| return { | ||
| success: false, | ||
| error: "An error occurred while creating the discount", | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // Server action to delete discount | ||
| async function handleDeleteDiscount( | ||
| discountId: string, | ||
| ): Promise<{ success: boolean }> { | ||
| "use server"; | ||
|
|
||
| const success = await deleteGlobalDiscount(discountId); | ||
| return { success }; | ||
| } | ||
|
|
||
| return ( | ||
| <div className="mx-auto flex w-full max-w-6xl flex-col gap-6 px-4 py-8 md:px-8"> | ||
| <header className="flex flex-col items-start justify-between gap-4 sm:flex-row sm:items-center"> | ||
| <div className="space-y-1"> | ||
| <div className="flex items-center gap-3"> | ||
| <div className="flex h-10 w-10 items-center justify-center rounded-lg bg-primary/10 text-primary"> | ||
| <Globe className="h-5 w-5" /> | ||
| </div> | ||
| <div> | ||
| <h1 className="text-2xl font-semibold tracking-tight"> | ||
| Global Discounts | ||
| </h1> | ||
| <p className="text-sm text-muted-foreground"> | ||
| Discounts that apply to all organizations | ||
| </p> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| {options && ( | ||
| <DiscountForm | ||
| providers={options.providers} | ||
| models={options.models} | ||
| onSubmit={handleCreateDiscount} | ||
| /> | ||
| )} | ||
| </header> | ||
|
|
||
| <div className="rounded-lg border border-border/60 bg-card"> | ||
| <Table> | ||
| <TableHeader> | ||
| <TableRow> | ||
| <TableHead>Provider</TableHead> | ||
| <TableHead>Model</TableHead> | ||
| <TableHead>Discount</TableHead> | ||
| <TableHead>Reason</TableHead> | ||
| <TableHead>Created</TableHead> | ||
| <TableHead className="w-[50px]" /> | ||
| </TableRow> | ||
| </TableHeader> | ||
| <TableBody> | ||
| {discounts.length === 0 ? ( | ||
| <TableRow> | ||
| <TableCell | ||
| colSpan={6} | ||
| className="h-24 text-center text-muted-foreground" | ||
| > | ||
| <div className="flex flex-col items-center gap-2"> | ||
| <Tag className="h-8 w-8 text-muted-foreground/50" /> | ||
| <p>No global discounts configured</p> | ||
| <p className="text-xs"> | ||
| Global discounts apply to all organizations and override | ||
| hardcoded model discounts | ||
| </p> | ||
| </div> | ||
| </TableCell> | ||
| </TableRow> | ||
| ) : ( | ||
| discounts.map((discount) => ( | ||
| <TableRow key={discount.id}> | ||
| <TableCell> | ||
| {discount.provider ? ( | ||
| <Badge variant="outline">{discount.provider}</Badge> | ||
| ) : ( | ||
| <span className="text-muted-foreground">All</span> | ||
| )} | ||
| </TableCell> | ||
| <TableCell> | ||
| {discount.model ? ( | ||
| <Badge variant="secondary">{discount.model}</Badge> | ||
| ) : ( | ||
| <span className="text-muted-foreground">All</span> | ||
| )} | ||
| </TableCell> | ||
| <TableCell> | ||
| <span className="font-medium text-green-600"> | ||
| {formatDiscount(discount.discountPercent)} off | ||
| </span> | ||
| </TableCell> | ||
| <TableCell className="max-w-[200px] truncate text-muted-foreground"> | ||
| {discount.reason || "—"} | ||
| </TableCell> | ||
| <TableCell className="text-muted-foreground"> | ||
| {formatDate(discount.createdAt)} | ||
| </TableCell> | ||
| <TableCell> | ||
| <DeleteDiscountButton | ||
| discountId={discount.id} | ||
| onDelete={handleDeleteDiscount} | ||
| /> | ||
| </TableCell> | ||
| </TableRow> | ||
| )) | ||
| )} | ||
| </TableBody> | ||
| </Table> | ||
| </div> | ||
|
|
||
| <div className="rounded-lg border border-border/60 bg-muted/30 p-4"> | ||
| <h3 className="text-sm font-medium">How global discounts work</h3> | ||
| <ul className="mt-2 space-y-1 text-sm text-muted-foreground"> | ||
| <li> | ||
| Global discounts apply to ALL organizations unless overridden by | ||
| org-specific discounts | ||
| </li> | ||
| <li> | ||
| They take precedence over hardcoded model discounts in the codebase | ||
| </li> | ||
| <li> | ||
| More specific discounts (provider + model) take precedence over | ||
| broader ones | ||
| </li> | ||
| <li> | ||
| A 30% discount means all customers pay 70% of the original price | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
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
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.
Remove console.error to satisfy no-console lint.
ESLint flags the console call on Line 93. Prefer dropping it or routing through an approved logger.
🧹 Suggested change
📝 Committable suggestion
🧰 Tools
🪛 ESLint
[error] 93-93: Unexpected console statement.
(no-console)
🤖 Prompt for AI Agents