File size: 1,571 Bytes
94ad3aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bce154c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6112db7
 
 
 
 
 
 
 
bce154c
 
 
 
 
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
/**
 * Repositorio de acceso a datos para el modelo Watchlist.
 *
 * Responsabilidades:
 *   - create(data)              β†’ inserta entrada en watchlist.
 *   - findByUser(userId)        β†’ lista con datos del mercado asociado.
 *   - deleteByUserAndMarket(...) β†’ elimina entrada especifica.
 *   - findAllWithThreshold()    β†’ entradas con alertThreshold definido (para alertas).
 *
 * Constraint:
 *   - @@unique([userId, marketId]) β†’ un usuario no puede duplicar un mercado.
 *
 * Todas las operaciones usan Prisma ORM.
 */

import { prisma } from '../utils/prisma.js';

export const watchlistRepository = {
  create({ userId, marketId, alertThreshold }) {
    return prisma.watchlist.create({ data: { userId, marketId, alertThreshold } });
  },

  findByUser(userId) {
    return prisma.watchlist.findMany({
      where: { userId },
      include: {
        market: { select: { id: true, question: true, yesPrice: true, noPrice: true, status: true } },
      },
      orderBy: { createdAt: 'desc' },
    });
  },

  deleteByUserAndMarket(userId, marketId) {
    return prisma.watchlist.deleteMany({ where: { userId, marketId } });
  },

  findAllWithThreshold() {
    return prisma.watchlist.findMany({
      where: { alertThreshold: { not: null } },
      include: {
        user: {
          select: {
            id: true,
            telegramChatId: true,
            telegramBotToken: true,
            telegramAlertsEnabled: true,
          },
        },
        market: { select: { id: true, question: true, yesPrice: true } },
      },
    });
  },
};