All files / src/controllers/public notification.controller.ts

52.94% Statements 9/17
100% Branches 0/0
0% Functions 0/3
46.67% Lines 7/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x 1x     1x                                                                                                   1x                                                    
import {NotificationService} from '@logchain/services';
import {authenticate} from '@loopback/authentication';
import {inject, service} from '@loopback/core';
import {get, HttpErrors, param, patch} from '@loopback/rest';
import {SecurityBindings, UserProfile} from '@loopback/security';
 
@authenticate('aws-cognito')
export class NotificationController {
  constructor(
    @service(NotificationService)
    private notificationService: NotificationService,
 
    @inject(SecurityBindings.USER)
    private currentUser: UserProfile,
  ) {}
 
  /**
   * GET /notifications
   * Lấy toàn bộ notification của user hiện tại, sort Newest → Oldest
   */
//   @get('/notifications')
//   async getNotifications() {
//     const cognitoId = this.currentUser[SecurityBindings.ID];
//     if (!cognitoId) throw new HttpErrors.Unauthorized();
 
//     const items = await this.notificationService.getNotificationsByUser(cognitoId);
 
//     // Format response: timestamp theo 24h hh:mm
//     return items.map(item => ({
//       id: item.id,
//       message: item.message,
//       uri: item.uri,
//       createdAt: item.createdAt, // ISO string — FE tự format
//       timestamp: formatTimestamp(item.createdAt), // "hh:mm" 24h
//       readStatus: item.readStatus === 'true', // boolean cho FE dễ dùng
//       isNew: item.readStatus === 'false',
//     }));
//   }
 
  /**
   * GET /notifications/count
   * Trả về số unread — dùng cho bell badge
   */
//   @get('/notifications/count')
//   async getUnreadCount() {
//     const cognitoId = this.currentUser[SecurityBindings.ID];
//     if (!cognitoId) throw new HttpErrors.Unauthorized();
 
//     const count = await this.notificationService.countUnread(cognitoId);
//     return {count};
//   }
 
  /**
   * PATCH /notifications/{id}/read
   * Đánh dấu 1 notification là đã đọc (khi user click vào)
   */
  @patch('/notifications/{id}/read')
  async markAsRead(@param.path.string('id') id: string) {
    await this.notificationService.markAsRead(id);
    return {success: true};
  }
 
//   /**
//    * PATCH /notifications/read-all
//    * Đánh dấu tất cả là đã đọc
//    */
//   @patch('/notifications/read-all')
//   async markAllAsRead() {
//     const cognitoId = this.currentUser[SecurityBindings.ID];
//     if (!cognitoId) throw new HttpErrors.Unauthorized();
 
//     await this.notificationService.markAllAsRead(cognitoId);
//     return {success: true};
//   }
}
 
// ─── Helper: format ISO string → "hh:mm" (24h) ──────────────────────────────
function formatTimestamp(isoString: string): string {
  const d = new Date(isoString);
  const hh = String(d.getHours()).padStart(2, '0');
  const mm = String(d.getMinutes()).padStart(2, '0');
  return `${hh}:${mm}`;
}