This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fe34d6
commit 56cf3f8
Showing
4 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { DynamoDbDataService } from 'fhir-works-on-aws-persistence-ddb'; | ||
import reaperHandler from './subscriptionReaper'; | ||
|
||
jest.mock('fhir-works-on-aws-persistence-ddb'); | ||
describe('subscriptionReaper', () => { | ||
test('no subscriptions to delete', async () => { | ||
const expectedResponse: { | ||
success: boolean; | ||
message: string; | ||
}[] = []; | ||
const subResource = [ | ||
{ | ||
resourceType: 'Subscription', | ||
id: 'sub1', | ||
status: 'requested', | ||
end: '2121-01-01T00:00:00Z', | ||
}, | ||
]; | ||
const mockGetActiveSubscriptions = jest.fn(); | ||
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions; | ||
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource); | ||
const actualResponse = await reaperHandler({}); | ||
expect(actualResponse).toEqual(expectedResponse); | ||
}); | ||
|
||
test('subscriptions that have expired should be deleted', async () => { | ||
const message = `Successfully deleted ResourceType: Subscription, Id: sub1, VersionId: 1`; | ||
const expectedResponse: { | ||
success: boolean; | ||
message: string; | ||
}[] = [ | ||
{ | ||
success: true, | ||
message, | ||
}, | ||
]; | ||
const subResource = [ | ||
{ | ||
resourceType: 'Subscription', | ||
id: 'sub1', | ||
status: 'requested', | ||
end: '2021-01-01T00:00:00Z', | ||
}, | ||
{ | ||
resourceType: 'Subscription', | ||
id: 'sub2', | ||
status: 'requested', | ||
end: '2121-01-01T00:00:00Z', | ||
}, | ||
]; | ||
const mockGetActiveSubscriptions = jest.fn(); | ||
const mockDeleteResource = jest.fn(); | ||
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions; | ||
DynamoDbDataService.prototype.deleteResource = mockDeleteResource; | ||
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource); | ||
mockDeleteResource.mockResolvedValueOnce([{ success: true, message }]); | ||
const actualResponse = await reaperHandler({}); | ||
expect(actualResponse).toEqual(expectedResponse); | ||
}); | ||
|
||
test('subscriptions that have no specified end should not be deleted', async () => { | ||
const expectedResponse: { | ||
success: boolean; | ||
message: string; | ||
}[] = []; | ||
const subResource = [ | ||
{ | ||
resourceType: 'Subscription', | ||
id: 'sub1', | ||
status: 'requested', | ||
}, | ||
]; | ||
const mockGetActiveSubscriptions = jest.fn(); | ||
DynamoDbDataService.prototype.getActiveSubscriptions = mockGetActiveSubscriptions; | ||
mockGetActiveSubscriptions.mockResolvedValueOnce(subResource); | ||
const actualResponse = await reaperHandler({}); | ||
expect(actualResponse).toEqual(expectedResponse); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
import { DynamoDbDataService, DynamoDb } from 'fhir-works-on-aws-persistence-ddb'; | ||
|
||
const enableMultiTenancy = process.env.ENABLE_MULTI_TENANCY === 'true'; | ||
const dbServiceWithTenancy = new DynamoDbDataService(DynamoDb, false, { | ||
enableMultiTenancy, | ||
}); | ||
const dbService = new DynamoDbDataService(DynamoDb); | ||
|
||
const reaperHandler = async (event: any) => { | ||
console.log('subscriptionReaper event', event); | ||
const subscriptions = await dbService.getActiveSubscriptions({}); | ||
const currentTime = new Date(); | ||
// filter out subscriptions without a defined end time. | ||
// check if subscription is past its end date (ISO format) | ||
// example format of subscriptions: https://www.hl7.org/fhir/subscription-example.json.html | ||
return Promise.all( | ||
subscriptions | ||
.filter((s: Record<string, any>) => { | ||
const date = new Date(s.end); | ||
if (date.toString() === 'Invalid Date') { | ||
console.log(`Skipping subscription ${s.id} since the end date is not in a valid format: ${s.end}`); | ||
return false; | ||
} | ||
return currentTime >= date; | ||
}) | ||
.map(async (subscription) => { | ||
// delete the subscription as it has reached its end time | ||
return dbServiceWithTenancy.deleteResource({ | ||
resourceType: subscription.resourceType, | ||
// eslint-disable-next-line no-underscore-dangle | ||
id: subscription._id, | ||
// _tenantId is an internal field, and getActiveSubscriptions returns the raw Record<string, any> | ||
// eslint-disable-next-line no-underscore-dangle | ||
tenantId: subscription._tenantId, | ||
}); | ||
}), | ||
); | ||
}; | ||
|
||
export default reaperHandler; |