| 75 | } |
| 76 | |
| 77 | class ChatClass extends mongoose.Model { |
| 78 | public static async getChatList(params) { |
| 79 | const { userId, teamId } = params; |
| 80 | |
| 81 | await this.checkPermission({ userId, teamId }); |
| 82 | |
| 83 | const filter = { |
| 84 | teamId, |
| 85 | chatParticipantIds: userId, |
| 86 | }; |
| 87 | |
| 88 | const chats = await this.find(filter).sort({ _id: 1 }).setOptions({ lean: true }); |
| 89 | |
| 90 | for (const chat of chats) { |
| 91 | const messages = await Message.getList({ |
| 92 | userId, |
| 93 | chatId: chat._id.toString(), |
| 94 | batchNumberForMessages: 1, |
| 95 | limit: 25, |
| 96 | }); |
| 97 | |
| 98 | const allMessagesPerChat = await Message.find({ chatId: chat._id.toString() }).setOptions({ |
| 99 | lean: true, |
| 100 | }); |
| 101 | |
| 102 | const numberOfMessagesPerChat = allMessagesPerChat.filter((m) => !m.parentMessageId).length; |
| 103 | |
| 104 | // Object.assign works |
| 105 | Object.assign(chat, { |
| 106 | initialMessages: messages.reverse(), |
| 107 | numberOfMessagesPerChat, |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | return chats; |
| 112 | } |
| 113 | |
| 114 | public static async createOrUpdate(data) { |
| 115 | const { chatParticipantIds = [], userId, teamId, id } = data; |
| 116 | |
| 117 | if (!teamId) { |
| 118 | throw new Error('Bad data'); |
| 119 | } |
| 120 | |
| 121 | const { isSubscriptionActiveForAccount, isTrialPeriodOverForAccount } = |
| 122 | await User.getSubscriptionStatus(userId); |
| 123 | |
| 124 | if (isTrialPeriodOverForAccount && !isSubscriptionActiveForAccount) { |
| 125 | throw new Error('This team is not subscribed to a paid plan and free trial period is over.'); |
| 126 | } |
| 127 | |
| 128 | await this.checkPermission({ userId, teamId }); |
| 129 | |
| 130 | let newOrUpdatedChat: IChatDocument; |
| 131 | let initialMessages: IMessageDocument[]; |
| 132 | |
| 133 | console.log(chatParticipantIds); |
| 134 |
nothing calls this directly
no outgoing calls
no test coverage detected