From 37dbf1b1773a79bc741606e14770535545f4f761 Mon Sep 17 00:00:00 2001 From: richardtekula Date: Wed, 28 Jan 2026 07:42:14 +0100 Subject: [PATCH] refactor: Extract notification logic from todo.service.js Create todo-notification.service.js with: - notifyNewTodoAssignment(): push notification for new todo assignments - notifyUpdatedTodoAssignment(): push notification for updated assignments todo.service.js now delegates to the notification service instead of containing inline push notification logic with error handling. Co-Authored-By: Claude Opus 4.5 --- src/services/todo-notification.service.js | 46 +++++++++++++++++++++++ src/services/todo.service.js | 43 ++------------------- 2 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 src/services/todo-notification.service.js diff --git a/src/services/todo-notification.service.js b/src/services/todo-notification.service.js new file mode 100644 index 0000000..b942cbb --- /dev/null +++ b/src/services/todo-notification.service.js @@ -0,0 +1,46 @@ +import { sendPushNotificationToUsers } from './push.service.js'; +import { logger } from '../utils/logger.js'; + +export const notifyNewTodoAssignment = async (assignedUserIds, title, todoId, excludeUserId) => { + if (!assignedUserIds || !Array.isArray(assignedUserIds) || assignedUserIds.length === 0) { + return; + } + + try { + await sendPushNotificationToUsers( + assignedUserIds, + { + title: 'Nová úloha', + body: `Bola vám priradená úloha: ${title}`, + icon: '/icon-192.png', + badge: '/badge-72.png', + data: { url: '/todos', todoId }, + }, + excludeUserId, + ); + } catch (error) { + logger.error('Failed to send push notifications for new todo', error); + } +}; + +export const notifyUpdatedTodoAssignment = async (newlyAssignedUserIds, title, todoId, excludeUserId) => { + if (!newlyAssignedUserIds || newlyAssignedUserIds.length === 0) { + return; + } + + try { + await sendPushNotificationToUsers( + newlyAssignedUserIds, + { + title: 'Priradená úloha', + body: `Bola vám priradená úloha: ${title}`, + icon: '/icon-192.png', + badge: '/badge-72.png', + data: { url: '/todos', todoId }, + }, + excludeUserId, + ); + } catch (error) { + logger.error('Failed to send push notifications for updated todo', error); + } +}; diff --git a/src/services/todo.service.js b/src/services/todo.service.js index c0b3dcd..a9fbee9 100644 --- a/src/services/todo.service.js +++ b/src/services/todo.service.js @@ -3,8 +3,7 @@ import { todos, todoUsers, notes, projects, companies, users } from '../db/schem import { eq, desc, ilike, or, and, inArray, lt, ne, sql } from 'drizzle-orm'; import { NotFoundError } from '../utils/errors.js'; import { getAccessibleResourceIds } from '../middlewares/auth/resourceAccessMiddleware.js'; -import { sendPushNotificationToUsers } from './push.service.js'; -import { logger } from '../utils/logger.js'; +import { notifyNewTodoAssignment, notifyUpdatedTodoAssignment } from './todo-notification.service.js'; import { logTodoCreated, logTodoDeleted, logTodoCompleted, logTodoUpdated, logTodoUncompleted, } from './audit.service.js'; @@ -213,23 +212,7 @@ export const createTodo = async (userId, data, auditContext = null) => { } // Send push notifications to assigned users (excluding creator) - if (assignedUserIds && Array.isArray(assignedUserIds) && assignedUserIds.length > 0) { - try { - await sendPushNotificationToUsers( - assignedUserIds, - { - title: 'Nová úloha', - body: `Bola vám priradená úloha: ${title}`, - icon: '/icon-192.png', - badge: '/badge-72.png', - data: { url: '/todos', todoId: newTodo.id }, - }, - userId // exclude creator - ); - } catch (error) { - logger.error('Failed to send push notifications for new todo', error); - } - } + await notifyNewTodoAssignment(assignedUserIds, title, newTodo.id, userId); if (auditContext) { await logTodoCreated(auditContext.userId, newTodo.id, newTodo.title, auditContext.ipAddress, auditContext.userAgent); @@ -333,29 +316,11 @@ export const updateTodo = async (todoId, data, updatedByUserId = null, auditCont await db.insert(todoUsers).values(todoUserInserts); - // Find newly assigned users (not in existing list) + // Send push notifications to newly assigned users const newlyAssignedUserIds = assignedUserIds.filter( id => !existingUserIds.includes(id) ); - - // Send push notifications to newly assigned users - if (newlyAssignedUserIds.length > 0) { - try { - await sendPushNotificationToUsers( - newlyAssignedUserIds, - { - title: 'Priradená úloha', - body: `Bola vám priradená úloha: ${updated.title}`, - icon: '/icon-192.png', - badge: '/badge-72.png', - data: { url: '/todos', todoId: todoId }, - }, - updatedByUserId // exclude user making the change - ); - } catch (error) { - logger.error('Failed to send push notifications for updated todo', error); - } - } + await notifyUpdatedTodoAssignment(newlyAssignedUserIds, updated.title, todoId, updatedByUserId); } }