File size: 4,987 Bytes
179db97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ═══════════════════════════════════════════════════════════════════════════
// REACT API - Send emoji reactions
// ═══════════════════════════════════════════════════════════════════════════
// POST /api/react - Send a reaction
// GET /api/react?sessionId=xxx - Get current reactions
// ═══════════════════════════════════════════════════════════════════════════

import { NextRequest, NextResponse } from 'next/server';
import { sessionManager } from '@/lib/session-manager';
import { ReactionType } from '@/lib/types';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

// Valid reaction emojis
const VALID_REACTIONS: ReactionType[] = ['πŸ‘', '❀️', 'πŸŽ‰', 'πŸ˜•', 'πŸš€', 'πŸ‘', 'πŸ”₯'];

// ═══════════════════════════════════════════════════════════════════════════
// GET REACTIONS
// ═══════════════════════════════════════════════════════════════════════════
// GET /api/react?sessionId=xxx
// Returns current reaction counts for a session
// ═══════════════════════════════════════════════════════════════════════════

export async function GET(request: NextRequest) {
  const { searchParams } = new URL(request.url);
  const sessionId = searchParams.get('sessionId');

  if (!sessionId) {
    return NextResponse.json({
      success: false,
      error: 'Missing required parameter: sessionId'
    }, { status: 400 });
  }

  const session = sessionManager.getSession(sessionId);
  if (!session) {
    return NextResponse.json({
      success: false,
      error: 'Session not found'
    }, { status: 404 });
  }

  const reactions = sessionManager.getReactions(sessionId);

  return NextResponse.json({
    success: true,
    data: {
      reactions,
      availableReactions: VALID_REACTIONS
    }
  });
}

// ═══════════════════════════════════════════════════════════════════════════
// SEND REACTION
// ═══════════════════════════════════════════════════════════════════════════
// POST /api/react
// Body: {
//   sessionId: string,       // Required: Session ID
//   emoji: string            // Required: Emoji reaction (πŸ‘ ❀️ πŸŽ‰ πŸ˜• πŸš€ πŸ‘ πŸ”₯)
// }
// ═══════════════════════════════════════════════════════════════════════════

export async function POST(request: NextRequest) {
  try {
    const body = await request.json();
    const { sessionId, emoji } = body;

    // Validate sessionId
    if (!sessionId) {
      return NextResponse.json({
        success: false,
        error: 'Missing required field: sessionId'
      }, { status: 400 });
    }

    // Validate emoji
    if (!emoji) {
      return NextResponse.json({
        success: false,
        error: 'Missing required field: emoji'
      }, { status: 400 });
    }

    // Check if emoji is valid
    if (!VALID_REACTIONS.includes(emoji as ReactionType)) {
      return NextResponse.json({
        success: false,
        error: `Invalid emoji. Valid reactions: ${VALID_REACTIONS.join(' ')}`
      }, { status: 400 });
    }

    // Check session exists
    const session = sessionManager.getSession(sessionId);
    if (!session) {
      return NextResponse.json({
        success: false,
        error: 'Session not found'
      }, { status: 404 });
    }

    // Add reaction
    const reactions = await sessionManager.addReaction(sessionId, emoji as ReactionType);

    return NextResponse.json({
      success: true,
      data: {
        emoji,
        reactions,
        timestamp: new Date().toISOString()
      },
      message: 'Reaction recorded'
    });

  } catch (error) {
    return NextResponse.json({
      success: false,
      error: 'Invalid JSON body'
    }, { status: 400 });
  }
}