| 48 | |
| 49 | if (path === '/batch') { |
| 50 | const batch = transactionRetries => { |
| 51 | let initialPromise = Promise.resolve(); |
| 52 | if (data.transaction === true) { |
| 53 | initialPromise = config.database.createTransactionalSession(); |
| 54 | } |
| 55 | return initialPromise.then(() => { |
| 56 | const promises = data.requests.map(request => { |
| 57 | return handleRequest(request.method, request.path, request.body, options, config).then( |
| 58 | response => { |
| 59 | if (options.returnStatus) { |
| 60 | const status = response._status; |
| 61 | const headers = response._headers; |
| 62 | delete response._status; |
| 63 | delete response._headers; |
| 64 | return { success: response, _status: status, _headers: headers }; |
| 65 | } |
| 66 | return { success: response }; |
| 67 | }, |
| 68 | error => { |
| 69 | return { |
| 70 | error: { code: error.code, error: error.message }, |
| 71 | }; |
| 72 | } |
| 73 | ); |
| 74 | }); |
| 75 | return Promise.all(promises) |
| 76 | .then(result => { |
| 77 | if (data.transaction === true) { |
| 78 | if (result.find(resultItem => typeof resultItem.error === 'object')) { |
| 79 | return config.database.abortTransactionalSession().then(() => { |
| 80 | return Promise.reject(result); |
| 81 | }); |
| 82 | } else { |
| 83 | return config.database.commitTransactionalSession().then(() => { |
| 84 | return result; |
| 85 | }); |
| 86 | } |
| 87 | } else { |
| 88 | return result; |
| 89 | } |
| 90 | }) |
| 91 | .catch(error => { |
| 92 | if ( |
| 93 | error && |
| 94 | error.find( |
| 95 | errorItem => typeof errorItem.error === 'object' && errorItem.error.code === 251 |
| 96 | ) && |
| 97 | transactionRetries > 0 |
| 98 | ) { |
| 99 | return batch(transactionRetries - 1); |
| 100 | } |
| 101 | throw error; |
| 102 | }); |
| 103 | }); |
| 104 | }; |
| 105 | return batch(5); |
| 106 | } |
| 107 |
no test coverage detected