Giter Club home page Giter Club logo

server's Introduction

UPchieve web server

Web server providing endpoints for the UPchieve web client

CircleCI

Table of Contents

Local Development

docker-compose

Docker provides an alternative for local development. A docker-compose file exists, tied to Mongo. Here's how to work in docker-compose.

  1. Navigate to this directory and run mkdir mongo-volume to create a directory for the MongoDB volume.
  2. Run cp config.example.ts config.ts to copy the default config as your own config.
  3. Run docker-compose up to launch the server.
  4. After any change: Run docker-compose down --rmi all to destry images and containers. Then run docker-compose up to see your changes.

Note: the default command ran when starting the server will populate/refresh all seed data, so any data changes to seed data will be overwritten.

Without docker-compose

If not using docker-compose, follow these steps to start required components.

Dependencies

The recommended tool for runtime version managment is asdf and Docker. To use asdf on Windows, first install the appropriate Linux shell distribution using WSL (Windows Subsystem for Linux).

Node 11.7.0

You must use Node.js version 11.7.0. Install the following asdf plugin:

asdf plugin-add nodejs https://github.com/asdf-vm/asdf-nodejs.git
bash ~/.asdf/plugins/nodejs/bin/import-release-team-keyring
asdf install nodejs 11.7.0
asdf global nodejs 11.7.0
node -v

The output of node -v should be: v11.7.0

Mongo 4.2.3

Using a Volume (else data is not saved), pull and run docker image mongo:4.2.3-bionic. You can use any empty directory to which you have write access for your volume.

docker run -i --rm --name mongoContainer -p 27017-27019:27017-27019 -v <Absolute Path to directory on your drive>:/data/db mongo:4.2.3-bionic

Redis 5.0.8

Pull and run docker image redis:5.0.8. You can use any empty directory to which you have write access for your volume.

docker run -i --rm --name redis -p 6379:6379 -v <Absolute Path to directory on your drive>:/data -t redis:5.0.8

Setup

  1. Try connecting to your database container by running mongo (see Mongo dependancy if this will not connect). Run quit() to exit the shell. You can also interface with the database using a free MongoDB GUI such as MongoDB Compass Community
  2. Run cp config.example.ts config.ts to copy the default config as your own config.
  3. Run npm install to install the required dependancies.
  4. Run npx ts-node init to seed the database with users, quiz questions, schools, and zip codes.
  5. If you want to test Twilio voice calling functionality, set the host property to [your public IP address]:3000 (minus the brackets), and configure your router/firewall to allow connections to port 3000 from the Internet. Twilio will need to connect to your system to obtain TwiML instructions.
  6. Run npm run dev to start the dev server on http://localhost:3000. If you get a bcrypt compilement error, run npm rebuild.
  7. See the web client repo for client installation.
  8. (optional) Run redis-server and npm run worker:dev to start the redis database and dev worker. The dev worker will automatically attempt to connect to your local Redis instance and read jobs from there. Additionally, you can run ts-node ./scripts/add-cron-jobs.ts to add all repeatable jobs to the job queue.

Test Users

The database is populated with the following users for local development:

email password
[email protected] Password123
[email protected] Password123
[email protected] Password123
[email protected] Password123

By default, none of the test users have an approvedHighschool set. The volunteers are all admins by default, and [email protected] and [email protected] have also passed all of their certifications. [email protected] has not passed any quizzes.

Structure

The root folder of the repository provides the bootstrap file main.js and a package definitions file.

Config

config.ts contains a map of configuration keys for running the server. All keys and sensitive information should be placed in this file.

Models

Model definitions that map to database models, along with related methods to act on those models, such as parsing, validation, and data transformations.

Router

Directory structure mimics the endpoint structure exposed by the server. Each file provides one or more endpoint routes, responsible for request acceptance/rejection and error handling.

Controllers

Routes use controllers to perform the business logic of the server, providing separation of concerns: the controllers have no need to be aware of how the endpoints work. Instead, a controller provides ways to allow the routes to trigger something (a user update, e.g.)

Services

A service is a step higher than a controller. Services provide abstract functions to one or many controllers, often to interface with third party services.

Endpoints

POST /auth/login

Expects the following request body:

{
  "email": "String",
  "password": "String"
}

Authenticates the user with a session if credentials are correct.

GET /auth/logout

Removes the user's current session.

POST /auth/register/checkcred

Check whether the credential user entered is valid. (first step of registeration) The server will check is there any duplications for email and validate the password.

{
  "email": "String",
  "password": "String"
}

Possible errors:

  • Email/password not provided
  • Password does not meet requirements
  • Email is not valid
  • Email already exists

POST /auth/register

Create a new account based on the information posted.

{
  "email": "String",
  "password": "String",
  "code": "String",
  "highSchool": "String",
  "firstName": "String",
  "lastName": "String"
}

Possible errors:

  • Email/password not provided
  • Password does not meet requirements
  • Email is not valid
  • Email already exists
  • Could not hash password
  • Could not send verification email (for volunteers)

