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.

feat(api): persist structured activity and notification events for i18n

Tin51c7a8c56b45
diff --git a/apps/api/src/activity/controllers/create-activity.ts b/apps/api/src/activity/controllers/create-activity.ts
index c84562cd5..22b8cdd89 100644
--- a/apps/api/src/activity/controllers/create-activity.ts
+++ b/apps/api/src/activity/controllers/create-activity.ts
@@ -5,13 +5,15 @@ async function createActivity(
taskId: string,
type: string,
userId: string,
- content: string,
+ content: string | null,
+ eventData?: Record<string, unknown> | null,
) {
const activity = await db.insert(activityTable).values({
taskId,
type,
userId,
content,
+ eventData: eventData ?? null,
});
return activity;
}
diff --git a/apps/api/src/activity/index.ts b/apps/api/src/activity/index.ts
index 069a2d139..4e9068ae7 100644
--- a/apps/api/src/activity/index.ts
+++ b/apps/api/src/activity/index.ts
@@ -13,27 +13,6 @@ import deleteComment from "./controllers/delete-comment";
import getActivities from "./controllers/get-activities";
import updateComment from "./controllers/update-comment";
-function toDisplayCase(value: string) {
- return value
- .replace(/[-_]/g, " ")
- .split(" ")
- .filter(Boolean)
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase())
- .join(" ");
-}
-
-function formatActivityDate(value: Date | string | null | undefined) {
- if (!value) return null;
- const date = value instanceof Date ? value : new Date(value);
- if (Number.isNaN(date.getTime())) return null;
-
- return new Intl.DateTimeFormat("en-US", {
- month: "short",
- day: "numeric",
- year: "numeric",
- }).format(date);
-}
-
const activity = new Hono<{
Variables: {
userId: string;
@@ -82,14 +61,21 @@ const activity = new Hono<{
v.object({
taskId: v.string(),
userId: v.string(),
- message: v.string(),
+ message: v.nullable(v.string()),
type: v.string(),
+ eventData: v.optional(v.nullable(v.record(v.string(), v.unknown()))),
}),
),
workspaceAccess.fromTaskId(),
async (c) => {
- const { taskId, userId, message, type } = c.req.valid("json");
- const activity = await createActivity(taskId, type, userId, message);
+ const { taskId, userId, message, type, eventData } = c.req.valid("json");
+ const activity = await createActivity(
+ taskId,
+ type,
+ userId,
+ message,
+ eventData,
+ );
return c.json(activity);
},
)
@@ -212,7 +198,7 @@ subscribeToEvent<{
if (!data.userId || !data.taskId || !data.type || !data.content) {
return;
}
- await createActivity(data.taskId, data.type, data.userId, data.content);
+ await createActivity(data.taskId, data.type, data.userId, data.content, null);
});
subscribeToEvent<{
@@ -228,7 +214,11 @@ subscribeToEvent<{
data.taskId,
data.type,
data.userId,
- `changed status from ${toDisplayCase(data.oldStatus)} to ${toDisplayCase(data.newStatus)}`,
+ null,
+ {
+ oldStatus: data.oldStatus,
+ newStatus: data.newStatus,
+ },
);
});
@@ -244,7 +234,11 @@ subscribeToEvent<{
data.taskId,
data.type,
data.userId,
- `changed priority from ${toDisplayCase(data.oldPriority)} to ${toDisplayCase(data.newPriority)}`,
+ null,
+ {
+ oldPriority: data.oldPriority,
+ newPriority: data.newPriority,
+ },
);
});
@@ -254,12 +248,7 @@ subscribeToEvent<{
title: string;
type: string;
}>("task.unassigned", async (data) => {
- await createActivity(
- data.taskId,
- data.type,
- data.userId,
- "unassigned the task",
- );
+ await createActivity(data.taskId, data.type, data.userId, null, {});
});
subscribeToEvent<{
@@ -271,21 +260,16 @@ subscribeToEvent<{
title: string;
type: string;
}>("task.assignee_changed", async (data) => {
- if (data.userId === data.newAssigneeId) {
- await createActivity(
- data.taskId,
- data.type,
- data.userId,
- "assigned the task to themselves",
- );
- return;
- }
-
await createActivity(
data.taskId,
data.type,
data.userId,
- `assigned the task to [[user:${data.newAssigneeId}|${data.newAssignee}]]`,
+ null,
+ {
+ newAssigneeId: data.newAssigneeId,
+ newAssignee: data.newAssignee,
+ isSelfAssigned: data.userId === data.newAssigneeId,
+ },
);
});
@@ -297,26 +281,16 @@ subscribeToEvent<{
title: string;
type: string;
}>("task.due_date_changed", async (data) => {
- const oldDate = formatActivityDate(data.oldDueDate);
-
- if (!data.newDueDate) {
- await createActivity(
- data.taskId,
- data.type,
- data.userId,
- "cleared the due date",
- );
- return;
- }
-
- const newDate = formatActivityDate(data.newDueDate);
- if (!newDate) return;
-
- const message = oldDate
- ? `changed due date from ${oldDate} to ${newDate}`
- : `set due date to ${newDate}`;
-
- await createActivity(data.taskId, data.type, data.userId, message);
+ await createActivity(data.taskId, data.type, data.userId, null, {
+ oldDueDate:
+ data.oldDueDate instanceof Date
+ ? data.oldDueDate.toISOString()
+ : data.oldDueDate,
+ newDueDate:
+ data.newDueDate instanceof Date
+ ? data.newDueDate.toISOString()
+ : data.newDueDate,
+ });
});
subscribeToEvent<{
@@ -331,7 +305,11 @@ subscribeToEvent<{
data.taskId,
data.type,
data.userId,
- `changed title from "${data.oldTitle}" to "${data.newTitle}"`,
+ null,
+ {
+ oldTitle: data.oldTitle,
+ newTitle: data.newTitle,
+ },
);
});
diff --git a/apps/api/src/notification/controllers/create-notification.ts b/apps/api/src/notification/controllers/create-notification.ts
index fa3b7ef1c..525a0d890 100644
--- a/apps/api/src/notification/controllers/create-notification.ts
+++ b/apps/api/src/notification/controllers/create-notification.ts
@@ -8,13 +8,15 @@ async function createNotification({
title,
content,
type,
+ eventData,
resourceId,
resourceType,
}: {
userId: string;
- title: string;
- content?: string;
+ title?: string | null;
+ content?: string | null;
type?: string;
+ eventData?: Record<string, unknown> | null;
resourceId?: string;
resourceType?: string;
}) {
@@ -23,9 +25,10 @@ async function createNotification({
.values({
id: createId(),
userId,
- title,
- content: content || "",
+ title: title ?? null,
+ content: content ?? null,
type: type || "info",
+ eventData: eventData ?? null,
resourceId: resourceId || null,
resourceType: resourceType || null,
})
diff --git a/apps/api/src/notification/index.ts b/apps/api/src/notification/index.ts
index b54073470..bd82b09bb 100644
--- a/apps/api/src/notification/index.ts
+++ b/apps/api/src/notification/index.ts
@@ -60,15 +60,23 @@ const notification = new Hono<{
validator(
"json",
v.object({
- title: v.string(),
- message: v.string(),
+ title: v.optional(v.nullable(v.string())),
+ message: v.optional(v.nullable(v.string())),
type: v.string(),
+ eventData: v.optional(v.nullable(v.record(v.string(), v.unknown()))),
relatedEntityId: v.optional(v.string()),
relatedEntityType: v.optional(v.string()),
}),
),
async (c) => {
- const { title, message, type, relatedEntityId, relatedEntityType } =
+ const {
+ title,
+ message,
+ type,
+ eventData,
+ relatedEntityId,
+ relatedEntityType,
+ } =
c.req.valid("json");
const userId = c.get("userId");
const notification = await createNotification({
@@ -76,6 +84,7 @@ const notification = new Hono<{
title,
content: message,
type,
+ eventData,
resourceId: relatedEntityId,
resourceType: relatedEntityType,
});
@@ -157,9 +166,10 @@ subscribeToEvent<{
if (data.userId) {
await createNotification({
userId: data.userId,
- title: "New Task Created",
- content: `Task "${data.title}" was created`,
type: "task_created",
+ eventData: {
+ taskTitle: data.title,
+ },
resourceId: data.taskId,
resourceType: "task",
});
@@ -175,9 +185,10 @@ subscribeToEvent<{
if (data.ownerId) {
await createNotification({
userId: data.ownerId,
- title: "Workspace created",
- content: `Your workspace "${data.workspaceName}" has been created successfully`,
type: "workspace_created",
+ eventData: {
+ workspaceName: data.workspaceName,
+ },
resourceId: data.workspaceId,
resourceType: "workspace",
});
@@ -195,9 +206,12 @@ subscribeToEvent<{
if (data.assigneeId && data.assigneeId !== data.userId) {
await createNotification({
userId: data.assigneeId,
- title: "Task status changed",
- content: `Task "${data.title}" status changed from "${data.oldStatus}" to "${data.newStatus}"`,
type: "task_status_changed",
+ eventData: {
+ taskTitle: data.title,
+ oldStatus: data.oldStatus,
+ newStatus: data.newStatus,
+ },
resourceId: data.taskId,
resourceType: "task",
});
@@ -215,9 +229,10 @@ subscribeToEvent<{
if (data.newAssigneeId) {
await createNotification({
userId: data.newAssigneeId,
- title: "Task assigned to you",
- content: `You have been assigned to task: ${data.title}`,
type: "task_assignee_changed",
+ eventData: {
+ taskTitle: data.title,
+ },
resourceId: data.taskId,
resourceType: "task",
});
@@ -234,9 +249,10 @@ subscribeToEvent<{
if (data.taskOwnerId && data.taskOwnerId !== data.userId) {
await createNotification({
userId: data.taskOwnerId,
- title: "Time tracking started",
- content: `Time tracking started on task${data.taskTitle ? `: ${data.taskTitle}` : ""}`,
type: "time_entry_created",
+ eventData: {
+ taskTitle: data.taskTitle ?? null,
+ },
resourceId: data.taskId,
resourceType: "task",
});