Initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
class DataStitchingService {
|
||||
constructor({ usersRepository }) {
|
||||
this.usersRepository = usersRepository;
|
||||
}
|
||||
|
||||
async populateBetsWithUsers(bets) {
|
||||
if (!bets || bets.length === 0) return [];
|
||||
|
||||
const userIds = new Set();
|
||||
bets.forEach((bet) => {
|
||||
bet.participants.forEach((p) => userIds.add(p.user));
|
||||
if (bet.winner) {
|
||||
userIds.add(bet.winner);
|
||||
}
|
||||
});
|
||||
|
||||
if (userIds.size === 0)
|
||||
return bets.map((b) => (typeof b.toJSON === 'function' ? b.toJSON() : b));
|
||||
|
||||
const users = await this.usersRepository.findAll({
|
||||
where: { id: Array.from(userIds) },
|
||||
});
|
||||
const userMap = new Map(
|
||||
users.map((u) => [u.id, typeof u.toJSON === 'function' ? u.toJSON() : u])
|
||||
);
|
||||
|
||||
return bets.map((bet) => {
|
||||
const betJson = typeof bet.toJSON === 'function' ? bet.toJSON() : bet;
|
||||
betJson.participants = betJson.participants.map((p) => ({
|
||||
...p,
|
||||
user: userMap.get(p.user) || null,
|
||||
}));
|
||||
if (betJson.winner) {
|
||||
betJson.winner = userMap.get(betJson.winner) || null;
|
||||
}
|
||||
return betJson;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DataStitchingService;
|
||||
Reference in New Issue
Block a user