Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

feat: Fetch egress store data by id instead of using a scan #741

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('DataEgressService', () => {
},
};

dbService.table.scan.mockImplementationOnce(() => {
dbService.table.get.mockImplementationOnce(() => {
throw new Error();
});
const requestContext = {};
Expand Down Expand Up @@ -439,7 +439,7 @@ describe('DataEgressService', () => {
},
};

dbService.table.scan.mockResolvedValue([]);
dbService.table.get.mockResolvedValue();
const requestContext = {};
const envId = 'test-id';

Expand All @@ -466,15 +466,13 @@ describe('DataEgressService', () => {
},
};

dbService.table.scan.mockResolvedValueOnce([
{
status: 'PROCESSING',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: 'test-egress-store-id',
},
]);
dbService.table.get.mockResolvedValueOnce({
status: 'PROCESSING',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: 'test-egress-store-id',
});
const requestContext = {};
const envId = 'test-workspace-id';

Expand Down Expand Up @@ -509,15 +507,13 @@ describe('DataEgressService', () => {
};

const egressStoreId = 'test-egress-store-id';
dbService.table.scan.mockResolvedValueOnce([
{
status: 'PROCESSED',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: egressStoreId,
},
]);
dbService.table.get.mockResolvedValueOnce({
status: 'PROCESSED',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: egressStoreId,
});
const requestContext = {};
const envId = 'test-workspace-id';

Expand Down Expand Up @@ -615,16 +611,14 @@ describe('DataEgressService', () => {
};

const egressStoreId = 'test-egress-store-id';
dbService.table.scan.mockResolvedValueOnce([
{
status: 'CREATED',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: egressStoreId,
isAbleToSubmitEgressRequest,
},
]);
dbService.table.get.mockResolvedValueOnce({
status: 'CREATED',
workspaceId: 'test-workspace-id',
s3BucketName: 'test-s3BucketName',
s3BucketPath: 'test-s3BucketPath',
id: egressStoreId,
isAbleToSubmitEgressRequest,
});
const requestContext = {};
const envId = 'test-workspace-id';

Expand Down Expand Up @@ -669,11 +663,7 @@ describe('DataEgressService', () => {

describe('Get Egress Store info', () => {
it('should get egress store info', async () => {
dbService.table.scan.mockResolvedValue([
{
workspaceId: 'test-egress-store-id',
},
]);
dbService.table.get.mockResolvedValue({ workspaceId: 'test-egress-store-id' });

const result = await dataEgressService.getEgressStoreInfo('test-egress-store-id');
expect(result).toStrictEqual({
Expand All @@ -682,7 +672,7 @@ describe('DataEgressService', () => {
});

it('should error out egress store info', async () => {
dbService.table.scan.mockImplementationOnce(() => {
dbService.table.get.mockImplementationOnce(() => {
throw new Error();
});

Expand All @@ -697,7 +687,7 @@ describe('DataEgressService', () => {
});

it('should error out without finding egress store info', async () => {
dbService.table.scan.mockResolvedValue([]);
dbService.table.get.mockResolvedValue();

const result = await dataEgressService.getEgressStoreInfo('test-egress-store-id');
expect(result).toStrictEqual(null);
Expand Down Expand Up @@ -872,7 +862,7 @@ describe('DataEgressService', () => {
ver: 'ver',
isAbleToSubmitEgressRequest: false,
};
dbService.table.scan.mockResolvedValue([mockEgressStoreInfo]);
dbService.table.get.mockResolvedValue(mockEgressStoreInfo);

await expect(dataEgressService.notifySNS(requestContext, 'workspaceId')).rejects.toThrow(
// It is better to check using boom.code instead of just the actual string, unless
Expand Down Expand Up @@ -915,7 +905,7 @@ describe('DataEgressService', () => {
ver: 'ver',
isAbleToSubmitEgressRequest: true,
};
dbService.table.scan.mockResolvedValue([mockEgressStoreInfo]);
dbService.table.get.mockResolvedValue(mockEgressStoreInfo);
const mockRequestContext = { principalIdentifier: { uid: 'test-createdBy' } };
await expect(dataEgressService.notifySNS(mockRequestContext, 'workspaceId')).rejects.toThrow(
// It is better to check using boom.code instead of just the actual string, unless
Expand Down Expand Up @@ -984,7 +974,7 @@ describe('DataEgressService', () => {
callback(null, {});
});

dbService.table.scan.mockResolvedValue([mockEgressStoreInfo]);
dbService.table.get.mockResolvedValue(mockEgressStoreInfo);
const mockRequestContext = { principalIdentifier: { uid: 'createdBy' } };
dataEgressService.lockAndUpdate = jest.fn();
dataEgressService.publishMessage = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,20 @@ class DataEgressService extends Service {

async getEgressStoreInfo(environmentId) {
const workspaceId = environmentId;
let egressStoreScanResult = [];
let egressStoreResult;

try {
egressStoreScanResult = await this._scanner()
.limit(1000)
.scan()
.then(egressStores => {
return egressStores.filter(store => store.workspaceId === workspaceId);
});
egressStoreResult = await this._getter()
.key({ id: workspaceId })
.get();
} catch (error) {
throw this.boom.notFound(`Error in fetch egress store info: ${JSON.stringify(error)}`, true);
}

if (egressStoreScanResult.length === 0) {
if (!egressStoreResult) {
return null;
}
if (egressStoreScanResult.length !== 1) {
throw this.boom.internalError(
`Error in getting egress store info: multiple results fetched from egrss store table`,
true,
);
}
return egressStoreScanResult[0];
return egressStoreResult;
}

async createEgressStore(requestContext, environment) {
Expand Down