@@ -2,8 +2,8 @@ import { acceptHMRUpdate, defineStore } from "pinia";
2
2
import { ref , watch } from "vue" ;
3
3
import { SagaHistory , SagaMessage } from "@/resources/SagaHistory" ;
4
4
import { useFetchFromServiceControl } from "@/composables/serviceServiceControlUrls" ;
5
- // import { MessageStatus } from "@/resources/Message";
6
-
5
+ import { MessageStatus } from "@/resources/Message" ;
6
+ const StandardKeys = [ "$type" , "Id" , "Originator" , "OriginalMessageId" ] ;
7
7
export interface SagaMessageDataItem {
8
8
key : string ;
9
9
value : string ;
@@ -21,7 +21,6 @@ export const useSagaDiagramStore = defineStore("sagaHistory", () => {
21
21
const fetchedMessages = ref ( new Set < string > ( ) ) ;
22
22
const messagesData = ref < SagaMessageData [ ] > ( [ ] ) ;
23
23
const MessageBodyEndpoint = "messages/{0}/body" ;
24
-
25
24
// Watch for changes to sagaId and fetch saga history data
26
25
watch ( sagaId , async ( newSagaId ) => {
27
26
if ( ! newSagaId ) {
@@ -48,10 +47,21 @@ export const useSagaDiagramStore = defineStore("sagaHistory", () => {
48
47
if ( change . outgoing_messages ) {
49
48
messages . push ( ...change . outgoing_messages . filter ( ( msg ) => ! fetchedMessages . value . has ( msg . message_id ) ) ) ;
50
49
}
51
-
52
50
return messages ;
53
51
} ) ;
54
52
53
+ // Check if any messages need body_url
54
+ const needsBodyUrl = messagesToFetch . every ( ( msg ) => ! msg . body_url ) ;
55
+ if ( needsBodyUrl && messagesToFetch . length > 0 ) {
56
+ const auditMessages = await getAuditMessages ( sagaId . value ! ) ;
57
+ messagesToFetch . forEach ( ( message ) => {
58
+ const auditMessage = auditMessages . find ( ( x ) => x . message_id === message . message_id ) ;
59
+ if ( auditMessage ) {
60
+ message . body_url = auditMessage . body_url ;
61
+ }
62
+ } ) ;
63
+ }
64
+
55
65
// Fetch data for each unfetched message in parallel and store results
56
66
const fetchPromises = messagesToFetch . map ( async ( message ) => {
57
67
const data = await fetchSagaMessageData ( message ) ;
@@ -86,7 +96,7 @@ export const useSagaDiagramStore = defineStore("sagaHistory", () => {
86
96
error . value = "Failed to fetch saga history" ;
87
97
} else {
88
98
const data = await response . json ( ) ;
89
-
99
+ console . log ( "Saga history data for sis" + id , data ) ;
90
100
sagaHistory . value = data ;
91
101
}
92
102
} catch ( e ) {
@@ -96,51 +106,131 @@ export const useSagaDiagramStore = defineStore("sagaHistory", () => {
96
106
loading . value = false ;
97
107
}
98
108
}
99
-
109
+ function createEmptyMessageData ( message_id : string ) : SagaMessageData {
110
+ return {
111
+ message_id,
112
+ data : [
113
+ {
114
+ key : "Content" ,
115
+ value : "EMPTY" ,
116
+ } ,
117
+ ] ,
118
+ } ;
119
+ }
100
120
async function fetchSagaMessageData ( message : SagaMessage ) : Promise < SagaMessageData > {
101
- const bodyUrl = message . body_url ?? formatUrl ( MessageBodyEndpoint , message . message_id ) ;
121
+ const bodyUrl = ( message . body_url ?? formatUrl ( MessageBodyEndpoint , message . message_id ) ) . replace ( / ^ \/ / , "" ) ;
102
122
loading . value = true ;
103
123
error . value = null ;
104
124
// const headers = {
105
- // "Cache-Control": message.message_status === MessageStatus.Successful ? "no-cache" : "no-cache",
125
+ // //"Cache-Control": message.message_status === MessageStatus.Successful ? "no-cache" : "no-cache",
126
+ // "Cache-Control": "no-cache",
106
127
// };
107
128
try {
129
+ console . log ( "MessageBodyEndpoint:" , MessageBodyEndpoint ) ;
130
+ console . log ( "message.body_url:" , message . body_url ) ;
131
+ console . log ( "message.message_id:" , message . message_id ) ;
132
+ console . log ( "bodyUrl:" , bodyUrl ) ;
108
133
const response = await useFetchFromServiceControl ( bodyUrl ) ;
109
- const body = response ;
134
+ if ( ! response . ok ) {
135
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
136
+ }
137
+ const body = await response . json ( ) ;
110
138
111
139
if ( ! body ) {
112
- return {
113
- message_id : message . message_id ,
114
- data : [ ] ,
115
- } ;
140
+ return createEmptyMessageData ( message . message_id ) ;
116
141
}
117
142
118
- // if (typeof body === "string" && body.trim().startsWith("<?xml")) {
119
- // // return getXmlData(body); // Implement XML parsing below
120
- // } else {
121
- // // return processJsonValues(body); // Implement JSON property extraction
122
- // }
123
-
124
- // Return dummy data for now
143
+ let data : SagaMessageDataItem [ ] ;
144
+ if ( typeof body === "string" && body . trim ( ) . startsWith ( "<?xml" ) ) {
145
+ data = getXmlData ( body ) ;
146
+ } else {
147
+ data = processJsonValues ( body ) ;
148
+ }
149
+ // Check if parsed data is empty
150
+ if ( ! data || data . length === 0 ) {
151
+ return createEmptyMessageData ( message . message_id ) ;
152
+ }
125
153
return {
126
154
message_id : message . message_id ,
127
- data : [
128
- { key : "Property1" , value : "Test Value 1" } ,
129
- { key : "Property2" , value : "Test Value 2" } ,
130
- { key : "Timestamp" , value : new Date ( ) . toISOString ( ) } ,
131
- ] ,
155
+ data,
132
156
} ;
133
157
} catch ( e ) {
134
158
error . value = e instanceof Error ? e . message : "Unknown error occurred" ;
135
- return {
136
- message_id : message . message_id ,
137
- data : [ ] ,
138
- } ;
159
+ console . log ( "InCATCH" + error . value ) ;
160
+ return createEmptyMessageData ( message . message_id ) ;
139
161
} finally {
140
162
loading . value = false ;
141
163
}
142
164
}
143
165
166
+ async function getAuditMessages ( sagaId : string ) {
167
+ try {
168
+ const response = await useFetchFromServiceControl ( `messages/search?q=${ sagaId } ` ) ;
169
+ if ( ! response . ok ) {
170
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
171
+ }
172
+ return await response . json ( ) ;
173
+ } catch ( error ) {
174
+ console . error ( "Error fetching audit messages:" , error ) ;
175
+ return { result : [ ] } ;
176
+ }
177
+ }
178
+
179
+ function getXmlData ( xmlString : string ) : SagaMessageDataItem [ ] {
180
+ try {
181
+ const parser = new DOMParser ( ) ;
182
+ const xmlDoc = parser . parseFromString ( xmlString , "application/xml" ) ;
183
+
184
+ // Get the root element
185
+ const rootElement = xmlDoc . documentElement ;
186
+ if ( ! rootElement ) {
187
+ return [ ] ;
188
+ }
189
+
190
+ // Handle both v5 and pre-v5 message formats
191
+ const messageRoot = rootElement . nodeName === "Messages" ? Array . from ( rootElement . children ) [ 0 ] : rootElement ;
192
+
193
+ if ( ! messageRoot ) {
194
+ return [ ] ;
195
+ }
196
+
197
+ // Convert child elements to SagaMessageDataItems
198
+ return Array . from ( messageRoot . children ) . map ( ( node ) => ( {
199
+ key : node . nodeName ,
200
+ value : node . textContent ?. trim ( ) || "" ,
201
+ } ) ) ;
202
+ } catch ( error ) {
203
+ console . error ( "Error parsing message data:" , error ) ;
204
+ return [ ] ;
205
+ }
206
+ }
207
+
208
+ // Replace or modify the existing processJsonValues function
209
+ function processJsonValues ( jsonBody : any ) : SagaMessageDataItem [ ] {
210
+ if ( typeof jsonBody === "string" ) {
211
+ try {
212
+ jsonBody = JSON . parse ( jsonBody ) ;
213
+ } catch ( e ) {
214
+ console . error ( "Error parsing JSON:" , e ) ;
215
+ return [ ] ;
216
+ }
217
+ }
218
+
219
+ const items : SagaMessageDataItem [ ] = [ ] ;
220
+
221
+ // Filter out standard keys and convert to KeyValuePairs
222
+ for ( const key in jsonBody ) {
223
+ if ( ! StandardKeys . includes ( key ) ) {
224
+ items . push ( {
225
+ key : key ,
226
+ value : String ( jsonBody [ key ] ?? "" ) ,
227
+ } ) ;
228
+ }
229
+ }
230
+
231
+ return items ;
232
+ }
233
+
144
234
function clearSagaHistory ( ) {
145
235
sagaHistory . value = null ;
146
236
sagaId . value = null ;
0 commit comments