Skip to content

Commit

Permalink
Feast product name should be FeastInAppPurchase in soft-opt-in queue (
Browse files Browse the repository at this point in the history
#1414)

The soft-opt-in consents for Feast IAPs are different from other IAPs. In the
soft-opt-in-consent-setter lambda the config has been updated to specify the
new consents, but that lambda needs to be able to distinguish Feast IAPs from
others. So in this PR we start providing a different productName
(FeastInAppPurchase) for Feast events.
  • Loading branch information
tjmw authored Mar 21, 2024
1 parent 74124ef commit 77c8ed0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
22 changes: 17 additions & 5 deletions typescript/src/soft-opt-ins/processSubscription.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {dynamoMapper, sendToSqs, sendToSqsComms, sendToSqsSoftOptIns, SoftOptInEvent} from "../utils/aws";
import {dynamoMapper, sendToSqs, sendToSqsComms, sendToSqsSoftOptIns, SoftOptInEvent, SoftOptInEventProductName} from "../utils/aws";
import {ReadSubscription} from "../models/subscription";
import {Region, Stage} from "../utils/appIdentity";
import fetch from 'node-fetch';
import { Response } from 'node-fetch';
import {getIdentityApiKey, getIdentityUrl, getMembershipAccountId} from "../utils/guIdentityApi";
import {plusDays} from "../utils/dates";
import { Platform } from "../models/platform";

export function isPostAcquisition(startTimestamp: string): boolean {
const twoDaysInMilliseconds = 48 * 60 * 60 * 1000;
Expand Down Expand Up @@ -52,11 +53,22 @@ async function getUserEmailAddress(identityId: string, identityApiKey: string):
}
}

async function sendSoftOptIns(identityId: string, subscriptionId: string, queueNamePrefix: string) {
const mapPlatformToSoftOptInProductName = (platform: string | undefined): SoftOptInEventProductName => {
switch (platform) {
case Platform.IosFeast:
return "FeastInAppPurchase";
default:
return "InAppPurchase";
}
};

async function sendSoftOptIns(identityId: string, subscriptionId: string, platform: string | undefined, queueNamePrefix: string) {
const productName = mapPlatformToSoftOptInProductName(platform);

const message: SoftOptInEvent = {
identityId: identityId,
eventType: "Acquisition",
productName: "InAppPurchase",
productName,
subscriptionId: subscriptionId
};

Expand Down Expand Up @@ -90,7 +102,7 @@ export async function processAcquisition(subscriptionRecord: ReadSubscription, i
const queueNamePrefix = `https://sqs.${Region}.amazonaws.com/${membershipAccountId}`;

try {
await sendSoftOptIns(identityId, subscriptionId, queueNamePrefix);
await sendSoftOptIns(identityId, subscriptionId, subscriptionRecord.platform, queueNamePrefix);
} catch (e) {
handleError(`Soft opt-in message send failed for subscriptionId: ${subscriptionId}. ${e}`)
}
Expand Down Expand Up @@ -118,4 +130,4 @@ export async function processAcquisition(subscriptionRecord: ReadSubscription, i
}

return true;
}
}
5 changes: 3 additions & 2 deletions typescript/src/utils/aws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,11 @@ export function sendToSqs(queueUrl: string, event: any, delaySeconds?: number):
}).promise()
}

export type SoftOptInEventProductName = "InAppPurchase" | "FeastInAppPurchase";
export interface SoftOptInEvent {
identityId: string;
eventType: "Acquisition" | "Cancellation" | "Switch";
productName: "InAppPurchase";
productName: SoftOptInEventProductName
subscriptionId: string;
}

Expand All @@ -158,4 +159,4 @@ export async function sendToSqsComms(queueUrl: string, event: any, delaySeconds?
MessageBody: JSON.stringify(event),
DelaySeconds: delaySeconds
}).promise();
}
}
46 changes: 44 additions & 2 deletions typescript/tests/soft-opt-ins/acquisition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {isPostAcquisition} from "../../src/soft-opt-ins/processSubscription";
import {handler} from "../../src/soft-opt-ins/acquisitions";
import {DynamoDBStreamEvent} from "aws-lambda";
import {ReadSubscription} from "../../src/models/subscription";
import { Platform } from "../../src/models/platform";

jest.mock('@aws/dynamodb-data-mapper', () => {
const actualDataMapper = jest.requireActual('@aws/dynamodb-data-mapper');
Expand Down Expand Up @@ -157,11 +158,52 @@ describe('handler', () => {

expect(mockSQS.sendMessage).toHaveBeenCalledTimes(1);

const expectedSendMessageParams1 = {
const expectedSendMessageParams = {
QueueUrl: `https://sqs.eu-west-1.amazonaws.com/mock-aws-account-id/soft-opt-in-consent-setter-queue-DEV`,
MessageBody: JSON.stringify({identityId: '67890', eventType: 'Acquisition', productName: "InAppPurchase", subscriptionId: "12345"}),
};

expect(mockSQS.sendMessage).toHaveBeenCalledWith(expectedSendMessageParams);
});

it('processes Feast acquisitions correctly', async () => {
const subscriptionId = '11111';
const identityId = '22222';
const event: DynamoDBStreamEvent = {
Records: [
{
eventName: 'INSERT',
dynamodb: {
NewImage: {
subscriptionId: { S: subscriptionId },
userId: { S: identityId },
},
},
},
]
};
// get the mock instances
const mockDataMapper = new (require('@aws/dynamodb-data-mapper').DataMapper)();
const mockSQS = new (require('aws-sdk/clients/sqs'))();
const sub = new ReadSubscription();
sub.subscriptionId = subscriptionId;
sub.startTimestamp = "2023-03-14 07:24:38 UTC";
sub.endTimestamp = "2023-03-14 07:24:38 UTC";
sub.platform = Platform.IosFeast;
setMockGet(() => sub);

await handler(event);

expect(mockDataMapper.get).toHaveBeenCalledTimes(1);
let expectedQuery = new ReadSubscription();
expectedQuery.setSubscriptionId(subscriptionId)
expect(mockDataMapper.get).toHaveBeenCalledWith(expectedQuery);

expect(mockSQS.sendMessage).toHaveBeenCalledTimes(1);
const expectedSendMessageParams1 = {
QueueUrl: `https://sqs.eu-west-1.amazonaws.com/mock-aws-account-id/soft-opt-in-consent-setter-queue-DEV`,
MessageBody: JSON.stringify({ identityId, eventType: 'Acquisition', productName: "FeastInAppPurchase", subscriptionId }),
};
expect(mockSQS.sendMessage).toHaveBeenCalledWith(expectedSendMessageParams1);
});

Expand Down Expand Up @@ -229,4 +271,4 @@ describe('handler', () => {

expect(mockSQS.sendMessage).toHaveBeenCalledWith(expectedSendMessageParams1);
});
});
});

0 comments on commit 77c8ed0

Please sign in to comment.