158 lines
4.2 KiB
Markdown
158 lines
4.2 KiB
Markdown
# Leantime API Fixes - Mark Notifications as Read
|
|
|
|
**Date**: 2026-01-01
|
|
**Issue**: Mark all as read failing due to incorrect API method names
|
|
**Status**: ✅ Fixed
|
|
|
|
---
|
|
|
|
## 🔍 Issues Found
|
|
|
|
### Issue 1: Incorrect Method Name for Single Notification
|
|
|
|
**Current Code** (WRONG):
|
|
```typescript
|
|
method: 'leantime.rpc.Notifications.Notifications.markNotificationAsRead'
|
|
params: {
|
|
userId: leantimeUserId,
|
|
notificationId: parseInt(sourceId) // Wrong parameter name
|
|
}
|
|
```
|
|
|
|
**Leantime Documentation** (CORRECT):
|
|
```typescript
|
|
method: 'leantime.rpc.Notifications.Notifications.markNotificationRead' // No "As" in method name
|
|
params: {
|
|
id: parseInt(sourceId), // Parameter is "id", not "notificationId"
|
|
userId: leantimeUserId
|
|
}
|
|
```
|
|
|
|
**Fix Applied**: ✅ Changed method name and parameter names to match Leantime API
|
|
|
|
---
|
|
|
|
### Issue 2: No "Mark All" Method Exists
|
|
|
|
**Problem**:
|
|
- Leantime API does NOT have a `markAllNotificationsAsRead` method
|
|
- Current code tries to call a non-existent method
|
|
|
|
**Solution**:
|
|
- Fetch all unread notifications
|
|
- Mark each one individually using `markNotificationRead`
|
|
- Process in parallel for better performance
|
|
|
|
**Fix Applied**: ✅ Implemented loop-based approach to mark all notifications individually
|
|
|
|
---
|
|
|
|
## ✅ Changes Made
|
|
|
|
### 1. Fixed `markAsRead` Method
|
|
|
|
**File**: `lib/services/notifications/leantime-adapter.ts`
|
|
|
|
**Changes**:
|
|
- ✅ Method name: `markNotificationAsRead` → `markNotificationRead`
|
|
- ✅ Parameter: `notificationId` → `id`
|
|
- ✅ Parameter order: `id` first, then `userId` (matching Leantime docs)
|
|
- ✅ Added request logging
|
|
|
|
---
|
|
|
|
### 2. Fixed `markAllAsRead` Method
|
|
|
|
**File**: `lib/services/notifications/leantime-adapter.ts`
|
|
|
|
**New Implementation**:
|
|
1. Fetch all unread notifications (up to 1000)
|
|
2. Filter to get only unread ones
|
|
3. Mark each notification individually using `markNotificationRead`
|
|
4. Process in parallel using `Promise.all()`
|
|
5. Return success if majority succeed
|
|
|
|
**Benefits**:
|
|
- ✅ Works with actual Leantime API
|
|
- ✅ Handles partial failures gracefully
|
|
- ✅ Parallel processing for better performance
|
|
- ✅ Detailed logging for each notification
|
|
|
|
---
|
|
|
|
## 📊 Expected Behavior After Fix
|
|
|
|
### Mark Single Notification as Read
|
|
|
|
**Before**: ❌ Failed (wrong method name)
|
|
**After**: ✅ Should work correctly
|
|
|
|
**Logs**:
|
|
```
|
|
[LEANTIME_ADAPTER] markAsRead - Request body: {"method":"markNotificationRead",...}
|
|
[LEANTIME_ADAPTER] markAsRead - Success: true
|
|
```
|
|
|
|
---
|
|
|
|
### Mark All Notifications as Read
|
|
|
|
**Before**: ❌ Failed (method doesn't exist)
|
|
**After**: ✅ Should work (marks each individually)
|
|
|
|
**Logs**:
|
|
```
|
|
[LEANTIME_ADAPTER] markAllAsRead - Fetching all unread notifications
|
|
[LEANTIME_ADAPTER] markAllAsRead - Found 66 unread notifications to mark
|
|
[LEANTIME_ADAPTER] markAllAsRead - Results: 66 succeeded, 0 failed out of 66 total
|
|
[LEANTIME_ADAPTER] markAllAsRead - Overall success: true
|
|
```
|
|
|
|
---
|
|
|
|
## 🎯 Count vs Display Issue
|
|
|
|
**Current Situation**:
|
|
- Count: 66 unread (from first 100 notifications)
|
|
- Display: 10 notifications shown (pagination)
|
|
|
|
**Why**:
|
|
- `getNotificationCount()` fetches first 100 notifications and counts unread
|
|
- `getNotifications()` with default limit=20 shows first 10-20
|
|
- This is expected behavior but can be confusing
|
|
|
|
**Options**:
|
|
1. **Accept limitation**: Document that count is based on first 100
|
|
2. **Fetch all for count**: More accurate but slower
|
|
3. **Use dedicated count API**: If Leantime provides one
|
|
4. **Show "66+ unread"**: If count reaches 100, indicate there may be more
|
|
|
|
**Recommendation**: Keep current behavior but add a note in UI if count = 100 (may have more)
|
|
|
|
---
|
|
|
|
## 🚀 Next Steps
|
|
|
|
1. ✅ **Test Mark Single as Read**: Should work now with correct method name
|
|
2. ✅ **Test Mark All as Read**: Should work by marking each individually
|
|
3. ⏳ **Verify Count Updates**: After marking, count should decrease
|
|
4. ⏳ **Monitor Performance**: Marking 66 notifications individually may take a few seconds
|
|
|
|
---
|
|
|
|
## 📝 Summary
|
|
|
|
**Fixes Applied**:
|
|
1. ✅ Fixed `markAsRead` method name and parameters
|
|
2. ✅ Implemented `markAllAsRead` using individual marking approach
|
|
3. ✅ Added comprehensive logging
|
|
|
|
**Status**: Ready for testing after `rm -rf .next && npm run build`
|
|
|
|
**Expected Result**: Mark all as read should now work correctly
|
|
|
|
---
|
|
|
|
**Generated**: 2026-01-01
|
|
|