11// SPDX-License-Identifier: Apache-2.0
22
33import apm from '../apm' ;
4- import { CalculateDuration } from '@frmscoe/frms-coe-lib/lib/helpers/calculatePrcg' ;
4+ // import { CalculateDuration } from '@frmscoe/frms-coe-lib/lib/helpers/calculatePrcg';
55import { databaseManager , loggerService } from '..' ;
6- import { type Channel , type Message , type NetworkMap , type Pacs002 } from '@frmscoe/frms-coe-lib/lib/interfaces' ;
7- import { type ChannelResult } from '@frmscoe/frms-coe-lib/lib/interfaces/processor-files/ChannelResult' ;
6+ import { type NetworkMap , type Pacs002 } from '@frmscoe/frms-coe-lib/lib/interfaces' ;
87import { type TypologyResult } from '@frmscoe/frms-coe-lib/lib/interfaces/processor-files/TypologyResult' ;
98
10- export const handleChannels = async (
11- message : Message ,
12- transaction : Pacs002 ,
13- networkMap : NetworkMap ,
14- channelResult : ChannelResult ,
15- ) : Promise < { channelResults : ChannelResult [ ] ; review : boolean } > => {
16- const span = apm . startSpan ( 'handleChannels' ) ;
17- const functionName = 'handleChannels()' ;
18-
19- try {
20- const transactionType = 'FIToFIPmtSts' ;
21- const transactionID = transaction [ transactionType ] . GrpHdr . MsgId ;
22- const cacheKey = `tadp_${ transactionID } _${ message . id } _${ message . cfg } ` ;
23- const spanDBMembers = apm . startSpan ( 'db.get.members' ) ;
24- const jchannelCount = await databaseManager . addOneGetCount ( cacheKey , { channelResult : { ...channelResult } } ) ;
25-
26- // check if all Channel results for this transaction is found
27- if ( jchannelCount && jchannelCount < message . channels . length ) {
28- span ?. end ( ) ;
29- loggerService . log ( 'All channels not completed.' , functionName , transactionID ) ;
30- return { channelResults : [ ] , review : false } ;
31- }
32- const jchannelResults = await databaseManager . getMemberValues ( cacheKey ) ;
33- spanDBMembers ?. end ( ) ;
34-
35- const channelResults : ChannelResult [ ] = jchannelResults . map (
36- ( jchannelResult : { channelResult : ChannelResult } ) => jchannelResult . channelResult ,
37- ) ;
38-
39- let review = false ;
40- for ( const configuredChannel of networkMap . messages [ 0 ] . channels ) {
41- if ( configuredChannel ) {
42- const channelRes = channelResults . find ( ( c ) => c . id === configuredChannel . id && c . cfg === configuredChannel . cfg ) ;
43- if ( ! channelRes ) continue ;
44- for ( const typology of configuredChannel . typologies ) {
45- const typologyResult = channelRes ?. typologyResult . find ( ( t ) => t . id === typology . id && t . cfg === typology . cfg ) ;
46- if ( ! typologyResult ) continue ;
47- if ( typologyResult . review ) review = true ;
48- }
49- }
50- }
51-
52- // Delete interim cache as transaction processed to fulfilment
53- await databaseManager . deleteKey ( cacheKey ) ;
54-
55- span ?. end ( ) ;
56- return { channelResults, review } ;
57- } catch ( error ) {
58- span ?. end ( ) ;
59- let innerError ;
60- loggerService . error ( error as string , innerError , functionName ) ;
61- throw error ;
62- }
63- } ;
64-
659export const handleTypologies = async (
6610 transaction : Pacs002 ,
67- channel : Channel ,
6811 networkMap : NetworkMap ,
6912 typologyResult : TypologyResult ,
70- ) : Promise < { channelResults : ChannelResult [ ] ; review : boolean } > => {
13+ ) : Promise < { typologyResult : TypologyResult [ ] ; review : boolean } > => {
7114 let span ;
72- const startTime = process . hrtime . bigint ( ) ;
15+ // const startTime = process.hrtime.bigint();
7316 const functionName = 'handleTypologies()' ;
7417 try {
18+ const typologies = networkMap . messages [ 0 ] . typologies . filter ( ( t ) => t . id === typologyResult . id && t . cfg === typologyResult . cfg ) ;
7519 const transactionID = transaction . FIToFIPmtSts . GrpHdr . MsgId ;
76- const cacheKey = `CADP_ ${ transactionID } _${ channel . id } _${ channel . cfg } ` ;
20+ const cacheKey = `TADP_ ${ transactionID } _${ typologyResult . id } _${ typologyResult . cfg } ` ;
7721 const jtypologyCount = await databaseManager . addOneGetCount ( cacheKey , { typologyResult : { ...typologyResult } } ) ;
7822
7923 // check if all results for this Channel is found
80- if ( jtypologyCount && jtypologyCount < channel . typologies . length ) {
24+ if ( jtypologyCount && jtypologyCount < typologies . length ) {
8125 return {
8226 review : false ,
83- channelResults : [ ] ,
27+ typologyResult : [ ] ,
8428 } ;
8529 }
8630
@@ -92,47 +36,46 @@ export const handleTypologies = async (
9236 if ( ! typologyResults || ! typologyResults . length )
9337 return {
9438 review : false ,
95- channelResults : [ ] ,
39+ typologyResult : [ ] ,
9640 } ;
9741
98- const channelResult : ChannelResult = {
99- prcgTm : CalculateDuration ( startTime ) ,
100- result : 0.0 ,
101- cfg : channel . cfg ,
102- id : channel . id ,
103- typologyResult : typologyResults ,
104- } ;
10542 const apmTadProc = apm . startSpan ( 'tadProc.exec' ) ;
10643
10744 const message = networkMap . messages . find ( ( tran ) => tran . txTp === transaction . TxTp ) ;
10845
10946 if ( ! message ) {
11047 let innerError ;
11148 loggerService . error (
112- `Failed to process Channel ${ channel . id } request , Message missing from networkmap.` ,
49+ `Failed to process Typology ${ typologyResult . id } @ ${ typologyResult . cfg } request , Message missing from networkmap.` ,
11350 innerError ,
11451 functionName ,
11552 transactionID ,
11653 ) ;
11754 return {
11855 review : false ,
119- channelResults : [ ] ,
56+ typologyResult : [ ] ,
12057 } ;
12158 }
12259
123- const { channelResults, review } = await handleChannels ( message , transaction , networkMap , channelResult ) ;
60+ let review = false ;
61+ for ( const typology of networkMap . messages [ 0 ] . typologies ) {
62+ const typologyResult = typologyResults . find ( ( t ) => t . id === typology . id && t . cfg === typology . cfg ) ;
63+ if ( ! typologyResult ) continue ;
64+ if ( typologyResult . review ) review = true ;
65+ }
66+
12467 apmTadProc ?. end ( ) ;
12568
12669 span = apm . startSpan ( `[${ transactionID } ] Delete Channel interim cache key` ) ;
12770 await databaseManager . deleteKey ( cacheKey ) ;
12871 span ?. end ( ) ;
129- return { channelResults , review } ;
72+ return { typologyResult : typologyResults , review } ;
13073 } catch ( error ) {
13174 span ?. end ( ) ;
132- loggerService . error ( `Failed to process Channel ${ channel . id } request` , error as Error , functionName ) ;
75+ loggerService . error ( `Failed to process Typology ${ typologyResult . id } @ ${ typologyResult . cfg } request` , error as Error , functionName ) ;
13376 return {
13477 review : false ,
135- channelResults : [ ] ,
78+ typologyResult : [ ] ,
13679 } ;
13780 }
13881} ;
0 commit comments