Refactor user role handling in TRPC context and routers

- Updated the user role property from `rol` to `role` across multiple TRPC context and router files to ensure consistency and clarity in role management.
- Adjusted conditional checks for user roles in various procedures to reflect the updated property name, enhancing code readability and maintainability.
This commit is contained in:
Mauricio Siu 2025-05-04 19:26:09 -06:00
parent c13a68dab4
commit 1c73dab719
12 changed files with 57 additions and 60 deletions

View File

@ -163,7 +163,7 @@ export const aiRouter = createTRPCRouter({
deploy: protectedProcedure
.input(deploySuggestionSchema)
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.session.activeOrganizationId,
input.projectId,
@ -216,7 +216,7 @@ export const aiRouter = createTRPCRouter({
}
}
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.session.activeOrganizationId,
ctx.user.ownerId,

View File

@ -62,7 +62,7 @@ export const applicationRouter = createTRPCRouter({
.input(apiCreateApplication)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -87,7 +87,7 @@ export const applicationRouter = createTRPCRouter({
}
const newApplication = await createApplication(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newApplication.applicationId,
@ -109,7 +109,7 @@ export const applicationRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneApplication)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.applicationId,
@ -168,7 +168,7 @@ export const applicationRouter = createTRPCRouter({
delete: protectedProcedure
.input(apiFindOneApplication)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.applicationId,

View File

@ -62,7 +62,7 @@ export const composeRouter = createTRPCRouter({
.input(apiCreateCompose)
.mutation(async ({ ctx, input }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -86,7 +86,7 @@ export const composeRouter = createTRPCRouter({
}
const newService = await createCompose(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newService.composeId,
@ -103,7 +103,7 @@ export const composeRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindCompose)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.composeId,
@ -137,7 +137,7 @@ export const composeRouter = createTRPCRouter({
delete: protectedProcedure
.input(apiDeleteCompose)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.composeId,
@ -408,7 +408,7 @@ export const composeRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -455,7 +455,7 @@ export const composeRouter = createTRPCRouter({
isolatedDeployment: true,
});
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
compose.composeId,

View File

@ -41,7 +41,7 @@ export const mariadbRouter = createTRPCRouter({
.input(apiCreateMariaDB)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -65,7 +65,7 @@ export const mariadbRouter = createTRPCRouter({
});
}
const newMariadb = await createMariadb(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newMariadb.mariadbId,
@ -92,7 +92,7 @@ export const mariadbRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneMariaDB)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mariadbId,
@ -219,7 +219,7 @@ export const mariadbRouter = createTRPCRouter({
remove: protectedProcedure
.input(apiFindOneMariaDB)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mariadbId,

View File

@ -41,7 +41,7 @@ export const mongoRouter = createTRPCRouter({
.input(apiCreateMongo)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -65,7 +65,7 @@ export const mongoRouter = createTRPCRouter({
});
}
const newMongo = await createMongo(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newMongo.mongoId,
@ -96,7 +96,7 @@ export const mongoRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneMongo)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mongoId,
@ -261,7 +261,7 @@ export const mongoRouter = createTRPCRouter({
remove: protectedProcedure
.input(apiFindOneMongo)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mongoId,

View File

@ -44,7 +44,7 @@ export const mysqlRouter = createTRPCRouter({
.input(apiCreateMySql)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -69,7 +69,7 @@ export const mysqlRouter = createTRPCRouter({
}
const newMysql = await createMysql(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newMysql.mysqlId,
@ -100,7 +100,7 @@ export const mysqlRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneMySql)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mysqlId,
@ -260,7 +260,7 @@ export const mysqlRouter = createTRPCRouter({
remove: protectedProcedure
.input(apiFindOneMySql)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.mysqlId,

View File

@ -15,7 +15,7 @@ export const organizationRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol !== "owner" && !IS_CLOUD) {
if (ctx.user.role !== "owner" && !IS_CLOUD) {
throw new TRPCError({
code: "FORBIDDEN",
message: "Only the organization owner can create an organization",
@ -86,7 +86,7 @@ export const organizationRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol !== "owner" && !IS_CLOUD) {
if (ctx.user.role !== "owner" && !IS_CLOUD) {
throw new TRPCError({
code: "FORBIDDEN",
message: "Only the organization owner can update it",
@ -109,7 +109,7 @@ export const organizationRouter = createTRPCRouter({
}),
)
.mutation(async ({ ctx, input }) => {
if (ctx.user.rol !== "owner" && !IS_CLOUD) {
if (ctx.user.role !== "owner" && !IS_CLOUD) {
throw new TRPCError({
code: "FORBIDDEN",
message: "Only the organization owner can delete it",

View File

@ -41,7 +41,7 @@ export const postgresRouter = createTRPCRouter({
.input(apiCreatePostgres)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -65,7 +65,7 @@ export const postgresRouter = createTRPCRouter({
});
}
const newPostgres = await createPostgres(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newPostgres.postgresId,
@ -96,7 +96,7 @@ export const postgresRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOnePostgres)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.postgresId,
@ -244,7 +244,7 @@ export const postgresRouter = createTRPCRouter({
remove: protectedProcedure
.input(apiFindOnePostgres)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.postgresId,

View File

@ -57,7 +57,7 @@ export const projectRouter = createTRPCRouter({
.input(apiCreateProject)
.mutation(async ({ ctx, input }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkProjectAccess(
ctx.user.id,
"create",
@ -78,7 +78,7 @@ export const projectRouter = createTRPCRouter({
input,
ctx.session.activeOrganizationId,
);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewProject(
ctx.user.id,
project.projectId,
@ -99,7 +99,7 @@ export const projectRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneProject)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
const { accessedServices } = await findMemberById(
ctx.user.id,
ctx.session.activeOrganizationId,
@ -118,15 +118,15 @@ export const projectRouter = createTRPCRouter({
eq(projects.organizationId, ctx.session.activeOrganizationId),
),
with: {
compose: {
where: buildServiceFilter(compose.composeId, accessedServices),
},
applications: {
where: buildServiceFilter(
applications.applicationId,
accessedServices,
),
},
compose: {
where: buildServiceFilter(compose.composeId, accessedServices),
},
mariadb: {
where: buildServiceFilter(mariadb.mariadbId, accessedServices),
},
@ -164,8 +164,7 @@ export const projectRouter = createTRPCRouter({
return project;
}),
all: protectedProcedure.query(async ({ ctx }) => {
// console.log(ctx.user);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
const { accessedProjects, accessedServices } = await findMemberById(
ctx.user.id,
ctx.session.activeOrganizationId,
@ -175,7 +174,7 @@ export const projectRouter = createTRPCRouter({
return [];
}
const query = await db.query.projects.findMany({
return await db.query.projects.findMany({
where: and(
sql`${projects.projectId} IN (${sql.join(
accessedProjects.map((projectId) => sql`${projectId}`),
@ -213,8 +212,6 @@ export const projectRouter = createTRPCRouter({
},
orderBy: desc(projects.createdAt),
});
return query;
}
return await db.query.projects.findMany({
@ -244,7 +241,7 @@ export const projectRouter = createTRPCRouter({
.input(apiRemoveProject)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkProjectAccess(
ctx.user.id,
"delete",
@ -316,7 +313,7 @@ export const projectRouter = createTRPCRouter({
)
.mutation(async ({ ctx, input }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkProjectAccess(
ctx.user.id,
"create",
@ -581,7 +578,7 @@ export const projectRouter = createTRPCRouter({
}
}
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewProject(
ctx.user.id,
newProject.projectId,
@ -604,10 +601,10 @@ function buildServiceFilter(
fieldName: AnyPgColumn,
accessedServices: string[],
) {
return accessedServices.length > 0
? sql`${fieldName} IN (${sql.join(
return accessedServices.length === 0
? sql`false`
: sql`${fieldName} IN (${sql.join(
accessedServices.map((serviceId) => sql`${serviceId}`),
sql`, `,
)})`
: sql`1 = 0`;
)})`;
}

View File

@ -41,7 +41,7 @@ export const redisRouter = createTRPCRouter({
.input(apiCreateRedis)
.mutation(async ({ input, ctx }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.projectId,
@ -65,7 +65,7 @@ export const redisRouter = createTRPCRouter({
});
}
const newRedis = await createRedis(input);
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await addNewService(
ctx.user.id,
newRedis.redisId,
@ -89,7 +89,7 @@ export const redisRouter = createTRPCRouter({
one: protectedProcedure
.input(apiFindOneRedis)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.redisId,
@ -251,7 +251,7 @@ export const redisRouter = createTRPCRouter({
remove: protectedProcedure
.input(apiFindOneRedis)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
await checkServiceAccess(
ctx.user.id,
input.redisId,

View File

@ -407,7 +407,7 @@ export const settingsRouter = createTRPCRouter({
.input(apiServerSchema)
.query(async ({ ctx, input }) => {
try {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
const canAccess = await canAccessToTraefikFiles(
ctx.user.id,
ctx.session.activeOrganizationId,
@ -428,7 +428,7 @@ export const settingsRouter = createTRPCRouter({
updateTraefikFile: protectedProcedure
.input(apiModifyTraefikConfig)
.mutation(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
const canAccess = await canAccessToTraefikFiles(
ctx.user.id,
ctx.session.activeOrganizationId,
@ -449,7 +449,7 @@ export const settingsRouter = createTRPCRouter({
readTraefikFile: protectedProcedure
.input(apiReadTraefikConfig)
.query(async ({ input, ctx }) => {
if (ctx.user.rol === "member") {
if (ctx.user.role === "member") {
const canAccess = await canAccessToTraefikFiles(
ctx.user.id,
ctx.session.activeOrganizationId,

View File

@ -30,7 +30,7 @@ import { ZodError } from "zod";
*/
interface CreateContextOptions {
user: (User & { rol: "member" | "admin" | "owner"; ownerId: string }) | null;
user: (User & { role: "member" | "admin" | "owner"; ownerId: string }) | null;
session: (Session & { activeOrganizationId: string }) | null;
req: CreateNextContextOptions["req"];
res: CreateNextContextOptions["res"];
@ -83,7 +83,7 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
? {
...user,
email: user.email,
rol: user.role as "owner" | "member" | "admin",
role: user.role as "owner" | "member" | "admin",
id: user.id,
ownerId: user.ownerId,
}
@ -180,7 +180,7 @@ export const uploadProcedure = async (opts: any) => {
};
export const cliProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.user || ctx.user.rol !== "owner") {
if (!ctx.session || !ctx.user || ctx.user.role !== "owner") {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
@ -194,7 +194,7 @@ export const cliProcedure = t.procedure.use(({ ctx, next }) => {
});
export const adminProcedure = t.procedure.use(({ ctx, next }) => {
if (!ctx.session || !ctx.user || ctx.user.rol !== "owner") {
if (!ctx.session || !ctx.user || ctx.user.role !== "owner") {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({