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.

fix: accept dbOrTx, add subscribeToEvent, fix status lookup,error handling, error toast, french

Tin2ce9f590523b
diff --git a/apps/api/src/activity/index.ts b/apps/api/src/activity/index.ts
index 1d1776496..f14bd7b4b 100644
--- a/apps/api/src/activity/index.ts
+++ b/apps/api/src/activity/index.ts
@@ -201,6 +201,30 @@ subscribeToEvent<{
await createActivity(data.taskId, data.type, data.userId, null, {});
});
+subscribeToEvent<{
+ taskId: string;
+ userId: string;
+ type: string;
+ content: string;
+ eventData: {
+ fromProjectId: string;
+ fromProjectName: string;
+ toProjectId: string;
+ toProjectName: string;
+ oldStatus: string;
+ newStatus: string;
+ };
+}>("task.moved", async (data) => {
+ await createActivity(data.taskId, data.type, data.userId, data.content, {
+ fromProjectId: data.eventData.fromProjectId,
+ fromProjectName: data.eventData.fromProjectName,
+ toProjectId: data.eventData.toProjectId,
+ toProjectName: data.eventData.toProjectName,
+ oldStatus: data.eventData.oldStatus,
+ newStatus: data.eventData.newStatus,
+ });
+});
+
subscribeToEvent<{
taskId: string;
userId: string;
diff --git a/apps/api/src/task/controllers/get-next-task-number.ts b/apps/api/src/task/controllers/get-next-task-number.ts
index a361fdaca..0a7a35a73 100644
--- a/apps/api/src/task/controllers/get-next-task-number.ts
+++ b/apps/api/src/task/controllers/get-next-task-number.ts
@@ -2,8 +2,10 @@ import { eq, max } from "drizzle-orm";
import db from "../../database";
import { taskTable } from "../../database/schema";
-async function getNextTaskNumber(projectId: string) {
- const [result] = await db
+type DbOrTx = typeof db | Parameters<Parameters<typeof db.transaction>[0]>[0];
+
+async function getNextTaskNumber(projectId: string, dbOrTx: DbOrTx = db) {
+ const [result] = await dbOrTx
.select({ maxNumber: max(taskTable.number) })
.from(taskTable)
.where(eq(taskTable.projectId, projectId));
diff --git a/apps/api/src/task/controllers/move-task.ts b/apps/api/src/task/controllers/move-task.ts
index fe16d0a27..cde0c4c26 100644
--- a/apps/api/src/task/controllers/move-task.ts
+++ b/apps/api/src/task/controllers/move-task.ts
@@ -2,14 +2,16 @@ import { and, asc, eq, max } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
- activityTable,
assetTable,
columnTable,
projectTable,
taskTable,
} from "../../database/schema";
+import { publishEvent } from "../../events";
import getNextTaskNumber from "./get-next-task-number";
+type DbOrTx = typeof db | Parameters<Parameters<typeof db.transaction>[0]>[0];
+
function isSameProjectMove(
sourceProjectId: string,
destinationProjectId: string,
@@ -56,11 +58,12 @@ async function resolveDestinationStatus(
}
async function getNextTaskPosition(
+ dbOrTx: DbOrTx,
projectId: string,
status: string,
columnId: string,
) {
- const [maxPositionResult] = await db
+ const [maxPositionResult] = await dbOrTx
.select({ maxPosition: max(taskTable.position) })
.from(taskTable)
.where(
@@ -128,16 +131,17 @@ async function moveTask({
destinationStatus,
);
- const [nextTaskNumber, nextPosition] = await Promise.all([
- getNextTaskNumber(destinationProjectId),
- getNextTaskPosition(
- destinationProjectId,
- resolvedColumn.slug,
- resolvedColumn.id,
- ),
- ]);
-
const movedTask = await db.transaction(async (tx) => {
+ const [nextTaskNumber, nextPosition] = await Promise.all([
+ getNextTaskNumber(destinationProjectId, tx),
+ getNextTaskPosition(
+ tx,
+ destinationProjectId,
+ resolvedColumn.slug,
+ resolvedColumn.id,
+ ),
+ ]);
+
const [updatedTask] = await tx
.update(taskTable)
.set({
@@ -161,24 +165,24 @@ async function moveTask({
.set({ projectId: destinationProjectId })
.where(eq(assetTable.taskId, taskId));
- await tx.insert(activityTable).values({
- taskId,
- type: "task",
- userId,
- content: `Moved task from ${sourceProject.name} to ${destinationProject.name}`,
- eventData: {
- fromProjectId: sourceProject.id,
- fromProjectName: sourceProject.name,
- toProjectId: destinationProject.id,
- toProjectName: destinationProject.name,
- oldStatus: existingTask.status,
- newStatus: resolvedColumn.slug,
- },
- });
-
return updatedTask;
});
+ await publishEvent("task.moved", {
+ taskId,
+ type: "task",
+ userId,
+ content: `Moved task from ${sourceProject.name} to ${destinationProject.name}`,
+ eventData: {
+ fromProjectId: sourceProject.id,
+ fromProjectName: sourceProject.name,
+ toProjectId: destinationProject.id,
+ toProjectName: destinationProject.name,
+ oldStatus: existingTask.status,
+ newStatus: resolvedColumn.slug,
+ },
+ });
+
return {
task: movedTask,
sourceProjectId: sourceProject.id,
diff --git a/apps/web/src/components/task/task-move-popover.tsx b/apps/web/src/components/task/task-move-popover.tsx
index 0e3c25357..2c2bd1d0c 100644
--- a/apps/web/src/components/task/task-move-popover.tsx
+++ b/apps/web/src/components/task/task-move-popover.tsx
@@ -21,7 +21,6 @@ import useGetProjects from "@/hooks/queries/project/use-get-projects";
import { useGetTasks } from "@/hooks/queries/task/use-get-tasks";
import { cn } from "@/lib/cn";
import { getStatusLabel } from "@/lib/i18n/domain";
-import { toast } from "@/lib/toast";
import type Task from "@/types/task";
type TaskMovePopoverProps = {
@@ -44,7 +43,7 @@ export default function TaskMovePopover({
const { data: projects = [] } = useGetProjects({ workspaceId });
const { mutateAsync: moveTask, isPending: isMovePending } = useMoveTask();
const destinationProjectId = selectedProjectId || "";
- const { data: destinationProject } = useGetTasks(destinationProjectId);
+ const { data: destinationProject, isLoading: isProjectLoading, isError: isProjectError } = useGetTasks(destinationProjectId);
const destinationProjects = useMemo(
() => projects.filter((project) => project.id !== task.projectId),
@@ -58,16 +57,16 @@ export default function TaskMovePopover({
const destinationColumns = destinationProject?.columns ?? [];
const canKeepCurrentStatus = destinationColumns.some(
- (column) => column.id === task.status,
+ (column) => column.slug === task.status,
);
- const fallbackStatus = destinationColumns[0]?.id ?? "";
+ const fallbackStatus = destinationColumns[0]?.slug ?? "";
const effectiveStatus = canKeepCurrentStatus
? task.status
: selectedStatus || fallbackStatus;
const selectedStatusLabel = useMemo(() => {
if (!effectiveStatus || destinationColumns.length === 0) return null;
- const column = destinationColumns.find((c) => c.id === effectiveStatus);
+ const column = destinationColumns.find((c) => c.slug === effectiveStatus);
return getStatusLabel(effectiveStatus) || column?.name || null;
}, [destinationColumns, effectiveStatus]);
@@ -102,7 +101,6 @@ export default function TaskMovePopover({
destinationStatus: effectiveStatus,
});
- toast.success(t("tasks:move.success"));
setOpen(false);
startTransition(() => {
navigate({
@@ -114,10 +112,8 @@ export default function TaskMovePopover({
},
});
});
- } catch (error) {
- toast.error(
- error instanceof Error ? error.message : t("tasks:move.error"),
- );
+ } catch {
+ // toast is handled by useMoveTask's onError
}
};
@@ -170,7 +166,21 @@ export default function TaskMovePopover({
</Select>
</div>
- {selectedProjectId && destinationColumns.length > 0 && (
+ {selectedProjectId && isProjectLoading && (
+ <div className="flex items-center justify-center py-2">
+ <span className="text-xs text-muted-foreground">
+ {t("tasks:move.statusLabel")}…
+ </span>
+ </div>
+ )}
+
+ {selectedProjectId && isProjectError && (
+ <p className="text-xs text-destructive">
+ {t("tasks:move.error")}
+ </p>
+ )}
+
+ {selectedProjectId && !isProjectLoading && !isProjectError && destinationColumns.length > 0 && (
<div className="flex flex-col gap-1.5">
<Label className="text-xs text-muted-foreground">
{t("tasks:move.statusLabel")}
@@ -187,8 +197,8 @@ export default function TaskMovePopover({
</SelectTrigger>
<SelectContent>
{destinationColumns.map((column) => (
- <SelectItem key={column.id} value={column.id}>
- {getStatusLabel(column.id) || column.name}
+ <SelectItem key={column.id} value={column.slug}>
+ {getStatusLabel(column.slug) || column.name}
</SelectItem>
))}
</SelectContent>
@@ -209,7 +219,9 @@ export default function TaskMovePopover({
!selectedProjectId ||
!effectiveStatus ||
isMovePending ||
- isPending
+ isPending ||
+ isProjectLoading ||
+ isProjectError
}
className="w-full font-medium"
>
diff --git a/apps/web/src/fetchers/task/move-task.ts b/apps/web/src/fetchers/task/move-task.ts
index bbc5090e0..512f25505 100644
--- a/apps/web/src/fetchers/task/move-task.ts
+++ b/apps/web/src/fetchers/task/move-task.ts
@@ -18,8 +18,17 @@ async function moveTask({
});
if (!response.ok) {
- const error = await response.text();
- throw new Error(error);
+ let message: string;
+ try {
+ const json = await response.json();
+ message =
+ (json as { message?: string; error?: string }).message ||
+ (json as { error?: string }).error ||
+ JSON.stringify(json);
+ } catch {
+ message = (await response.text().catch(() => "")) || `API error ${response.status}`;
+ }
+ throw new Error(message);
}
return response.json();
diff --git a/apps/web/src/hooks/mutations/task/use-move-task.ts b/apps/web/src/hooks/mutations/task/use-move-task.ts
index de67dc960..ac1055ccb 100644
--- a/apps/web/src/hooks/mutations/task/use-move-task.ts
+++ b/apps/web/src/hooks/mutations/task/use-move-task.ts
@@ -1,12 +1,16 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
+import { useTranslation } from "react-i18next";
import moveTask from "@/fetchers/task/move-task";
+import { toast } from "@/lib/toast";
export function useMoveTask() {
+ const { t } = useTranslation();
const queryClient = useQueryClient();
return useMutation({
mutationFn: moveTask,
onSuccess: (result, variables) => {
+ toast.success(t("tasks:move.success"));
queryClient.invalidateQueries({
queryKey: ["task", variables.taskId],
});
@@ -26,5 +30,10 @@ export function useMoveTask() {
queryKey: ["notifications"],
});
},
+ onError: (error) => {
+ toast.error(
+ error instanceof Error ? error.message : t("tasks:move.error"),
+ );
+ },
});
}
diff --git a/i18n/fr-FR.json b/i18n/fr-FR.json
index 2a682d109..805e04377 100644
--- a/i18n/fr-FR.json
+++ b/i18n/fr-FR.json
@@ -1135,7 +1135,7 @@
"projectLabel": "Projet de destination",
"projectPlaceholder": "Sélectionner un projet",
"statusLabel": "Statut de destination",
- "statusHintKeep": "Le statut actuel est déjà disponible dans ce projet.",
+ "statusHintKeep": "Le workflow du projet de destination supporte déjà le statut actuel.",
"statusHintAdjust": "Choisissez le statut à utiliser dans le projet de destination.",
"action": "Déplacer la tâche",
"success": "Tâche déplacée avec succès",