POST /auth/reset/send

{
  "email": "String"
}

POST /auth/reset/confirm

{
  "email": "String",
  "password": "String",
  "newpassword": "String",
  "token": "String"
}

POST /auth/reset/verify

{
  "token": "String"
}

GET /auth/partner/volunteer

Expects the following query string:

?partnerId=PARTNER_ID

where PARTNER_ID is the key name of the volunteer partner organization defined in config.ts under volunteerPartnerManifests.

Returns a volunteer partner manifest object.

GET /auth/partner/student

Expects the following query string:

?partnerId=PARTNER_ID

where PARTNER_ID is the key name of the student partner organization defined in config.ts under studentPartnerManifests.

Returns a student partner manifest object.

GET /auth/partner/student/code

Expects the following query string:

?partnerSignupCode=PARTNER_SIGNUP_CODE

where PARTNER_SIGNUP_CODE is equal to a signupCode defined in config.ts under studentPartnerManifests.

Returns a student partner manifest key name.

POST /api/session/new

{
  "sessionType": "String",
  "sessionSubTopic": "String"
}

POST /api/session/check

{
  "sessionId": "String"
}

POST /api/session/check

{
  "user_id": "String"
}

POST /api/training/questions

{
  "category": "String"
}

POST /api/training/score

{
  "idAnswerMap": "String",
  "category": "String"
}

POST /api/calendar/save

{
  "availability": {
    "Sunday": {
      "12a": "Boolean",
      "1a": "Boolean",
      ...
    },
    "Monday": {
      ...
    },
    ...
  },
  "tz": "String"
}

POST /api/feedback

{
  "sessionId": "String",
  "topic": "String",
  "subTopic": "String",
  "responseData": "String"
}

GET /api/user

Returns a sanitized public user record for the currently authenticated user

PUT /api/user

Accepts a request body with fields mapping to profile fields to update for the currently authenticated user:

{
  "phone": "String"
}

GET /api/user/:id

Returns a sanitized public user record for a user with the given id. May perform checks on the authorization level of the current user to strip out priveliged information.

POST /api/volunteers

Returns an object with all the users who are volunteers. All the keys are user ids.

POST /api/volunteers/availability

Returns a map with the availability of all the volunteers. All the keys are user ids.

POST /api/verify/send

Sends an email to verify the current user with unique hash. The email provided will overwrite the user record's email, in the event that the two do not match.

{
  "email": "String"
}

POST /api/verify/confirm

Accepts a token used to verify the current user.

{
  "token": "String"
}

POST /moderate/message

Expects the following request body:

{
  "content": "string with the content of a message"
}

Returns a boolean indicating whether or not the message is clean.

The response body looks like this if no error occurred:

{
  "isClean": true // or false
}

POST /eligibility/check

Expects the following request body:

{
  "schoolUpchieveId": "String",
  "zipCode": "String",
  "email": "String"
}

GET /eligibility/school/search

Expects the following query string:

?q=SEARCH_STRING

where SEARCH_STRING is the string to be searched.

Searches the database of schools for a name or upchieveId matching the search string. The search string may match only part of the school's name, but if searching for an upchieveId the string must match exactly.

If there are no errors, the response body contains the list of schools matching the search string in the format:

{
  "results": [
    {
      "upchieveId": "UPchieve ID",
      "name": "high school name",
      "districtName": "district name",
      "city": "city name",
      "state": "state postal code"
    },
    // ...
  ]
}

Adds an email address to the list of email addresses to notify when the school is approved by UPchieve.

If no error occurred, the response body looks like:

{
  "schoolId": "School's UPchieve ID"
}

Checks if a student is eligible for UPchieve based on their school and zip code. If no error occurs, the response looks like:

{
  "isEligible": true // or false
}

GET /eligibility/school/studentusers/:schoolUpchieveId

Lists all student users registered with a school. Restricted to admins only. If no error occurs, the response looks like:

{
  "upchieveId": "8-digit identifier",
  "studentUsers": [
    {
      "email": "[email protected]",
      "firstname": "Firstname",
      "lastname": "Lastname",
      "userId": "user's ObjectID in MongoDB"
    },
    // ...
  ]
}

Worker

A Bull worker reading from a local Redis database. Job definitions live in worker/jobs and are registered in worker/jobs/index.ts. A script scripts/add-cron-jobs.ts will insert all repeatable jobs into the local Redis database.

Worker Jobs

server's People

Contributors

alymurray avatar davepic avatar ddmck avatar dependabot[bot] avatar dmnisson avatar ericyliu avatar georgiagallant333 avatar ilakumar avatar jkeat avatar jma26 avatar jmromer avatar joncancode avatar jsheen avatar kadinlz avatar lualparedes avatar markupch avatar n-mathis avatar ravb2019 avatar rossedwardsus avatar stackptr avatar stewartwallace1115 avatar ti2ger92 avatar toldyaonce avatar toofarm avatar treystevens avatar volksport avatar waftring avatar zuoyuanh avatar zuoyuanh97 avatar

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.