|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { Participant } from '../entities/participant.entity'; |
| 5 | +import { Split } from '../entities/split.entity'; |
| 6 | +import { Activity } from '../entities/activity.entity'; |
| 7 | +import { DashboardSummaryDto, DashboardActivityDto, QuickAction } from './dto/dashboard.dto'; |
| 8 | + |
| 9 | +@Injectable() |
| 10 | +export class DashboardService { |
| 11 | + constructor( |
| 12 | + @InjectRepository(Participant) |
| 13 | + private readonly participantRepo: Repository<Participant>, |
| 14 | + @InjectRepository(Split) |
| 15 | + private readonly splitRepo: Repository<Split>, |
| 16 | + @InjectRepository(Activity) |
| 17 | + private readonly activityRepo: Repository<Activity>, |
| 18 | + ) {} |
| 19 | + |
| 20 | + async getSummary(userId: string): Promise<DashboardSummaryDto> { |
| 21 | + // Run all aggregation queries in parallel for efficiency |
| 22 | + const [owedResult, owedToUserResult, activeSplitsCount, splitsCreatedCount, unreadCount] = |
| 23 | + await Promise.all([ |
| 24 | + // Total the user owes (amountOwed - amountPaid) across non-completed splits |
| 25 | + this.participantRepo |
| 26 | + .createQueryBuilder('p') |
| 27 | + .select('COALESCE(SUM((p.amountOwed - p.amountPaid)::numeric), 0)', 'total') |
| 28 | + .innerJoin(Split, 's', 's.id = p.splitId') |
| 29 | + .where('p.userId = :userId', { userId }) |
| 30 | + .andWhere("p.status != 'paid'") |
| 31 | + .andWhere("s.status != 'completed'") |
| 32 | + .andWhere('s.deletedAt IS NULL') |
| 33 | + .getRawOne<{ total: string }>(), |
| 34 | + |
| 35 | + // Total owed to the user: sum of what others owe on splits the user created |
| 36 | + this.participantRepo |
| 37 | + .createQueryBuilder('p') |
| 38 | + .select('COALESCE(SUM((p.amountOwed - p.amountPaid)::numeric), 0)', 'total') |
| 39 | + .innerJoin(Split, 's', 's.id = p.splitId') |
| 40 | + .where('s.creatorWalletAddress = :userId', { userId }) |
| 41 | + .andWhere('p.userId != :userId', { userId }) |
| 42 | + .andWhere("p.status != 'paid'") |
| 43 | + .andWhere("s.status != 'completed'") |
| 44 | + .andWhere('s.deletedAt IS NULL') |
| 45 | + .getRawOne<{ total: string }>(), |
| 46 | + |
| 47 | + // Active splits the user participates in |
| 48 | + this.participantRepo |
| 49 | + .createQueryBuilder('p') |
| 50 | + .innerJoin(Split, 's', 's.id = p.splitId') |
| 51 | + .where('p.userId = :userId', { userId }) |
| 52 | + .andWhere("s.status != 'completed'") |
| 53 | + .andWhere('s.deletedAt IS NULL') |
| 54 | + .getCount(), |
| 55 | + |
| 56 | + // Splits the user created that are still active |
| 57 | + this.splitRepo |
| 58 | + .createQueryBuilder('s') |
| 59 | + .where('s.creatorWalletAddress = :userId', { userId }) |
| 60 | + .andWhere("s.status != 'completed'") |
| 61 | + .andWhere('s.deletedAt IS NULL') |
| 62 | + .getCount(), |
| 63 | + |
| 64 | + // Unread activity count |
| 65 | + this.activityRepo.count({ where: { userId, isRead: false } }), |
| 66 | + ]); |
| 67 | + |
| 68 | + const totalOwed = parseFloat(owedResult?.total ?? '0'); |
| 69 | + const totalOwedToUser = parseFloat(owedToUserResult?.total ?? '0'); |
| 70 | + |
| 71 | + const quickActions: QuickAction[] = [ |
| 72 | + { id: 'new-split', label: 'New Split', route: '/splits/new' }, |
| 73 | + { id: 'my-splits', label: 'My Splits', route: '/splits', badge: activeSplitsCount }, |
| 74 | + { id: 'activity', label: 'Activity', route: '/activity', badge: unreadCount || undefined }, |
| 75 | + { id: 'analytics', label: 'Analytics', route: '/analytics' }, |
| 76 | + ]; |
| 77 | + |
| 78 | + return { |
| 79 | + totalOwed, |
| 80 | + totalOwedToUser, |
| 81 | + activeSplits: activeSplitsCount, |
| 82 | + splitsCreated: splitsCreatedCount, |
| 83 | + unreadNotifications: unreadCount, |
| 84 | + quickActions, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + async getActivity( |
| 89 | + userId: string, |
| 90 | + page: number, |
| 91 | + limit: number, |
| 92 | + ): Promise<DashboardActivityDto> { |
| 93 | + const [data, total] = await this.activityRepo.findAndCount({ |
| 94 | + where: { userId }, |
| 95 | + order: { createdAt: 'DESC' }, |
| 96 | + skip: (page - 1) * limit, |
| 97 | + take: limit, |
| 98 | + }); |
| 99 | + |
| 100 | + const unreadCount = await this.activityRepo.count({ |
| 101 | + where: { userId, isRead: false }, |
| 102 | + }); |
| 103 | + |
| 104 | + return { |
| 105 | + data: data.map((a) => ({ |
| 106 | + id: a.id, |
| 107 | + activityType: a.activityType, |
| 108 | + splitId: a.splitId, |
| 109 | + metadata: a.metadata, |
| 110 | + isRead: a.isRead, |
| 111 | + createdAt: a.createdAt, |
| 112 | + })), |
| 113 | + total, |
| 114 | + page, |
| 115 | + limit, |
| 116 | + hasMore: page * limit < total, |
| 117 | + unreadCount, |
| 118 | + }; |
| 119 | + } |
| 120 | +} |
0 commit comments