service: heisenberg-courier
package:
  excludeDevDependencies: true
  individually: true
  exclude:
    - ./**

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

app:  heisenberg-courier

provider:
  name: aws
  apiGateway:
    apiKeys:
      - courier-${opt:stage, self:provider.stage}
  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
  # 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}
  # Courier
  courierUser: ${file(${self:custom.stage}.env.yml):courier.COURIER_USER}
  courierPass: ${file(${self:custom.stage}.env.yml):courier.COURIER_PASS}
  courierPath: ${file(${self:custom.stage}.env.yml):courier.COURIER_PATH}

functions:
  ShippingDetails:
    handler: src/courier/shipping-details/handler.handle
    vpc:
      securityGroupIds:
        - {"Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup"}
      subnetIds:
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet1"}
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet2"}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: courier/{order_id}
          method: get
          private: true
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/shipping-details/**
  SaveWaybill:
    handler: src/courier/waybill/handler.handle
    vpc:
      securityGroupIds:
        - {"Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup"}
      subnetIds:
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet1"}
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet2"}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: courier/{order_id}/waybill
          method: post
          private: true
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**
  TrackingDetails:
    handler: src/courier/tracking-details/handler.handle
    vpc:
      securityGroupIds:
        - {"Fn::ImportValue": "${self:custom.stage}LambdaSecurityGroup"}
      subnetIds:
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet1"}
        - {"Fn::ImportValue": "${self:custom.stage}PublicSubnet2"}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):db-lambda}
    events:
      - http: 
          path: courier/tracking
          method: post
          private: true
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/tracking-details/**

  CourierWaybillAuth:
    handler: src/courier/waybill/handler.handle
    name: courier-waybill-auth-${self:custom.stage}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):courier}
    events:
      - http: 
          path: waybill/auth
          method: post
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**        

  CourierWaybillCapture:
    handler: src/courier/waybill/handler.capture_waybill
    name: courier-waybill-capture-${self:custom.stage}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):courier}
    events:
      - http: 
          path: waybill/capture
          method: post
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**           

  CourierParcelCapture:
    handler: src/courier/waybill/handler.capture_parcel
    name: courier-parcel-capture-${self:custom.stage}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):courier}
    events:
      - http: 
          path: parcel/capture
          method: post
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**         

  CourierWaybillComplete:
    handler: src/courier/waybill/handler.complete_waybill
    name: courier-waybill-complete-${self:custom.stage}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):courier}
    events:
      - http: 
          path: waybill/complete
          method: post
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**            

  CourierWaybillTrackAndTrace:
    handler: src/courier/waybill/handler.track_and_trace
    name: courier-waybill-track-and-trace-${self:custom.stage}
    memorySize: 128
    timeout: 19
    environment: ${file(${self:custom.stage}.env.yml):courier}
    events:
      - http: 
          path: waybill/trackandtrace
          method: get
          cors:
            origin: '*'
            headers: 
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - AccessToken
              - IdToken
            allowCredentials: false
    layers:
      - ${self:custom.databaseLayer}
      - ${self:custom.generalUtilsLayer}
    package:  
      include:
        - src/courier/waybill/**              