@memberjunction/server 2.7.1 → 2.8.0

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.
@@ -99,11 +99,14 @@ export class AskSkipResolver {
99
99
  if (!user) throw new Error(`User ${userPayload.email} not found in UserCache`);
100
100
 
101
101
  // now load up the messages. We will load up ALL of the messages for this conversation, and then pass them to the Skip API
102
- const messages: SkipMessage[] = await this.LoadConversationDetailsIntoSkipMessages(
103
- dataSource,
104
- ConversationId,
105
- AskSkipResolver._maxHistoricalMessages
106
- );
102
+ let messages: SkipMessage[] = [];
103
+ if (ConversationId && ConversationId.length > 0) {
104
+ messages = await this.LoadConversationDetailsIntoSkipMessages(
105
+ dataSource,
106
+ ConversationId,
107
+ AskSkipResolver._maxHistoricalMessages
108
+ );
109
+ }
107
110
 
108
111
  const md = new Metadata();
109
112
  const { convoEntity, dataContextEntity, convoDetailEntity, dataContext } = await this.HandleSkipInitialObjectLoading(
@@ -465,12 +468,13 @@ export class AskSkipResolver {
465
468
  if (!DataContextId || DataContextId.length === 0) {
466
469
  dataContextEntity.NewRecord();
467
470
  dataContextEntity.UserID = user.ID;
468
- dataContextEntity.Name = 'Data Context for Skip Conversation';
471
+ dataContextEntity.Name = 'Data Context for Skip Conversation ';
469
472
  if (!(await dataContextEntity.Save())) {
470
473
  LogError(`Creating a new data context failed`, undefined, dataContextEntity.LatestResult);
471
474
  throw new Error(`Creating a new data context failed`);
472
475
  }
473
- } else {
476
+ }
477
+ else {
474
478
  const dcLoadResult = await dataContextEntity.Load(DataContextId);
475
479
  if (!dcLoadResult) {
476
480
  throw new Error(`Loading DataContextEntity for DataContextId ${DataContextId} failed`);
@@ -487,32 +491,61 @@ export class AskSkipResolver {
487
491
  LogError(`Error saving DataContextEntity for conversation: ${ConversationId}`, undefined, dataContextEntity.LatestResult);
488
492
  }
489
493
  }
490
- } else {
494
+ }
495
+ else {
491
496
  LogError(`Creating a new conversation failed`, undefined, convoEntity.LatestResult);
492
497
  throw new Error(`Creating a new conversation failed`);
493
498
  }
494
- } else {
499
+ }
500
+ else {
495
501
  throw new Error(`User ${userPayload.email} not found in UserCache`);
496
502
  }
497
- } else {
503
+ }
504
+ else {
498
505
  await convoEntity.Load(ConversationId); // load the existing conversation, will need it later
499
506
  dataContextEntity = await md.GetEntityObject<DataContextEntity>('Data Contexts', user);
500
507
 
501
- // note - we ignore the parameter DataContextId if it is passed in, we will use the data context from the conversation that is saved. If a user wants to change the data context for a convo, they can do that elsewhere
508
+ // check to see if the DataContextId is passed in and if it is different than the DataContextID in the conversation
502
509
  if (DataContextId && DataContextId.length > 0 && DataContextId !== convoEntity.DataContextID) {
503
510
  if (convoEntity.DataContextID === null) {
511
+ // use the DataContextId passed in if the conversation doesn't have a DataContextID
504
512
  convoEntity.DataContextID = DataContextId;
505
513
  const convoEntitySaveResult: boolean = await convoEntity.Save();
506
514
  if (!convoEntitySaveResult) {
507
515
  LogError(`Error saving conversation entity for conversation: ${ConversationId}`, undefined, convoEntity.LatestResult);
508
516
  }
509
- } else
517
+ }
518
+ else {
519
+ // note - we ignore the parameter DataContextId if it is passed in, we will use the data context from the conversation that is saved.
520
+ // If a user wants to change the data context for a convo, they can do that elsewhere
510
521
  console.warn(
511
522
  `AskSkipResolver: DataContextId ${DataContextId} was passed in but it was ignored because it was different than the DataContextID in the conversation ${convoEntity.DataContextID}`
512
523
  );
524
+ }
525
+ // only load if we have a data context here, otherwise we have a new record in the dataContext entity
526
+ if (convoEntity.DataContextID)
527
+ await dataContextEntity.Load(convoEntity.DataContextID);
528
+ }
529
+ else if ((!DataContextId || DataContextId.length === 0) && (!convoEntity.DataContextID || convoEntity.DataContextID.length === 0)) {
530
+ // in this branch of the logic we don't have a passed in DataContextId and we don't have a DataContextID in the conversation, so we need to save the data context, get the ID,
531
+ // update the conversation and save it as well
532
+ dataContextEntity.NewRecord();
533
+ dataContextEntity.UserID = user.ID;
534
+ dataContextEntity.Name = 'Data Context for Skip Conversation ' + ConversationId;
535
+ if (await dataContextEntity.Save()) {
536
+ DataContextId = convoEntity.DataContextID;
537
+ convoEntity.DataContextID = dataContextEntity.ID;
538
+ if (!await convoEntity.Save()) {
539
+ LogError(`Error saving conversation entity for conversation: ${ConversationId}`, undefined, convoEntity.LatestResult);
540
+ }
541
+ }
542
+ else
543
+ LogError(`Error saving DataContextEntity for conversation: ${ConversationId}`, undefined, dataContextEntity.LatestResult);
544
+ }
545
+ else {
546
+ // finally in this branch we get here if we have a DataContextId passed in and it is the same as the DataContextID in the conversation, in this case simply load the data context
547
+ await dataContextEntity.Load(convoEntity.DataContextID);
513
548
  }
514
-
515
- await dataContextEntity.Load(convoEntity.DataContextID);
516
549
  }
