MCPcopy Create free account
hub / github.com/async-labs/async / createOrUpdate

Method createOrUpdate

api/server/models/Chat.ts:114–182  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

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
135 const chatThatExists = await this.findOne({
136 teamId,
137 chatParticipantIds,
138 }).setOptions({
139 lean: true,
140 });
141
142 console.log(chatThatExists);
143
144 if (chatThatExists) {
145 return { newOrUpdatedChat: null, initialMessages: [] };
146 }
147
148 if (id) {
149 newOrUpdatedChat = await this.findOneAndUpdate(
150 { _id: id },
151 {
152 chatParticipantIds,
153 lastUpdatedAt: new Date(),
154 },
155 { runValidators: true, new: true },
156 );
157
158 initialMessages = await Message.getList({
159 userId,
160 chatId: id,
161 batchNumberForMessages: 1,
162 limit: 25,
163 });
164 } else {
165 newOrUpdatedChat = await this.create({
166 chatParticipantIds,
167 chatCreatorId: userId,
168 teamId,
169 createdAt: new Date(),
170 lastUpdatedAt: new Date(),
171 });

Callers

nothing calls this directly

Calls 3

checkPermissionMethod · 0.95
getSubscriptionStatusMethod · 0.65
getListMethod · 0.65

Tested by

no test coverage detected