Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion apps/admin/src/app/discounts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default async function GlobalDiscountsPage() {
model: string | null;
discountPercent: number;
reason: string | null;
expiresAt: string | null;
}): Promise<{ success: boolean; error?: string }> {
"use server";

Expand All @@ -79,6 +80,7 @@ export default async function GlobalDiscountsPage() {
model: data.model,
discountPercent: data.discountPercent,
reason: data.reason,
expiresAt: data.expiresAt,
});

if (!result) {
Expand Down Expand Up @@ -143,6 +145,7 @@ export default async function GlobalDiscountsPage() {
<TableHead>Model</TableHead>
<TableHead>Discount</TableHead>
<TableHead>Reason</TableHead>
<TableHead>Expires</TableHead>
<TableHead>Created</TableHead>
<TableHead className="w-[50px]" />
</TableRow>
Expand All @@ -151,7 +154,7 @@ export default async function GlobalDiscountsPage() {
{discounts.length === 0 ? (
<TableRow>
<TableCell
colSpan={6}
colSpan={7}
className="h-24 text-center text-muted-foreground"
>
<div className="flex flex-col items-center gap-2">
Expand Down Expand Up @@ -189,6 +192,21 @@ export default async function GlobalDiscountsPage() {
<TableCell className="max-w-[200px] truncate text-muted-foreground">
{discount.reason ?? "—"}
</TableCell>
<TableCell className="text-muted-foreground">
{discount.expiresAt ? (
<span
className={
new Date(discount.expiresAt) < new Date()
? "text-destructive"
: ""
}
>
{formatDate(discount.expiresAt)}
</span>
) : (
"Never"
)}
</TableCell>
<TableCell className="text-muted-foreground">
{formatDate(discount.createdAt)}
</TableCell>
Expand Down
20 changes: 19 additions & 1 deletion apps/admin/src/app/organizations/[orgId]/discounts/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default async function OrganizationDiscountsPage({
model: string | null;
discountPercent: number;
reason: string | null;
expiresAt: string | null;
}): Promise<{ success: boolean; error?: string }> {
"use server";

Expand All @@ -93,6 +94,7 @@ export default async function OrganizationDiscountsPage({
model: data.model,
discountPercent: data.discountPercent,
reason: data.reason,
expiresAt: data.expiresAt,
});

if (!result) {
Expand Down Expand Up @@ -164,6 +166,7 @@ export default async function OrganizationDiscountsPage({
<TableHead>Model</TableHead>
<TableHead>Discount</TableHead>
<TableHead>Reason</TableHead>
<TableHead>Expires</TableHead>
<TableHead>Created</TableHead>
<TableHead className="w-[50px]" />
</TableRow>
Expand All @@ -172,7 +175,7 @@ export default async function OrganizationDiscountsPage({
{discounts.length === 0 ? (
<TableRow>
<TableCell
colSpan={6}
colSpan={7}
className="h-24 text-center text-muted-foreground"
>
<div className="flex flex-col items-center gap-2">
Expand Down Expand Up @@ -209,6 +212,21 @@ export default async function OrganizationDiscountsPage({
<TableCell className="max-w-[200px] truncate text-muted-foreground">
{discount.reason ?? "—"}
</TableCell>
<TableCell className="text-muted-foreground">
{discount.expiresAt ? (
<span
className={
new Date(discount.expiresAt) < new Date()
? "text-destructive"
: ""
}
>
{formatDate(discount.expiresAt)}
</span>
) : (
"Never"
)}
</TableCell>
<TableCell className="text-muted-foreground">
{formatDate(discount.createdAt)}
</TableCell>
Expand Down
17 changes: 17 additions & 0 deletions apps/admin/src/components/discount-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ interface DiscountFormProps {
model: string | null;
discountPercent: number;
reason: string | null;
expiresAt: string | null;
}) => Promise<{ success: boolean; error?: string }>;
}

Expand All @@ -53,6 +54,7 @@ export function DiscountForm({
const [model, setModel] = useState<string>("__all__");
const [discountPercent, setDiscountPercent] = useState("");
const [reason, setReason] = useState("");
const [expiresAt, setExpiresAt] = useState("");

// Filter mappings by selected provider
const filteredMappings = useMemo(() => {
Expand Down Expand Up @@ -131,6 +133,7 @@ export function DiscountForm({
model: model === "__all__" ? null : model,
discountPercent: percent,
reason: reason || null,
expiresAt: expiresAt ? new Date(expiresAt).toISOString() : null,
});

setLoading(false);
Expand All @@ -141,6 +144,7 @@ export function DiscountForm({
setModel("__all__");
setDiscountPercent("");
setReason("");
setExpiresAt("");
router.refresh();
} else {
setError(result.error ?? "Failed to create discount");
Expand Down Expand Up @@ -264,6 +268,19 @@ export function DiscountForm({
/>
</div>

<div className="space-y-2">
<Label htmlFor="expiresAt">Expires At (optional)</Label>
<Input
id="expiresAt"
type="datetime-local"
value={expiresAt}
onChange={(e) => setExpiresAt(e.target.value)}
/>
<p className="text-xs text-muted-foreground">
Leave empty for a discount that never expires
</p>
</div>

{error && (
<div className="rounded-md bg-destructive/10 p-3 text-sm text-destructive">
{error}
Expand Down
107 changes: 107 additions & 0 deletions apps/admin/src/lib/api/v1.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,53 @@ export interface paths {
patch?: never;
trace?: never;
};
"/public/discounts/model/{modelId}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header?: never;
path: {
modelId: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description Active global discounts for the specified model */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
discounts: {
id: string;
provider: string | null;
model: string | null;
discountPercent: string;
reason: string | null;
expiresAt: string | null;
createdAt: string;
}[];
};
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/user/me": {
parameters: {
query?: never;
Expand Down Expand Up @@ -3884,6 +3931,66 @@ export interface paths {
patch?: never;
trace?: never;
};
"/orgs/{id}/discounts": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
get: {
parameters: {
query?: never;
header?: never;
path: {
id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description Active discounts for the organization (org-specific and global) */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
orgDiscounts: {
id: string;
organizationId: string | null;
provider: string | null;
model: string | null;
discountPercent: string;
reason: string | null;
expiresAt: string | null;
createdAt: string;
updatedAt: string;
}[];
globalDiscounts: {
id: string;
organizationId: string | null;
provider: string | null;
model: string | null;
discountPercent: string;
reason: string | null;
expiresAt: string | null;
createdAt: string;
updatedAt: string;
}[];
};
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/team/{organizationId}/members": {
parameters: {
query?: never;
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { tracingMiddleware } from "./middleware/tracing.js";
import { beacon } from "./routes/beacon.js";
import { routes } from "./routes/index.js";
import { internalModels } from "./routes/internal-models.js";
import { publicDiscounts } from "./routes/public-discounts.js";
import { referral } from "./routes/referral.js";
import { stripeRoutes } from "./stripe.js";

Expand Down Expand Up @@ -220,6 +221,8 @@ app.route("/", referral);

app.route("/internal", internalModels);

app.route("/public/discounts", publicDiscounts);

Comment thread
smakosh marked this conversation as resolved.
app.doc("/json", config);

app.get("/docs", swaggerUI({ url: "./json" }));
Expand Down
80 changes: 79 additions & 1 deletion apps/api/src/routes/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { z } from "zod";
import { userHasOrganizationAccess } from "@/utils/authorization.js";

import { logAuditEvent } from "@llmgateway/audit";
import { db, eq, tables } from "@llmgateway/db";
import { and, db, desc, eq, gte, isNull, or, tables } from "@llmgateway/db";

import type { ServerTypes } from "@/vars.js";

Expand Down Expand Up @@ -739,4 +739,82 @@ organization.openapi(getReferralStats, async (c) => {
});
});

const discountSchema = z.object({
id: z.string(),
organizationId: z.string().nullable(),
provider: z.string().nullable(),
model: z.string().nullable(),
discountPercent: z.string(),
reason: z.string().nullable(),
expiresAt: z.date().nullable(),
createdAt: z.date(),
updatedAt: z.date(),
});

const getOrgDiscounts = createRoute({
method: "get",
path: "/{id}/discounts",
request: {
params: z.object({
id: z.string(),
}),
},
responses: {
200: {
content: {
"application/json": {
schema: z.object({
orgDiscounts: z.array(discountSchema).openapi({}),
globalDiscounts: z.array(discountSchema).openapi({}),
}),
},
},
description:
"Active discounts for the organization (org-specific and global)",
},
},
});

organization.openapi(getOrgDiscounts, async (c) => {
const user = c.get("user");
if (!user) {
throw new HTTPException(401, {
message: "Unauthorized",
});
}

const { id } = c.req.param();

const hasAccess = await userHasOrganizationAccess(user.id, id);
if (!hasAccess) {
throw new HTTPException(403, {
message: "You do not have access to this organization",
});
}

const now = new Date();
const notExpired = or(
isNull(tables.discount.expiresAt),
gte(tables.discount.expiresAt, now),
);

const [orgDiscounts, globalDiscounts] = await Promise.all([
db
.select()
.from(tables.discount)
.where(and(eq(tables.discount.organizationId, id), notExpired))
.orderBy(desc(tables.discount.createdAt)),
db
.select()
.from(tables.discount)
.where(and(isNull(tables.discount.organizationId), notExpired))
.orderBy(desc(tables.discount.createdAt)),
Comment thread
smakosh marked this conversation as resolved.
]);

return c.json({
orgDiscounts,
globalDiscounts,
});
});

export default organization;
Loading
Loading