version: 2.1
orbs:
  #snyk: snyk/snyk@0.0.8
  #cypress: cypress-io/cypress@1.23.0
  aws-s3: circleci/aws-s3@4.0.0
  aws-cli: circleci/aws-cli@4.0
  eb: circleci/aws-elastic-beanstalk@2.0.1
jobs:
  imex-api-deploy:
    docker:
      - image: cimg/node:18.18.2
    steps:
      - checkout
      - eb/setup
      - run:
          command: |
            eb init imex-online-production-api -r ca-central-1 -p "Node.js 18 running on 64bit Amazon Linux 2"
            eb status --verbose
            eb deploy
            eb status

  imex-hasura-migrate:
    docker:
      - image: cimg/node:18.18.2
    parameters:
      secret:
        type: string
        default: $HASURA_PROD_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            npm install hasura-cli -g
            hasura migrate apply --endpoint https://db.imex.online/ --admin-secret << parameters.secret >>
            hasura metadata apply --endpoint https://db.imex.online/ --admin-secret << parameters.secret >>
            hasura metadata reload --endpoint https://db.imex.online/ --admin-secret << parameters.secret >>

  imex-app-build:
    docker:
      - image: cimg/node:18.18.2
    resource_class: large
    working_directory: ~/repo/client
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:imex

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: build
          to: "s3://imex-online-production/"
          arguments: "--exclude '*.map'"

  imex-app-beta-build:
    docker:
      - image: cimg/node:18.18.2
    resource_class: large
    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:production:imex

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://imex-online-beta/"
          arguments: "--exclude '*.map'"

  rome-api-deploy:
    docker:
      - image: "cimg/base:stable"
    steps:
      - checkout
      - eb/setup
      - run:
          command: |
            eb init romeonline-productionapi -r us-east-2 -p "Node.js 18 running on 64bit Amazon Linux 2"
            eb status --verbose
            eb deploy
            eb status

  rome-hasura-migrate:
    docker:
      - image: cimg/node:18.18.2
    parameters:
      secret:
        type: string
        default: $HASURA_ROME_PROD_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            npm install hasura-cli -g
            hasura migrate apply --endpoint https://db.romeonline.io/ --admin-secret << parameters.secret >>
            hasura metadata apply --endpoint https://db.romeonline.io/ --admin-secret << parameters.secret >>
            hasura metadata reload --endpoint https://db.romeonline.io/ --admin-secret << parameters.secret >>

  rome-app-build:
    docker:
      - image: cimg/node:18.18.2

    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:production:rome

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://rome-online-production/"
          arguments: "--exclude '*.map'"

  promanager-app-build:
    docker:
      - image: cimg/node:18.18.2

    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:production:promanager

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://promanager-production/"
          arguments: "--exclude '*.map'"

  test-rome-hasura-migrate:
    docker:
      - image: cimg/node:18.18.2
    parameters:
      secret:
        type: string
        default: $HASURA_ROME_TEST_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            npm install hasura-cli -g
            hasura migrate apply --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>
            hasura metadata apply --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>
            hasura metadata reload --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>

  test-rome-app-build:
    docker:
      - image: cimg/node:18.18.2

    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:test:rome

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://rome-online-test/"
          arguments: "--exclude '*.map'"

  test-promanager-app-build:
    docker:
      - image: cimg/node:18.18.2

    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:test:promanager

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://promanager-testing/"
          arguments: "--exclude '*.map'"

  test-hasura-migrate:
    docker:
      - image: cimg/node:18.18.2
    parameters:
      secret:
        type: string
        default: $HASURA_TEST_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            npm install hasura-cli -g
            hasura migrate apply --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>
            hasura metadata apply --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>
            hasura metadata reload --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>

  imex-test-app-build:
    docker:
      - image: cimg/node:18.18.2
    resource_class: large
    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:test:imex

      - aws-s3/sync:
          from: build
          to: "s3://imex-online-test/"
          arguments: "--exclude '*.map'"

  imex-test-app-beta-build:
    docker:
      - image: cimg/node:18.18.2
    resource_class: large
    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo

      - run:
          name: Install Dependencies
          command: npm i

      - run: npm run build:test:imex

      - aws-cli/setup:
          aws_access_key_id: AWS_ACCESS_KEY_ID
          aws_secret_access_key: AWS_SECRET_ACCESS_KEY
          region: AWS_REGION

      - aws-s3/sync:
          from: dist
          to: "s3://imex-online-test-beta/"
          arguments: "--exclude '*.map'"


  admin-app-build:
    docker:
      - image: cimg/node:16.15.0

    working_directory: ~/repo/admin

    steps:
      - checkout:
          path: ~/repo

      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-
      - run: npm i

      - save_cache:
          paths:
            - node_modules
            - ~/.npm
            - ~/.cache
          key: v1-dependencies-{{ checksum "package.json" }}

      - run: npm run build

      - aws-s3/sync:
          from: build
          to: "s3://adm.imex.online/"

workflows:
  deploy_and_build:
    jobs:
      - imex-api-deploy:
          filters:
            branches:
              only: master-AIO
      - imex-app-build:
          filters:
            branches:
              only: master
      - imex-app-beta-build:
          filters:
            branches:
              only: master-AIO
      - imex-hasura-migrate:
          secret: ${HASURA_PROD_SECRET}
          filters:
            branches:
              only: master
      - rome-api-deploy:
          filters:
            branches:
              only: master-AIO
      - rome-app-build:
          filters:
            branches:
              only: master-AIO
      - rome-hasura-migrate:
          secret: ${HASURA_PROD_SECRET}
          filters:
            branches:
              only: master-AIO
      - imex-test-app-build:
          filters:
            branches:
              only: test
      - imex-test-app-beta-build:
          filters:
            branches:
              only: test-AIO
      - test-hasura-migrate:
          secret: ${HASURA_TEST_SECRET}
          filters:
            branches:
              only: test-AIO
      - test-rome-app-build:
          filters:
            branches:
              only: test-AIO
      - test-promanager-app-build:
          filters:
            branches:
              only: test-AIO
      - promanager-app-build:
          filters:
            branches:
              only: master-AIO
      - test-rome-hasura-migrate:
          secret: ${HASURA_ROME_TEST_SECRET}
          filters:
            branches:
              only: test-AIO
      #- admin-app-build:
      #filters:
      #branches:
      #only: master
