refactor(patch): unify patch retrieval and application logic

- Consolidated patch retrieval for applications and composes into a single query method, improving code clarity and reducing redundancy.
- Updated the ShowPatches component to utilize the new unified query, simplifying data fetching logic.
- Refactored patch application commands to streamline the process for both application and compose types, enhancing maintainability and consistency across the codebase.
This commit is contained in:
Mauricio Siu
2026-02-17 01:35:08 -06:00
parent 9818e3c3ba
commit 46ac272f3f
5 changed files with 86 additions and 168 deletions
@@ -33,22 +33,8 @@ export const ShowPatches = ({ id, type }: Props) => {
const utils = api.useUtils();
const queryMap = {
application: () =>
api.patch.byApplicationId.useQuery(
{ applicationId: id },
{ enabled: !!id },
),
compose: () =>
api.patch.byComposeId.useQuery({ composeId: id }, { enabled: !!id }),
};
const { data: patches, isLoading: isPatchesLoading } = queryMap[type]
? queryMap[type]()
: api.patch.byApplicationId.useQuery(
{ applicationId: id },
{ enabled: !!id },
);
const { data: patches, isLoading: isPatchesLoading } =
api.patch.byEntityId.useQuery({ id, type }, { enabled: !!id });
const mutationMap = {
application: () => api.patch.delete.useMutation(),
@@ -166,11 +152,9 @@ export const ShowPatches = ({ id, type }: Props) => {
})
.then(() => {
toast.success("Patch updated");
utils.patch.byApplicationId.invalidate({
applicationId: id,
});
utils.patch.byComposeId.invalidate({
composeId: id,
utils.patch.byEntityId.invalidate({
id,
type,
});
})
.catch((err) => {
@@ -190,11 +174,9 @@ export const ShowPatches = ({ id, type }: Props) => {
mutateAsync({ patchId: patch.patchId })
.then(() => {
toast.success("Patch deleted");
utils.patch.byApplicationId.invalidate({
applicationId: id,
});
utils.patch.byComposeId.invalidate({
composeId: id,
utils.patch.byEntityId.invalidate({
id,
type,
});
})
.catch((err) => {
+29 -34
View File
@@ -8,9 +8,7 @@ import {
findComposeById,
findPatchByFilePath,
findPatchById,
findPatchesByApplicationId,
findPatchesByComposeId,
generatePatch,
findPatchesByEntityId,
readPatchRepoDirectory,
readPatchRepoFile,
updatePatch,
@@ -26,8 +24,6 @@ import {
apiCreatePatch,
apiDeletePatch,
apiFindPatch,
apiFindPatchesByApplicationId,
apiFindPatchesByComposeId,
apiTogglePatchEnabled,
apiUpdatePatch,
} from "@/server/db/schema";
@@ -77,38 +73,37 @@ export const patchRouter = createTRPCRouter({
return await findPatchById(input.patchId);
}),
byApplicationId: protectedProcedure
.input(apiFindPatchesByApplicationId)
byEntityId: protectedProcedure
.input(
z.object({ id: z.string(), type: z.enum(["application", "compose"]) }),
)
.query(async ({ input, ctx }) => {
const app = await findApplicationById(input.applicationId);
if (
app.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this application",
});
if (input.type === "application") {
const app = await findApplicationById(input.id);
if (
app.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this application",
});
}
} else if (input.type === "compose") {
const compose = await findComposeById(input.id);
if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this compose",
});
}
}
const result = await findPatchesByEntityId(input.id, input.type);
return await findPatchesByApplicationId(input.applicationId);
}),
byComposeId: protectedProcedure
.input(apiFindPatchesByComposeId)
.query(async ({ input, ctx }) => {
const compose = await findComposeById(input.composeId);
if (
compose.environment.project.organizationId !==
ctx.session.activeOrganizationId
) {
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to access this compose",
});
}
return await findPatchesByComposeId(input.composeId);
return result;
}),
update: protectedProcedure