@peopl-health/nexus 6.0.15 → 6.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -83,7 +83,7 @@ const createAssistantCore = async (code, assistant_id, _messages = [], force = f
83
83
  };
84
84
  Thread.setConversationId(thread, initialThread.id);
85
85
 
86
- const condition = { $or: [{ code: code }] };
86
+ const condition = { code };
87
87
  const options = { new: true, upsert: true };
88
88
  const updatedThread = await Thread.findOneAndUpdate(condition, { ...thread }, options);
89
89
  await setThreadPromptId(code, assistant_id);
@@ -30,7 +30,7 @@ const threadSchema = new mongoose.Schema({
30
30
  lastMessageMedia: { type: Object, default: null }
31
31
  }, { timestamps: true });
32
32
 
33
- threadSchema.index({ code: 1 });
33
+ threadSchema.index({ code: 1 }, { unique: true });
34
34
  threadSchema.index({ thread_id: 1 });
35
35
  threadSchema.index({ conversation_id: 1 });
36
36
  threadSchema.index({ lastMessageAt: -1 }, { name: 'last_message_at_idx', sparse: true });
@@ -173,15 +173,20 @@ const rewriteOps = (docs, setFor) => docs.map((doc) => ({
173
173
  updateOne: { filter: { _id: doc._id }, update: { $set: setFor(doc) } },
174
174
  }));
175
175
 
176
+ // The new number may write mid-migration and get its own thread; the source thread is then archived like a merge.
177
+ const renameThread = (thread, target) => Thread.updateOne({ _id: thread._id }, { $set: { code: target } }).then(() => true, (error) => {
178
+ if (error.code !== 11000) throw error;
179
+ return false;
180
+ });
181
+
176
182
  async function moveThreads(source, target, state, migrationId) {
177
- const archived = `archived:${source}`;
178
183
  const [newest, ...rest] = [...state.sourceThreads].sort((a, b) => new Date(b.createdAt || 0) - new Date(a.createdAt || 0));
179
- const toArchive = state.targetThread ? state.sourceThreads : rest;
180
- if (!state.targetThread && newest) await Thread.updateOne({ _id: newest._id }, { $set: { code: target } });
184
+ const renamed = !state.targetThread && !!newest && await renameThread(newest, target);
185
+ const toArchive = renamed ? rest : state.sourceThreads;
181
186
  if (toArchive.length) {
182
- await Thread.updateMany({ _id: { $in: toArchive.map((doc) => doc._id) } }, { $set: { code: archived, stopped: true } });
187
+ await Thread.bulkWrite(rewriteOps(toArchive, (doc) => ({ code: `archived:${source}:${doc._id}`, stopped: true })));
183
188
  }
184
- logger.debug('[migratePatientCode] Threads moved', { migrationId, renamed: !state.targetThread && !!newest, archived: toArchive.length });
189
+ logger.debug('[migratePatientCode] Threads moved', { migrationId, renamed, archived: toArchive.length });
185
190
  }
186
191
 
187
192
  async function migratePatientCode({ from, to, asCaregiver = false, apply = false, operator = null } = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.0.15",
3
+ "version": "6.0.16",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",