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

Commit

Permalink
feat: Fetch egress store data by id instead of using a scan
Browse files Browse the repository at this point in the history
  • Loading branch information
jn1119 committed Oct 11, 2021
1 parent 129dc4c commit a9f48b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
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,14 @@ describe('DataEgressService', () => {
},
};

dbService.table.scan.mockResolvedValueOnce([
{
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 +508,14 @@ describe('DataEgressService', () => {
};

const egressStoreId = 'test-egress-store-id';
dbService.table.scan.mockResolvedValueOnce([
{
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 +613,15 @@ describe('DataEgressService', () => {
};

const egressStoreId = 'test-egress-store-id';
dbService.table.scan.mockResolvedValueOnce([
{
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 +666,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 +675,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 +690,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 +865,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 +908,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 +977,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

0 comments on commit a9f48b9

Please sign in to comment.