forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreputation-processor.ts
More file actions
89 lines (77 loc) · 2.57 KB
/
Copy pathreputation-processor.ts
File metadata and controls
89 lines (77 loc) · 2.57 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
/**
* Reputation Update Processor
*
* Handles reputation score calculations and updates.
* Aggregates ratings and maintains user reputation history.
*/
import { ReputationUpdatePayload, JobResult } from '../types';
import { createLogger } from '../../logger';
/**
* Process reputation update job
*
* @param payload - Reputation update data
* @returns Job result with updated reputation score
* @throws Error if validation fails
*/
export async function processReputationUpdate(
payload: ReputationUpdatePayload,
): Promise<JobResult> {
const log = createLogger({
processor: 'reputation',
...(payload.correlationId && { correlationId: payload.correlationId }),
...(payload.requestId && { requestId: payload.requestId }),
});
// Validate user ID
if (!payload.userId || payload.userId.length < 5) {
log.warn('Reputation update rejected: invalid userId');
throw new Error('Invalid user ID');
}
// Validate rating range
if (payload.rating < 1 || payload.rating > 5) {
log.warn('Reputation update rejected: rating out of range', { rating: payload.rating });
throw new Error('Rating must be between 1 and 5');
}
// Validate contract ID
if (!payload.contractId) {
log.warn('Reputation update rejected: missing contractId');
throw new Error('Contract ID is required');
}
log.info('Processing reputation update', { rating: payload.rating });
// Calculate new reputation score
const newScore = await calculateReputationScore(payload);
// Store reputation update (simulate database operation)
await storeReputationUpdate(payload, newScore, log);
log.info('Reputation update stored', { newScore });
return {
success: true,
message: `Reputation updated for user ${payload.userId}`,
data: {
userId: payload.userId,
newScore,
rating: payload.rating,
contractId: payload.contractId,
},
};
}
/**
* Calculate new reputation score based on rating.
* In production, this would aggregate historical ratings.
*/
async function calculateReputationScore(
payload: ReputationUpdatePayload,
): Promise<number> {
await new Promise((resolve) => setTimeout(resolve, 200));
return Math.round((payload.rating / 5) * 100);
}
/**
* Store reputation update in database
*/
async function storeReputationUpdate(
payload: ReputationUpdatePayload,
score: number,
log: ReturnType<typeof createLogger>,
): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 100));
// userId is kept in structured field, not interpolated into the message string
log.debug('Reputation record persisted', { score });
}