File size: 2,859 Bytes
e3fefe8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { dbGet, dbSet, dbUpdate, dbGetAll, dbPush } = require('../config/firebase');

const CHATS_PATH = 'chats';
const BOOKINGS_PATH = 'bookings';

const ChatModel = {
  getChatId(uid1, uid2) {
    return [uid1, uid2].sort().join('_');
  },

  async getOrCreate(uid1, uid2) {
    const chatId = this.getChatId(uid1, uid2);
    const existing = await dbGet(`${CHATS_PATH}/${chatId}`);
    if (existing) return existing;

    const sorted = [uid1, uid2].sort();
    const chat = {
      chatId,
      participant1: sorted[0],
      participant2: sorted[1],
      participants: sorted,
      lastMessage: '',
      lastUpdated: Date.now(),
      _createdAt: Date.now(),
    };
    await dbSet(`${CHATS_PATH}/${chatId}`, chat);
    return chat;
  },

  /**
   * Get messages (latest N messages from RTDB).
   */
  async getMessages(chatId, limit = 50) {
    const { db } = require('../config/firebase');
    const snap = await db.ref(`${CHATS_PATH}/${chatId}/messages`)
      .orderByChild('timestamp')
      .limitToLast(limit)
      .once('value');

    if (!snap.exists()) return [];
    const messages = [];
    snap.forEach(child => {
      const value = child.val();
      messages.push({
        id: child.key,
        senderId: value.senderId || '',
        text: value.text || '',
        timestamp: value.timestamp || 0,
      });
    });
    return messages.sort((a, b) => a.timestamp - b.timestamp);
  },

  /**
   * Send a message.
   */
  async sendMessage(chatId, senderId, text) {
    const { db } = require('../config/firebase');
    const msgRef = db.ref(`${CHATS_PATH}/${chatId}/messages`).push();
    const message = {
      id: msgRef.key,
      senderId,
      text,
      timestamp: Date.now(),
    };
    await msgRef.set(message);

    // Update chat metadata for shared conversation
    await dbUpdate(`${CHATS_PATH}/${chatId}`, {
      lastMessage: text.length > 60 ? text.substring(0, 60) + '...' : text,
      lastUpdated: Date.now(),
    });

    return message;
  },

  /**
   * Check if two users have an active confirmed booking (access control for chat).
   */
  async hasActiveBooking(uid1, uid2) {
    const allBookings = await dbGetAll(BOOKINGS_PATH);
    return allBookings.some(b =>
      ['confirmed', 'in_progress', 'completed'].includes(b.status) &&
      ((b.customerId === uid1 && b.professionalId === uid2) ||
       (b.customerId === uid2 && b.professionalId === uid1))
    );
  },

  /**
   * Get all conversations for a user.
   */
  async getConversationsForUser(uid) {
    const all = await dbGetAll(CHATS_PATH);
    return all.filter(chat => {
      const participants = Array.isArray(chat.participants) ? chat.participants : Object.values(chat.participants || {});
      return participants.includes(uid);
    }).sort((a, b) => (b.lastUpdated || 0) - (a.lastUpdated || 0));
  },
};

module.exports = ChatModel;