From d3f12885fb426c4e8f30b2b65ef0dc8bf8ff454f Mon Sep 17 00:00:00 2001 From: skyflow-vivek Date: Thu, 11 Jan 2024 13:47:31 +0530 Subject: [PATCH] SK-1317 Add continue on error in batch Insert - Added requestId in metadata of API response. - Removed requestId from responses where not required. - Fixed failing Unit Tests as a consequence of changes. --- src/vault-api/Controller.ts | 4 +-- src/vault-api/client/index.ts | 5 ++-- src/vault-api/core/Collect.ts | 16 ++++++----- src/vault-api/core/Delete.ts | 2 +- src/vault-api/core/Reveal.ts | 8 +++--- test/vault-api/Client.test.js | 5 +++- test/vault-api/Skyflow.test.js | 52 ++++++++++++++++++---------------- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/vault-api/Controller.ts b/src/vault-api/Controller.ts index cc33898..47f9814 100644 --- a/src/vault-api/Controller.ts +++ b/src/vault-api/Controller.ts @@ -326,8 +326,8 @@ class Controller { body: config.requestBody, headers: { 'x-skyflow-authorization': res, 'content-type': ContentType.APPLICATIONORJSON,...toLowerKeys(config.requestHeader) }, }); - invokeRequest.then((response) => { - rootResolve(response); + invokeRequest.then((response: any) => { + rootResolve(response.data); }).catch((err) => { rootReject({ errors: [err] }); }); diff --git a/src/vault-api/client/index.ts b/src/vault-api/client/index.ts index db4d4c3..7c39737 100644 --- a/src/vault-api/client/index.ts +++ b/src/vault-api/client/index.ts @@ -63,8 +63,9 @@ class Client { headers: this.getHeaders(data,headerKeys) } ).then((res)=> { - res.data['requestId'] = res.headers['x-request-id'] - resolve(res.data) + // res.data['requestId'] = res.headers['x-request-id'] + let requestId = res.headers['x-request-id'] + resolve({data: res.data, metadata: {requestId}}) }).catch((err)=> { this.failureResponse(err).catch((err)=>reject(err)) }) diff --git a/src/vault-api/core/Collect.ts b/src/vault-api/core/Collect.ts index d14602a..c8c545c 100644 --- a/src/vault-api/core/Collect.ts +++ b/src/vault-api/core/Collect.ts @@ -41,7 +41,7 @@ export const constructInsertRecordResponse = ( if (options.continueOnError) { const successObjects: any = []; const failureObjects: any= []; - responseBody.responses + responseBody.data.responses .forEach((response, index) => { const status = response['Status'] const body = response['Body'] @@ -65,9 +65,11 @@ export const constructInsertRecordResponse = ( } } else { failureObjects.push({ - code: status, - ddescription: `${body['error']} - requestId: ${responseBody.requestId}`, - request_index: index, + error: { + code: status, + description: `${body['error']} - requestId: ${responseBody.metadata.requestId}`, + request_index: index, + } }) } }) @@ -81,9 +83,9 @@ export const constructInsertRecordResponse = ( return finalResponse; } else if (options.tokens) { return { - records: responseBody.responses + records: responseBody.data.responses .map((res, index) => { - const skyflowId = responseBody.responses[index].records[0].skyflow_id; + const skyflowId = res.records[0].skyflow_id; const tokens = res.records[0].tokens; return { table: records[index].table, @@ -97,7 +99,7 @@ export const constructInsertRecordResponse = ( }; } return { - records: responseBody.responses.map((res, index) => ({ + records: responseBody.data.responses.map((res, index) => ({ table: records[index].table, skyflow_id: res.records[0].skyflow_id, "request_index": index diff --git a/src/vault-api/core/Delete.ts b/src/vault-api/core/Delete.ts index 922074e..a73f56f 100644 --- a/src/vault-api/core/Delete.ts +++ b/src/vault-api/core/Delete.ts @@ -47,7 +47,7 @@ export const deleteRecordsBySkyflowID = ( ) .then( (response: any) => { - deleteResponse.push(response); + deleteResponse.push(response.data); }, (cause: any) => { deleteResponse.push(formatForPureJsFailure(cause, deleteRecord.id)); diff --git a/src/vault-api/core/Reveal.ts b/src/vault-api/core/Reveal.ts index 32a7a5c..6f34f24 100644 --- a/src/vault-api/core/Reveal.ts +++ b/src/vault-api/core/Reveal.ts @@ -131,8 +131,8 @@ export const fetchRecordsByTokenId = ( const apiResponse: any = []; getTokenRecordsFromVault(tokenRecord, client, authToken as string) .then( - (response: IApiSuccessResponse) => { - const fieldsData = formatForPureJsSuccess(response); + (response: any) => { + const fieldsData = formatForPureJsSuccess(response.data); apiResponse.push(...fieldsData); }, (cause: any) => { @@ -184,7 +184,7 @@ export const fetchRecordsBySkyflowID = async ( .then( (resolvedResult: any) => { const response: any[] = []; - const recordsData: any[] = resolvedResult.records; + const recordsData: any[] = resolvedResult.data.records; recordsData.forEach((fieldData) => { const id = fieldData.fields.skyflow_id; const currentRecord = { @@ -276,7 +276,7 @@ export const updateRecordsBySkyflowID = ( updateRecordInVault(updateRecord, options, client, authToken as string) .then( (response: any) => { - updateResponse.push(formatUpdateSuccessResponse(response)); + updateResponse.push(formatUpdateSuccessResponse(response.data)); }, (cause: any) => { updateResponse.push(formatUpdateFailureResponse(cause,updateRecord.id)); diff --git a/test/vault-api/Client.test.js b/test/vault-api/Client.test.js index c835c32..edb7e87 100644 --- a/test/vault-api/Client.test.js +++ b/test/vault-api/Client.test.js @@ -44,7 +44,10 @@ describe("Client Class",()=>{ data: data, headers: headers, }); - expect(response).toEqual({ message: "Success", requestId: "22r5-dfbf-3543" }); + expect(response).toEqual({ + data: {message: "Success"}, + metadata: {requestId: "22r5-dfbf-3543"} + }); }); test("should return an error if the request to client fails", async () => { diff --git a/test/vault-api/Skyflow.test.js b/test/vault-api/Skyflow.test.js index 088a3bb..31dbc24 100644 --- a/test/vault-api/Skyflow.test.js +++ b/test/vault-api/Skyflow.test.js @@ -171,7 +171,7 @@ describe('skyflow insert', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(insertResponse)); + const clientReq = jest.fn(() => Promise.resolve({data:insertResponse})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -202,7 +202,7 @@ describe('skyflow insert', () => { test('insert success without tokens', () => { - const clientReq = jest.fn(() => Promise.resolve(insertResponseWithoutTokens)); + const clientReq = jest.fn(() => Promise.resolve({data:insertResponseWithoutTokens})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -265,7 +265,7 @@ describe('skyflow insert', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(insertResponse)); + const clientReq = jest.fn(() => Promise.resolve({data:insertResponse})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -356,7 +356,9 @@ describe('skyflow insert', () => { __esModule: true, isTokenValid:jest.fn(() => true), })); - const clientReq = jest.fn(() => Promise.resolve(insertResponseCOEWithTokens)); + const clientReq = jest.fn(() => Promise.resolve({ + data: insertResponseCOEWithTokens, metadata: {requestId: 123} + })); const mockClient = { config: skyflowConfig, request: clientReq, @@ -397,7 +399,9 @@ describe('skyflow insert', () => { __esModule: true, isTokenValid:jest.fn(() => true), })); - const clientReq = jest.fn(() => Promise.resolve(insertResponseCOEWithoutTokens)); + const clientReq = jest.fn(() => Promise.resolve({ + data: insertResponseCOEWithoutTokens, metadata: {requestId: 123} + })); const mockClient = { config: skyflowConfig, request: clientReq, @@ -481,7 +485,7 @@ describe('skyflow detokenize', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(detokenizeRes)); + const clientReq = jest.fn(() => Promise.resolve({data:detokenizeRes})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -787,7 +791,7 @@ describe('skyflow getById', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(getByIdRes)); + const clientReq = jest.fn(() => Promise.resolve({data:getByIdRes})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -938,7 +942,7 @@ describe('skyflow get method', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(getByIdRes)); + const clientReq = jest.fn(() => Promise.resolve({data:getByIdRes})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -1196,7 +1200,7 @@ describe('skyflow invoke connection', () => { __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(invokeConnectionRes)); + const clientReq = jest.fn(() => Promise.resolve({data:invokeConnectionRes})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -1410,7 +1414,7 @@ describe("Update method",()=>{ __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(successUpdateRequestResponse)); + const clientReq = jest.fn(() => Promise.resolve({data:successUpdateRequestResponse})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -1443,7 +1447,7 @@ describe("Update method",()=>{ __esModule: true, isTokenValid:jest.fn(()=>true), })); - const clientReq = jest.fn(() => Promise.resolve(successUpdateRequestWithoutTokensResponse)); + const clientReq = jest.fn(() => Promise.resolve({data:successUpdateRequestWithoutTokensResponse})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -1480,7 +1484,7 @@ describe("Update method",()=>{ const clientReq = jest.fn().mockImplementation((args) => { const check = args.url.includes('test_update_id') if(check) - return Promise.resolve(successUpdateRequestResponse); + return Promise.resolve({data:successUpdateRequestResponse}); else return Promise.reject(errorUpdateRequestResponse); }); @@ -1588,7 +1592,7 @@ describe('skyflow detokenize with redaction', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(detokenizeRes) + return Promise.resolve({data:detokenizeRes}) }); const mockClient = { @@ -1636,7 +1640,7 @@ describe('skyflow detokenize with redaction', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(detokenizeRes) + return Promise.resolve({data:detokenizeRes}) }); const mockClient = { @@ -1684,7 +1688,7 @@ describe('skyflow detokenize with redaction', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(detokenizeRes) + return Promise.resolve({data:detokenizeRes}) }); const mockClient = { @@ -1732,7 +1736,7 @@ describe('skyflow detokenize with redaction', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(detokenizeRes) + return Promise.resolve({data:detokenizeRes}) }); const mockClient = { @@ -1780,7 +1784,7 @@ describe('skyflow detokenize with redaction', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(detokenizeRes) + return Promise.resolve({data:detokenizeRes}) }); const mockClient = { @@ -1897,7 +1901,7 @@ describe('get method with options', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(getByIdRes) + return Promise.resolve({data:getByIdRes}) }); const mockClient = { @@ -1934,7 +1938,7 @@ describe('get method with options', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(getByIdRes) + return Promise.resolve({data:getByIdRes}) }); const mockClient = { @@ -1971,7 +1975,7 @@ describe('get method with options', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(getByIdRes) + return Promise.resolve({data:getByIdRes}) }); const mockClient = { @@ -2063,7 +2067,7 @@ describe('get method with options', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(getByIdRes); + return Promise.resolve({data:getByIdRes}); }); const mockClient = { @@ -2096,7 +2100,7 @@ describe('get method with options', () => { let reqArg; const clientReq = jest.fn((arg) => { reqArg = arg; - return Promise.resolve(getByIdRes); + return Promise.resolve({data:getByIdRes}); }); const mockClient = { @@ -2222,7 +2226,7 @@ describe('skyflow delete method', () => { isTokenValid: jest.fn(() => true), })); - const clientReq = jest.fn(() => Promise.resolve(successDeleteRequestResponse)); + const clientReq = jest.fn(() => Promise.resolve({data:successDeleteRequestResponse})); const mockClient = { config: skyflowConfig, request: clientReq, @@ -2301,7 +2305,7 @@ describe('skyflow delete method', () => { const clientReq = jest.fn((args) => { const check = args.url.includes('test_delete_id') if (check) { - return Promise.resolve(successDeleteRequestResponse); + return Promise.resolve({data:successDeleteRequestResponse}); } else { return Promise.reject(errorDeleteRequestResponse); }