const studyMiddleware = middleware(async (opts) => {
if (!opts.ctx.user) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Unauthorized",
});
}
const parsedInput = z
.object({
studyId: z.string().cuid(),
})
.parse(opts.input);
const study = await studyRepository.findOneByIdAndClientId({
id: parsedInput.studyId,
clientId: opts.ctx.user.client.id,
});
if (!study) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Study not found",
});
}
return opts.next({
ctx: {
...opts.ctx,
user: opts.ctx.user,
study,
},
});
});
export const budgetRouter = createTRPCRouter({
create: protectedProcedure
.use(studyMiddleware)
.input(createBudgetInputSchema)
.mutation(({ input, ctx: { user, study } }) =>
budgetService.create(input, user, study),
),
});
const studyMiddleware = middleware(async (opts) => {
if (!opts.ctx.user) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "Unauthorized",
});
}
const parsedInput = z
.object({
studyId: z.string().cuid(),
})
.parse(opts.input);
const study = await studyRepository.findOneByIdAndClientId({
id: parsedInput.studyId,
clientId: opts.ctx.user.client.id,
});
if (!study) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Study not found",
});
}
return opts.next({
ctx: {
...opts.ctx,
user: opts.ctx.user,
study,
},
});
});
export const budgetRouter = createTRPCRouter({
create: protectedProcedure
.use(studyMiddleware)
.input(createBudgetInputSchema)
.mutation(({ input, ctx: { user, study } }) =>
budgetService.create(input, user, study),
),
});