AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  datafy

  Sample SAM Template for datafy

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 900
    MemorySize: 1024

    # You can add LoggingConfig parameters such as the Logformat, Log Group, and SystemLogLevel or ApplicationLogLevel. Learn more here https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html#sam-function-loggingconfig.
    LoggingConfig:
      LogFormat: JSON
Resources:
  ReadFiles:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: read_files/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - lambda:InvokeFunction
                - ssm:GetParameter
              Resource: 
              - arn:aws:lambda:eu-west-1:587594388832:function:datafyNew-BatchUpdateSjreport-utMCtdWkdNo8
              - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
              - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon   
              - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
              - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
              - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
              - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client
      # Events:
        # ReadFilesScheduler:
        #   Type: Schedule
        #   Properties:
        #     Schedule: rate(1 minute)
        #     Input: '{}'
        # ReadFilesApi:
        #   Type: Api
        #   Properties:
        #     Path: /read-files
        #     Method: post
        #     RestApiId: !Ref ReadFilesApi
        # ReadFilesOptions:
        #   Type: Api
        #   Properties:
        #     Path: /read-files
        #     Method: options
        #     RestApiId: !Ref ReadFilesApi

  ReadPromosKaching:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: read_promo_kaching/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64    
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client 
      # Events:
        # ReadPromoScheduler: # Added scheduler event
          # Type: Schedule
          # Properties:
          #   Schedule: rate(1 minute) # Trigger every minute
          #   Input: '{}' # You can provide input JSON for the event here if needed  
        # ReadPromosKachingApi:
        #   Type: Api
        #   Properties:
        #     Path: /read-promos-kaching
        #     Method: post
        #     RestApiId: !Ref ReadFilesApi
        # ReadPromosKachingOptions:
        #   Type: Api
        #   Properties:
        #     Path: /read-promos-kaching
        #     Method: options
        #     RestApiId: !Ref ReadFilesApi

  CreateTasks:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: create_tasks/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:     
            - arn:aws:lambda:eu-west-1:587594388832:function:datafyNew-StartProcessQueue-r6W4pr83cx2y        
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client 

  BatchUpdateSjreport:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: batch_update_sjreport/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - lambda:InvokeFunction
              - ssm:GetParameter
            Resource: 
            - arn:aws:lambda:eu-west-1:587594388832:function:datafyNew-CreateTasks-ccMR48YAjXLP
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client  

  UpdateStoreIssues:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: update_store_issues/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client

  UpdateTaskIssues:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: update_task_issues/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client

  BackupSjreport:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: backup_sjreport/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client

  SJReportClearOldData:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: sjreport_clear_old_data/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client

  StartProcessQueue:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: start_process_queue/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
      - x86_64      
      Policies:
      - Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Action:
              - ssm:GetParameter
            Resource:             
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/url   
            - arn:aws:ssm:eu-west-1:587594388832:parameter/supabase/anon 
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
            - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client


  ApplicationResourceGroup:
    Type: AWS::ResourceGroups::Group
    Properties:
      Name:
        Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
      ResourceQuery:
        Type: CLOUDFORMATION_STACK_1_0
  ApplicationInsightsMonitoring:
    Type: AWS::ApplicationInsights::Application
    Properties:
      ResourceGroupName:
        Ref: ApplicationResourceGroup
      AutoConfigurationEnabled: 'true'

  ReadFilesApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: prod
      Cors:
        AllowMethods: "'POST,OPTIONS'"
        AllowHeaders: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
        AllowOrigin: "'*'"

  TriggerReadFiles:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: trigger_read_files/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Environment:
        Variables:
          READ_FILES_ARN: !GetAtt ReadFiles.Arn
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - lambda:InvokeFunction
                - ssm:GetParameter
              Resource: 
                - !GetAtt ReadFiles.Arn
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client
      Events:
        TriggerReadFilesApi:
          Type: Api
          Properties:
            Path: /trigger-read-files
            Method: post
            RestApiId: !Ref ReadFilesApi
        TriggerReadFilesOptions:
          Type: Api
          Properties:
            Path: /trigger-read-files
            Method: options
            RestApiId: !Ref ReadFilesApi

  TriggerReadPromosKaching:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: trigger_read_promos_kaching/
      Handler: app.lambda_handler
      Runtime: python3.9
      Architectures:
        - x86_64
      Environment:
        Variables:
          READ_PROMOS_KACHING_ARN: !GetAtt ReadPromosKaching.Arn
      Policies:
        - Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - lambda:InvokeFunction
                - ssm:GetParameter
              Resource: 
                - !GetAtt ReadPromosKaching.Arn
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/username
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/app_password
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails
                - arn:aws:ssm:eu-west-1:587594388832:parameter/gmail/to_emails_client
      Events:
        TriggerReadPromosKachingApi:
          Type: Api
          Properties:
            Path: /trigger-read-promos-kaching
            Method: post
            RestApiId: !Ref ReadFilesApi
        TriggerReadPromosKachingOptions:
          Type: Api
          Properties:
            Path: /trigger-read-promos-kaching
            Method: options
            RestApiId: !Ref ReadFilesApi

# Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  # HelloWorldApi:
  #   Description: API Gateway endpoint URL for Prod stage for Hello World function
  #   Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  # HelloWorldFunction:
  #   Description: Hello World Lambda Function ARN
  #   Value: !GetAtt HelloWorldFunction.Arn
  # HelloWorldFunctionIamRole:
  #   Description: Implicit IAM Role created for Hello World function
  #   Value: !GetAtt HelloWorldFunctionRole.Arn
