export async function toDynamoEvent_apple_async()

in typescript/src/pubsub/apple.ts [13:90]


export async function toDynamoEvent_apple_async(
  notification: StatusUpdateNotification,
  useStoreKitForExtra: boolean
): Promise<SubscriptionEvent> {
  const now = new Date();
  const eventType = notification.notification_type;
  const receiptInfo = notification.unified_receipt.latest_receipt_info;
  console.log(
    `notification is from ${
      notification.environment
    }, latest_receipt_info is undefined: ${
      notification.unified_receipt.latest_receipt_info === undefined
    }`,
  );
  const platform = appleBundleToPlatform(notification.bid);
  if (!platform) {
    console.warn(`Unknown bundle id ${notification.bid}`);
  }

  if (receiptInfo.length === 0) {
    console.warn(
      `No latest_receipt_info has been found, it has returned an empty array`,
    );
  }

  const receiptsInOrder = receiptInfo.sort((receipt1, receipt2) => {
    return (
      Number.parseInt(receipt2.purchase_date_ms) -
      Number.parseInt(receipt1.purchase_date_ms)
    );
  });

  // The Guardian's "free trial" period definition is slightly different from Apple, hence why we test for is_in_intro_offer_period
  const freeTrial =
    receiptsInOrder[0].is_trial_period === 'true' ||
    receiptsInOrder[0].is_in_intro_offer_period === 'true';

  // Preventin:g ERROR: Unable to process event[object Object] ValidationException: Item size has exceeded the maximum allowed size
  // Which for some reasons has only been observed in CODE
  if (
    Stage === 'CODE' &&
    notification.unified_receipt.latest_receipt.length > 100 * 1024
  ) {
    // Bigger than 100Kb
    notification.unified_receipt.latest_receipt = '';
  }

  var extra = '';
  if (useStoreKitForExtra) {
    // Defining the two variables we need to call for the extra data
    const original_transaction_id = receiptsInOrder[0].original_transaction_id;
    const appBundleId = notification.bid;
    const extra_object = await transactionIdToAppleStoreKitSubscriptionDataDerivationForExtra(appBundleId, original_transaction_id); 
    extra = JSON.stringify(extra_object);
  }

  const subscription = new SubscriptionEvent(
    receiptsInOrder[0].original_transaction_id,
    now.toISOString() + '|' + eventType,
    now.toISOString().substr(0, 10),
    now.toISOString(),
    eventType,
    platform ?? 'unknown',
    notification.bid,
    freeTrial,
    null,
    notification, // applePayload
    dateToSecondTimestamp(thirtyMonths(now)),
    notification.promotional_offer_id, // SubscriptionEvent.promotional_offer_id
    notification.promotional_offer_name, // SubscriptionEvent.promotional_offer_name
    notification.product_id, // SubscriptionEvent.product_id
    notification.purchase_date_ms, // SubscriptionEvent.purchase_date_ms
    notification.expires_date_ms, // SubscriptionEvent.expires_date_ms
    extra,
  );

  return Promise.resolve(subscription);
}