service: heisenberg-individual
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-individual

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}
  # Layer
  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:
  IndividualList:
    handler: src/individual/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: individual-list-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: individual
          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/individual/list/**
  IndividualView:
    handler: src/individual/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: individual-view-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: individual/{individual_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/individual/view/**
  IndividualCreate:
    handler: src/individual/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: individual-create-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: individual
          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.prescriptionAuthorizer}
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/individual/create/**
  IndividualEdit:
    handler: src/individual/edit/handler.handle
    vpc:
        securityGroupIds:
          - {"Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup"}
        subnetIds:
          - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet1"}
          - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet2"}
    name: individual-edit-${self:custom.stage}
    memorySize: 128
    timeout: 6
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: individual/{individual_id}
          method: put
          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.superAdminAuthorizer}
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/individual/edit/**