Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

refactor: fetch custom field values per project

MonsPropre63c87651b41d
diff --git a/apps/api/src/custom-field/controllers/get-custom-field-values-by-project.ts b/apps/api/src/custom-field/controllers/get-custom-field-values-by-project.ts
new file mode 100644
index 000000000..a399759be
--- /dev/null
+++ b/apps/api/src/custom-field/controllers/get-custom-field-values-by-project.ts
@@ -0,0 +1,29 @@
+import { eq } from "drizzle-orm";
+import db from "../../database";
+import {
+ customFieldDefinitionTable,
+ customFieldValueTable,
+ taskTable,
+} from "../../database/schema";
+
+async function getCustomFieldValuesByProject(projectId: string) {
+ return db
+ .select({
+ id: customFieldValueTable.id,
+ taskId: customFieldValueTable.taskId,
+ fieldId: customFieldValueTable.fieldId,
+ value: customFieldValueTable.value,
+ fieldName: customFieldDefinitionTable.name,
+ fieldType: customFieldDefinitionTable.type,
+ fieldOptions: customFieldDefinitionTable.options,
+ })
+ .from(customFieldValueTable)
+ .innerJoin(taskTable, eq(customFieldValueTable.taskId, taskTable.id))
+ .innerJoin(
+ customFieldDefinitionTable,
+ eq(customFieldValueTable.fieldId, customFieldDefinitionTable.id),
+ )
+ .where(eq(taskTable.projectId, projectId));
+}
+
+export default getCustomFieldValuesByProject;
diff --git a/apps/api/src/custom-field/index.ts b/apps/api/src/custom-field/index.ts
index 3964aa15f..01b8d873f 100644
--- a/apps/api/src/custom-field/index.ts
+++ b/apps/api/src/custom-field/index.ts
@@ -6,6 +6,7 @@ import { workspaceAccess } from "../utils/workspace-access-middleware";
import createCustomField from "./controllers/create-custom-field";
import deleteCustomField from "./controllers/delete-custom-field";
import getCustomFieldFilterValues from "./controllers/get-custom-field-filter-values";
+import getCustomFieldValuesByProject from "./controllers/get-custom-field-values-by-project";
import getCustomFieldValuesByTask from "./controllers/get-custom-field-values-by-task";
import getCustomFieldsByProject from "./controllers/get-custom-fields-by-project";
import reorderCustomFields from "./controllers/reorder-custom-field";
@@ -65,6 +66,34 @@ const customField = new Hono<{
return c.json(fields);
},
)
+ .get(
+ "/project/:projectId/values",
+ describeRoute({
+ operationId: "getCustomFieldValuesByProject",
+ tags: ["Custom Fields"],
+ description: "Get all custom field values for a project",
+ responses: {
+ 200: {
+ description:
+ "List of custom field values for all tasks in the project",
+ content: {
+ "application/json": {
+ schema: resolver(v.array(customFieldValueSchema)),
+ },
+ },
+ },
+ },
+ }),
+ validator("param", v.object({ projectId: v.string() })),
+ workspaceAccess.fromProject("projectId"),
+ async (c) => {
+ const { projectId } = c.req.valid("param");
+
+ const values = await getCustomFieldValuesByProject(projectId);
+
+ return c.json(values);
+ },
+ )
.get(
"/task/:taskId",
describeRoute({
diff --git a/apps/docs/openapi.json b/apps/docs/openapi.json
index 9fb6a58d5..60d555e70 100644
--- a/apps/docs/openapi.json
+++ b/apps/docs/openapi.json
@@ -3161,6 +3161,41 @@
}
}
},
+ "/custom-field/project/{projectId}/values": {
+ "get": {
+ "operationId": "getCustomFieldValuesByProject",
+ "tags": [
+ "Custom Fields"
+ ],
+ "summary": "Get field values by project",
+ "description": "Get all custom field values for a project",
+ "parameters": [
+ {
+ "name": "projectId",
+ "in": "path",
+ "required": true,
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "List of custom field values with their definitions",
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/CustomFieldValue"
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ },
"/custom-field/task/{taskId}": {
"get": {
"operationId": "getCustomFieldValuesByTask",
diff --git a/apps/web/src/components/backlog-list-view/backlog-task-row.tsx b/apps/web/src/components/backlog-list-view/backlog-task-row.tsx
index 36c28e9b5..93b914512 100644
--- a/apps/web/src/components/backlog-list-view/backlog-task-row.tsx
+++ b/apps/web/src/components/backlog-list-view/backlog-task-row.tsx
@@ -27,7 +27,7 @@ import {
HoverCardTrigger,
} from "@/components/ui/preview-card";
import { useDeleteTask } from "@/hooks/mutations/task/use-delete-task";
-import useGetCustomFieldValuesByTask from "@/hooks/queries/custom-field/use-get-custom-field-values-by-task";
+import useGetCustomFieldValuesByProject from "@/hooks/queries/custom-field/use-get-custom-field-values-by-project";
import useActiveWorkspace from "@/hooks/queries/workspace/use-active-workspace";
import { useGetActiveWorkspaceUsers } from "@/hooks/queries/workspace-users/use-get-active-workspace-users";
import { cn } from "@/lib/cn";
@@ -91,13 +91,21 @@ export default function BacklogTaskRow({ task }: BacklogTaskRowProps) {
);
}, [workspaceUsers, task.userId]);
- const { data: customFieldValues = [] } = useGetCustomFieldValuesByTask(
- task.id,
+ const { data: projectCustomFieldValues = [] } =
+ useGetCustomFieldValuesByProject(task.projectId);
+
+ const customFieldValues = useMemo(
+ () => projectCustomFieldValues.filter((field) => field.taskId === task.id),
+ [projectCustomFieldValues, task.id],
);
- const activeCustomFieldValues = useMemo(() => {
- return customFieldValues.filter((f) => f.value !== null && f.value !== "");
- }, [customFieldValues]);
+ const activeCustomFieldValues = useMemo(
+ () =>
+ customFieldValues.filter(
+ (field) => field.value !== null && field.value !== "",
+ ),
+ [customFieldValues],
+ );
const style: CSSProperties = {
transform: CSS.Transform.toString(transform),
diff --git a/apps/web/src/components/kanban-board/task-card.tsx b/apps/web/src/components/kanban-board/task-card.tsx
index 7860a244f..efa318189 100644
--- a/apps/web/src/components/kanban-board/task-card.tsx
+++ b/apps/web/src/components/kanban-board/task-card.tsx
@@ -28,7 +28,7 @@ import {
HoverCardTrigger,
} from "@/components/ui/preview-card";
import { useDeleteTask } from "@/hooks/mutations/task/use-delete-task";
-import useGetCustomFieldValuesByTask from "@/hooks/queries/custom-field/use-get-custom-field-values-by-task";
+import useGetCustomFieldValuesByProject from "@/hooks/queries/custom-field/use-get-custom-field-values-by-project";
import useActiveWorkspace from "@/hooks/queries/workspace/use-active-workspace";
import { useGetActiveWorkspaceUsers } from "@/hooks/queries/workspace-users/use-get-active-workspace-users";
import {
@@ -114,13 +114,21 @@ function TaskCard({ task, disableDragDrop = false }: TaskCardProps) {
};
};
- const { data: customFieldValues = [] } = useGetCustomFieldValuesByTask(
- task.id,
+ const { data: projectCustomFieldValues = [] } =
+ useGetCustomFieldValuesByProject(task.projectId);
+
+ const customFieldValues = useMemo(
+ () => projectCustomFieldValues.filter((field) => field.taskId === task.id),
+ [projectCustomFieldValues, task.id],
);
- const activeCustomFieldValues = useMemo(() => {
- return customFieldValues.filter((f) => f.value !== null && f.value !== "");
- }, [customFieldValues]);
+ const activeCustomFieldValues = useMemo(
+ () =>
+ customFieldValues.filter(
+ (field) => field.value !== null && field.value !== "",
+ ),
+ [customFieldValues],
+ );
const style: CSSProperties = {
transform: CSS.Transform.toString(transform),
diff --git a/apps/web/src/fetchers/custom-field/get-custom-field-values-by-project.ts b/apps/web/src/fetchers/custom-field/get-custom-field-values-by-project.ts
new file mode 100644
index 000000000..69a2516ea
--- /dev/null
+++ b/apps/web/src/fetchers/custom-field/get-custom-field-values-by-project.ts
@@ -0,0 +1,22 @@
+import { client } from "@kaneo/libs";
+
+async function getCustomFieldValuesByProject({
+ projectId,
+}: {
+ projectId: string;
+}) {
+ const response = await client["custom-field"].project[
+ ":projectId"
+ ].values.$get({
+ param: { projectId },
+ });
+
+ if (!response.ok) {
+ const error = await response.text();
+ throw new Error(error);
+ }
+
+ return response.json();
+}
+
+export default getCustomFieldValuesByProject;
diff --git a/apps/web/src/hooks/queries/custom-field/use-get-custom-field-values-by-project.ts b/apps/web/src/hooks/queries/custom-field/use-get-custom-field-values-by-project.ts
new file mode 100644
index 000000000..12574396e
--- /dev/null
+++ b/apps/web/src/hooks/queries/custom-field/use-get-custom-field-values-by-project.ts
@@ -0,0 +1,22 @@
+import { useQuery } from "@tanstack/react-query";
+import getCustomFieldValuesByProject from "@/fetchers/custom-field/get-custom-field-values-by-project";
+
+export type CustomFieldValue = {
+ id: string;
+ taskId: string;
+ fieldId: string;
+ value: string | null;
+ fieldName: string;
+ fieldType: string;
+ fieldOptions: unknown;
+};
+
+function useGetCustomFieldValuesByProject(projectId: string) {
+ return useQuery<CustomFieldValue[]>({
+ queryKey: ["custom-field-values", projectId],
+ queryFn: () => getCustomFieldValuesByProject({ projectId }),
+ enabled: Boolean(projectId),
+ });
+}
+
+export default useGetCustomFieldValuesByProject;