# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: songwhip-batch-api

# app and org for use with dashboard.serverless.com
app: songwhip
org: wilsonpage

plugins:
  - serverless-dotenv-plugin
  - serverless-webpack
  - serverless-offline

custom:
  stage: ${opt:stage, 'local'}
  resourcesStage: ${opt:resourcesState, self:custom.stage}

  # TODO: find a way to import from central file
  databaseDeploymentName: songwhip-batch-api-database-${self:custom.resourcesStage}

  # table name is defined in resources/database/serverless.yml
  databaseTableName: ${self:custom.databaseDeploymentName}-table

  queueName: ${self:service}-${self:custom.stage}-queue

  dotenv:
    path: '../.env'

  serverless-offline:
    port: 9001

  webpack:
    webpackConfig: 'webpack.config.js' # Name of webpack configuration file
    includeModules: true
    excludeFiles: handlers/**/*.test.js
    packager: 'yarn' # Packager that will be used to package your external modules

  debug:
    local: songwhip*
    staging: songwhip*
    production: songwhip*

  sentryDsn:
    local:
    staging: https://89c9b9391f99416dbf9904836d8c3204@o22178.ingest.sentry.io/5993340
    production: https://89c9b9391f99416dbf9904836d8c3204@o22178.ingest.sentry.io/5993340

  endpoint:
    local: http://localhost:9001/
    staging: https://staging.api.batch.songwhip.com/
    production: https://api.batch.songwhip.com/

# Create an optimized package for our functions
package:
  individually: true

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${self:custom.stage}
  region: us-east-1

  # default to smallest (cheapest) memory size functions that
  # require more memory can override in their function definition
  memorySize: 128

  # default timeout (override per function if need be)
  timeout: 12

  # you can define service wide environment variables here
  environment:
    STAGE: ${self:custom.stage}
    TABLE_NAME: ${self:custom.databaseTableName}
    SONGWHIP_LOOKUP_ENDPOINT: https://lookup.songwhip.com/
    DEBUG: ${self:custom.debug.${self:custom.stage}}
    SENTRY_DSN: ${self:custom.sentryDsn.${self:custom.stage}}
    SENTRY_ENV: ${self:custom.stage}
    SELF_ENDPOINT: ${self:custom.endpoint.${self:custom.stage}}
    JWT_AUTH_SECRET: ${env:JWT_AUTH_SECRET}
    SQS_QUEUE_URL:
      Ref: SongwhipBatchApiQueue

  # https://github.com/serverless/serverless/issues/6241#issuecomment-531562596
  iamManagedPolicies:
    - 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole'

  # TODO: learn wtf this
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:DescribeTable
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:BatchWriteItem
      Resource:
        - 'Fn::ImportValue': ${self:custom.databaseTableName}-arn
    - Effect: Allow
      Action:
        - sqs:SendMessage
        - sqs:GetQueueUrl
      Resource:
        Fn::GetAtt: [SongwhipBatchApiQueue, Arn]
    # - Effect: Allow
    #   Action:
    #     - sqs:ListQueues
    #   Resource: arn:aws:sqs:${self:provider.region}:*

resources:
  Resources:
    SongwhipBatchApiQueue:
      Type: 'AWS::SQS::Queue'
      Properties:
        QueueName: ${self:custom.queueName}

functions:
  createAccount:
    handler: handlers/accounts/create.handle
    events:
      - http:
          path: /accounts
          method: post
          cors: true

  getAccount:
    handler: handlers/accounts/get.handle
    events:
      - http:
          path: /accounts/{accountId}
          method: get
          cors: true

  sendAccountToken:
    handler: handlers/accounts/sendToken.handle
    events:
      - http:
          path: /accounts/{accountId}/sendToken
          method: post
          cors: true

  getAllAccounts:
    handler: handlers/accounts/getAll.handle
    events:
      - http:
          path: /accounts
          method: get
          cors: true

  deleteAccount:
    handler: handlers/accounts/delete.handle
    events:
      - http:
          path: /accounts/{accountId}
          method: delete
          cors: true

  getBatchesByAccount:
    handler: handlers/accounts/batches/getAll.handle
    events:
      - http:
          path: /accounts/{accountId}/batches
          method: get
          cors: true

  createBatch:
    handler: handlers/accounts/batches/create.handle
    events:
      - http:
          path: /accounts/{accountId}/batches
          method: post
          cors: true

  getBatch:
    handler: handlers/accounts/batches/get.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}
          method: get
          cors: true

  deleteBatch:
    handler: handlers/accounts/batches/delete.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}
          method: delete
          cors: true

  downloadBatch:
    handler: handlers/accounts/batches/download.handle
    memorySize: 256
    timeout: 20
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/download
          method: get
          cors: true

  createBatchItem:
    handler: handlers/accounts/batches/items/create.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/items
          method: post
          cors: true

  getBatchItem:
    handler: handlers/accounts/batches/items/get.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/items/{batchItemId}
          method: get
          cors: true

  deleteBatchItem:
    handler: handlers/accounts/batches/items/delete.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/items/{batchItemId}
          method: delete
          cors: true

  processBatchItem:
    handler: handlers/accounts/batches/items/process.handle
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/items/{batchItemId}/process
          method: post
          cors: true

  getBatchItemsByBatch:
    handler: handlers/accounts/batches/items/getAll.handle
    memorySize: 256
    events:
      - http:
          path: /accounts/{accountId}/batches/{batchId}/items
          method: get
          cors: true

  getAllBatchItems:
    handler: handlers/getAllBatchItems.handle
    events:
      - http:
          path: /batch-items
          method: get
          cors: true

  queueItem:
    handler: handlers/queueItem.handle
    events:
      - http:
          path: /queue-item
          method: post
          cors: true

  onQueueItem:
    handler: handlers/onQueueItem.handle
    # only allow once instance to run at a time
    reservedConcurrency: 1

    # with retry failed lookups this can take some time
    timeout: 30

    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - SongwhipBatchApiQueue
              - Arn
          batchSize: 1
