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
  jira: circleci/jira@2.1.0
jobs:
  imex-api-deploy:
    docker:
      - image: cimg/node:22.13.1
    steps:
      - checkout
      - eb/setup
      - run:
          command: |
            eb init imex-online-production-api -r ca-central-1 -p "Node.js 22 running on 64bit Amazon Linux 2023"
            eb status --verbose
            eb deploy
            eb status
      - jira/notify:
          environment: Production (ImEX) - API
          environment_type: production
          job_type: deployment
          pipeline_id: << pipeline.id >>
          pipeline_number: << pipeline.number >>

  imex-hasura-migrate:
    docker:
      - image: cimg/node:22.13.1
    parameters:
      secret:
        type: string
        default: $HASURA_PROD_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
            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 >>
      - jira/notify:
          environment: Production (ImEX) - Hasura
          environment_type: production
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>
  imex-app-build:
    docker:
      - image: cimg/node:22.13.1
    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:22.13.1
    resource_class: large
    working_directory: ~/repo/client

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

      - run: NODE_OPTIONS=--max-old-space-size=8192 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'"
      - jira/notify:
          environment: Production (ImEX) - Front End
          environment_type: production
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

  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 22 running on 64bit Amazon Linux 2023"
            eb status --verbose
            eb deploy
            eb status
      - jira/notify:
          environment: Production (Rome) - API
          environment_type: production
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>
  rome-hasura-migrate:
    docker:
      - image: cimg/node:22.13.1
    parameters:
      secret:
        type: string
        default: $HASURA_ROME_PROD_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
            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 >>
      - jira/notify:
          environment: Production (Rome) - Hasura
          environment_type: production
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>
  rome-app-build:
    docker:
      - image: cimg/node:22.13.1
    resource_class: large
    working_directory: ~/repo/client

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

      - run: NODE_OPTIONS=--max-old-space-size=8192 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'"
      - jira/notify:
          environment: Production (Rome) - Front End
          environment_type: production
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

  test-rome-hasura-migrate:
    docker:
      - image: cimg/node:22.13.1
    parameters:
      secret:
        type: string
        default: $HASURA_ROME_TEST_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
            hasura migrate apply --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>
            sleep 5
            hasura metadata apply --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>
            sleep 10
            hasura metadata reload --endpoint https://db.test.romeonline.io/ --admin-secret << parameters.secret >>
      - jira/notify:
          environment: Test (Rome) - Hasura
          environment_type: testing
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

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

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

      - run: NODE_OPTIONS=--max-old-space-size=8192 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'"
      - jira/notify:
          environment: Test (Rome) - Front End
          environment_type: testing
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

  test-hasura-migrate:
    docker:
      - image: cimg/node:22.13.1
    parameters:
      secret:
        type: string
        default: $HASURA_TEST_SECRET
    working_directory: ~/repo/hasura
    steps:
      - checkout:
          path: ~/repo
      - run:
          name: Execute migration
          command: |
            curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
            hasura migrate apply --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>
            sleep 15
            hasura metadata apply --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>
            sleep 30
            hasura metadata reload --endpoint https://db.test.bodyshop.app/ --admin-secret << parameters.secret >>
      - jira/notify:
          environment: Test (ImEX) - Hasura
          environment_type: testing
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

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

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

      - run: NODE_OPTIONS=--max-old-space-size=8192 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:22.13.1
    resource_class: large
    working_directory: ~/repo/client

    steps:
      - checkout:
          path: ~/repo

      - run:
          name: Install Dependencies
          command: npm i

      - run: NODE_OPTIONS=--max-old-space-size=8192 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'"
      - jira/notify:
          environment: Test (ImEX) - Front End
          environment_type: testing
          pipeline_id: << pipeline.id >>
          job_type: deployment
          pipeline_number: << pipeline.number >>

  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-AIO
      - rome-api-deploy:
          filters:
            branches:
              only: master-AIO
      - rome-app-build:
          filters:
            branches:
              only: master-AIO
      - rome-hasura-migrate:
          secret: ${HASURA_ROME_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-rome-hasura-migrate:
          secret: ${HASURA_ROME_TEST_SECRET}
          filters:
            branches:
              only: test-AIO
      #- admin-app-build:
      #filters:
      #branches:
      #only: master
