@memberjunction/core-entities 6.2.1 → 6.3.0-edge.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.
- package/dist/.tsbuildinfo +1 -1
- package/dist/custom/MJUserViewEntityExtended.d.ts +4 -7
- package/dist/custom/MJUserViewEntityExtended.d.ts.map +1 -1
- package/dist/custom/MJUserViewEntityExtended.js +15 -97
- package/dist/custom/MJUserViewEntityExtended.js.map +1 -1
- package/dist/engines/CanonGraphEngine.d.ts +122 -0
- package/dist/engines/CanonGraphEngine.d.ts.map +1 -0
- package/dist/engines/CanonGraphEngine.js +495 -0
- package/dist/engines/CanonGraphEngine.js.map +1 -0
- package/dist/engines/UserInfoEngine.d.ts.map +1 -1
- package/dist/engines/UserInfoEngine.js +12 -7
- package/dist/engines/UserInfoEngine.js.map +1 -1
- package/dist/engines/conversations.d.ts +159 -10
- package/dist/engines/conversations.d.ts.map +1 -1
- package/dist/engines/conversations.js +434 -18
- package/dist/engines/conversations.js.map +1 -1
- package/dist/generated/entities/__mj.d.ts +4237 -899
- package/dist/generated/entities/__mj.d.ts.map +1 -1
- package/dist/generated/entities/__mj.js +4605 -448
- package/dist/generated/entities/__mj.js.map +1 -1
- package/dist/generated/queries/__mj.d.ts +269 -0
- package/dist/generated/queries/__mj.d.ts.map +1 -0
- package/dist/generated/queries/__mj.js +100 -0
- package/dist/generated/queries/__mj.js.map +1 -0
- package/dist/generated/queries/index.d.ts +2 -0
- package/dist/generated/queries/index.d.ts.map +1 -0
- package/dist/generated/queries/index.js +4 -0
- package/dist/generated/queries/index.js.map +1 -0
- package/dist/generated/remote_operations.d.ts +50 -22
- package/dist/generated/remote_operations.d.ts.map +1 -1
- package/dist/generated/remote_operations.js +3 -3
- package/dist/generated/remote_operations.js.map +1 -1
- package/dist/graph/BaseGraphEdgeProvider.d.ts +158 -0
- package/dist/graph/BaseGraphEdgeProvider.d.ts.map +1 -0
- package/dist/graph/BaseGraphEdgeProvider.js +70 -0
- package/dist/graph/BaseGraphEdgeProvider.js.map +1 -0
- package/dist/graph/GraphEngine.d.ts +255 -0
- package/dist/graph/GraphEngine.d.ts.map +1 -0
- package/dist/graph/GraphEngine.js +1066 -0
- package/dist/graph/GraphEngine.js.map +1 -0
- package/dist/graph/GraphExpansion.d.ts +142 -0
- package/dist/graph/GraphExpansion.d.ts.map +1 -0
- package/dist/graph/GraphExpansion.js +87 -0
- package/dist/graph/GraphExpansion.js.map +1 -0
- package/dist/graph/index.d.ts +15 -0
- package/dist/graph/index.d.ts.map +1 -0
- package/dist/graph/index.js +30 -0
- package/dist/graph/index.js.map +1 -0
- package/dist/graph/providers/AlternateBaseViewEdgeProvider.d.ts +16 -0
- package/dist/graph/providers/AlternateBaseViewEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/AlternateBaseViewEdgeProvider.js +115 -0
- package/dist/graph/providers/AlternateBaseViewEdgeProvider.js.map +1 -0
- package/dist/graph/providers/EntityMetadataEdgeProvider.d.ts +17 -0
- package/dist/graph/providers/EntityMetadataEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/EntityMetadataEdgeProvider.js +145 -0
- package/dist/graph/providers/EntityMetadataEdgeProvider.js.map +1 -0
- package/dist/graph/providers/ForeignKeyEdgeProvider.d.ts +25 -0
- package/dist/graph/providers/ForeignKeyEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/ForeignKeyEdgeProvider.js +131 -0
- package/dist/graph/providers/ForeignKeyEdgeProvider.js.map +1 -0
- package/dist/graph/providers/JoinEntityEdgeProvider.d.ts +37 -0
- package/dist/graph/providers/JoinEntityEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/JoinEntityEdgeProvider.js +170 -0
- package/dist/graph/providers/JoinEntityEdgeProvider.js.map +1 -0
- package/dist/graph/providers/QueryEdgeProvider.d.ts +33 -0
- package/dist/graph/providers/QueryEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/QueryEdgeProvider.js +120 -0
- package/dist/graph/providers/QueryEdgeProvider.js.map +1 -0
- package/dist/graph/providers/QueryMetadataEdgeProvider.d.ts +18 -0
- package/dist/graph/providers/QueryMetadataEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/QueryMetadataEdgeProvider.js +163 -0
- package/dist/graph/providers/QueryMetadataEdgeProvider.js.map +1 -0
- package/dist/graph/providers/RecordLinkEdgeProvider.d.ts +20 -0
- package/dist/graph/providers/RecordLinkEdgeProvider.d.ts.map +1 -0
- package/dist/graph/providers/RecordLinkEdgeProvider.js +172 -0
- package/dist/graph/providers/RecordLinkEdgeProvider.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
|
@@ -16,6 +16,56 @@ const CONVERSATIONS_RESOURCE_TYPE_ID = '81D4BC3D-9FEB-EF11-B01A-286B35C04427';
|
|
|
16
16
|
* folder inherit access to conversations inside it.
|
|
17
17
|
*/
|
|
18
18
|
export const PROJECTS_RESOURCE_TYPE_ID = '88D0DF27-AF8F-43AC-9EC4-2AC314F49355';
|
|
19
|
+
/**
|
|
20
|
+
* Canonical built-in `MJ: Conversation Types.Code` constants.
|
|
21
|
+
* Custom Open Apps may seed additional `ConversationType` rows and look them up via
|
|
22
|
+
* {@link ConversationEngine.TypeByCode} or {@link ConversationEngine.ResolveConversationTypeID}.
|
|
23
|
+
*/
|
|
24
|
+
export const CONVERSATION_TYPE_CODES = {
|
|
25
|
+
CHAT: 'CHAT',
|
|
26
|
+
SKIP: 'SKIP',
|
|
27
|
+
ROUTINE: 'ROUTINE',
|
|
28
|
+
MEETING_ROOM: 'MEETING_ROOM',
|
|
29
|
+
COMMENT: 'COMMENT',
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Seeded primary-key UUIDs for built-in `MJ: Conversation Types` rows, used as deterministic
|
|
33
|
+
* fallbacks when {@link ConversationEngine.Config} has not yet run (for example in unit tests).
|
|
34
|
+
*/
|
|
35
|
+
export const BUILT_IN_CONVERSATION_TYPE_IDS = {
|
|
36
|
+
CHAT: '7F81FF57-C5A8-464D-B3B4-35A4FCF10717',
|
|
37
|
+
SKIP: 'FDAE66B9-CF87-48EA-8A12-8E521740A66A',
|
|
38
|
+
ROUTINE: '18D12ABA-BE8C-4087-B440-835B6D077DD7',
|
|
39
|
+
MEETING_ROOM: '2E9EF85D-D18A-4B8E-B6AB-F65C7F91A926',
|
|
40
|
+
COMMENT: '17E6A105-AE51-43A2-9D56-290BC8D1D6BB',
|
|
41
|
+
};
|
|
42
|
+
const BUILT_IN_CONVERSATION_TYPE_NAMES = {
|
|
43
|
+
chat: 'CHAT',
|
|
44
|
+
skip: 'SKIP',
|
|
45
|
+
routine: 'ROUTINE',
|
|
46
|
+
'meeting room': 'MEETING_ROOM',
|
|
47
|
+
meeting_room: 'MEETING_ROOM',
|
|
48
|
+
comment: 'COMMENT',
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Canonical built-in `MJ: Comment Types.Code` constants.
|
|
52
|
+
*/
|
|
53
|
+
export const COMMENT_TYPE_CODES = {
|
|
54
|
+
COMMENT: 'COMMENT',
|
|
55
|
+
FIELD_SUGGESTION: 'FIELD_SUGGESTION',
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Seeded primary-key UUIDs for built-in `MJ: Comment Types` rows.
|
|
59
|
+
*/
|
|
60
|
+
export const BUILT_IN_COMMENT_TYPE_IDS = {
|
|
61
|
+
COMMENT: '4B3A3797-EFA5-419F-80FF-F9515E5F4AA8',
|
|
62
|
+
FIELD_SUGGESTION: '8918DBA5-EDDD-4AF4-A518-FEB387ED02F9',
|
|
63
|
+
};
|
|
64
|
+
const BUILT_IN_COMMENT_TYPE_NAMES = {
|
|
65
|
+
comment: 'COMMENT',
|
|
66
|
+
'field suggestion': 'FIELD_SUGGESTION',
|
|
67
|
+
field_suggestion: 'FIELD_SUGGESTION',
|
|
68
|
+
};
|
|
19
69
|
/**
|
|
20
70
|
* The spellings of `"inheritFolderAccess": true` the SQL filter recognises: the compact one every MJ writer
|
|
21
71
|
* produces (`JSON.stringify` and the server's save, which rewrites `AdditionalData` compactly) and the
|
|
@@ -137,14 +187,18 @@ export function ComparePermissionLevels(a, b) {
|
|
|
137
187
|
* fields, sections, groups, and contributions, while enforcing FLS visibility when
|
|
138
188
|
* `entityInfo` and `currentUser` are provided.
|
|
139
189
|
*/
|
|
140
|
-
export function ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo) {
|
|
190
|
+
export function ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo, commentTypeCodeByConversationId) {
|
|
141
191
|
let totalOpenCount = 0;
|
|
142
192
|
let totalResolvedCount = 0;
|
|
143
193
|
let recordLevelOpenCount = 0;
|
|
194
|
+
let openNoteCount = 0;
|
|
195
|
+
let openSuggestionCount = 0;
|
|
144
196
|
const byField = new Map();
|
|
145
197
|
const bySection = new Map();
|
|
146
198
|
const byGroup = new Map();
|
|
147
199
|
const byContribution = new Map();
|
|
200
|
+
const byFieldSuggestions = new Map();
|
|
201
|
+
const byFieldByType = new Map();
|
|
148
202
|
for (const conv of conversations) {
|
|
149
203
|
if (!conv.AnchorType) {
|
|
150
204
|
continue;
|
|
@@ -159,6 +213,18 @@ export function ComputeAnchoredCommentCounts(conversations, currentUser, entityI
|
|
|
159
213
|
continue;
|
|
160
214
|
}
|
|
161
215
|
totalOpenCount++;
|
|
216
|
+
const normId = conv.ID ? NormalizeUUID(conv.ID) : '';
|
|
217
|
+
const rawTypeCode = normId && commentTypeCodeByConversationId
|
|
218
|
+
? commentTypeCodeByConversationId.get(normId)
|
|
219
|
+
: undefined;
|
|
220
|
+
const typeCode = (rawTypeCode?.trim().toUpperCase()) || COMMENT_TYPE_CODES.COMMENT;
|
|
221
|
+
const isSuggestion = typeCode === COMMENT_TYPE_CODES.FIELD_SUGGESTION;
|
|
222
|
+
if (isSuggestion) {
|
|
223
|
+
openSuggestionCount++;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
openNoteCount++;
|
|
227
|
+
}
|
|
162
228
|
const meta = ParseConversationAnchorMetadata(conv.AnchorMetadataObject ?? conv.AnchorMetadata);
|
|
163
229
|
const rawKey = conv.AnchorKey?.trim() ?? '';
|
|
164
230
|
if (conv.AnchorType === 'Record') {
|
|
@@ -168,6 +234,15 @@ export function ComputeAnchoredCommentCounts(conversations, currentUser, entityI
|
|
|
168
234
|
const fieldKey = (rawKey || meta?.fieldName?.trim() || '').toLowerCase();
|
|
169
235
|
if (fieldKey) {
|
|
170
236
|
byField.set(fieldKey, (byField.get(fieldKey) ?? 0) + 1);
|
|
237
|
+
if (isSuggestion) {
|
|
238
|
+
byFieldSuggestions.set(fieldKey, (byFieldSuggestions.get(fieldKey) ?? 0) + 1);
|
|
239
|
+
}
|
|
240
|
+
let typeMap = byFieldByType.get(fieldKey);
|
|
241
|
+
if (!typeMap) {
|
|
242
|
+
typeMap = new Map();
|
|
243
|
+
byFieldByType.set(fieldKey, typeMap);
|
|
244
|
+
}
|
|
245
|
+
typeMap.set(typeCode, (typeMap.get(typeCode) ?? 0) + 1);
|
|
171
246
|
}
|
|
172
247
|
}
|
|
173
248
|
else if (conv.AnchorType === 'Section') {
|
|
@@ -208,7 +283,11 @@ export function ComputeAnchoredCommentCounts(conversations, currentUser, entityI
|
|
|
208
283
|
ByField: byField,
|
|
209
284
|
BySection: bySection,
|
|
210
285
|
ByGroup: byGroup,
|
|
211
|
-
ByContribution: byContribution
|
|
286
|
+
ByContribution: byContribution,
|
|
287
|
+
OpenNoteCount: openNoteCount,
|
|
288
|
+
OpenSuggestionCount: openSuggestionCount,
|
|
289
|
+
ByFieldSuggestions: byFieldSuggestions,
|
|
290
|
+
ByFieldByType: byFieldByType,
|
|
212
291
|
};
|
|
213
292
|
}
|
|
214
293
|
/**
|
|
@@ -601,6 +680,8 @@ export class ConversationEngine extends BaseEngine {
|
|
|
601
680
|
* which would cause redundant cache updates or infinite loops.
|
|
602
681
|
*/
|
|
603
682
|
this._selfMutating = false;
|
|
683
|
+
this._conversationTypes = [];
|
|
684
|
+
this._commentTypes = [];
|
|
604
685
|
}
|
|
605
686
|
/**
|
|
606
687
|
* Returns the global instance of the class. This is a singleton class, so there is only
|
|
@@ -676,26 +757,134 @@ export class ConversationEngine extends BaseEngine {
|
|
|
676
757
|
return level === 'Edit' || level === 'Owner';
|
|
677
758
|
}
|
|
678
759
|
// ========================================================================
|
|
760
|
+
// CONVERSATION & COMMENT TYPE LOOKUPS
|
|
761
|
+
// ========================================================================
|
|
762
|
+
/** Cached `MJ: Conversation Types` rows loaded during {@link Config}. */
|
|
763
|
+
get ConversationTypes() {
|
|
764
|
+
return this._conversationTypes;
|
|
765
|
+
}
|
|
766
|
+
/** Cached `MJ: Comment Types` rows loaded during {@link Config}. */
|
|
767
|
+
get CommentTypes() {
|
|
768
|
+
return this._commentTypes;
|
|
769
|
+
}
|
|
770
|
+
/** Looks up a cached `MJ: Conversation Types` row by its case-insensitive `Code`. */
|
|
771
|
+
TypeByCode(code) {
|
|
772
|
+
const normalized = code?.trim().toUpperCase();
|
|
773
|
+
if (!normalized) {
|
|
774
|
+
return undefined;
|
|
775
|
+
}
|
|
776
|
+
return this._conversationTypes.find((t) => t.Code?.trim().toUpperCase() === normalized);
|
|
777
|
+
}
|
|
778
|
+
/** Looks up a cached `MJ: Conversation Types` row by its case-insensitive `Name`. */
|
|
779
|
+
TypeByName(name) {
|
|
780
|
+
const normalized = name?.trim().toLowerCase();
|
|
781
|
+
if (!normalized) {
|
|
782
|
+
return undefined;
|
|
783
|
+
}
|
|
784
|
+
return this._conversationTypes.find((t) => t.Name?.trim().toLowerCase() === normalized);
|
|
785
|
+
}
|
|
786
|
+
/** Looks up a cached `MJ: Conversation Types` row by `ID`. */
|
|
787
|
+
TypeByID(id) {
|
|
788
|
+
if (!id) {
|
|
789
|
+
return undefined;
|
|
790
|
+
}
|
|
791
|
+
return this._conversationTypes.find((t) => UUIDsEqual(t.ID, id));
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Resolves a `ConversationType.ID` by `Code` or `Name`, consulting cached `ConversationTypes`
|
|
795
|
+
* first and falling back to built-in seeded UUIDs when {@link Config} has not yet populated cache.
|
|
796
|
+
*/
|
|
797
|
+
ResolveConversationTypeID(codeOrName) {
|
|
798
|
+
const trimmed = codeOrName?.trim();
|
|
799
|
+
if (!trimmed) {
|
|
800
|
+
return null;
|
|
801
|
+
}
|
|
802
|
+
const fromCache = this.TypeByCode(trimmed) ?? this.TypeByName(trimmed);
|
|
803
|
+
if (fromCache?.ID) {
|
|
804
|
+
return fromCache.ID;
|
|
805
|
+
}
|
|
806
|
+
const upper = trimmed.toUpperCase();
|
|
807
|
+
if (upper in BUILT_IN_CONVERSATION_TYPE_IDS) {
|
|
808
|
+
return BUILT_IN_CONVERSATION_TYPE_IDS[upper];
|
|
809
|
+
}
|
|
810
|
+
const mappedCode = BUILT_IN_CONVERSATION_TYPE_NAMES[trimmed.toLowerCase()];
|
|
811
|
+
return mappedCode ? BUILT_IN_CONVERSATION_TYPE_IDS[mappedCode] : null;
|
|
812
|
+
}
|
|
813
|
+
/** Looks up a cached `MJ: Comment Types` row by its case-insensitive `Code`. */
|
|
814
|
+
CommentTypeByCode(code) {
|
|
815
|
+
const normalized = code?.trim().toUpperCase();
|
|
816
|
+
if (!normalized) {
|
|
817
|
+
return undefined;
|
|
818
|
+
}
|
|
819
|
+
return this._commentTypes.find((t) => t.Code?.trim().toUpperCase() === normalized);
|
|
820
|
+
}
|
|
821
|
+
/** Looks up a cached `MJ: Comment Types` row by its case-insensitive `Name`. */
|
|
822
|
+
CommentTypeByName(name) {
|
|
823
|
+
const normalized = name?.trim().toLowerCase();
|
|
824
|
+
if (!normalized) {
|
|
825
|
+
return undefined;
|
|
826
|
+
}
|
|
827
|
+
return this._commentTypes.find((t) => t.Name?.trim().toLowerCase() === normalized);
|
|
828
|
+
}
|
|
829
|
+
/** Looks up a cached `MJ: Comment Types` row by `ID`. */
|
|
830
|
+
CommentTypeByID(id) {
|
|
831
|
+
if (!id) {
|
|
832
|
+
return undefined;
|
|
833
|
+
}
|
|
834
|
+
return this._commentTypes.find((t) => UUIDsEqual(t.ID, id));
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Resolves a `CommentType.ID` by `Code` or `Name`, consulting cached `CommentTypes`
|
|
838
|
+
* first and falling back to built-in seeded UUIDs when {@link Config} has not yet populated cache.
|
|
839
|
+
*/
|
|
840
|
+
ResolveCommentTypeID(codeOrName) {
|
|
841
|
+
const trimmed = codeOrName?.trim();
|
|
842
|
+
if (!trimmed) {
|
|
843
|
+
return null;
|
|
844
|
+
}
|
|
845
|
+
const fromCache = this.CommentTypeByCode(trimmed) ?? this.CommentTypeByName(trimmed);
|
|
846
|
+
if (fromCache?.ID) {
|
|
847
|
+
return fromCache.ID;
|
|
848
|
+
}
|
|
849
|
+
const upper = trimmed.toUpperCase();
|
|
850
|
+
if (upper in BUILT_IN_COMMENT_TYPE_IDS) {
|
|
851
|
+
return BUILT_IN_COMMENT_TYPE_IDS[upper];
|
|
852
|
+
}
|
|
853
|
+
const mappedCode = BUILT_IN_COMMENT_TYPE_NAMES[trimmed.toLowerCase()];
|
|
854
|
+
return mappedCode ? BUILT_IN_COMMENT_TYPE_IDS[mappedCode] : null;
|
|
855
|
+
}
|
|
856
|
+
// ========================================================================
|
|
679
857
|
// ENGINE CONFIG (BaseEngine pattern)
|
|
680
858
|
// ========================================================================
|
|
681
859
|
/**
|
|
682
|
-
* Configures the engine
|
|
683
|
-
*
|
|
684
|
-
*
|
|
685
|
-
*
|
|
686
|
-
* Call this once at startup to initialize the engine. Conversation data is loaded
|
|
687
|
-
* separately via LoadConversations().
|
|
860
|
+
* Configures the engine, bulk-loading small lookup tables (`MJ: Conversation Types`
|
|
861
|
+
* and `MJ: Comment Types`) while keeping user-scoped conversation data lazy via
|
|
862
|
+
* {@link LoadConversations}.
|
|
688
863
|
*/
|
|
689
864
|
async Config(forceRefresh, contextUser, provider) {
|
|
690
|
-
// Ensure ArtifactMetadataEngine is loaded — conversation details reference artifacts
|
|
691
|
-
// and we need artifact types available before processing. Passing false means
|
|
692
|
-
// this is a no-op if it's already loaded.
|
|
693
865
|
await ArtifactMetadataEngine.Instance.Config(forceRefresh, contextUser, provider);
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
866
|
+
const activeProvider = provider ?? this.ProviderToUse;
|
|
867
|
+
const entities = activeProvider?.Entities;
|
|
868
|
+
const hasTypeEntities = !Array.isArray(entities) ||
|
|
869
|
+
entities.length === 0 ||
|
|
870
|
+
(entities.some((e) => e.Name === 'MJ: Conversation Types') &&
|
|
871
|
+
entities.some((e) => e.Name === 'MJ: Comment Types'));
|
|
872
|
+
const configs = hasTypeEntities
|
|
873
|
+
? [
|
|
874
|
+
{
|
|
875
|
+
Type: 'entity',
|
|
876
|
+
EntityName: 'MJ: Conversation Types',
|
|
877
|
+
PropertyName: '_conversationTypes',
|
|
878
|
+
CacheLocal: true,
|
|
879
|
+
},
|
|
880
|
+
{
|
|
881
|
+
Type: 'entity',
|
|
882
|
+
EntityName: 'MJ: Comment Types',
|
|
883
|
+
PropertyName: '_commentTypes',
|
|
884
|
+
CacheLocal: true,
|
|
885
|
+
},
|
|
886
|
+
]
|
|
887
|
+
: [];
|
|
699
888
|
await this.Load(configs, provider, forceRefresh, contextUser);
|
|
700
889
|
}
|
|
701
890
|
// ========================================================================
|
|
@@ -1390,6 +1579,11 @@ export class ConversationEngine extends BaseEngine {
|
|
|
1390
1579
|
conversation.Name = name;
|
|
1391
1580
|
conversation.EnvironmentID = environmentId;
|
|
1392
1581
|
conversation.UserID = contextUser.ID;
|
|
1582
|
+
const resolvedTypeId = options?.typeId ??
|
|
1583
|
+
this.ResolveConversationTypeID(options?.typeCode ??
|
|
1584
|
+
(options?.anchorType ? CONVERSATION_TYPE_CODES.COMMENT : CONVERSATION_TYPE_CODES.CHAT)) ??
|
|
1585
|
+
BUILT_IN_CONVERSATION_TYPE_IDS.CHAT;
|
|
1586
|
+
conversation.TypeID = resolvedTypeId;
|
|
1393
1587
|
if (description)
|
|
1394
1588
|
conversation.Description = description;
|
|
1395
1589
|
if (projectId)
|
|
@@ -1593,6 +1787,9 @@ export class ConversationEngine extends BaseEngine {
|
|
|
1593
1787
|
conversation.EnvironmentID = this._lastEnvironmentId;
|
|
1594
1788
|
}
|
|
1595
1789
|
conversation.UserID = contextUser.ID;
|
|
1790
|
+
conversation.TypeID =
|
|
1791
|
+
this.ResolveConversationTypeID(CONVERSATION_TYPE_CODES.COMMENT) ??
|
|
1792
|
+
BUILT_IN_CONVERSATION_TYPE_IDS.COMMENT;
|
|
1596
1793
|
conversation.ApplicationScope = 'Global';
|
|
1597
1794
|
conversation.LinkedEntityID = params.linkedEntityId;
|
|
1598
1795
|
conversation.LinkedRecordID = params.linkedRecordId;
|
|
@@ -1602,12 +1799,40 @@ export class ConversationEngine extends BaseEngine {
|
|
|
1602
1799
|
if (params.defaultAgentId !== undefined) {
|
|
1603
1800
|
conversation.DefaultAgentID = params.defaultAgentId;
|
|
1604
1801
|
}
|
|
1802
|
+
const requestedCommentTypeId = params.commentTypeId ??
|
|
1803
|
+
(params.commentTypeCode ? this.ResolveCommentTypeID(params.commentTypeCode) : null);
|
|
1605
1804
|
this._selfMutating = true;
|
|
1606
1805
|
try {
|
|
1607
1806
|
const saved = await conversation.Save();
|
|
1608
1807
|
if (!saved) {
|
|
1609
1808
|
throw new Error(conversation.LatestResult?.CompleteMessage || conversation.LatestResult?.Message || 'Failed to create anchored conversation');
|
|
1610
1809
|
}
|
|
1810
|
+
if (requestedCommentTypeId &&
|
|
1811
|
+
!UUIDsEqual(requestedCommentTypeId, BUILT_IN_COMMENT_TYPE_IDS.COMMENT)) {
|
|
1812
|
+
const isaComment = conversation.ISAChild;
|
|
1813
|
+
if (isaComment) {
|
|
1814
|
+
isaComment.CommentTypeID = requestedCommentTypeId;
|
|
1815
|
+
const commentSaved = await isaComment.Save();
|
|
1816
|
+
if (!commentSaved) {
|
|
1817
|
+
throw new Error(isaComment.LatestResult?.CompleteMessage ||
|
|
1818
|
+
isaComment.LatestResult?.Message ||
|
|
1819
|
+
'Failed to update comment type on anchored conversation');
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
else {
|
|
1823
|
+
const commentObj = await md.GetEntityObject('MJ: Comments', contextUser);
|
|
1824
|
+
const loadedComment = await commentObj.Load(conversation.ID);
|
|
1825
|
+
if (loadedComment) {
|
|
1826
|
+
commentObj.CommentTypeID = requestedCommentTypeId;
|
|
1827
|
+
const commentSaved = await commentObj.Save();
|
|
1828
|
+
if (!commentSaved) {
|
|
1829
|
+
throw new Error(commentObj.LatestResult?.CompleteMessage ||
|
|
1830
|
+
commentObj.LatestResult?.Message ||
|
|
1831
|
+
'Failed to update comment type on anchored conversation');
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1611
1836
|
}
|
|
1612
1837
|
finally {
|
|
1613
1838
|
this._selfMutating = false;
|
|
@@ -1654,12 +1879,203 @@ export class ConversationEngine extends BaseEngine {
|
|
|
1654
1879
|
}
|
|
1655
1880
|
return conversation;
|
|
1656
1881
|
}
|
|
1882
|
+
/**
|
|
1883
|
+
* Batch-loads `MJ: Comments` and `MJ: Field Suggestions` for the specified anchored
|
|
1884
|
+
* `conversationIds` in a single batched `RunViews` round trip, avoiding N+1 queries
|
|
1885
|
+
* when rendering forms with multiple comments and field suggestions.
|
|
1886
|
+
*/
|
|
1887
|
+
async LoadAnchoredCommentsAndSuggestions(conversationIds, contextUser, provider, bypassCache = false) {
|
|
1888
|
+
const commentsByConversationId = new Map();
|
|
1889
|
+
const suggestionsByConversationId = new Map();
|
|
1890
|
+
const commentTypeCodeByConversationId = new Map();
|
|
1891
|
+
const cleanIds = conversationIds.map((id) => id?.trim()).filter((id) => !!id);
|
|
1892
|
+
if (cleanIds.length === 0) {
|
|
1893
|
+
return {
|
|
1894
|
+
CommentsByConversationId: commentsByConversationId,
|
|
1895
|
+
SuggestionsByConversationId: suggestionsByConversationId,
|
|
1896
|
+
CommentTypeCodeByConversationId: commentTypeCodeByConversationId,
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1899
|
+
const md = provider ?? this.ProviderToUse;
|
|
1900
|
+
const entities = md?.Entities;
|
|
1901
|
+
const hasCommentEntities = !Array.isArray(entities) ||
|
|
1902
|
+
entities.length === 0 ||
|
|
1903
|
+
(entities.some((e) => e.Name === 'MJ: Comments') &&
|
|
1904
|
+
entities.some((e) => e.Name === 'MJ: Field Suggestions'));
|
|
1905
|
+
if (!hasCommentEntities) {
|
|
1906
|
+
return {
|
|
1907
|
+
CommentsByConversationId: commentsByConversationId,
|
|
1908
|
+
SuggestionsByConversationId: suggestionsByConversationId,
|
|
1909
|
+
CommentTypeCodeByConversationId: commentTypeCodeByConversationId,
|
|
1910
|
+
};
|
|
1911
|
+
}
|
|
1912
|
+
const rv = RunView.FromMetadataProvider(md);
|
|
1913
|
+
const inList = cleanIds.map((id) => `'${EscapeSQLString(id)}'`).join(',');
|
|
1914
|
+
const [commentsResult, suggestionsResult] = await rv.RunViews([
|
|
1915
|
+
{
|
|
1916
|
+
EntityName: 'MJ: Comments',
|
|
1917
|
+
ExtraFilter: `ID IN (${inList})`,
|
|
1918
|
+
ResultType: 'entity_object',
|
|
1919
|
+
BypassCache: bypassCache,
|
|
1920
|
+
},
|
|
1921
|
+
{
|
|
1922
|
+
EntityName: 'MJ: Field Suggestions',
|
|
1923
|
+
ExtraFilter: `ID IN (${inList})`,
|
|
1924
|
+
ResultType: 'entity_object',
|
|
1925
|
+
BypassCache: bypassCache,
|
|
1926
|
+
},
|
|
1927
|
+
], contextUser);
|
|
1928
|
+
if (commentsResult && !commentsResult.Success) {
|
|
1929
|
+
throw new Error(commentsResult.ErrorMessage || 'Failed to load anchored MJ: Comments records');
|
|
1930
|
+
}
|
|
1931
|
+
if (suggestionsResult && !suggestionsResult.Success) {
|
|
1932
|
+
throw new Error(suggestionsResult.ErrorMessage || 'Failed to load anchored MJ: Field Suggestions records');
|
|
1933
|
+
}
|
|
1934
|
+
if (commentsResult?.Success && Array.isArray(commentsResult.Results)) {
|
|
1935
|
+
for (const rawComment of commentsResult.Results) {
|
|
1936
|
+
const normId = NormalizeUUID(rawComment.ID);
|
|
1937
|
+
commentsByConversationId.set(normId, rawComment);
|
|
1938
|
+
const ct = this.CommentTypeByID(rawComment.CommentTypeID);
|
|
1939
|
+
if (ct?.Code) {
|
|
1940
|
+
commentTypeCodeByConversationId.set(normId, ct.Code.trim().toUpperCase());
|
|
1941
|
+
}
|
|
1942
|
+
else if (UUIDsEqual(rawComment.CommentTypeID, BUILT_IN_COMMENT_TYPE_IDS.FIELD_SUGGESTION)) {
|
|
1943
|
+
commentTypeCodeByConversationId.set(normId, COMMENT_TYPE_CODES.FIELD_SUGGESTION);
|
|
1944
|
+
}
|
|
1945
|
+
else {
|
|
1946
|
+
commentTypeCodeByConversationId.set(normId, COMMENT_TYPE_CODES.COMMENT);
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
if (suggestionsResult?.Success && Array.isArray(suggestionsResult.Results)) {
|
|
1951
|
+
for (const rawSuggestion of suggestionsResult.Results) {
|
|
1952
|
+
const normId = NormalizeUUID(rawSuggestion.ID);
|
|
1953
|
+
suggestionsByConversationId.set(normId, rawSuggestion);
|
|
1954
|
+
commentTypeCodeByConversationId.set(normId, COMMENT_TYPE_CODES.FIELD_SUGGESTION);
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
return {
|
|
1958
|
+
CommentsByConversationId: commentsByConversationId,
|
|
1959
|
+
SuggestionsByConversationId: suggestionsByConversationId,
|
|
1960
|
+
CommentTypeCodeByConversationId: commentTypeCodeByConversationId,
|
|
1961
|
+
};
|
|
1962
|
+
}
|
|
1963
|
+
/**
|
|
1964
|
+
* Creates a new `MJ: Field Suggestions` comment thread (saving `Conversation`, `Comment`,
|
|
1965
|
+
* and `FieldSuggestion` in a single IS-A chain transaction) and optionally posts the opening
|
|
1966
|
+
* rationale message into `MJ: Conversation Details`.
|
|
1967
|
+
*/
|
|
1968
|
+
async CreateFieldSuggestion(params, contextUser) {
|
|
1969
|
+
const md = params.provider ?? this.ProviderToUse;
|
|
1970
|
+
const suggestion = await md.GetEntityObject('MJ: Field Suggestions', contextUser);
|
|
1971
|
+
suggestion.NewRecord();
|
|
1972
|
+
const cleanFieldName = params.fieldName.trim();
|
|
1973
|
+
const fallbackTitle = `${params.anchorMetadata?.anchorDisplayName || cleanFieldName} Suggestion`;
|
|
1974
|
+
suggestion.Name = (params.name?.trim() || fallbackTitle).slice(0, 255);
|
|
1975
|
+
if (params.environmentId) {
|
|
1976
|
+
suggestion.EnvironmentID = params.environmentId;
|
|
1977
|
+
}
|
|
1978
|
+
else if (this._lastEnvironmentId) {
|
|
1979
|
+
suggestion.EnvironmentID = this._lastEnvironmentId;
|
|
1980
|
+
}
|
|
1981
|
+
suggestion.UserID = contextUser.ID;
|
|
1982
|
+
suggestion.TypeID =
|
|
1983
|
+
this.ResolveConversationTypeID(CONVERSATION_TYPE_CODES.COMMENT) ??
|
|
1984
|
+
BUILT_IN_CONVERSATION_TYPE_IDS.COMMENT;
|
|
1985
|
+
suggestion.CommentTypeID =
|
|
1986
|
+
this.ResolveCommentTypeID(COMMENT_TYPE_CODES.FIELD_SUGGESTION) ??
|
|
1987
|
+
BUILT_IN_COMMENT_TYPE_IDS.FIELD_SUGGESTION;
|
|
1988
|
+
suggestion.ApplicationScope = 'Global';
|
|
1989
|
+
suggestion.LinkedEntityID = params.linkedEntityId;
|
|
1990
|
+
suggestion.LinkedRecordID = params.linkedRecordId;
|
|
1991
|
+
suggestion.AnchorType = 'Field';
|
|
1992
|
+
suggestion.AnchorKey = cleanFieldName;
|
|
1993
|
+
suggestion.AnchorMetadata = JSON.stringify({
|
|
1994
|
+
...(params.anchorMetadata ?? {}),
|
|
1995
|
+
fieldName: cleanFieldName,
|
|
1996
|
+
});
|
|
1997
|
+
suggestion.SuggestedValue = params.suggestedValue;
|
|
1998
|
+
suggestion.Source = params.source ?? 'Agent';
|
|
1999
|
+
suggestion.Confidence = params.confidence ?? null;
|
|
2000
|
+
suggestion.AIPromptRunID = params.aiPromptRunId ?? null;
|
|
2001
|
+
suggestion.Outcome = null;
|
|
2002
|
+
this._selfMutating = true;
|
|
2003
|
+
try {
|
|
2004
|
+
const saved = await suggestion.Save();
|
|
2005
|
+
if (!saved) {
|
|
2006
|
+
throw new Error(suggestion.LatestResult?.CompleteMessage ||
|
|
2007
|
+
suggestion.LatestResult?.Message ||
|
|
2008
|
+
'Failed to create field suggestion');
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
finally {
|
|
2012
|
+
this._selfMutating = false;
|
|
2013
|
+
}
|
|
2014
|
+
let initialDetail = null;
|
|
2015
|
+
const messageText = params.initialMessage?.trim();
|
|
2016
|
+
if (messageText) {
|
|
2017
|
+
initialDetail = await md.GetEntityObject('MJ: Conversation Details', contextUser);
|
|
2018
|
+
initialDetail.NewRecord();
|
|
2019
|
+
initialDetail.ConversationID = suggestion.ID;
|
|
2020
|
+
initialDetail.UserID = contextUser.ID;
|
|
2021
|
+
initialDetail.Role = 'User';
|
|
2022
|
+
initialDetail.Message = messageText;
|
|
2023
|
+
const detailSaved = await initialDetail.Save();
|
|
2024
|
+
if (!detailSaved) {
|
|
2025
|
+
throw new Error(initialDetail.LatestResult?.CompleteMessage ||
|
|
2026
|
+
initialDetail.LatestResult?.Message ||
|
|
2027
|
+
'Failed to save initial field suggestion message');
|
|
2028
|
+
}
|
|
2029
|
+
}
|
|
2030
|
+
return { Suggestion: suggestion, InitialDetail: initialDetail };
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* Accepts a pending `MJ: Field Suggestions` row as `contextUser`. Server-side
|
|
2034
|
+
* `FieldSuggestionCommentDriver.OnAccept` writes `SuggestedValue` onto the target record
|
|
2035
|
+
* through a normal `Save()` as `contextUser` (enforcing RLS, FLS, and `Update` permission,
|
|
2036
|
+
* and recording `Record Changes`), then stamps `Outcome = 'Accepted'`, `IsResolved = true`,
|
|
2037
|
+
* `ResolvedByUserID`, and `ResolvedAt`.
|
|
2038
|
+
*/
|
|
2039
|
+
async AcceptFieldSuggestion(suggestionId, contextUser, provider) {
|
|
2040
|
+
return this.transitionFieldSuggestionOutcome(suggestionId, 'Accepted', contextUser, provider);
|
|
2041
|
+
}
|
|
2042
|
+
/**
|
|
2043
|
+
* Rejects a pending `MJ: Field Suggestions` row as `contextUser`. Server-side
|
|
2044
|
+
* `FieldSuggestionCommentDriver.OnReject` verifies `contextUser` holds update rights on the
|
|
2045
|
+
* target record and field, then stamps `Outcome = 'Rejected'`, `IsResolved = true`,
|
|
2046
|
+
* `ResolvedByUserID`, and `ResolvedAt`.
|
|
2047
|
+
*/
|
|
2048
|
+
async RejectFieldSuggestion(suggestionId, contextUser, provider) {
|
|
2049
|
+
return this.transitionFieldSuggestionOutcome(suggestionId, 'Rejected', contextUser, provider);
|
|
2050
|
+
}
|
|
2051
|
+
async transitionFieldSuggestionOutcome(suggestionId, outcome, contextUser, provider) {
|
|
2052
|
+
const md = provider ?? this.ProviderToUse;
|
|
2053
|
+
const suggestion = await md.GetEntityObject('MJ: Field Suggestions', contextUser);
|
|
2054
|
+
const loaded = await suggestion.Load(suggestionId);
|
|
2055
|
+
if (!loaded) {
|
|
2056
|
+
throw new Error(`Field suggestion ${suggestionId} not found`);
|
|
2057
|
+
}
|
|
2058
|
+
suggestion.Outcome = outcome;
|
|
2059
|
+
this._selfMutating = true;
|
|
2060
|
+
try {
|
|
2061
|
+
const saved = await suggestion.Save();
|
|
2062
|
+
if (!saved) {
|
|
2063
|
+
throw new Error(suggestion.LatestResult?.CompleteMessage ||
|
|
2064
|
+
suggestion.LatestResult?.Message ||
|
|
2065
|
+
`Failed to mark field suggestion as ${outcome}`);
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
finally {
|
|
2069
|
+
this._selfMutating = false;
|
|
2070
|
+
}
|
|
2071
|
+
return suggestion;
|
|
2072
|
+
}
|
|
1657
2073
|
/**
|
|
1658
2074
|
* Computes open and resolved anchored comment counts across fields, sections, groups,
|
|
1659
2075
|
* and contributions for a record, respecting FLS for `currentUser`.
|
|
1660
2076
|
*/
|
|
1661
|
-
ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo) {
|
|
1662
|
-
return ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo);
|
|
2077
|
+
ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo, commentTypeCodeByConversationId) {
|
|
2078
|
+
return ComputeAnchoredCommentCounts(conversations, currentUser, entityInfo, commentTypeCodeByConversationId);
|
|
1663
2079
|
}
|
|
1664
2080
|
/**
|
|
1665
2081
|
* Folds a conversation that was created OUTSIDE this engine (e.g. server-side by a
|