From ba17af05c34055068dc2c78b5e60a468136515b3 Mon Sep 17 00:00:00 2001 From: naftis Date: Wed, 18 Sep 2024 13:30:12 +0300 Subject: [PATCH 1/3] refactor(env-vars): data-seeder --- package.json | 4 +-- packages/data-seeder/package.json | 1 + packages/data-seeder/src/constants.ts | 8 ----- packages/data-seeder/src/environment.ts | 19 +++++++++++ packages/data-seeder/src/index.ts | 8 ++--- packages/data-seeder/src/locations.ts | 13 +++----- packages/data-seeder/src/roles.ts | 8 ++--- packages/data-seeder/src/users.ts | 12 +++---- yarn.lock | 42 ++++++++++++++++++++----- 9 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 packages/data-seeder/src/environment.ts diff --git a/package.json b/package.json index d4cf2e0a72..a738391673 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,8 @@ "build:image": "export VERSION=`git log -1 --pretty=format:%h` && docker build --build-arg VERSION=${VERSION} -t opencrvs-build . && unset VERSION", "build:clean": "lerna run build:clean", "check:license": "license-check-and-add check -f license-config.json", - "seed:dev": "ACTIVATE_USERS=true lerna run seed --stream --scope @opencrvs/data-seeder", - "seed:prod": "lerna run seed --stream --scope @opencrvs/data-seeder", + "seed:dev": "NODE_ENV=development ACTIVATE_USERS=true lerna run seed --stream --scope @opencrvs/data-seeder", + "seed:prod": "NODE_ENV=production lerna run seed --stream --scope @opencrvs/data-seeder", "add:license": "license-check-and-add add -f license-config.json", "build:components": "lerna run build --scope @opencrvs/components", "debug": "bash debug-service-in-chrome.sh" diff --git a/packages/data-seeder/package.json b/packages/data-seeder/package.json index e092edace8..2edad08866 100644 --- a/packages/data-seeder/package.json +++ b/packages/data-seeder/package.json @@ -21,6 +21,7 @@ "@types/fhir": "^0.0.37", "@types/node": "^16.18.39", "@types/node-fetch": "^2.5.12", + "envalid": "^8.0.0", "graphql": "^15.0.0", "graphql-tag": "^2.12.6", "jwt-decode": "^2.2.0", diff --git a/packages/data-seeder/src/constants.ts b/packages/data-seeder/src/constants.ts index ef306e4779..7093ddb028 100644 --- a/packages/data-seeder/src/constants.ts +++ b/packages/data-seeder/src/constants.ts @@ -8,12 +8,4 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -export const AUTH_HOST = process.env.AUTH_HOST || 'http://localhost:4040' -export const COUNTRY_CONFIG_HOST = - process.env.COUNTRY_CONFIG_HOST || 'http://localhost:3040' -export const GATEWAY_HOST = process.env.GATEWAY_HOST || 'http://localhost:7070' export const OPENCRVS_SPECIFICATION_URL = 'http://opencrvs.org/specs/' - -export const SUPER_USER_PASSWORD = process.env.SUPER_USER_PASSWORD ?? 'password' - -export const ACTIVATE_USERS = process.env.ACTIVATE_USERS ?? 'false' diff --git a/packages/data-seeder/src/environment.ts b/packages/data-seeder/src/environment.ts new file mode 100644 index 0000000000..30d3c7495f --- /dev/null +++ b/packages/data-seeder/src/environment.ts @@ -0,0 +1,19 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * OpenCRVS is also distributed under the terms of the Civil Registration + * & Healthcare Disclaimer located at http://opencrvs.org/license. + * + * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. + */ +import { bool, cleanEnv, str, url } from 'envalid' + +export const env = cleanEnv(process.env, { + AUTH_HOST: url({ devDefault: 'http://localhost:4040' }), + COUNTRY_CONFIG_HOST: url({ devDefault: 'http://localhost:3040' }), + GATEWAY_HOST: url({ devDefault: 'http://localhost:7070' }), + SUPER_USER_PASSWORD: str({ devDefault: 'password' }), + ACTIVATE_USERS: bool({ devDefault: false }) +}) diff --git a/packages/data-seeder/src/index.ts b/packages/data-seeder/src/index.ts index 4b2b64a981..279e2fc15c 100644 --- a/packages/data-seeder/src/index.ts +++ b/packages/data-seeder/src/index.ts @@ -8,7 +8,7 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -import { AUTH_HOST, GATEWAY_HOST, SUPER_USER_PASSWORD } from './constants' +import { env } from './environment' import fetch from 'node-fetch' import { seedLocations } from './locations' import { seedRoles } from './roles' @@ -19,12 +19,12 @@ import gql from 'graphql-tag' import decode from 'jwt-decode' async function getToken(): Promise { - const authUrl = new URL('authenticate-super-user', AUTH_HOST).toString() + const authUrl = new URL('authenticate-super-user', env.AUTH_HOST).toString() const res = await fetch(authUrl, { method: 'POST', body: JSON.stringify({ username: 'o.admin', - password: SUPER_USER_PASSWORD + password: env.SUPER_USER_PASSWORD }), headers: { 'Content-Type': 'application/json' @@ -70,7 +70,7 @@ function getTokenPayload(token: string): TokenPayload { async function deactivateSuperuser(token: string) { const { sub } = getTokenPayload(token) - const res = await fetch(`${GATEWAY_HOST}/graphql`, { + const res = await fetch(`${env.GATEWAY_HOST}/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/packages/data-seeder/src/locations.ts b/packages/data-seeder/src/locations.ts index ab08539e28..98a4237cbb 100644 --- a/packages/data-seeder/src/locations.ts +++ b/packages/data-seeder/src/locations.ts @@ -9,11 +9,8 @@ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ import fetch from 'node-fetch' -import { - COUNTRY_CONFIG_HOST, - GATEWAY_HOST, - OPENCRVS_SPECIFICATION_URL -} from './constants' +import { OPENCRVS_SPECIFICATION_URL } from './constants' +import { env } from './environment' import { TypeOf, z } from 'zod' import { raise } from './utils' import { inspect } from 'util' @@ -156,7 +153,7 @@ function validateAdminStructure(locations: TypeOf) { } async function getLocations() { - const url = new URL('locations', COUNTRY_CONFIG_HOST).toString() + const url = new URL('locations', env.COUNTRY_CONFIG_HOST).toString() const res = await fetch(url) if (!res.ok) { raise(`Expected to get the locations from ${url}`) @@ -211,7 +208,7 @@ export async function seedLocations(token: string) { const savedLocations = ( await Promise.all( ['ADMIN_STRUCTURE', 'CRVS_OFFICE', 'HEALTH_FACILITY'].map((type) => - fetch(`${GATEWAY_HOST}/locations?type=${type}&_count=0`, { + fetch(`${env.GATEWAY_HOST}/locations?type=${type}&_count=0`, { headers: { 'Content-Type': 'application/fhir+json' } @@ -227,7 +224,7 @@ export async function seedLocations(token: string) { const locations = (await getLocations()).filter((location) => { return !savedLocationsSet.has(location.id) }) - const res = await fetch(`${GATEWAY_HOST}/locations?`, { + const res = await fetch(`${env.GATEWAY_HOST}/locations?`, { method: 'POST', headers: { Authorization: `Bearer ${token}`, diff --git a/packages/data-seeder/src/roles.ts b/packages/data-seeder/src/roles.ts index e088a17562..a138022158 100644 --- a/packages/data-seeder/src/roles.ts +++ b/packages/data-seeder/src/roles.ts @@ -8,7 +8,7 @@ * * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ -import { COUNTRY_CONFIG_HOST, GATEWAY_HOST } from './constants' +import { env } from './environment' import { raise, parseGQLResponse } from './utils' import fetch from 'node-fetch' import { z } from 'zod' @@ -94,7 +94,7 @@ const getSystemRolesQuery = print(gql` `) async function fetchSystemRoles(token: string): Promise { - const res = await fetch(`${GATEWAY_HOST}/graphql`, { + const res = await fetch(`${env.GATEWAY_HOST}/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -123,7 +123,7 @@ async function updateRoles( let roleIdMap: RoleIdMap = {} await Promise.all( systemRoles.map((systemRole) => - fetch(`${GATEWAY_HOST}/graphql`, { + fetch(`${env.GATEWAY_HOST}/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -150,7 +150,7 @@ async function updateRoles( } async function fetchCountryRoles(token: string) { - const url = new URL('roles', COUNTRY_CONFIG_HOST).toString() + const url = new URL('roles', env.COUNTRY_CONFIG_HOST).toString() const res = await fetch(url, { method: 'GET', headers: { diff --git a/packages/data-seeder/src/users.ts b/packages/data-seeder/src/users.ts index 506c01105a..b7bb730b6f 100644 --- a/packages/data-seeder/src/users.ts +++ b/packages/data-seeder/src/users.ts @@ -9,7 +9,7 @@ * Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS. */ import fetch from 'node-fetch' -import { ACTIVATE_USERS, COUNTRY_CONFIG_HOST, GATEWAY_HOST } from './constants' +import { env } from './environment' import { z } from 'zod' import { parseGQLResponse, raise, delay } from './utils' import { print } from 'graphql' @@ -66,7 +66,7 @@ const createUserMutation = print(gql` `) async function getUsers(token: string) { - const url = new URL('users', COUNTRY_CONFIG_HOST).toString() + const url = new URL('users', env.COUNTRY_CONFIG_HOST).toString() const res = await fetch(url, { method: 'GET', headers: { @@ -101,7 +101,7 @@ async function userAlreadyExists( token: string, username: string ): Promise { - const searchResponse = await fetch(`${GATEWAY_HOST}/graphql`, { + const searchResponse = await fetch(`${env.GATEWAY_HOST}/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -122,7 +122,7 @@ async function userAlreadyExists( async function getOfficeIdFromIdentifier(identifier: string) { const response = await fetch( - `${GATEWAY_HOST}/location?identifier=${identifier}`, + `${env.GATEWAY_HOST}/location?identifier=${identifier}`, { headers: { 'Content-Type': 'application/fhir+json' @@ -142,7 +142,7 @@ async function getOfficeIdFromIdentifier(identifier: string) { } async function callCreateUserMutation(token: string, userPayload: unknown) { - return fetch(`${GATEWAY_HOST}/graphql`, { + return fetch(`${env.GATEWAY_HOST}/graphql`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -200,7 +200,7 @@ export async function seedUsers( firstNames: givenNames } ], - ...(ACTIVATE_USERS === 'true' && { status: 'active' }), + ...(env.ACTIVATE_USERS && { status: 'active' }), primaryOffice, username } diff --git a/yarn.lock b/yarn.lock index fcbe8d9a01..1692d139f2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7898,7 +7898,7 @@ dependencies: "@types/react" "*" -"@types/react@*", "@types/react@16 || 17 || 18", "@types/react@18.3.1", "@types/react@>=16", "@types/react@^16": +"@types/react@*", "@types/react@16 || 17 || 18", "@types/react@18.3.1", "@types/react@>=16": version "18.3.1" resolved "https://registry.yarnpkg.com/@types/react/-/react-18.3.1.tgz#fed43985caa834a2084d002e4771e15dfcbdbe8e" integrity sha512-V0kuGBX3+prX+DQ/7r2qsv1NsdfnCLnTgnRJ1pYnxykBhGMz+qj+box5lq7XsO5mtZsBqpjwwTu/7wszPfMBcw== @@ -7906,6 +7906,15 @@ "@types/prop-types" "*" csstype "^3.0.2" +"@types/react@^16": + version "16.14.61" + resolved "https://registry.yarnpkg.com/@types/react/-/react-16.14.61.tgz#ce498029a046d17908001e6e563f3febbaf41e7f" + integrity sha512-CK3zd17pDWAEMnN5TdzwQJQlto2dK/lb0WZsI74owWgQ8PR60WRk0sCeBxLWuSuuqqDZKqeUcxod/8yECqrP/g== + dependencies: + "@types/prop-types" "*" + "@types/scheduler" "^0.16" + csstype "^3.0.2" + "@types/readdir-glob@*": version "1.1.3" resolved "https://registry.npmjs.org/@types/readdir-glob/-/readdir-glob-1.1.3.tgz" @@ -7974,6 +7983,11 @@ dependencies: htmlparser2 "^8.0.0" +"@types/scheduler@^0.16": + version "0.16.8" + resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.8.tgz#ce5ace04cfeabe7ef87c0091e50752e36707deff" + integrity sha512-WZLiwShhwLRmeV6zH+GkbOFT6Z6VklCItrDioxUnv+u4Ll+8vKeFySoFyK/0ctcRpOmwAicELfmys1sDc/Rw+A== + "@types/semver@^7.3.4": version "7.5.4" resolved "https://registry.npmjs.org/@types/semver/-/semver-7.5.4.tgz" @@ -12023,6 +12037,13 @@ env-paths@^2.2.0: resolved "https://registry.npmjs.org/env-paths/-/env-paths-2.2.1.tgz" integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A== +envalid@^8.0.0: + version "8.0.0" + resolved "https://registry.yarnpkg.com/envalid/-/envalid-8.0.0.tgz#2314451e18e88051c98540ab60640e330279e486" + integrity sha512-PGeYJnJB5naN0ME6SH8nFcDj9HVbLpYIfg1p5lAyM9T4cH2lwtu2fLbozC/bq+HUUOIFxhX/LP0/GmlqPHT4tQ== + dependencies: + tslib "2.6.2" + envinfo@7.8.1: version "7.8.1" resolved "https://registry.yarnpkg.com/envinfo/-/envinfo-7.8.1.tgz#06377e3e5f4d379fea7ac592d5ad8927e0c4d475" @@ -13284,7 +13305,14 @@ fast-url-parser@1.1.3, fast-url-parser@^1.1.3: dependencies: punycode "^1.3.2" -fast-xml-parser@4.2.5, fast-xml-parser@4.4.1, fast-xml-parser@^4.1.3, fast-xml-parser@^4.2.2: +fast-xml-parser@4.2.5: + version "4.2.5" + resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.2.5.tgz#a6747a09296a6cb34f2ae634019bf1738f3b421f" + integrity sha512-B9/wizE4WngqQftFPmdaMYlXoJlJOYxGQOanC77fq9k8+Z0v5dDSVh+3glErdIROP//s/jgb7ZuxKfB8nVyo0g== + dependencies: + strnum "^1.0.5" + +fast-xml-parser@^4.1.3, fast-xml-parser@^4.2.2: version "4.4.1" resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.4.1.tgz#86dbf3f18edf8739326447bcaac31b4ae7f6514f" integrity sha512-xkjOecfnKGkSsOwtZ5Pz7Us/T6mrbPQrq0nh+aCO5V9nk5NLWmasAHumTKjiPJPWANe+kAZ84Jc8ooJkzZ88Sw== @@ -22863,16 +22891,16 @@ tslib@2.0.1: resolved "https://registry.npmjs.org/tslib/-/tslib-2.0.1.tgz" integrity sha512-SgIkNheinmEBgx1IUNirK0TUD4X9yjjBRTqqjggWCU3pUEqIk3/Uwl3yRixYKT6WjQuGiwDv4NomL3wqRCj+CQ== +tslib@2.6.2, tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0, tslib@^2.5.0, tslib@^2.6.1, tslib@^2.6.2: + version "2.6.2" + resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz" + integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q== + tslib@^1.10.0, tslib@^1.11.1, tslib@^1.13.0, tslib@^1.8.1, tslib@^1.9.3: version "1.14.1" resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== -tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3.1, tslib@^2.4.0, tslib@^2.5.0, tslib@^2.6.1, tslib@^2.6.2: - version "2.6.2" - resolved "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz" - integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q== - tslib@~2.5.0: version "2.5.3" resolved "https://registry.npmjs.org/tslib/-/tslib-2.5.3.tgz" From 5ba2cf152efafea5098f6b7661a3bac84e110e6b Mon Sep 17 00:00:00 2001 From: naftis Date: Wed, 18 Sep 2024 13:34:00 +0300 Subject: [PATCH 2/3] refactor: set activate users to be false on prod --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a738391673..bc5ed9157f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "build:clean": "lerna run build:clean", "check:license": "license-check-and-add check -f license-config.json", "seed:dev": "NODE_ENV=development ACTIVATE_USERS=true lerna run seed --stream --scope @opencrvs/data-seeder", - "seed:prod": "NODE_ENV=production lerna run seed --stream --scope @opencrvs/data-seeder", + "seed:prod": "NODE_ENV=production ACTIVATE_USERS=false lerna run seed --stream --scope @opencrvs/data-seeder", "add:license": "license-check-and-add add -f license-config.json", "build:components": "lerna run build --scope @opencrvs/components", "debug": "bash debug-service-in-chrome.sh" From b39a74d6b39b9c132051ff4ab9714db62b9067e4 Mon Sep 17 00:00:00 2001 From: naftis Date: Wed, 18 Sep 2024 13:34:36 +0300 Subject: [PATCH 3/3] fix: default activate users on dev to true --- packages/data-seeder/src/environment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-seeder/src/environment.ts b/packages/data-seeder/src/environment.ts index 30d3c7495f..b0a16bf122 100644 --- a/packages/data-seeder/src/environment.ts +++ b/packages/data-seeder/src/environment.ts @@ -15,5 +15,5 @@ export const env = cleanEnv(process.env, { COUNTRY_CONFIG_HOST: url({ devDefault: 'http://localhost:3040' }), GATEWAY_HOST: url({ devDefault: 'http://localhost:7070' }), SUPER_USER_PASSWORD: str({ devDefault: 'password' }), - ACTIVATE_USERS: bool({ devDefault: false }) + ACTIVATE_USERS: bool({ devDefault: true }) })