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

Method createOrUpdate

api/server/models/Discussion.ts:154–230  ·  view source on GitHub ↗
(data)

Source from the content-addressed store, hash-verified

152 }
153
154 public static async createOrUpdate(data) {
155 const { discussionName, discussionMemberIds = [], userId, teamId, id, content, files } = data;
156
157 if (!discussionName || !teamId) {
158 throw new Error('Bad data');
159 }
160
161 const { isSubscriptionActiveForAccount, isTrialPeriodOverForAccount } =
162 await User.getSubscriptionStatus(userId);
163
164 if (isTrialPeriodOverForAccount && !isSubscriptionActiveForAccount) {
165 throw new Error('This team is not subscribed to a paid plan');
166 }
167
168 await this.checkPermission({ userId, teamId });
169
170 let newOrUpdatedDiscussion: IDiscussionDocument;
171 let initialComments: ICommentDocument[];
172
173 if (id) {
174 newOrUpdatedDiscussion = await this.findOneAndUpdate(
175 { _id: id },
176 {
177 discussionName,
178 discussionMemberIds,
179 lastUpdatedAt: new Date(),
180 },
181 { runValidators: true, new: true },
182 );
183
184 await Comment.addOrEdit({
185 content,
186 discussionId: newOrUpdatedDiscussion._id.toString(),
187 teamId,
188 userId,
189 id: newOrUpdatedDiscussion.firstCommentId,
190 });
191
192 initialComments = await Comment.getList({
193 userId,
194 discussionId: id,
195 });
196 } else {
197 const newDiscussion = await this.create({
198 discussionName,
199 discussionMemberIds,
200 discussionLeaderId: userId,
201 teamId,
202 isDiscussionArchived: false,
203 });
204
205 const { comment } = await Comment.addOrEdit({
206 content,
207 discussionId: newDiscussion._id.toString(),
208 teamId,
209 userId,
210 id: null,
211 files,

Callers

nothing calls this directly

Calls 4

checkPermissionMethod · 0.95
getSubscriptionStatusMethod · 0.65
addOrEditMethod · 0.65
getListMethod · 0.65

Tested by

no test coverage detected