Coder Social home page Coder Social logo

bcgov / landuseplanning-api Goto Github PK

View Code? Open in Web Editor NEW
6.0 10.0 3.0 70.98 MB

Land Use Planning Engagement Platform - API Layer

License: Apache License 2.0

JavaScript 99.69% Shell 0.28% Dockerfile 0.03%
nodejs mongodb express api land-use-plan citz

landuseplanning-api's Introduction

bcgov/landuseplanning-api

Lifecycle:Stable

Minimal API for the Land Use Planning Public and Admin apps -->

How to run this

Before running the api, you must set some environment variables:

  1. MINIO_HOST='foo.pathfinder.gov.bc.ca'
  2. MINIO_ACCESS_KEY='xxxx'
  3. MINIO_SECRET_KEY='xxxx'
  4. KEYCLOAK_ENABLED=true
  5. MONGODB_DATABASE='landuseplanning'

One way to do this is to edit your ~/.bash_profile file to contain:

export MONGODB_DATABASE="landuseplanning"
export MINIO_HOST="foo.pathfinder.gov.bc.ca"
export MINIO_ACCESS_KEY="xxxx"
export MINIO_SECRET_KEY="xxxx"
export KEYCLOAK_ENABLED=true

Please note that these values are case sensitive so don't use upper-case TRUE for example.

Don't forget to reload your .bash_profile file so that your terminal environment is up to date with the correct values

source ~/.bash_profile
env

The above env command will show you your environment variables and allow you to check that the correct values are present.

Start the server by running npm run start-watch

Prerequisites

Technology Version Website Description
node 12.x.x https://nodejs.org/en/ JavaScript Runtime
npm 6.x.x https://www.npmjs.com/ Node Package Manager
yarn latest https://yarnpkg.com/en/ Package Manager (more efficient than npm)
mongodb 3.6 https://docs.mongodb.com/v3.6/installation/ NoSQL database

Install Node + NPM

Note: Windows users can use NVM Windows to install and manage multiple versions of Node+Npm.

Install Yarn

npm install -g yarn

Install MongoDB

Build and Run

  1. Download dependencies: yarn install

  2. Run the app: npm start

  3. Go to http://localhost:3000/api/docs to verify that the application is running.

    Note: To change the default port edit swagger.yaml.

  4. POST http://localhost:3000/api/login/token with the following body:

{
"username": #{username},
"password": #{password}
}

API Specification

The API is defined in swagger.yaml.

If the this landuseplanning-api is running locally, you can view the api docs at: http://localhost:3000/api/docs/

This project uses npm package swagger-tools via ./app.js to automatically generate the express server and its routes.

Recommend reviewing the Open API Specification before making any changes to the swagger.yaml file.

Initial Setup

Node and NPM

We use a version manager so as to allow concurrent versions of node and other software. asdf is recommended. Installation of asdf and required node packages is covered here

Database

If possible, acquire a dump of the database from one of the live environments.

To make sure you don't have an existing old copy (careful, this is destructive):

mongo
use epic
db.dropDatabase()

Load database dump:

  1. Download and unzip archived dump file.
  2. Restore the dump into your local mongo:
mongo
use landuseplanning
db.dropDatabase()

Seed with generated data:

Described in seed README

Loading legacy data:

To restore the database dump you have from the old epic system (ie ESM):

mongorestore -d epic dump/[old_database_name_most_likely_esm]

Then run the contents of dataload against that database. You may need to edit the commands slightly to match your db name or to remove the ".gz --gzip" portion if your dump unpacks as straight ".bson" files.

Developing

  1. Code Reuse Strategy
  2. Testing
  3. Configuring Environment Variables
  4. Logging

Code Reuse Strategy

See Code Reuse Strategy

Note on Ecmascript feature availability

This app is designed to be run on Node 12.22.12 in Openshift. As a result, almost all modern ECMAScript features are available except for:

  • The nullish coalescing operator
  • Optional object chaining

See here for more details on ES feature availability: https://node.green/

Testing

An overview of the EPIC test stack can be found here.

This project is using jest as a testing framework. You can run tests with yarn test or jest. Running either command with the --watch flag will re-run the tests every time a file is changed.

To run the tests in one file, simply pass the path of the file name e.g. jest api/test/search.test.js --watch. To run only one test in that file, chain the .only command e.g. test.only("Search returns results", () => {}).

The MOST IMPORTANT thing to know about this project's test environment is the router setup. At the time of writing this, it wasn't possible to get swagger-tools router working in the test environment. As a result, all tests COMPLETELY bypass the real life swagger-tools router. Instead, a middleware router called supertest is used to map routes to controller actions. In each controller test, you will need to add code like the following:

