-
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.
Publish Feast/Apple sub notifications to SQS
- Loading branch information
1 parent
a317e31
commit 5f4c199
Showing
3 changed files
with
178 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import {APIGatewayProxyEvent, APIGatewayProxyResult} from "aws-lambda"; | ||
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda"; | ||
import { parsePayload, toSqsSubReference } from "../../pubsub/apple"; | ||
import { AppleSubscriptionReference } from "../../models/subscriptionReference"; | ||
import Sqs from 'aws-sdk/clients/sqs'; | ||
import { sendToSqs } from "../../utils/aws"; | ||
import { AWSError } from "aws-sdk"; | ||
import { PromiseResult } from "aws-sdk/lib/request"; | ||
import { HTTPResponses } from "../../models/apiGatewayHttp"; | ||
|
||
export async function handler(request: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> { | ||
console.log(`[34ef7aa3] ${JSON.stringify(request)}`) | ||
return { statusCode: 200, body: JSON.stringify(request) } | ||
return processEvent()(request) | ||
} | ||
|
||
export function processEvent( | ||
sendMessageToSqs: (queueUrl: string, message: AppleSubscriptionReference) => Promise<PromiseResult<Sqs.SendMessageResult, AWSError>> = sendToSqs | ||
): (request: APIGatewayProxyEvent) => Promise<APIGatewayProxyResult> { | ||
return async (request: APIGatewayProxyEvent) => { | ||
|
||
const statusUpdateNotification = | ||
parsePayload(request.body) | ||
if (statusUpdateNotification instanceof Error) { | ||
return HTTPResponses.INVALID_REQUEST | ||
} | ||
|
||
const appleSubscriptionReference = | ||
toSqsSubReference(statusUpdateNotification) | ||
|
||
try { | ||
const queueUrl = process.env.QueueUrl; | ||
if (queueUrl === undefined) throw new Error("No QueueUrl env parameter provided"); | ||
|
||
await sendMessageToSqs(queueUrl, appleSubscriptionReference) | ||
|
||
return HTTPResponses.OK | ||
} catch (e) { | ||
console.error("Internal server error", e); | ||
return HTTPResponses.INTERNAL_ERROR | ||
} | ||
} | ||
} |
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,89 @@ | ||
import { StatusUpdateNotification } from "../../../src/pubsub/apple"; | ||
import Mock = jest.Mock; | ||
import { APIGatewayProxyEvent } from "aws-lambda"; | ||
import { processEvent } from "../../../src/feast/pubsub/pubsub"; | ||
import { HTTPResponses } from "../../../src/models/apiGatewayHttp"; | ||
|
||
describe("The Feast Apple pubsub", () => { | ||
test("Should return HTTP 200 and publish the event to SQS", () => { | ||
process.env['QueueUrl'] = ""; | ||
|
||
const mockSqsFunction: Mock<Promise<any>, [string, {receipt: string}]> = jest.fn((queueurl, event) => Promise.resolve({})); | ||
|
||
const body: StatusUpdateNotification = { | ||
original_transaction_id: "TEST", | ||
cancellation_date: "TEST", | ||
web_order_line_item_id: "TEST", | ||
auto_renew_adam_id: "TEST", | ||
expiration_intent: "TEST", | ||
auto_renew_product_id: "uk.co.guardian.gla.12months.2018Dec.withFreeTrial", | ||
auto_renew_status: "true", | ||
bid: "uk.co.guardian.iphone2", | ||
bvrs: "TEST", | ||
environment: "Sandbox", | ||
notification_type: "INITIAL_BUY", | ||
unified_receipt: { | ||
environment: "Sandbox", | ||
latest_receipt: "TEST", | ||
latest_receipt_info: [{ | ||
app_item_id: "TEST", | ||
bvrs: "TEST", | ||
is_in_intro_offer_period: "false", | ||
is_trial_period: "true", | ||
item_id: "TEST", | ||
original_transaction_id: "TEST", | ||
product_id: "uk.co.guardian.gla.12months.2018Dec.withFreeTrial", | ||
quantity: "1", | ||
transaction_id: "TEST", | ||
unique_identifier: "TEST", | ||
unique_vendor_identifier: "TEST", | ||
version_external_identifier: "TEST", | ||
web_order_line_item_id: "TEST", | ||
purchase_date_ms: "TEST", | ||
original_purchase_date_ms: "TEST", | ||
expires_date: "TEST", | ||
expires_date_ms: "TEST" | ||
}], | ||
pending_renewal_info: [ | ||
{ | ||
auto_renew_product_id: "uk.co.guardian.gla.12months.2018Dec.withFreeTrial", | ||
auto_renew_status: "1", | ||
original_transaction_id: "TEST", | ||
product_id: "uk.co.guardian.gla.12months.2018Dec.withFreeTrial", | ||
price_consent_status: '', | ||
price_increase_status: '', | ||
} | ||
], | ||
status: 0 | ||
}, | ||
promotional_offer_id: "promotional_offer_id", | ||
promotional_offer_name: "promotional_offer_name", | ||
product_id: "product_id", | ||
purchase_date_ms: 0, | ||
expires_date_ms: 0 | ||
} | ||
|
||
const input: APIGatewayProxyEvent = { | ||
body: JSON.stringify(body), | ||
headers: {}, | ||
multiValueHeaders: {}, | ||
httpMethod: "POST", | ||
isBase64Encoded: false, | ||
path: '', | ||
pathParameters: {}, | ||
multiValueQueryStringParameters: {}, | ||
// @ts-ignore | ||
requestContext: null, | ||
resource: '', | ||
|
||
} | ||
|
||
const expectedSubscriptionReferenceInSqs = {receipt: "TEST"}; | ||
|
||
return processEvent(mockSqsFunction)(input).then(result => { | ||
expect(result).toStrictEqual(HTTPResponses.OK); | ||
expect(mockSqsFunction.mock.calls.length).toEqual(1); | ||
expect(mockSqsFunction.mock.calls[0][1]).toStrictEqual(expectedSubscriptionReferenceInSqs); | ||
}); | ||
}); | ||
}); |