@@ -8,7 +8,7 @@ import * as metrics from '../metrics'
88import * as transactions from '../model/transactions'
99import * as utils from '../utils'
1010
11- const { transactionStatus } = transactions
11+ const { transactionStatus} = transactions
1212
1313function copyMapWithEscapedReservedCharacters ( map ) {
1414 const escapedMap = { }
@@ -30,7 +30,9 @@ export function storeTransaction(ctx, done) {
3030 const headers = copyMapWithEscapedReservedCharacters ( ctx . header )
3131
3232 const tx = new transactions . TransactionModel ( {
33- status : transactionStatus . PROCESSING ,
33+ status : ctx ?. matchingChannel ?. isAsynchronousProcess
34+ ? transactionStatus . PENDING_ASYNC
35+ : transactionStatus . PROCESSING ,
3436 clientID : ctx . authenticated != null ? ctx . authenticated . _id : undefined ,
3537 channelID : ctx . authorisedChannel . _id ,
3638 clientIP : ctx . ip ,
@@ -87,9 +89,18 @@ export function storeTransaction(ctx, done) {
8789
8890export function storeResponse ( ctx , done ) {
8991 const headers = copyMapWithEscapedReservedCharacters ( ctx . response . header )
92+ const isAsynchronousProcess =
93+ Boolean ( ctx ?. matchingChannel ?. isAsynchronousProcess ) ||
94+ Boolean ( ctx ?. authorisedChannel ?. isAsynchronousProcess )
95+ const status =
96+ isAsynchronousProcess && ctx . response . status < 400
97+ ? 202
98+ : ctx . response . status
99+ // if the channel is asynchronous and the response is successful, change the status to 202 otherwise use the original status
100+ ctx . response . status = status
90101
91102 const res = {
92- status : ctx . response . status ,
103+ status,
93104 headers,
94105 body : ! ctx . response . body ? '' : ctx . response . body . toString ( ) ,
95106 timestamp : ctx . response . timestamp
@@ -202,6 +213,10 @@ export function storeNonPrimaryResponse(ctx, route, done) {
202213 * This should only be called once all routes have responded.
203214 */
204215export function setFinalStatus ( ctx , callback ) {
216+ const isAsynchronousProcess =
217+ Boolean ( ctx ?. matchingChannel ?. isAsynchronousProcess ) ||
218+ Boolean ( ctx ?. authorisedChannel ?. isAsynchronousProcess )
219+
205220 let transactionId = ''
206221 if (
207222 ctx . request != null &&
@@ -253,7 +268,11 @@ export function setFinalStatus(ctx, callback) {
253268 ctx . response . status <= 299 &&
254269 routeSuccess
255270 ) {
256- tx . status = transactionStatus . SUCCESSFUL
271+ if ( isAsynchronousProcess ) {
272+ tx . status = transactionStatus . PENDING_ASYNC
273+ } else {
274+ tx . status = transactionStatus . SUCCESSFUL
275+ }
257276 }
258277 if (
259278 ctx . response . status >= 400 &&
0 commit comments