forked from rinafcode/teachLink_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessaging.service.ts
More file actions
131 lines (122 loc) · 4.33 KB
/
Copy pathmessaging.service.ts
File metadata and controls
131 lines (122 loc) · 4.33 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Injectable, Logger } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { CreateMessageDto } from './message.dto';
import { Message } from './message.entity';
import { InjectQueue } from '@nestjs/bull';
import { Queue, Job } from 'bull';
import { QUEUE_NAMES } from '../common/constants/queue.constants';
import { TracingService } from './tracing/tracing.service';
import { enrichWithCorrelation } from '../queues/utils/correlation-job.util';
import { PaginationService } from '../common/services/pagination.service';
import { PaginationQueryDto } from '../common/dto/pagination.dto';
import { clampLimit } from '../common/utils/pagination.utils';
/**
* Provides messaging operations.
*/
@Injectable()
export class MessagingService {
private readonly logger = new Logger(MessagingService.name);
constructor(
@InjectQueue(QUEUE_NAMES.MESSAGE_QUEUE)
private readonly messageQueue: Queue,
private readonly tracingService: TracingService,
@InjectRepository(Message)
private readonly messageRepo: Repository<Message>,
private readonly paginationService: PaginationService,
) {}
/**
* Executes add Message To Queue.
* @param data The data to process.
* @param options The options.
* @returns The resulting job<any>.
*/
async createMessage(dto: CreateMessageDto): Promise<Message> {
const span = this.tracingService.startSpan('create-message');
try {
const message = this.messageRepo.create({
...dto,
readAt: null,
});
const saved = await this.messageRepo.save(message);
// Add to queue for async processing if needed.
// Enrich payload with the active correlation ID so the worker can
// restore the originating request's tracing context (#829).
await this.messageQueue.add(enrichWithCorrelation({ ...saved }));
return saved;
} catch (error) {
this.logger.error('Failed to create message', error);
throw error;
} finally {
this.tracingService.endSpan(span);
}
}
async getConversation(
userId: string,
otherUserId: string,
query?: PaginationQueryDto,
): Promise<any> {
const span = this.tracingService.startSpan('get-conversation');
try {
const limit = clampLimit(query?.limit);
const offset =
query?.offset ?? (query?.cursor ? undefined : ((query?.page ?? 1) - 1) * limit);
const qb = this.messageRepo
.createQueryBuilder('message')
.where(
'(message.senderId = :userId AND message.recipientId = :otherUserId) OR (message.senderId = :otherUserId AND message.recipientId = :userId)',
{ userId, otherUserId },
);
return this.paginationService.paginate(qb, query?.cursor, limit, offset, 'createdAt');
} finally {
this.tracingService.endSpan(span);
}
}
async markAsRead(messageId: string): Promise<void> {
const span = this.tracingService.startSpan('mark-as-read');
try {
await this.messageRepo.update(messageId, { readAt: new Date() });
} finally {
this.tracingService.endSpan(span);
}
}
/**
* Processes messages.
*/
async processMessages(): Promise<void> {
this.messageQueue.process(async (job: Job) => {
const span = this.tracingService.startSpan('process-message');
try {
this.logger.log(`Processing message: ${job.id}`);
// Process the message here
// This would typically emit events or call other services
await this.handleMessage(job.data);
} catch (error) {
this.logger.error(`Failed to process message ${job.id}`, error);
throw error;
} finally {
this.tracingService.endSpan(span);
}
});
}
private async handleMessage(data: any): Promise<void> {
// Implement message handling logic
this.logger.log('Handling message:', data);
}
/**
* Retrieves queue Status.
* @returns The operation result.
*/
async getQueueStatus(): Promise<any> {
const waiting = await this.messageQueue.getWaiting();
const active = await this.messageQueue.getActive();
const completed = await this.messageQueue.getCompleted();
const failed = await this.messageQueue.getFailed();
return {
waiting: waiting.length,
active: active.length,
completed: completed.length,
failed: failed.length,
};
}
}