@@ -3,8 +3,6 @@ import type { MetaFunction } from "@remix-run/react";
33import {
44 json ,
55 redirect ,
6- type ActionFunction ,
7- type LoaderFunctionArgs ,
86} from "@remix-run/server-runtime" ;
97import { tryCatch } from "@trigger.dev/core" ;
108import { typedjson , useTypedLoaderData } from "remix-typedjson" ;
@@ -43,6 +41,7 @@ import { NavBar, PageAccessories, PageTitle } from "~/components/primitives/Page
4341import { prisma } from "~/db.server" ;
4442import { featuresForRequest } from "~/features.server" ;
4543import { useScrollContainerToTop } from "~/hooks/useScrollContainerToTop" ;
44+ import { resolveOrgIdFromSlug } from "~/models/organization.server" ;
4645import {
4746 commitSession ,
4847 getSession ,
@@ -59,6 +58,7 @@ import {
5958 setBillingAlert ,
6059 setBillingLimit ,
6160} from "~/services/platform.v3.server" ;
61+ import { dashboardAction , dashboardLoader } from "~/services/routeBuilders/dashboardBuilder" ;
6262import type { BillingLimitResult } from "~/services/billingLimit.schemas" ;
6363import {
6464 getAlertsResetRequested ,
@@ -77,15 +77,31 @@ import {
7777 v3BillingLimitsPath ,
7878 v3BillingPath ,
7979} from "~/utils/pathBuilder" ;
80- import { requireUserId } from "~/services/session.server" ;
80+
81+ const billingLimitsAuthorization = {
82+ action : "manage" as const ,
83+ resource : { type : "billing" as const } ,
84+ } ;
8185
8286export const meta : MetaFunction = ( ) => {
8387 return [ { title : `Billing limits | Trigger.dev` } ] ;
8488} ;
8589
86- export async function loader ( { params, request } : LoaderFunctionArgs ) {
87- const userId = await requireUserId ( request ) ;
88- const { organizationSlug } = OrganizationParamsSchema . parse ( params ) ;
90+ export const loader = dashboardLoader (
91+ {
92+ params : OrganizationParamsSchema ,
93+ context : async ( params ) => {
94+ const organizationId = await resolveOrgIdFromSlug ( params . organizationSlug ) ;
95+ return organizationId ? { organizationId } : { } ;
96+ } ,
97+ authorization : {
98+ ...billingLimitsAuthorization ,
99+ message : "With your current role, you can't manage billing limits." ,
100+ } ,
101+ } ,
102+ async ( { params, request, user } ) => {
103+ const userId = user . id ;
104+ const { organizationSlug } = params ;
89105
90106 const { isManagedCloud } = featuresForRequest ( request ) ;
91107 if ( ! isManagedCloud ) {
@@ -131,9 +147,9 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
131147 firstDayOfMonth . setUTCHours ( 0 , 0 , 0 , 0 ) ;
132148
133149 const firstDayOfNextMonth = new Date ( ) ;
134- firstDayOfNextMonth . setUTCMonth ( firstDayOfNextMonth . getUTCMonth ( ) + 1 ) ;
135150 firstDayOfNextMonth . setUTCDate ( 1 ) ;
136151 firstDayOfNextMonth . setUTCHours ( 0 , 0 , 0 , 0 ) ;
152+ firstDayOfNextMonth . setUTCMonth ( firstDayOfNextMonth . getUTCMonth ( ) + 1 ) ;
137153
138154 const [ usage , queuedRunCount , billingLimitPauseEnvCount ] = await Promise . all ( [
139155 getCachedUsage ( organization . id , { from : firstDayOfMonth , to : firstDayOfNextMonth } ) ,
@@ -166,7 +182,8 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
166182 submittedResumeMode,
167183 suggestedNewLimitDollars,
168184 } ) ;
169- }
185+ }
186+ ) ;
170187
171188type LoaderData = {
172189 billingLimit : BillingLimitResult ;
@@ -182,9 +199,18 @@ type LoaderData = {
182199 suggestedNewLimitDollars : number ;
183200} ;
184201
185- export const action : ActionFunction = async ( { request, params } ) => {
186- const userId = await requireUserId ( request ) ;
187- const { organizationSlug } = OrganizationParamsSchema . parse ( params ) ;
202+ export const action = dashboardAction (
203+ {
204+ params : OrganizationParamsSchema ,
205+ context : async ( params ) => {
206+ const organizationId = await resolveOrgIdFromSlug ( params . organizationSlug ) ;
207+ return organizationId ? { organizationId } : { } ;
208+ } ,
209+ authorization : billingLimitsAuthorization ,
210+ } ,
211+ async ( { request, params, user } ) => {
212+ const userId = user . id ;
213+ const { organizationSlug } = params ;
188214
189215 const organization = await prisma . organization . findFirst ( {
190216 where : { slug : organizationSlug , members : { some : { userId } } } ,
@@ -476,7 +502,8 @@ export const action: ActionFunction = async ({ request, params }) => {
476502 }
477503
478504 return json ( { error : "Unknown form intent" } , { status : 400 } ) ;
479- } ;
505+ }
506+ ) ;
480507
481508export default function Page ( ) {
482509 const {
0 commit comments