const test_helper = require('./test_helper');
const app = test_helper.app;
const featureController = require('../controllers/feature.js');
const fieldNames = ['tags', 'properties', 'applicationID'];

app.get('/api/feature/:id', function(req, res) {
  let params = test_helper.buildParams({'featureId': req.params.id});
  let paramsWithFeatureId = test_helper.createPublicSwaggerParams(fieldNames, params);
  return featureController.protectedGet(paramsWithFeatureId, res);
});

test("GET /api/feature/:id  returns 200", done => {
  request(app)
    .get('/api/feature/AAABBB')
    .expect(200)
    .then(done)
});

This code will stand in for the swagger-tools router, and help build the objects that swagger-tools magically generates when HTTP calls go through it's router. The above code will send an object like below to the api/controllers/feature.js controller protectedGet function as the first parameter (typically called args).

{
  swagger: {
    params: {
      auth_payload: {
        scopes: ['sysadmin', 'public'],
        userID: null
      },
      fields: {
        value: ['tags', 'properties', 'applicationID']
      },
      featureId: {
        value: 'AAABBB'
      }
    }
  }
}

Unfortunately, this results in a lot of boilerplate code in each of the controller tests. There are some helpers to reduce the amount you need to write, but you will still need to check the parameter field names sent by your middleware router match what the controller(and swagger router) expect. However, this method results in pretty effective integration tests as they exercise the controller code and save objects in the database.

Test Database

The tests run on an in-memory MongoDB server, using the mongodb-memory-server package. The setup can be viewed at test_helper.js, and additional config in [config/mongoose_options.js]. It is currently configured to wipe out the database after each test run to prevent database pollution.

Factory-Girl is used to easily create models(persisted to db) for testing purposes.

Mocking http requests

External http calls (such as GETs to BCGW) are mocked with a tool called nock. Currently sample JSON responses are stored in the test/fixtures directory. This allows you to intercept a call to an external service such as bcgw, and respond with your own sample data.

  const bcgwDomain = 'https://openmaps.gov.bc.ca';
  const searchPath = '/geo/pub/FOOO';
  const crownlandsResponse = require('./fixtures/crownlands_response.json');
  var bcgw = nock(bcgwDomain);
  let dispositionId = 666666;

  beforeEach(() => {
    bcgw.get(searchPath + urlEncodedDispositionId)
      .reply(200, crownlandsResponse);
  });

  test('returns the features data from bcgw', done => {
    request(app).get('/api/public/search/bcgw/dispositionTransactionId/' + dispositionId)
      .expect(200)
      .then(response => {
        let firstFeature = response.body.features[0];
        expect(firstFeature).toHaveProperty('properties');
        expect(firstFeature.properties).toHaveProperty('DISPOSITION_TRANSACTION_SID');
        done();
      });
  });

Configuring Environment Variables

Recall the environment variables we need for local dev:

  1. MINIO_HOST='foo.pathfinder.gov.bc.ca'
  2. MINIO_ACCESS_KEY='xxxx'
  3. MINIO_SECRET_KEY='xxxx'
  4. KEYCLOAK_ENABLED=true
  5. MONGODB_DATABASE='epic'
  6. SILENCE_DEFAULT_LOG=false

To get actual values for the above fields in the deployed environments, examine the openshift environment you wish to target:

oc project [projectname]
oc get routes | grep 'minio'
oc get secrets | grep 'minio'

Note: SILENCE_DEFAULT_LOG is used for local development only. See Logging below.

You will not be able to see the above value of the secret if you try examine it. You will only see the encrypted values. Approach your team member with admin access in the openshift project in order to get the access key and secret key values for the secret name you got from the above command. Make sure to ask for the correct environment (dev, test, prod) for the appropriate values.

Logging

The winston package is used to log nearly every operation in the app. console.log is discouraged in favour of winston.

When developing, one may choose to silence the verbose log output to be able to focus on specific operations. To do this:

  1. Set the SILENCE_DEFAULT_LOG environment variable to "true".
  2. Switch the winston.loggers.get() call argument from defaultLog to devLog.
  3. When you're finished, restore all logging to use defaultLog.

The Winston loggers have been set up in config/loggers.js.

API Controllers begin with a log output that states:

  • The model name
  • The level of access
  • The HTTP method

So an example log for a controller would be "COMMENT PERIOD PROTECTED GET."

landuseplanning-api's People

Contributors

