AWSTemplateFormatVersion: 2010-09-09
Description: 'Mobile Purchases DynamoDB'

Parameters:
  Stack:
    Description: Stack name
    Type: String
  App:
    Description: Application name
    Type: String
  Stage:
    Description: Stage name
    Type: String
    AllowedValues:
      - CODE
      - PROD

Resources:
  UserSubscriptionEventsTableV2:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub ${App}-${Stage}-subscription-events-v2
      AttributeDefinitions:
        - AttributeName: subscriptionId
          AttributeType: S
        - AttributeName: timestampAndType
          AttributeType: S
        - AttributeName: date
          AttributeType: S
        - AttributeName: timestamp
          AttributeType: S
      KeySchema:
        - AttributeName: subscriptionId
          KeyType: HASH
        - AttributeName: timestampAndType
          KeyType: RANGE
      GlobalSecondaryIndexes:
        - IndexName: date-timestamp-index-v2
          KeySchema:
            - AttributeName: date
              KeyType: HASH
            - AttributeName: timestamp
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: false
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: true
      TimeToLiveSpecification:
        Enabled: true
        AttributeName: ttl
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App
        - Key: devx-backup-enabled
          Value: true

  SubscriptionTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub ${App}-${Stage}-subscriptions
      AttributeDefinitions:
        - AttributeName: subscriptionId
          AttributeType: S
        - AttributeName: endTimestamp
          AttributeType: S
      KeySchema:
        - AttributeName: subscriptionId
          KeyType: HASH
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: true
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: true
      TimeToLiveSpecification:
        Enabled: true
        AttributeName: ttl
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      GlobalSecondaryIndexes:
        - IndexName: ios-endTimestamp-revalidation-index-with-platform
          KeySchema:
            - AttributeName: subscriptionId
              KeyType: HASH
            - AttributeName: endTimestamp
              KeyType: RANGE
          Projection:
            NonKeyAttributes:
              - autoRenewing
              - platform
              - receipt
            ProjectionType: INCLUDE
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App
        - Key: devx-backup-enabled
          Value: true

  SubscriptionParallelTestTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub ${App}-${Stage}-subscriptions-parallel-test
      AttributeDefinitions:
        - AttributeName: subscriptionId
          AttributeType: S
        - AttributeName: endTimestamp
          AttributeType: S
      KeySchema:
        - AttributeName: subscriptionId
          KeyType: HASH
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: true
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: true
      TimeToLiveSpecification:
        Enabled: true
        AttributeName: ttl
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      GlobalSecondaryIndexes:
        - IndexName: ios-endTimestamp-revalidation-index
          KeySchema:
            - AttributeName: subscriptionId
              KeyType: HASH
            - AttributeName: endTimestamp
              KeyType: RANGE
          Projection:
            NonKeyAttributes:
              - autoRenewing
              - receipt
            ProjectionType: INCLUDE
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App
        - Key: devx-backup-enabled
          Value: true

  UserSubscriptionTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Sub ${App}-${Stage}-user-subscriptions
      AttributeDefinitions:
        - AttributeName: subscriptionId
          AttributeType: S
        - AttributeName: userId
          AttributeType: S
      KeySchema:
        - AttributeName: userId
          KeyType: HASH
        - AttributeName: subscriptionId
          KeyType: RANGE
      PointInTimeRecoverySpecification:
        PointInTimeRecoveryEnabled: true
      BillingMode: PAY_PER_REQUEST
      SSESpecification:
        SSEEnabled: true
      StreamSpecification:
        StreamViewType: NEW_IMAGE
      GlobalSecondaryIndexes:
        - IndexName: subscriptionId-userId
          KeySchema:
            - AttributeName: subscriptionId
              KeyType: HASH
            - AttributeName: userId
              KeyType: RANGE
          Projection:
            ProjectionType: ALL
      Tags:
        - Key: Stage
          Value: !Ref Stage
        - Key: Stack
          Value: !Ref Stack
        - Key: App
          Value: !Ref App
        - Key: devx-backup-enabled
          Value: true

Outputs:
  SubscriptionTableStream:
    Description: The ARN of the dynamo DB stream
    Value: !GetAtt SubscriptionTable.StreamArn
    Export:
      Name: !Sub ${App}-${Stage}-subscriptions-stream-arn

  UserSubscriptionTableStream:
    Description: The ARN of the dynamo DB stream
    Value: !GetAtt UserSubscriptionTable.StreamArn
    Export:
      Name: !Sub ${App}-${Stage}-user-subscriptions-stream-arn