517
550
 
518
551
  // now, create a conversation detail record for the user message
@@ -528,7 +561,7 @@ export class AskSkipResolver {
528
561
  LogError(`Error saving conversation detail entity for user message: ${UserQuestion}`, undefined, convoDetailEntity.LatestResult);
529
562
  }
530
563
 
531
- const dataContext = MJGlobal.Instance.ClassFactory.CreateInstance<DataContext>(DataContext); // await this.LoadDataContext(md, dataSource, dataContextEntity, user, false);
564
+ const dataContext = MJGlobal.Instance.ClassFactory.CreateInstance<DataContext>(DataContext);
532
565
  await dataContext.Load(dataContextEntity.ID, dataSource, false, false, 0, user);
533
566
  return { dataContext, convoEntity, dataContextEntity, convoDetailEntity };
534
567
  }
@@ -539,6 +572,10 @@ export class AskSkipResolver {
539
572
  maxHistoricalMessages?: number
540
573
  ): Promise<SkipMessage[]> {
541
574
  try {
575
+ if (!ConversationId || ConversationId.length === 0) {
576
+ throw new Error(`ConversationId is required`);
577
+ }
578
+
542
579
  // load up all the conversation details from the database server
543
580
  const md = new Metadata();
544
581
  const e = md.Entities.find((e) => e.Name === 'Conversation Details');
@@ -551,7 +588,8 @@ export class AskSkipResolver {
551
588
  ORDER
552
589
  BY __mj_CreatedAt DESC`;
553
590
  const result = await dataSource.query(sql);
554
- if (!result) throw new Error(`Error running SQL: ${sql}`);
591
+ if (!result)
592
+ throw new Error(`Error running SQL: ${sql}`);
555
593
  else {
556
594
  // first, let's sort the result array into a local variable called returnData and in that we will sort by __mj_CreatedAt in ASCENDING order so we have the right chronological order
557
595
  // the reason we're doing a LOCAL sort here is because in the SQL query above, we're sorting in DESCENDING order so we can use the TOP clause to limit the number of records and get the