actionanalytics avatar baelx avatar danieltruong avatar jareth-whitney avatar kathydo avatar marklise avatar maxwardle avatar mtcarto avatar nickphura avatar repo-mountie[bot] avatar rpyyj avatar severinbeauvais avatar shawnturple avatar stevemhoward avatar w8896699 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

landuseplanning-api's Issues

Add project lifecycle badge

No Project Lifecycle Badge found in your readme!

Hello! I scanned your readme and could not find a project lifecycle badge. A project lifecycle badge will provide contributors to your project as well as other stakeholders (platform services, executive) insight into the lifecycle of your repository.

What is a Project Lifecycle Badge?

It is a simple image that neatly describes your project's stage in its lifecycle. More information can be found in the project lifecycle badges documentation.

What do I need to do?

I suggest you make a PR into your README.md and add a project lifecycle badge near the top where it is easy for your users to pick it up :). Once it is merged feel free to close this issue. I will not open up a new one :)

Add missing topics

TL;DR

Topics greatly improve the discoverability of repos; please add the short code from the table below to the topics of your repo so that ministries can use GitHub's search to find out what repos belong to them and other visitors can find useful content (and reuse it!).

Why Topic

In short order we'll add our 800th repo. This large number clearly demonstrates the success of using GitHub and our Open Source initiative. This huge success means its critical that we work to make our content as discoverable as possible; Through discoverability, we promote code reuse across a large decentralized organization like the Government of British Columbia as well as allow ministries to find the repos they own.

What to do

Below is a table of abbreviation a.k.a short codes for each ministry; they're the ones used in all @gov.bc.ca email addresses. Please add the short codes of the ministry or organization that "owns" this repo as a topic.

add a topic

That's in, you're done!!!

How to use

Once topics are added, you can use them in GitHub's search. For example, enter something like org:bcgov topic:citz to find all the repos that belong to Citizens' Services. You can refine this search by adding key words specific to a subject you're interested in. To learn more about searching through repos check out GitHub's doc on searching.

Pro Tip ๐Ÿค“

  • If your org is not in the list below, or the table contains errors, please create an issue here.

  • While you're doing this, add additional topics that would help someone searching for "something". These can be the language used javascript or R; something like opendata or data for data only repos; or any other key words that are useful.

  • Add a meaningful description to your repo. This is hugely valuable to people looking through our repositories.

  • If your application is live, add the production URL.

Ministry Short Codes

Short Code Organization Name
AEST Advanced Education, Skills & Training
AGRI Agriculture
ALC Agriculture Land Commission
AG Attorney General
MCF Children & Family Development
CITZ Citizens' Services
DBC Destination BC
EMBC Emergency Management BC
EAO Environmental Assessment Office
EDUC Education
EMPR Energy, Mines & Petroleum Resources
ENV Environment & Climate Change Strategy
FIN Finance
FLNR Forests, Lands, Natural Resource Operations & Rural Development
HLTH Health
FLNR Indigenous Relations & Reconciliation
JEDC Jobs, Economic Development & Competitiveness
LBR Labour Policy & Legislation
LDB BC Liquor Distribution Branch
MMHA Mental Health & Addictions
MAH Municipal Affairs & Housing
BCPC Pension Corporation
PSA Public Safety & Solicitor General & Emergency B.C.
SDPR Social Development & Poverty Reduction
TCA Tourism, Arts & Culture
TRAN Transportation & Infrastructure

NOTE See an error or omission? Please create an issue here to get it remedied.

It's Been a While Since This Repository has Been Updated

This issue is a kind reminder that your repository has been inactive for 180 days. Some repositories are maintained in accordance with business requirements that infrequently change thus appearing inactive, and some repositories are inactive because they are unmaintained.

To help differentiate products that are unmaintained from products that do not require frequent maintenance, repomountie will open an issue whenever a repository has not been updated in 180 days.

  • If this product is being actively maintained, please close this issue.
  • If this repository isn't being actively maintained anymore, please archive this repository. Also, for bonus points, please add a dormant or retired life cycle badge.

Thank you for your help ensuring effective governance of our open-source ecosystem!

It's Been a While Since This Repository has Been Updated

This issue is a kind reminder that your repository has been inactive for 181 days. Some repositories are maintained in accordance with business requirements that infrequently change thus appearing inactive, and some repositories are inactive because they are unmaintained.

To help differentiate products that are unmaintained from products that do not require frequent maintenance, repomountie will open an issue whenever a repository has not been updated in 180 days.

  • If this product is being actively maintained, please close this issue.
  • If this repository isn't being actively maintained anymore, please archive this repository. Also, for bonus points, please add a dormant or retired life cycle badge.

Thank you for your help ensuring effective governance of our open-source ecosystem!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.