-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a Lambda to update our cache of Feast subscriptions
- Loading branch information
1 parent
308f67b
commit f87e0a2
Showing
5 changed files
with
132 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { SQSEvent, SQSRecord } from "aws-lambda"; | ||
import { validateReceipt } from "../../services/appleValidateReceipts"; | ||
import { AppleSubscriptionReference } from "../../models/subscriptionReference"; | ||
import { toAppleSubscription } from "../../update-subs/apple"; | ||
import { Subscription } from "../../models/subscription"; | ||
import { dynamoMapper } from "../../utils/aws"; | ||
import { App } from "../../models/app" | ||
|
||
const decodeSubscriptionReference = | ||
(record: SQSRecord): AppleSubscriptionReference => { | ||
return JSON.parse(record.body) as AppleSubscriptionReference; | ||
} | ||
|
||
const defaultFetchSubscriptionsFromApple = | ||
(reference: AppleSubscriptionReference): Promise<Subscription[]> => { | ||
return validateReceipt(reference.receipt, {sandboxRetry: false}, App.Feast).then(subs => subs.map(toAppleSubscription)) | ||
} | ||
|
||
const defaultStoreSubscriptionInDynamo = | ||
(subscription: Subscription): Promise<void> => { | ||
return dynamoMapper.put({item: subscription}).then(_ => {}) | ||
} | ||
|
||
export function buildHandler( | ||
fetchSubscriptionsFromApple: (reference: AppleSubscriptionReference) => Promise<Subscription[]> = defaultFetchSubscriptionsFromApple, | ||
storeSubscriptionInDynamo: (subscription: Subscription) => Promise<void> = defaultStoreSubscriptionInDynamo | ||
): (event: SQSEvent) => Promise<string> { | ||
return async (event: SQSEvent) => { | ||
const work = | ||
event.Records.map(async record => { | ||
const reference = | ||
decodeSubscriptionReference(record) | ||
|
||
const subscriptions = | ||
await fetchSubscriptionsFromApple(reference) | ||
|
||
await Promise.all(subscriptions.map(storeSubscriptionInDynamo)) | ||
}) | ||
|
||
return Promise.all(work).then(_ => "OK") | ||
} | ||
} | ||
|
||
export const handler = buildHandler(); |
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,4 @@ | ||
export enum App { | ||
Live = "live", | ||
Feast = "feast" | ||
} |
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,69 @@ | ||
|
||
import { SQSEvent } from "aws-lambda"; | ||
import { buildHandler } from "../../../src/feast/update-subs/updatesubs"; | ||
import { Subscription } from "../../../src/models/subscription"; | ||
import { AppleSubscriptionReference } from "../../../src/models/subscriptionReference"; | ||
|
||
describe("The Feast (Apple) subscription updater", () => { | ||
it("Should fetch the subscription(s) associated with the reference from Apple and persist them to Dynamo", async () => { | ||
const event = | ||
buildSqsEvent(["TEST_RECEIPT_1", "TEST_RECEIPT_2"]) | ||
|
||
const handler = | ||
buildHandler(stubFetchSubscriptionsFromApple, mockStoreSubscriptionInDynamo) | ||
|
||
const result = | ||
await handler(event) | ||
|
||
expect(mockStoreSubscriptionInDynamo.mock.calls.length).toEqual(3) | ||
|
||
const storedSubscriptionIds = | ||
mockStoreSubscriptionInDynamo.mock.calls.map(call => call[0].subscriptionId) | ||
|
||
// The receipts `"TEST_RECEIPT_1"` & `"TEST_RECEIPT_2"` together reference the subscriptions with IDs | ||
// `"sub-1"`, `"sub-2"` & `"sub-3"`. Importantly, `"sub-4"` is referenced by the receipt `"TEST_RECEIPT_3"`, | ||
// which is not present in the event payload and therefore not looked up and persisted to the Dynamo table. | ||
expect(storedSubscriptionIds).toEqual(["sub-1", "sub-2", "sub-3"]) | ||
}); | ||
}); | ||
|
||
const buildSqsEvent = (receipts: string[]): SQSEvent => { | ||
const records = receipts.map(receipt => | ||
({ | ||
messageId: "", | ||
receiptHandle: "", | ||
body: JSON.stringify({ receipt: receipt }), | ||
attributes: { | ||
ApproximateReceiveCount: "", | ||
SentTimestamp: "", | ||
SenderId: "", | ||
ApproximateFirstReceiveTimestamp: "", | ||
}, | ||
messageAttributes: {}, | ||
md5OfBody: "", | ||
eventSource: "", | ||
eventSourceARN: "", | ||
awsRegion: "", | ||
})) | ||
|
||
return { | ||
Records: records | ||
} | ||
} | ||
|
||
const mockStoreSubscriptionInDynamo = | ||
jest.fn((subscription: Subscription) => Promise.resolve()); | ||
|
||
const stubFetchSubscriptionsFromApple = | ||
(reference: AppleSubscriptionReference) => Promise.resolve(subscriptions.filter(s => s.receipt == reference.receipt)); | ||
|
||
const subscription = | ||
(id: string, product: string, receipt: string) => | ||
new Subscription(id, "", "", "", false, product, "ios-feast", false, "6M", null, receipt, null) | ||
|
||
const subscriptions = [ | ||
subscription( "sub-1", "prod-1", "TEST_RECEIPT_1"), | ||
subscription( "sub-2", "prod-1", "TEST_RECEIPT_2"), | ||
subscription( "sub-3", "prod-2", "TEST_RECEIPT_2"), | ||
subscription( "sub-4", "prod-1", "TEST_RECEIPT_3") | ||
] |