Skip to content

Commit

Permalink
SK-1372 fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
skyflow-bharti committed Jan 18, 2024
1 parent 7c0ef2e commit 9b05e51
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 16 deletions.
116 changes: 106 additions & 10 deletions src/core-utils/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import SkyflowError from '../libs/skyflow-error';
import { getAccessToken } from '../utils/bus-events';
import {
IInsertRecordInput, IInsertRecord, IValidationRule, ValidationRuleType,
MessageType, LogLevel,
MessageType, LogLevel, IInsertCollectResponse, IInsertOptions,
} from '../utils/common';
import SKYFLOW_ERROR_CODE from '../utils/constants';
import { printLog } from '../utils/logs-helper';
Expand All @@ -33,7 +33,7 @@ export const getUpsertColumn = (tableName: string, options:Array<IUpsertOptions>
};
export const constructInsertRecordRequest = (
records: IInsertRecordInput,
options: Record<string, any> = { tokens: true },
options: IInsertOptions = { tokens: true, continueOnError: true },
) => {
let requestBody: any = [];
records.records.forEach((record) => {
Expand All @@ -51,13 +51,14 @@ export const constructInsertRecordRequest = (
return requestBody;
};

export const constructInsertRecordResponse = (
export const constructInsertRecordResponseWithoutContinueOnError = (
responseBody: any,
options: Record<string, any> = { tokens: true },
records: IInsertRecord[],
) => {
let finalResponse: IInsertCollectResponse = {};
if (options.tokens) {
return {
finalResponse = {
records: responseBody.responses
.map((res, index) => {
const skyflowId = responseBody.responses[index].records[0].skyflow_id;
Expand All @@ -67,16 +68,66 @@ export const constructInsertRecordResponse = (
skyflow_id: skyflowId,
...res.records[0].tokens,
},
request_index: index,
};
}),
};
} else {
finalResponse = {
records: responseBody.responses.map((res, index) => ({
table: records[index].table,
skyflow_id: responseBody.responses[index].records[0].skyflow_id,
request_index: index,
})),
};
}
return {
records: responseBody.responses.map((res, index) => ({
table: records[index].table,
skyflow_id: res.records[0].skyflow_id,
})),
};
return finalResponse;
};

export const constructInsertRecordResponseWithContinueOnError = (responseBody: any,
options: Record<string, any> = { tokens: true },
records: IInsertRecord[]) => {
const successRecord:any = [];
const failedRecord:any = [];
responseBody.responses

Check warning on line 92 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L90-L92

Added lines #L90 - L92 were not covered by tests
.forEach((response, index) => {
const body = response.Body;
const status = response.Status;

Check warning on line 95 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L94-L95

Added lines #L94 - L95 were not covered by tests
if ('records' in body) {
const record = body.records[0];

Check warning on line 97 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L97

Added line #L97 was not covered by tests
if (options.tokens) {
successRecord.push({

Check warning on line 99 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L99

Added line #L99 was not covered by tests
table: records[index].table,
fields: {
skyflow_id: record.skyflow_id,
...record.tokens,
},
request_index: index,
});
} else {
successRecord.push({

Check warning on line 108 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L108

Added line #L108 was not covered by tests
table: records[index].table,
skyflow_id: record.skyflow_id,
request_index: index,
});
}
} else {
failedRecord.push({

Check warning on line 115 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L115

Added line #L115 was not covered by tests
code: status,
description: `${body.error} - requestId: ${responseBody.requestId}`,
request_index: index,
});
}
});
const finalResponse: IInsertCollectResponse = {};

Check warning on line 122 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L122

Added line #L122 was not covered by tests

if (successRecord.length > 0) {
finalResponse.records = successRecord;

Check warning on line 125 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L125

Added line #L125 was not covered by tests
}
if (failedRecord.length > 0) {
finalResponse.errors = failedRecord;

Check warning on line 128 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L128

Added line #L128 was not covered by tests
}
return finalResponse;

Check warning on line 130 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L130

Added line #L130 was not covered by tests
};

export const constructFinalUpdateRecordResponse = (
Expand Down Expand Up @@ -199,6 +250,51 @@ const updateRecordsInVault = (
});
};

export const insertDataInCollect = async (
records,
client: Client,
options,
finalInsertRecords,
) => new Promise((rootResolve, rootReject) => {
let insertResponse;
const clientId = client.toJSON()?.metaData?.uuid || '';
getAccessToken(clientId).then((authToken) => {
client
.request({
body: { ...records },
requestMethod: 'POST',
url: `${client.config.vaultURL}/v1/vaults/${client.config.vaultID}`,
headers: {
authorization: `Bearer ${authToken}`,
'content-type': 'application/json',
},
})
.then((response: any) => {
if (!options.continueOnError) {
insertResponse = constructInsertRecordResponseWithoutContinueOnError(
response,
options,
finalInsertRecords.records,
);
} else {
insertResponse = constructInsertRecordResponseWithContinueOnError(

Check warning on line 280 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L280

Added line #L280 was not covered by tests
response,
options,
finalInsertRecords.records,
);
}
rootResolve(insertResponse);
},
(rejectedResult) => {
rootReject(rejectedResult);
}).catch((err) => {
rootReject(err);

Check warning on line 291 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L291

Added line #L291 was not covered by tests
});
}).catch((err) => {
rootReject(err);

Check warning on line 294 in src/core-utils/collect.ts

View check run for this annotation

Codecov / codecov/patch

src/core-utils/collect.ts#L294

Added line #L294 was not covered by tests
});
});

export const updateRecordsBySkyflowID = async (
skyflowIdRecords,
client: Client,
Expand Down
14 changes: 8 additions & 6 deletions src/core/internal/iframe-form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import SKYFLOW_ERROR_CODE from '../../../utils/constants';
import logs from '../../../utils/logs';
import {
Context,
IInsertCollectResponse,
IValidationRule,
LogLevel,
MessageType,
Expand Down Expand Up @@ -1067,33 +1068,34 @@ export class IFrameForm {
Promise.allSettled(insertPromiseSet).then((resultSet: any) => {
const recordsResponse: any[] = [];
const errorsResponse: any[] = [];
resultSet.forEach((result: { status: string; value: any; reason?: any; }) => {
resultSet.forEach((result:
{ status: string; value: IInsertCollectResponse; reason?: IInsertCollectResponse; }) => {
if (result.status === 'fulfilled') {
if (result.value.records) {
if (result.value.records !== undefined && Array.isArray(result.value.records)) {
result.value.records.forEach((record) => {
recordsResponse.push(record);
});
}
if (result.value.errors) {
if (result.value.errors !== undefined && Array.isArray(result.value.errors)) {
result.value.errors.forEach((error) => {
errorsResponse.push(error);

Check warning on line 1081 in src/core/internal/iframe-form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/internal/iframe-form/index.ts#L1080-L1081

Added lines #L1080 - L1081 were not covered by tests
});
}
} else {
if (result.reason.records) {
if (result.reason?.records !== undefined && Array.isArray(result.reason?.records)) {
result.reason.records.forEach((record) => {
recordsResponse.push(record);

Check warning on line 1087 in src/core/internal/iframe-form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/internal/iframe-form/index.ts#L1086-L1087

Added lines #L1086 - L1087 were not covered by tests
});
}
if (result.reason.errors) {
if (result.reason?.errors !== undefined && Array.isArray(result.reason?.errors)) {
result.reason.errors.forEach((error) => {
errorsResponse.push(error);
});
}
}
});
if (errorsResponse.length === 0) {
rootResolve(recordsResponse);
rootResolve({ records: recordsResponse });
} else if (recordsResponse.length === 0) rootReject({ errors: errorsResponse });
else rootReject({ records: recordsResponse, errors: errorsResponse });

Check warning on line 1100 in src/core/internal/iframe-form/index.ts

View check run for this annotation

Codecov / codecov/patch

src/core/internal/iframe-form/index.ts#L1100

Added line #L1100 was not covered by tests
});
Expand Down

0 comments on commit 9b05e51

Please sign in to comment.