From a20171a4f46ff9753b5117c65b59337bb3dc9f13 Mon Sep 17 00:00:00 2001 From: Kirill Date: Wed, 19 Apr 2023 14:01:41 +0300 Subject: [PATCH] fix(handlers.user): get user exists check --- .env.example | 17 +++++----- compose.yml | 33 ------------------- deployments/dev/docker-compose.yaml | 40 +++++++++++++++++++++++ nodemon.json | 18 +++++----- package.json | 2 ++ src/configuration/configure-env.ts | 4 --- src/configuration/constitute-mongo-uri.ts | 2 +- src/handlers/account/index.ts | 9 ++++- 8 files changed, 69 insertions(+), 56 deletions(-) delete mode 100644 compose.yml create mode 100644 deployments/dev/docker-compose.yaml diff --git a/.env.example b/.env.example index 681db84..acae095 100644 --- a/.env.example +++ b/.env.example @@ -9,14 +9,15 @@ AUTH_SERVICE_PORT=8081 # Database Options DB_HOST=127.0.0.1 DB_PORT=27017 -DB_USERNAME=admin -DB_PASSWORD=admin +DB_USERNAME=test +DB_PASSWORD=test DB_NAME=admin # Other -PUBLIC_ACCESS_SECRET_KEY=`-----BEGIN PUBLIC KEY----- -MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHgnvr7O2tiApjJfid1orFnIGm69 -80fZp+Lpbjo+NC/0whMFga2Biw5b1G2Q/B2u0tpO1Fs/E8z7Lv1nYfr5jx2S8x6B -dA4TS2kB9Kf0wn0+7wSlyikHoKhbtzwXHZl17GsyEi6wHnsqNBSauyIWhpha8i+Y -+3GyaOY536H47qyXAgMBAAE= ------END PUBLIC KEY-----` \ No newline at end of file +PUBLIC_ACCESS_SECRET_KEY= | + -----BEGIN PUBLIC KEY----- + MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLW1tlHyKC9AG0hGpmkksET2DE + r7ojSPemxFWAgFgcPJWQ7x3uNbsdJ3bIZFoA/FClaWKMCZmjnH9tv0bKZtY/CDhM + ZEyHpMruRSn6IKrxjtQZWy4uv/w6MzUeyBYG0OvNCiYpdvz5SkAGAUHD5ZNFqn2w + KKFD0I2Dr59BFVSGJwIDAQAB + -----END PUBLIC KEY----- \ No newline at end of file diff --git a/compose.yml b/compose.yml deleted file mode 100644 index c9ed84c..0000000 --- a/compose.yml +++ /dev/null @@ -1,33 +0,0 @@ -services: - auth: - build: . - ports: - - 8090:8080 - depends_on: - - mongo - networks: - - penahub_ntwrk - - default - mongo: - image: mongo:6 - hostname: mongo - restart: always - container_name: mongodb - networks: - - penahub_ntwrk - entrypoint: ["/usr/bin/mongod","--bind_ip_all","--replSet","rs0"] - mongosetup: - image: mongo:6 - depends_on: - - mongo - restart: "no" - networks: - - penahub_ntwrk - entrypoint: [ "bash", "-c", "sleep 5 && mongosh --host mongo:27017 --eval 'rs.initiate()'"] -volumes: - mongo_state: {} -networks: - penahub_ntwrk: - driver: bridge - attachable: true - internal: true diff --git a/deployments/dev/docker-compose.yaml b/deployments/dev/docker-compose.yaml new file mode 100644 index 0000000..1770fa0 --- /dev/null +++ b/deployments/dev/docker-compose.yaml @@ -0,0 +1,40 @@ +version: "3.3" + +services: + admin: + build: + context: ../../. + dockerfile: Dockerfile + environment: + - DB_HOST=10.6.0.11 + - DB_PORT=27017 + - ENVIRONMENT=staging + - HTTP_HOST=0.0.0.0 + - HTTP_PORT=8005 + - AUTH_SERVICE_HOST=http://pena-auth-service + - AUTH_SERVICE_PORT=8000 + - DB_USERNAME=test + - DB_PASSWORD=test + - DB_NAME=admin + - PUBLIC_ACCESS_SECRET_KEY= | + -----BEGIN PUBLIC KEY----- + MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLW1tlHyKC9AG0hGpmkksET2DE + r7ojSPemxFWAgFgcPJWQ7x3uNbsdJ3bIZFoA/FClaWKMCZmjnH9tv0bKZtY/CDhM + ZEyHpMruRSn6IKrxjtQZWy4uv/w6MzUeyBYG0OvNCiYpdvz5SkAGAUHD5ZNFqn2w + KKFD0I2Dr59BFVSGJwIDAQAB + -----END PUBLIC KEY----- + networks: + - dev + + mongo: + image: "mongo:6.0.3" + environment: + MONGO_INITDB_ROOT_USERNAME: test + MONGO_INITDB_ROOT_PASSWORD: test + ports: + - "27017:27017" + networks: + - dev + +networks: + dev: diff --git a/nodemon.json b/nodemon.json index ced4c51..a4204e8 100644 --- a/nodemon.json +++ b/nodemon.json @@ -1,9 +1,9 @@ -{ - "ignore": ["test", ".git", "node_modules"], - "watch": ["src"], - "exec": "npm run start:server", - "ext": "ts", - "env": { - "ENVIRONMENT": "development" - } -} +{ + "ignore": ["test", ".git", "node_modules"], + "watch": ["src"], + "exec": "npm run start", + "ext": "ts", + "env": { + "ENVIRONMENT": "development" + } +} diff --git a/package.json b/package.json index e999e84..11d76c9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,8 @@ "test": "jest --coverage", "test:watch": "jest --watch", "build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json", + "compose:start:dev": "docker-compose -f deployments/dev/docker-compose.yaml up -d", + "compose:stop:dev": "docker-compose -f deployments/dev/docker-compose.yaml down --volumes --rmi local", "code:check": "prettier --check \"src/**/*.{ts,tsx,js,css,scss,html}\"", "code:format": "prettier --write \"src/**/*.{ts,tsx,js,css,scss,html}\"", "code:format:specific-file": "prettier --write", diff --git a/src/configuration/configure-env.ts b/src/configuration/configure-env.ts index 0ce7e8e..2105cf2 100644 --- a/src/configuration/configure-env.ts +++ b/src/configuration/configure-env.ts @@ -13,10 +13,6 @@ export const configureENV = () => { }); } - if (environment === "staging") { - return dotenv.config({ debug: true }); - } - return dotenv.config(); }; diff --git a/src/configuration/constitute-mongo-uri.ts b/src/configuration/constitute-mongo-uri.ts index 4f1aaec..b5d933f 100644 --- a/src/configuration/constitute-mongo-uri.ts +++ b/src/configuration/constitute-mongo-uri.ts @@ -2,6 +2,6 @@ import type { DatabaseOptions } from "@/types/configuration/database-options"; export const constituteMongoURI = ({ username, password, host, port, database }: DatabaseOptions) => { const dbConnection = `mongodb://${username}:${password}@${host}:${port}`; - + return database ? `${dbConnection}/${database}` : dbConnection; }; diff --git a/src/handlers/account/index.ts b/src/handlers/account/index.ts index d8fcd45..cc42379 100644 --- a/src/handlers/account/index.ts +++ b/src/handlers/account/index.ts @@ -51,7 +51,14 @@ export const getAccount = async (request: GetAccountRequest, reply: FastifyReply return new Error("invalid user id"); } - return AccountModel.findOne({ userId: getAccountRequestParams.userId }).lean(); + const account = await AccountModel.findOne({ userId: getAccountRequestParams.userId }).lean(); + + if (!account) { + reply.status(404); + throw new Error("account not found"); + } + + return account; }; export const setAccountRole = async (request: SetAccountRoleRequest, reply: FastifyReply) => {