@ns8/ns8-switchboard-operator
Advanced tools
Comparing version
@@ -12,2 +12,4 @@ import { CreateOrderActionSwitch } from 'ns8-switchboard-interfaces'; | ||
handle: (event: any) => Promise<Order>; | ||
private postOrderAndHandleDuplicate; | ||
private isDuplicateError; | ||
} |
@@ -23,5 +23,6 @@ "use strict"; | ||
const v2Token = switchContext.merchant.accessTokens[0].id; | ||
const order = await this.v2Client.withAuthToken(v2Token) | ||
.withBaseUrl(v2BaseUrl) | ||
.postOrder(switchContext.data); | ||
const order = await this.postOrderAndHandleDuplicate(v2BaseUrl, v2Token, switchContext); | ||
if (!order) { | ||
return undefined; | ||
} | ||
await this.v2Client.withAuthToken(v2Token) | ||
@@ -35,4 +36,34 @@ .withBaseUrl(v2BaseUrl) | ||
} | ||
async postOrderAndHandleDuplicate(v2BaseUrl, v2Token, switchContext) { | ||
try { | ||
return await this.v2Client | ||
.withAuthToken(v2Token) | ||
.withBaseUrl(v2BaseUrl) | ||
.postOrder(switchContext.data); | ||
} | ||
catch (err) { | ||
if (!this.isDuplicateError(err)) { | ||
throw err; | ||
} | ||
const order = await this.v2Client.withAuthToken(v2Token) | ||
.withBaseUrl(v2BaseUrl) | ||
.getOrderByName(switchContext.data.name); | ||
if (!order) { | ||
const formattedOrder = switchContext.data && typeof switchContext.data === 'object' | ||
? `\nPOST Order Data: ${JSON.stringify(switchContext.data, undefined, 2)}` | ||
: ''; | ||
const message = 'Unable to retrieve order associated with 409 by name.'; | ||
throw new Error(`${message} Order Name: ${switchContext.data.name}${formattedOrder}`); | ||
} | ||
const existingEq8Assessment = (order.fraudAssessments != null && Array.isArray(order.fraudAssessments)) | ||
? order.fraudAssessments.find((assessment) => (assessment.providerType === ns8_protect_models_1.ProviderType.EQ8)) | ||
: undefined; | ||
return !existingEq8Assessment ? order : undefined; | ||
} | ||
} | ||
isDuplicateError(err) { | ||
return err.statusCode === 409 || err.status === 409 || err.code === 409; | ||
} | ||
} | ||
exports.CreateOrderActionOperator = CreateOrderActionOperator; | ||
//# sourceMappingURL=CreateOrderActionOperator.js.map |
@@ -21,2 +21,3 @@ /// <reference types="node" /> | ||
private formatErrorForOutput; | ||
private isAwsElbBadGateway; | ||
} |
@@ -75,2 +75,6 @@ "use strict"; | ||
async checkForErrorResponse(request, response, url) { | ||
const baseMessage = 'Operator V2Client - Protect API error:'; | ||
if (this.isAwsElbBadGateway(response)) { | ||
throw new Error(`${baseMessage} AWS ELB 502 - Bad Gateway`); | ||
} | ||
if (response.status > V2Client.MAX_SUCCESS_CODE) { | ||
@@ -83,4 +87,3 @@ const body = await this.safeParseJSON(response); | ||
const requestDetails = this.formatObjectForOutput({ url, request, response }); | ||
const message = 'Operator V2Client - Protect API error:'; | ||
throw new Error(`${message} ${errorDetails} ${requestDetails}`); | ||
throw new Error(`${baseMessage} ${errorDetails} ${requestDetails}`); | ||
} | ||
@@ -104,2 +107,7 @@ } | ||
} | ||
isAwsElbBadGateway(response) { | ||
const awsElbLabel = 'awselb/2.0'; | ||
const responseServer = response.headers.get('server'); | ||
return !response.ok && response.status === 502 && responseServer === awsElbLabel; | ||
} | ||
} | ||
@@ -106,0 +114,0 @@ exports.default = V2Client; |
{ | ||
"name": "@ns8/ns8-switchboard-operator", | ||
"version": "1.0.111", | ||
"version": "1.0.112", | ||
"description": "Operator project contains switchboard interfaces and orchestration code", | ||
@@ -23,2 +23,3 @@ "main": "dist/index.js", | ||
"@types/chai": "4.2.6", | ||
"@types/chai-as-promised": "^7.1.2", | ||
"@types/isomorphic-fetch": "0.0.35", | ||
@@ -31,2 +32,3 @@ "@types/mocha": "5.2.7", | ||
"chai": "4.2.0", | ||
"chai-as-promised": "^7.1.1", | ||
"class-transformer": "0.2.3", | ||
@@ -33,0 +35,0 @@ "eslint": "6.7.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
36214
11.12%455
10.17%22
10%