service: heisenberg-batch-item
package:
  excludeDevDependencies: true
  individually: true
  exclude:
    - database/**
    - docker/**
    - openapi/**
    - .serverless/**
    - serverless/**
    - src/**
    - tests/**
    - .env
    - .env.example
    - env.yml.example
    - "*.env.yml"
    - .gitlab-ci.yml
    - .gitignore
    - README.md
    - node_modules/**
    - tests/**

plugins:
  - serverless-jetpack
  - serverless-import-apigateway

app: heisenberg-batch-item

provider:
  name: aws
  runtime: nodejs14.x
  stage: dev
  region: eu-west-1
  deploymentBucket:
    name: heisenberg-serverless-deployments-${self:custom.stage}
  logs:
    restApi: true

custom:
  # General
  stage: "${opt:stage, self:provider.stage}"
  region: ${opt:region, self:provider.region}
  accountId: ${file(./serverless/helpers/account-id.js):getAccountId}
  # API Gateway
  importApiGateway:
    name: admin-${self:custom.stage} # Required
    path: /
  # Authorizer
  superAdminAuthorizer: !ImportValue ${self:custom.stage}AdminApiGatewaySuperAdminAuthorizerId
  prescriptionAuthorizer: !ImportValue ${self:custom.stage}AdminApiGatewayPrescriptionAuthorizerId
  fulfilmentAuthorizer: !ImportValue ${self:custom.stage}AdminApiGatewayFulfilmentAuthorizerId
  packagingAuthorizer: !ImportValue ${self:custom.stage}AdminApiGatewayPackagingAuthorizerId
  cognitoAuthorizer: !ImportValue ${self:custom.stage}AdminApiGatewayCognitoAuthorizerId
  # RDS
  rdsUser: ${file(${self:custom.stage}.env.yml):db_details.RDS_USER}
  rdsPass: ${file(${self:custom.stage}.env.yml):db_details.RDS_PASS}
  rdsDBName: ${file(${self:custom.stage}.env.yml):db_details.RDS_DB}
  # Layers
  authorizeLayer: arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:layer:authorize-${self:custom.stage}:${file(${self:custom.stage}.env.yml):layer-versions.AUTHORIZE}
  awsServicesLayer: arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:layer:aws-services-${self:custom.stage}:${file(${self:custom.stage}.env.yml):layer-versions.AWS_SERVICES}
  databaseLayer: arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:layer:database-${self:custom.stage}:${file(${self:custom.stage}.env.yml):layer-versions.DATABASE}
  generalUtilsLayer: arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:layer:general-utils-${self:custom.stage}:${file(${self:custom.stage}.env.yml):layer-versions.GENERAL_UTILS}
  emailLayer: arn:aws:lambda:${self:custom.region}:${self:custom.accountId}:layer:email-${self:custom.stage}:${file(${self:custom.stage}.env.yml):layer-versions.EMAIL}

functions:
  BatchItemList:
    handler: src/batch-item/list/handler.handle
    vpc:
      securityGroupIds:
        - { "Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup" }
      subnetIds:
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet1" }
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet2" }
    name: batch-item-list-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http:
          path: batch-item
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
          authorizer:
            type: COGNITO_USER_POOLS
            authorizerId: ${self:custom.cognitoAuthorizer}
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:
      include:
        - src/batch-item/list/**
  BatchItemView:
    handler: src/batch-item/view/handler.handle
    vpc:
      securityGroupIds:
        - { "Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup" }
      subnetIds:
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet1" }
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet2" }
    name: batch-item-view-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http:
          path: batch-item/{batch_item_id}
          method: get
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
          authorizer:
            type: COGNITO_USER_POOLS
            authorizerId: ${self:custom.cognitoAuthorizer}
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:
      include:
        - src/batch-item/view/**
  BatchItemCreate:
    handler: src/batch-item/create/handler.handle
    vpc:
      securityGroupIds:
        - { "Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup" }
      subnetIds:
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet1" }
        - { "Fn::ImportValue": "${self:custom.stage}PublicSubnet2" }
    name: batch-item-create-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http:
          path: batch-item
          method: post
          cors:
            origin: "*"
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
          authorizer:
            type: TOKEN
            authorizerId: ${self:custom.fulfilmentAuthorizer}
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:
      include:
        - src/batch-item/create/**
