From fbb38f356d04696f06629c118dc45bfc820edf7a Mon Sep 17 00:00:00 2001 From: ashwin1104 Date: Sat, 26 Sep 2020 14:52:56 -0500 Subject: [PATCH 01/27] add checkin routes file --- api/src/routes/checkin.js | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 api/src/routes/checkin.js diff --git a/api/src/routes/checkin.js b/api/src/routes/checkin.js new file mode 100644 index 0000000..bfc3ffa --- /dev/null +++ b/api/src/routes/checkin.js @@ -0,0 +1,6 @@ +const express = require("express"); +const router = express.Router(); + +// router.post("/") +// router.get("/") +// router.delete("/") \ No newline at end of file From 3c79fe4dd697dd5ad14252039cb121a1e8bfa714 Mon Sep 17 00:00:00 2001 From: ashwin1104 Date: Mon, 28 Sep 2020 19:28:12 -0500 Subject: [PATCH 02/27] create API readme --- api/README.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 api/README.md diff --git a/api/README.md b/api/README.md new file mode 100644 index 0000000..e5755ec --- /dev/null +++ b/api/README.md @@ -0,0 +1,8 @@ +# Closegap API + +This folder contains the backend api of the application. + +## Install & Run +```bash +yarn && yarn start +``` \ No newline at end of file From bbce007e1a593d45330a7011b6aac3ee67a9f03c Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Mon, 28 Sep 2020 19:50:12 -0500 Subject: [PATCH 03/27] Install dotenv --- api/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/api/package.json b/api/package.json index 9660320..3249497 100644 --- a/api/package.json +++ b/api/package.json @@ -13,6 +13,7 @@ "cookie-parser": "~1.4.4", "cors": "^2.8.5", "debug": "~2.6.9", + "dotenv": "^8.2.0", "express": "~4.16.1", "http-errors": "~1.6.3", "morgan": "~1.9.1", From 0eb8cfe5d2c1dcba5e6f891c414ce3995ba8aa3c Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Mon, 28 Sep 2020 19:58:09 -0500 Subject: [PATCH 04/27] Add default config for db --- api/config/dev.env.default | 6 ++++++ api/package.json | 2 +- api/src/app.js | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 api/config/dev.env.default diff --git a/api/config/dev.env.default b/api/config/dev.env.default new file mode 100644 index 0000000..092291b --- /dev/null +++ b/api/config/dev.env.default @@ -0,0 +1,6 @@ +# PostgreSQL DB Configuration +PGUSER=dbuser +PGHOST=database.server.com +PGPASSWORD=secretpassword +PGDATABASE=mydb +PGPORT=3211 diff --git a/api/package.json b/api/package.json index 3249497..495b627 100644 --- a/api/package.json +++ b/api/package.json @@ -7,7 +7,7 @@ "format:check": "prettier --check \"./**/*.{js,jsx,json,md}\"", "lint": "eslint \"./**/*.js\"", "lint:fix": "eslint --fix \"./**/*.js\"", - "start": "nodemon ./src/bin/www" + "start": "NODE_ENV=dev nodemon ./src/bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", diff --git a/api/src/app.js b/api/src/app.js index 4d918c4..39b1e71 100644 --- a/api/src/app.js +++ b/api/src/app.js @@ -3,11 +3,17 @@ var express = require("express"); var path = require("path"); var cookieParser = require("cookie-parser"); var logger = require("morgan"); +var dotenv = require("dotenv"); var indexRouter = require("./routes/index"); var app = express(); +// use config/NODE_ENV.env as config +dotenv.config({ + path: path.resolve(__dirname, `../config/${process.env.NODE_ENV}.env`), +}); + // view engine setup app.set("views", path.join(__dirname, "views")); From 505f01802cff284e41ae5314ac2354acb329da38 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 02:53:29 -0500 Subject: [PATCH 05/27] Install pg --- api/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/package.json b/api/package.json index 495b627..5a13f60 100644 --- a/api/package.json +++ b/api/package.json @@ -17,7 +17,8 @@ "express": "~4.16.1", "http-errors": "~1.6.3", "morgan": "~1.9.1", - "nodemon": "^2.0.4" + "nodemon": "^2.0.4", + "pg": "^8.3.3" }, "devDependencies": { "@h4iuiuc/eslint-plugin": "^1.0.12", From 28686d5fc3c826a92c2dd8944840c227c142d5ac Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 13:53:05 -0500 Subject: [PATCH 06/27] Install pg-promise --- api/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/package.json b/api/package.json index 5a13f60..44e6469 100644 --- a/api/package.json +++ b/api/package.json @@ -18,7 +18,8 @@ "http-errors": "~1.6.3", "morgan": "~1.9.1", "nodemon": "^2.0.4", - "pg": "^8.3.3" + "pg": "^8.3.3", + "pg-promise": "^10.6.1" }, "devDependencies": { "@h4iuiuc/eslint-plugin": "^1.0.12", From 6ef64865a59a2e42588b876d2e36828b5dbd280c Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 14:47:53 -0500 Subject: [PATCH 07/27] Add ruby db schema --- api/schema/db/schema.rb | 217 ++++++++++++++++++++++++++++++++++++++++ api/schema/db/seeds.rb | 130 ++++++++++++++++++++++++ 2 files changed, 347 insertions(+) create mode 100644 api/schema/db/schema.rb create mode 100644 api/schema/db/seeds.rb diff --git a/api/schema/db/schema.rb b/api/schema/db/schema.rb new file mode 100644 index 0000000..1eb4b31 --- /dev/null +++ b/api/schema/db/schema.rb @@ -0,0 +1,217 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2019_12_09_204754) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "admin_texts", force: :cascade do |t| + t.string "field", null: false + t.text "text", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "check_ins", force: :cascade do |t| + t.bigint "student_id" + t.string "feeling", null: false + t.string "body_sensations", null: false, array: true + t.boolean "talk_to_adult", default: false + t.integer "fuel", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "user_id" + t.boolean "has_eaten" + t.boolean "has_slept" + t.boolean "hurt_or_sick" + t.boolean "ok_at_home" + t.boolean "bullied_at_school" + t.bigint "followed_up_id" + t.string "intervention" + t.index ["followed_up_id"], name: "index_check_ins_on_followed_up_id" + t.index ["student_id"], name: "index_check_ins_on_student_id" + t.index ["user_id"], name: "index_check_ins_on_user_id" + end + + create_table "students", force: :cascade do |t| + t.bigint "user_id" + t.string "first_name", null: false + t.string "middle_name" + t.string "last_name", null: false + t.date "birthdate", null: false + t.string "homeroom_teacher", null: false + t.string "grade", null: false + t.string "gender", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "school_name" + t.index ["user_id"], name: "index_students_on_user_id" + end + + create_table "students_caregivers", force: :cascade do |t| + t.bigint "user_id" + t.bigint "student_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["student_id"], name: "index_students_caregivers_on_student_id" + t.index ["user_id", "student_id"], name: "index_students_caregivers_on_user_id_and_student_id", unique: true + t.index ["user_id"], name: "index_students_caregivers_on_user_id" + end + + create_table "users", force: :cascade do |t| + t.string "provider", default: "email", null: false + t.string "uid", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.boolean "allow_password_change", default: false + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0, null: false + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "unconfirmed_email" + t.string "name" + t.string "nickname" + t.string "image" + t.string "email" + t.json "tokens" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "first_name" + t.string "last_name" + t.string "role" + t.string "phone_number" + t.boolean "accepted_terms" + t.datetime "birthdate" + t.bigint "student_id" + t.boolean "change_password_on_next_login", default: false + t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true + t.index ["email"], name: "index_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + t.index ["student_id"], name: "index_users_on_student_id" + t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true + end + + add_foreign_key "check_ins", "students", on_delete: :cascade + add_foreign_key "check_ins", "users", column: "followed_up_id" + add_foreign_key "check_ins", "users", on_delete: :cascade + add_foreign_key "students", "users", on_delete: :cascade + add_foreign_key "students_caregivers", "students", on_delete: :cascade + add_foreign_key "students_caregivers", "users", on_delete: :cascade + add_foreign_key "users", "students" + + create_view "students_views", sql_definition: <<-SQL + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM (students + JOIN users ON ((students.user_id = users.id))) + UNION ALL + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students_caregivers.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + check_ins.feeling, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM ((students + JOIN students_caregivers ON ((students.id = students_caregivers.student_id))) + JOIN users ON ((students.user_id = users.id))); + SQL +end diff --git a/api/schema/db/seeds.rb b/api/schema/db/seeds.rb new file mode 100644 index 0000000..a716ebb --- /dev/null +++ b/api/schema/db/seeds.rb @@ -0,0 +1,130 @@ +# frozen_string_literal: true + +# rubocop:disable all +require 'model_factories' +include ModelFactories + +puts 'Seeding database with test data...' +seed_database +puts 'done!' +# rubocop:enable all + +# Create Users +user1 = User.create!( + email: 'foo1@bar.com', password: '123456789', first_name: 'John' +) +user2 = User.create!( + email: 'foo2@bar.com', password: '123456789', first_name: 'Mary' +) + +# Create Students +student1 = Student.create!( + user: user1, first_name: 'Ipsum1', middle_name: 'Lorem1', grade: '1st', + last_name: 'Dolor1', birthdate: '2010/11/14', homeroom_teacher: 'Amet1', + gender: 'male', school_name: 'some school name' +) +student2 = Student.create!( + user: user1, first_name: 'Ipsum2', middle_name: 'Lorem2', gender: 'female', + last_name: 'Dolor2', birthdate: '2008/09/04', homeroom_teacher: 'Amet2', + grade: '2nd', school_name: 'some school name' +) +student3 = Student.create!( + user: user1, first_name: 'Ipsum3', middle_name: 'Lorem3', grade: '3rd', + last_name: 'Dolor3', birthdate: '2006/03/25', homeroom_teacher: 'Amet3', + gender: 'male', school_name: 'some school name' +) +student4 = Student.create!( + user: user1, first_name: 'Ipsum4', middle_name: 'Lorem4', grade: '4th', + last_name: 'Dolor4', birthdate: '2005/05/02', homeroom_teacher: 'Amet4', + gender: 'other', school_name: 'some school name' +) +student5 = Student.create!( + user: user1, first_name: 'Ipsum5', middle_name: 'Lorem5', grade: '5th', + last_name: 'Dolor5', birthdate: '2003/03/08', homeroom_teacher: 'Amet5', + gender: 'male', school_name: 'some school name' +) +student6 = Student.create!( + user: user1, first_name: 'Ipsum6', middle_name: 'Lorem6', gender: 'female', + last_name: 'Dolor6', birthdate: '2002/07/23', homeroom_teacher: 'Amet6', + grade: '6th', school_name: 'some school name' +) +student7 = Student.create!( # Student of user2 + user: user2, first_name: 'Ipsum7', middle_name: 'Lorem7', grade: '7th', + last_name: 'Dolor7', birthdate: '2001/11/15', homeroom_teacher: 'Amet7', + gender: 'other', school_name: 'some school name' +) +Student.create!( # Student of user1 without check-in + user: user1, first_name: 'Ipsum8', middle_name: 'Lorem8', + last_name: 'Dolor8', birthdate: '2000/05/02', homeroom_teacher: 'Amet8', + grade: '8st', gender: 'other', school_name: 'some school name' +) + +# Create Students check-ins +CheckIn.create!( # Check-in - Talk to adult - red (today) + user: user1, student: student1, feeling: 'sad', fuel: 'red', + talk_to_adult: true, body_sensations: %w[stuck-or-frozen tired-or-weak empty] +) +CheckIn.create!( # Check-in - Talk to adult - blue (today) + user: user1, student: student2, feeling: 'tired', fuel: 'blue', + talk_to_adult: true, + body_sensations: %w[spacey-or-zoned-out light-or-empty sleepy] +) +CheckIn.create!( # Check-in - Don't talk to adult - blue (today) + user: user1, student: student3, feeling: 'tired', fuel: 'blue', + talk_to_adult: false, + body_sensations: %w[nothing-or-numb heavy-or-full fuzzy-or-unclear] +) +CheckIn.create!( # Check-in - Don't talk to adult - yellow (today) + user: user1, student: student4, feeling: 'silly', fuel: 'yellow', + talk_to_adult: false, body_sensations: %w[comfy-or-at-ease warm] +) +CheckIn.create!( # Check-in - Don't talk to adult - green (today) + user: user1, student: student5, feeling: 'happy', fuel: 'green', + talk_to_adult: false, body_sensations: %w[awake heart-flutters energetic] +) +CheckIn.create!( # Check-in - Talk to adult - blue (last week) + user: user1, student: student6, feeling: 'happy', fuel: 'blue', + talk_to_adult: true, + body_sensations: %w[warm awake], created_at: Date.current.weeks_ago(1) +) +CheckIn.create!( # Check-in - (user2) to check if doesn't appear at user1 + user: user2, student: student7, feeling: 'upset', fuel: 'green', + talk_to_adult: false, + body_sensations: %w[heavy-chest-or-heart sick-to-my-stomache shakey-or-fidgety + hot-or-sweaty] +) + +# Create more today and last week check-ins for student1 +CheckIn.create!( # Check-in - Talk to adult - blue (last week) + user: user1, student: student1, feeling: 'happy', fuel: 'blue', + talk_to_adult: true, + body_sensations: %w[warm awake], created_at: Date.current.weeks_ago(1) +) +CheckIn.create!( # Check-in - Don't talk to adult - blue (today) + user: user1, student: student1, feeling: 'tired', fuel: 'blue', + talk_to_adult: false, + body_sensations: %w[nothing-or-numb heavy-or-full fuzzy-or-unclear] +) +CheckIn.create!( # Check-in - Don't talk to adult - yellow (today) + user: user1, student: student1, feeling: 'silly', fuel: 'yellow', + talk_to_adult: false, body_sensations: %w[comfy-or-at-ease warm] +) + +AdminText.create!(field: 'student_user_welcome_title', text: 'Welcome Student') + +AdminText.create!( + field: 'student_user_welcome_subject', + text: 'Welcome Student' +) + +AdminText.create!( + field: 'student_user_welcome_description', + text: <<-TEXT + You have been subscribed to our system. please click on the link below to change your password + TEXT +) + +AdminText.create!( + field: 'student_user_welcome_link_text', + text: 'Create my password' +) From a0cbc74841f250a1a9471dafe96c549d4d940655 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 14:48:15 -0500 Subject: [PATCH 08/27] Add db module --- api/src/db.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 api/src/db.js diff --git a/api/src/db.js b/api/src/db.js new file mode 100644 index 0000000..7b5d504 --- /dev/null +++ b/api/src/db.js @@ -0,0 +1,12 @@ +const pgp = require("pg-promise")(); + +// setup DB +const db = pgp({ + host: process.env.PGHOST, + user: process.env.PGUSER, + password: process.env.PGPASSWORD, + database: process.env.PGDATABASE, + port: process.env.PGPORT, +}); + +module.exports = db; From fdc0033f7f6ea8c9147743586fb8e0503661072e Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:36:56 -0500 Subject: [PATCH 09/27] Add schema-only and seeded db dumps --- api/schema/db/development.sql | 613 ++++++++++++++++++++++ api/schema/db/development_seeded.sql | 751 +++++++++++++++++++++++++++ api/schema/db/schema.rb | 217 -------- api/schema/db/seeds.rb | 130 ----- api/schema/db/test.sql | 22 + api/schema/db/test_seeded.sql | 22 + 6 files changed, 1408 insertions(+), 347 deletions(-) create mode 100644 api/schema/db/development.sql create mode 100644 api/schema/db/development_seeded.sql delete mode 100644 api/schema/db/schema.rb delete mode 100644 api/schema/db/seeds.rb create mode 100644 api/schema/db/test.sql create mode 100644 api/schema/db/test_seeded.sql diff --git a/api/schema/db/development.sql b/api/schema/db/development.sql new file mode 100644 index 0000000..6e9a5c1 --- /dev/null +++ b/api/schema/db/development.sql @@ -0,0 +1,613 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.4 +-- Dumped by pg_dump version 12.4 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: admin_texts; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.admin_texts ( + id bigint NOT NULL, + field character varying NOT NULL, + text text NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.admin_texts OWNER TO "closegap-api"; + +-- +-- Name: admin_texts_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.admin_texts_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.admin_texts_id_seq OWNER TO "closegap-api"; + +-- +-- Name: admin_texts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.admin_texts_id_seq OWNED BY public.admin_texts.id; + + +-- +-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.ar_internal_metadata ( + key character varying NOT NULL, + value character varying, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.ar_internal_metadata OWNER TO "closegap-api"; + +-- +-- Name: check_ins; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.check_ins ( + id bigint NOT NULL, + student_id bigint, + feeling character varying NOT NULL, + body_sensations character varying[] NOT NULL, + talk_to_adult boolean DEFAULT false, + fuel integer NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + user_id bigint, + has_eaten boolean, + has_slept boolean, + hurt_or_sick boolean, + ok_at_home boolean, + bullied_at_school boolean, + followed_up_id bigint, + intervention character varying +); + + +ALTER TABLE public.check_ins OWNER TO "closegap-api"; + +-- +-- Name: check_ins_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.check_ins_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.check_ins_id_seq OWNER TO "closegap-api"; + +-- +-- Name: check_ins_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.check_ins_id_seq OWNED BY public.check_ins.id; + + +-- +-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.schema_migrations ( + version character varying NOT NULL +); + + +ALTER TABLE public.schema_migrations OWNER TO "closegap-api"; + +-- +-- Name: students; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.students ( + id bigint NOT NULL, + user_id bigint, + first_name character varying NOT NULL, + middle_name character varying, + last_name character varying NOT NULL, + birthdate date NOT NULL, + homeroom_teacher character varying NOT NULL, + grade character varying NOT NULL, + gender character varying NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + school_name character varying +); + + +ALTER TABLE public.students OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.students_caregivers ( + id bigint NOT NULL, + user_id bigint, + student_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.students_caregivers OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.students_caregivers_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_caregivers_id_seq OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.students_caregivers_id_seq OWNED BY public.students_caregivers.id; + + +-- +-- Name: students_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.students_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_id_seq OWNER TO "closegap-api"; + +-- +-- Name: students_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.students_id_seq OWNED BY public.students.id; + + +-- +-- Name: users; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.users ( + id bigint NOT NULL, + provider character varying DEFAULT 'email'::character varying NOT NULL, + uid character varying DEFAULT ''::character varying NOT NULL, + encrypted_password character varying DEFAULT ''::character varying NOT NULL, + reset_password_token character varying, + reset_password_sent_at timestamp without time zone, + allow_password_change boolean DEFAULT false, + remember_created_at timestamp without time zone, + sign_in_count integer DEFAULT 0 NOT NULL, + current_sign_in_at timestamp without time zone, + last_sign_in_at timestamp without time zone, + current_sign_in_ip character varying, + last_sign_in_ip character varying, + confirmation_token character varying, + confirmed_at timestamp without time zone, + confirmation_sent_at timestamp without time zone, + unconfirmed_email character varying, + name character varying, + nickname character varying, + image character varying, + email character varying, + tokens json, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + first_name character varying, + last_name character varying, + role character varying, + phone_number character varying, + accepted_terms boolean, + birthdate timestamp without time zone, + student_id bigint, + change_password_on_next_login boolean DEFAULT false +); + + +ALTER TABLE public.users OWNER TO "closegap-api"; + +-- +-- Name: students_views; Type: VIEW; Schema: public; Owner: closegap-api +-- + +CREATE VIEW public.students_views AS + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM public.users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM public.check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM public.users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM (public.students + JOIN public.users ON ((students.user_id = users.id))) +UNION ALL + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students_caregivers.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + check_ins.feeling, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM public.users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM public.check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM public.users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM ((public.students + JOIN public.students_caregivers ON ((students.id = students_caregivers.student_id))) + JOIN public.users ON ((students.user_id = users.id))); + + +ALTER TABLE public.students_views OWNER TO "closegap-api"; + +-- +-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.users_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.users_id_seq OWNER TO "closegap-api"; + +-- +-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; + + +-- +-- Name: admin_texts id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.admin_texts ALTER COLUMN id SET DEFAULT nextval('public.admin_texts_id_seq'::regclass); + + +-- +-- Name: check_ins id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins ALTER COLUMN id SET DEFAULT nextval('public.check_ins_id_seq'::regclass); + + +-- +-- Name: students id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students ALTER COLUMN id SET DEFAULT nextval('public.students_id_seq'::regclass); + + +-- +-- Name: students_caregivers id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers ALTER COLUMN id SET DEFAULT nextval('public.students_caregivers_id_seq'::regclass); + + +-- +-- Name: users id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); + + +-- +-- Name: admin_texts admin_texts_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.admin_texts + ADD CONSTRAINT admin_texts_pkey PRIMARY KEY (id); + + +-- +-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.ar_internal_metadata + ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key); + + +-- +-- Name: check_ins check_ins_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT check_ins_pkey PRIMARY KEY (id); + + +-- +-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.schema_migrations + ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: students_caregivers students_caregivers_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT students_caregivers_pkey PRIMARY KEY (id); + + +-- +-- Name: students students_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_pkey PRIMARY KEY (id); + + +-- +-- Name: index_check_ins_on_followed_up_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_followed_up_id ON public.check_ins USING btree (followed_up_id); + + +-- +-- Name: index_check_ins_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_student_id ON public.check_ins USING btree (student_id); + + +-- +-- Name: index_check_ins_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_user_id ON public.check_ins USING btree (user_id); + + +-- +-- Name: index_students_caregivers_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_caregivers_on_student_id ON public.students_caregivers USING btree (student_id); + + +-- +-- Name: index_students_caregivers_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_caregivers_on_user_id ON public.students_caregivers USING btree (user_id); + + +-- +-- Name: index_students_caregivers_on_user_id_and_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_students_caregivers_on_user_id_and_student_id ON public.students_caregivers USING btree (user_id, student_id); + + +-- +-- Name: index_students_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_on_user_id ON public.students USING btree (user_id); + + +-- +-- Name: index_users_on_confirmation_token; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_confirmation_token ON public.users USING btree (confirmation_token); + + +-- +-- Name: index_users_on_email; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_email ON public.users USING btree (email); + + +-- +-- Name: index_users_on_reset_password_token; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_reset_password_token ON public.users USING btree (reset_password_token); + + +-- +-- Name: index_users_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_users_on_student_id ON public.users USING btree (student_id); + + +-- +-- Name: index_users_on_uid_and_provider; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_uid_and_provider ON public.users USING btree (uid, provider); + + +-- +-- Name: students fk_rails_148c9e88f4; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT fk_rails_148c9e88f4 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: users fk_rails_59eb2cd051; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT fk_rails_59eb2cd051 FOREIGN KEY (student_id) REFERENCES public.students(id); + + +-- +-- Name: check_ins fk_rails_772facc9bc; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_772facc9bc FOREIGN KEY (followed_up_id) REFERENCES public.users(id); + + +-- +-- Name: check_ins fk_rails_b15c016c97; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_b15c016c97 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: students_caregivers fk_rails_b1de17c559; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT fk_rails_b1de17c559 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: students_caregivers fk_rails_d335bc9d60; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT fk_rails_d335bc9d60 FOREIGN KEY (student_id) REFERENCES public.students(id) ON DELETE CASCADE; + + +-- +-- Name: check_ins fk_rails_e70677f00c; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_e70677f00c FOREIGN KEY (student_id) REFERENCES public.students(id) ON DELETE CASCADE; + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/api/schema/db/development_seeded.sql b/api/schema/db/development_seeded.sql new file mode 100644 index 0000000..191114a --- /dev/null +++ b/api/schema/db/development_seeded.sql @@ -0,0 +1,751 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.4 +-- Dumped by pg_dump version 12.4 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: admin_texts; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.admin_texts ( + id bigint NOT NULL, + field character varying NOT NULL, + text text NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.admin_texts OWNER TO "closegap-api"; + +-- +-- Name: admin_texts_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.admin_texts_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.admin_texts_id_seq OWNER TO "closegap-api"; + +-- +-- Name: admin_texts_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.admin_texts_id_seq OWNED BY public.admin_texts.id; + + +-- +-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.ar_internal_metadata ( + key character varying NOT NULL, + value character varying, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.ar_internal_metadata OWNER TO "closegap-api"; + +-- +-- Name: check_ins; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.check_ins ( + id bigint NOT NULL, + student_id bigint, + feeling character varying NOT NULL, + body_sensations character varying[] NOT NULL, + talk_to_adult boolean DEFAULT false, + fuel integer NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + user_id bigint, + has_eaten boolean, + has_slept boolean, + hurt_or_sick boolean, + ok_at_home boolean, + bullied_at_school boolean, + followed_up_id bigint, + intervention character varying +); + + +ALTER TABLE public.check_ins OWNER TO "closegap-api"; + +-- +-- Name: check_ins_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.check_ins_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.check_ins_id_seq OWNER TO "closegap-api"; + +-- +-- Name: check_ins_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.check_ins_id_seq OWNED BY public.check_ins.id; + + +-- +-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.schema_migrations ( + version character varying NOT NULL +); + + +ALTER TABLE public.schema_migrations OWNER TO "closegap-api"; + +-- +-- Name: students; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.students ( + id bigint NOT NULL, + user_id bigint, + first_name character varying NOT NULL, + middle_name character varying, + last_name character varying NOT NULL, + birthdate date NOT NULL, + homeroom_teacher character varying NOT NULL, + grade character varying NOT NULL, + gender character varying NOT NULL, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + school_name character varying +); + + +ALTER TABLE public.students OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.students_caregivers ( + id bigint NOT NULL, + user_id bigint, + student_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL +); + + +ALTER TABLE public.students_caregivers OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.students_caregivers_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_caregivers_id_seq OWNER TO "closegap-api"; + +-- +-- Name: students_caregivers_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.students_caregivers_id_seq OWNED BY public.students_caregivers.id; + + +-- +-- Name: students_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.students_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_id_seq OWNER TO "closegap-api"; + +-- +-- Name: students_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.students_id_seq OWNED BY public.students.id; + + +-- +-- Name: users; Type: TABLE; Schema: public; Owner: closegap-api +-- + +CREATE TABLE public.users ( + id bigint NOT NULL, + provider character varying DEFAULT 'email'::character varying NOT NULL, + uid character varying DEFAULT ''::character varying NOT NULL, + encrypted_password character varying DEFAULT ''::character varying NOT NULL, + reset_password_token character varying, + reset_password_sent_at timestamp without time zone, + allow_password_change boolean DEFAULT false, + remember_created_at timestamp without time zone, + sign_in_count integer DEFAULT 0 NOT NULL, + current_sign_in_at timestamp without time zone, + last_sign_in_at timestamp without time zone, + current_sign_in_ip character varying, + last_sign_in_ip character varying, + confirmation_token character varying, + confirmed_at timestamp without time zone, + confirmation_sent_at timestamp without time zone, + unconfirmed_email character varying, + name character varying, + nickname character varying, + image character varying, + email character varying, + tokens json, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + first_name character varying, + last_name character varying, + role character varying, + phone_number character varying, + accepted_terms boolean, + birthdate timestamp without time zone, + student_id bigint, + change_password_on_next_login boolean DEFAULT false +); + + +ALTER TABLE public.users OWNER TO "closegap-api"; + +-- +-- Name: students_views; Type: VIEW; Schema: public; Owner: closegap-api +-- + +CREATE VIEW public.students_views AS + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM public.users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM public.check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM public.users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM (public.students + JOIN public.users ON ((students.user_id = users.id))) +UNION ALL + SELECT students.id, + students.first_name, + students.middle_name, + students.last_name, + students.birthdate, + students.homeroom_teacher, + students.grade, + students.gender, + students.school_name, + students_caregivers.user_id AS owner_user_id, + students.user_id AS owner_id, + students.updated_at, + ( SELECT row_to_json(latest_check_in.*) AS row_to_json + FROM ( SELECT check_ins.id, + check_ins.user_id, + check_ins.student_id, + check_ins.feeling, + CASE check_ins.fuel + WHEN 0 THEN 'red'::text + WHEN 1 THEN 'blue'::text + WHEN 2 THEN 'yellow'::text + WHEN 3 THEN 'green'::text + ELSE NULL::text + END AS fuel, + check_ins.talk_to_adult, + check_ins.body_sensations, + check_ins.has_eaten, + check_ins.has_slept, + check_ins.hurt_or_sick, + check_ins.ok_at_home, + check_ins.bullied_at_school, + check_ins.followed_up_id, + check_ins.intervention, + check_ins.created_at, + ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat + FROM public.users users_1 + WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name + FROM public.check_ins + WHERE (check_ins.student_id = students.id) + ORDER BY check_ins.created_at DESC + LIMIT 1) latest_check_in) AS latest_check_in, + concat(users.first_name, ' ', users.last_name) AS owner_full_name, + users.role AS owner_role, + ( SELECT users_1.email + FROM public.users users_1 + WHERE (users_1.student_id = students.id)) AS email + FROM ((public.students + JOIN public.students_caregivers ON ((students.id = students_caregivers.student_id))) + JOIN public.users ON ((students.user_id = users.id))); + + +ALTER TABLE public.students_views OWNER TO "closegap-api"; + +-- +-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: closegap-api +-- + +CREATE SEQUENCE public.users_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.users_id_seq OWNER TO "closegap-api"; + +-- +-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: closegap-api +-- + +ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; + + +-- +-- Name: admin_texts id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.admin_texts ALTER COLUMN id SET DEFAULT nextval('public.admin_texts_id_seq'::regclass); + + +-- +-- Name: check_ins id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins ALTER COLUMN id SET DEFAULT nextval('public.check_ins_id_seq'::regclass); + + +-- +-- Name: students id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students ALTER COLUMN id SET DEFAULT nextval('public.students_id_seq'::regclass); + + +-- +-- Name: students_caregivers id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers ALTER COLUMN id SET DEFAULT nextval('public.students_caregivers_id_seq'::regclass); + + +-- +-- Name: users id; Type: DEFAULT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass); + + +-- +-- Data for Name: admin_texts; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.admin_texts (id, field, text, created_at, updated_at) FROM stdin; +1 student_user_welcome_title Welcome Student 2020-10-01 04:31:50.984452 2020-10-01 04:31:50.984452 +2 student_user_welcome_subject Welcome Student 2020-10-01 04:31:50.986451 2020-10-01 04:31:50.986451 +3 student_user_welcome_description You have been subscribed to our system. please click on the link below to change your password\n 2020-10-01 04:31:50.987851 2020-10-01 04:31:50.987851 +4 student_user_welcome_link_text Create my password 2020-10-01 04:31:50.989354 2020-10-01 04:31:50.989354 +\. + + +-- +-- Data for Name: ar_internal_metadata; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.ar_internal_metadata (key, value, created_at, updated_at) FROM stdin; +environment development 2020-10-01 04:31:40.679091 2020-10-01 04:31:40.679091 +\. + + +-- +-- Data for Name: check_ins; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.check_ins (id, student_id, feeling, body_sensations, talk_to_adult, fuel, created_at, updated_at, user_id, has_eaten, has_slept, hurt_or_sick, ok_at_home, bullied_at_school, followed_up_id, intervention) FROM stdin; +1 1 sad {stuck-or-frozen,tired-or-weak,empty} t 0 2020-10-01 04:31:50.947562 2020-10-01 04:31:50.947562 1 \N \N \N \N \N \N \N +2 2 tired {spacey-or-zoned-out,light-or-empty,sleepy} t 1 2020-10-01 04:31:50.951891 2020-10-01 04:31:50.951891 1 \N \N \N \N \N \N \N +3 3 tired {nothing-or-numb,heavy-or-full,fuzzy-or-unclear} f 1 2020-10-01 04:31:50.954885 2020-10-01 04:31:50.954885 1 \N \N \N \N \N \N \N +4 4 silly {comfy-or-at-ease,warm} f 2 2020-10-01 04:31:50.957874 2020-10-01 04:31:50.957874 1 \N \N \N \N \N \N \N +5 5 happy {awake,heart-flutters,energetic} f 3 2020-10-01 04:31:50.960978 2020-10-01 04:31:50.960978 1 \N \N \N \N \N \N \N +6 6 happy {warm,awake} t 1 2020-09-23 07:00:00 2020-10-01 04:31:50.96393 1 \N \N \N \N \N \N \N +7 7 upset {heavy-chest-or-heart,sick-to-my-stomache,shakey-or-fidgety,hot-or-sweaty} f 3 2020-10-01 04:31:50.967049 2020-10-01 04:31:50.967049 2 \N \N \N \N \N \N \N +8 1 happy {warm,awake} t 1 2020-09-23 07:00:00 2020-10-01 04:31:50.969807 1 \N \N \N \N \N \N \N +9 1 tired {nothing-or-numb,heavy-or-full,fuzzy-or-unclear} f 1 2020-10-01 04:31:50.972612 2020-10-01 04:31:50.972612 1 \N \N \N \N \N \N \N +10 1 silly {comfy-or-at-ease,warm} f 2 2020-10-01 04:31:50.975242 2020-10-01 04:31:50.975242 1 \N \N \N \N \N \N \N +\. + + +-- +-- Data for Name: schema_migrations; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.schema_migrations (version) FROM stdin; +20190312195037 +20190312215055 +20190314160820 +20190319183354 +20190325162036 +20190330180049 +20190606154400 +20190807191115 +20190808182348 +20190812193013 +20190812193737 +20190813202059 +20190813205412 +20190813222627 +20190813224925 +20190819215957 +20190820133036 +20190822141236 +20190828120218 +20190828142109 +20191105201340 +20191209204754 +\. + + +-- +-- Data for Name: students; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.students (id, user_id, first_name, middle_name, last_name, birthdate, homeroom_teacher, grade, gender, created_at, updated_at, school_name) FROM stdin; +8 1 Ipsum8 Lorem8 Dolor8 2000-05-02 Amet8 8st other 2020-10-01 04:31:50.927534 2020-10-01 04:31:50.927534 some school name +2 1 Ipsum2 Lorem2 Dolor2 2008-09-04 Amet2 2nd female 2020-10-01 04:31:50.91377 2020-10-01 04:31:50.95297 some school name +3 1 Ipsum3 Lorem3 Dolor3 2006-03-25 Amet3 3rd male 2020-10-01 04:31:50.916451 2020-10-01 04:31:50.955899 some school name +4 1 Ipsum4 Lorem4 Dolor4 2005-05-02 Amet4 4th other 2020-10-01 04:31:50.919054 2020-10-01 04:31:50.958944 some school name +5 1 Ipsum5 Lorem5 Dolor5 2003-03-08 Amet5 5th male 2020-10-01 04:31:50.921167 2020-10-01 04:31:50.962011 some school name +6 1 Ipsum6 Lorem6 Dolor6 2002-07-23 Amet6 6th female 2020-10-01 04:31:50.923323 2020-10-01 04:31:50.965061 some school name +7 2 Ipsum7 Lorem7 Dolor7 2001-11-15 Amet7 7th other 2020-10-01 04:31:50.925388 2020-10-01 04:31:50.967905 some school name +1 1 Ipsum1 Lorem1 Dolor1 2010-11-14 Amet1 1st male 2020-10-01 04:31:50.910186 2020-10-01 04:31:50.976185 some school name +\. + + +-- +-- Data for Name: students_caregivers; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.students_caregivers (id, user_id, student_id, created_at, updated_at) FROM stdin; +\. + + +-- +-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: closegap-api +-- + +COPY public.users (id, provider, uid, encrypted_password, reset_password_token, reset_password_sent_at, allow_password_change, remember_created_at, sign_in_count, current_sign_in_at, last_sign_in_at, current_sign_in_ip, last_sign_in_ip, confirmation_token, confirmed_at, confirmation_sent_at, unconfirmed_email, name, nickname, image, email, tokens, created_at, updated_at, first_name, last_name, role, phone_number, accepted_terms, birthdate, student_id, change_password_on_next_login) FROM stdin; +1 email foo1@bar.com $2a$11$iiQXqqTVtE2oDtaw/KbZuuFgWYPkmOaGvI2YagZGcwzHiROlFMuGC \N \N f \N 0 \N \N \N \N \N \N \N \N \N \N \N foo1@bar.com {} 2020-10-01 04:31:50.596206 2020-10-01 04:31:50.596206 John \N \N \N \N \N \N f +2 email foo2@bar.com $2a$11$otPNG8JMQuw2DamwaUS/7ufmk.3LmfZtTPcBaV3A.tnTJIwJ6Iidi \N \N f \N 0 \N \N \N \N \N \N \N \N \N \N \N foo2@bar.com {} 2020-10-01 04:31:50.883133 2020-10-01 04:31:50.883133 Mary \N \N \N \N \N \N f +\. + + +-- +-- Name: admin_texts_id_seq; Type: SEQUENCE SET; Schema: public; Owner: closegap-api +-- + +SELECT pg_catalog.setval('public.admin_texts_id_seq', 4, true); + + +-- +-- Name: check_ins_id_seq; Type: SEQUENCE SET; Schema: public; Owner: closegap-api +-- + +SELECT pg_catalog.setval('public.check_ins_id_seq', 10, true); + + +-- +-- Name: students_caregivers_id_seq; Type: SEQUENCE SET; Schema: public; Owner: closegap-api +-- + +SELECT pg_catalog.setval('public.students_caregivers_id_seq', 1, false); + + +-- +-- Name: students_id_seq; Type: SEQUENCE SET; Schema: public; Owner: closegap-api +-- + +SELECT pg_catalog.setval('public.students_id_seq', 8, true); + + +-- +-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: closegap-api +-- + +SELECT pg_catalog.setval('public.users_id_seq', 2, true); + + +-- +-- Name: admin_texts admin_texts_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.admin_texts + ADD CONSTRAINT admin_texts_pkey PRIMARY KEY (id); + + +-- +-- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.ar_internal_metadata + ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key); + + +-- +-- Name: check_ins check_ins_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT check_ins_pkey PRIMARY KEY (id); + + +-- +-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.schema_migrations + ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version); + + +-- +-- Name: students_caregivers students_caregivers_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT students_caregivers_pkey PRIMARY KEY (id); + + +-- +-- Name: students students_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT users_pkey PRIMARY KEY (id); + + +-- +-- Name: index_check_ins_on_followed_up_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_followed_up_id ON public.check_ins USING btree (followed_up_id); + + +-- +-- Name: index_check_ins_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_student_id ON public.check_ins USING btree (student_id); + + +-- +-- Name: index_check_ins_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_check_ins_on_user_id ON public.check_ins USING btree (user_id); + + +-- +-- Name: index_students_caregivers_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_caregivers_on_student_id ON public.students_caregivers USING btree (student_id); + + +-- +-- Name: index_students_caregivers_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_caregivers_on_user_id ON public.students_caregivers USING btree (user_id); + + +-- +-- Name: index_students_caregivers_on_user_id_and_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_students_caregivers_on_user_id_and_student_id ON public.students_caregivers USING btree (user_id, student_id); + + +-- +-- Name: index_students_on_user_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_students_on_user_id ON public.students USING btree (user_id); + + +-- +-- Name: index_users_on_confirmation_token; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_confirmation_token ON public.users USING btree (confirmation_token); + + +-- +-- Name: index_users_on_email; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_email ON public.users USING btree (email); + + +-- +-- Name: index_users_on_reset_password_token; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_reset_password_token ON public.users USING btree (reset_password_token); + + +-- +-- Name: index_users_on_student_id; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE INDEX index_users_on_student_id ON public.users USING btree (student_id); + + +-- +-- Name: index_users_on_uid_and_provider; Type: INDEX; Schema: public; Owner: closegap-api +-- + +CREATE UNIQUE INDEX index_users_on_uid_and_provider ON public.users USING btree (uid, provider); + + +-- +-- Name: students fk_rails_148c9e88f4; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT fk_rails_148c9e88f4 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: users fk_rails_59eb2cd051; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.users + ADD CONSTRAINT fk_rails_59eb2cd051 FOREIGN KEY (student_id) REFERENCES public.students(id); + + +-- +-- Name: check_ins fk_rails_772facc9bc; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_772facc9bc FOREIGN KEY (followed_up_id) REFERENCES public.users(id); + + +-- +-- Name: check_ins fk_rails_b15c016c97; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_b15c016c97 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: students_caregivers fk_rails_b1de17c559; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT fk_rails_b1de17c559 FOREIGN KEY (user_id) REFERENCES public.users(id) ON DELETE CASCADE; + + +-- +-- Name: students_caregivers fk_rails_d335bc9d60; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.students_caregivers + ADD CONSTRAINT fk_rails_d335bc9d60 FOREIGN KEY (student_id) REFERENCES public.students(id) ON DELETE CASCADE; + + +-- +-- Name: check_ins fk_rails_e70677f00c; Type: FK CONSTRAINT; Schema: public; Owner: closegap-api +-- + +ALTER TABLE ONLY public.check_ins + ADD CONSTRAINT fk_rails_e70677f00c FOREIGN KEY (student_id) REFERENCES public.students(id) ON DELETE CASCADE; + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/api/schema/db/schema.rb b/api/schema/db/schema.rb deleted file mode 100644 index 1eb4b31..0000000 --- a/api/schema/db/schema.rb +++ /dev/null @@ -1,217 +0,0 @@ -# This file is auto-generated from the current state of the database. Instead -# of editing this file, please use the migrations feature of Active Record to -# incrementally modify your database, and then regenerate this schema definition. -# -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). -# -# It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 2019_12_09_204754) do - - # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" - - create_table "admin_texts", force: :cascade do |t| - t.string "field", null: false - t.text "text", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "check_ins", force: :cascade do |t| - t.bigint "student_id" - t.string "feeling", null: false - t.string "body_sensations", null: false, array: true - t.boolean "talk_to_adult", default: false - t.integer "fuel", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.bigint "user_id" - t.boolean "has_eaten" - t.boolean "has_slept" - t.boolean "hurt_or_sick" - t.boolean "ok_at_home" - t.boolean "bullied_at_school" - t.bigint "followed_up_id" - t.string "intervention" - t.index ["followed_up_id"], name: "index_check_ins_on_followed_up_id" - t.index ["student_id"], name: "index_check_ins_on_student_id" - t.index ["user_id"], name: "index_check_ins_on_user_id" - end - - create_table "students", force: :cascade do |t| - t.bigint "user_id" - t.string "first_name", null: false - t.string "middle_name" - t.string "last_name", null: false - t.date "birthdate", null: false - t.string "homeroom_teacher", null: false - t.string "grade", null: false - t.string "gender", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "school_name" - t.index ["user_id"], name: "index_students_on_user_id" - end - - create_table "students_caregivers", force: :cascade do |t| - t.bigint "user_id" - t.bigint "student_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["student_id"], name: "index_students_caregivers_on_student_id" - t.index ["user_id", "student_id"], name: "index_students_caregivers_on_user_id_and_student_id", unique: true - t.index ["user_id"], name: "index_students_caregivers_on_user_id" - end - - create_table "users", force: :cascade do |t| - t.string "provider", default: "email", null: false - t.string "uid", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.string "reset_password_token" - t.datetime "reset_password_sent_at" - t.boolean "allow_password_change", default: false - t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.string "confirmation_token" - t.datetime "confirmed_at" - t.datetime "confirmation_sent_at" - t.string "unconfirmed_email" - t.string "name" - t.string "nickname" - t.string "image" - t.string "email" - t.json "tokens" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "first_name" - t.string "last_name" - t.string "role" - t.string "phone_number" - t.boolean "accepted_terms" - t.datetime "birthdate" - t.bigint "student_id" - t.boolean "change_password_on_next_login", default: false - t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true - t.index ["email"], name: "index_users_on_email", unique: true - t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true - t.index ["student_id"], name: "index_users_on_student_id" - t.index ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true - end - - add_foreign_key "check_ins", "students", on_delete: :cascade - add_foreign_key "check_ins", "users", column: "followed_up_id" - add_foreign_key "check_ins", "users", on_delete: :cascade - add_foreign_key "students", "users", on_delete: :cascade - add_foreign_key "students_caregivers", "students", on_delete: :cascade - add_foreign_key "students_caregivers", "users", on_delete: :cascade - add_foreign_key "users", "students" - - create_view "students_views", sql_definition: <<-SQL - SELECT students.id, - students.first_name, - students.middle_name, - students.last_name, - students.birthdate, - students.homeroom_teacher, - students.grade, - students.gender, - students.school_name, - students.user_id AS owner_user_id, - students.user_id AS owner_id, - students.updated_at, - ( SELECT row_to_json(latest_check_in.*) AS row_to_json - FROM ( SELECT check_ins.id, - check_ins.user_id, - check_ins.student_id, - CASE check_ins.fuel - WHEN 0 THEN 'red'::text - WHEN 1 THEN 'blue'::text - WHEN 2 THEN 'yellow'::text - WHEN 3 THEN 'green'::text - ELSE NULL::text - END AS fuel, - check_ins.talk_to_adult, - check_ins.body_sensations, - check_ins.has_eaten, - check_ins.has_slept, - check_ins.hurt_or_sick, - check_ins.ok_at_home, - check_ins.bullied_at_school, - check_ins.followed_up_id, - check_ins.intervention, - check_ins.created_at, - ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat - FROM users users_1 - WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name - FROM check_ins - WHERE (check_ins.student_id = students.id) - ORDER BY check_ins.created_at DESC - LIMIT 1) latest_check_in) AS latest_check_in, - concat(users.first_name, ' ', users.last_name) AS owner_full_name, - users.role AS owner_role, - ( SELECT users_1.email - FROM users users_1 - WHERE (users_1.student_id = students.id)) AS email - FROM (students - JOIN users ON ((students.user_id = users.id))) - UNION ALL - SELECT students.id, - students.first_name, - students.middle_name, - students.last_name, - students.birthdate, - students.homeroom_teacher, - students.grade, - students.gender, - students.school_name, - students_caregivers.user_id AS owner_user_id, - students.user_id AS owner_id, - students.updated_at, - ( SELECT row_to_json(latest_check_in.*) AS row_to_json - FROM ( SELECT check_ins.id, - check_ins.user_id, - check_ins.student_id, - check_ins.feeling, - CASE check_ins.fuel - WHEN 0 THEN 'red'::text - WHEN 1 THEN 'blue'::text - WHEN 2 THEN 'yellow'::text - WHEN 3 THEN 'green'::text - ELSE NULL::text - END AS fuel, - check_ins.talk_to_adult, - check_ins.body_sensations, - check_ins.has_eaten, - check_ins.has_slept, - check_ins.hurt_or_sick, - check_ins.ok_at_home, - check_ins.bullied_at_school, - check_ins.followed_up_id, - check_ins.intervention, - check_ins.created_at, - ( SELECT concat(users_1.first_name, ' ', users_1.last_name) AS concat - FROM users users_1 - WHERE (users_1.id = check_ins.followed_up_id)) AS follower_full_name - FROM check_ins - WHERE (check_ins.student_id = students.id) - ORDER BY check_ins.created_at DESC - LIMIT 1) latest_check_in) AS latest_check_in, - concat(users.first_name, ' ', users.last_name) AS owner_full_name, - users.role AS owner_role, - ( SELECT users_1.email - FROM users users_1 - WHERE (users_1.student_id = students.id)) AS email - FROM ((students - JOIN students_caregivers ON ((students.id = students_caregivers.student_id))) - JOIN users ON ((students.user_id = users.id))); - SQL -end diff --git a/api/schema/db/seeds.rb b/api/schema/db/seeds.rb deleted file mode 100644 index a716ebb..0000000 --- a/api/schema/db/seeds.rb +++ /dev/null @@ -1,130 +0,0 @@ -# frozen_string_literal: true - -# rubocop:disable all -require 'model_factories' -include ModelFactories - -puts 'Seeding database with test data...' -seed_database -puts 'done!' -# rubocop:enable all - -# Create Users -user1 = User.create!( - email: 'foo1@bar.com', password: '123456789', first_name: 'John' -) -user2 = User.create!( - email: 'foo2@bar.com', password: '123456789', first_name: 'Mary' -) - -# Create Students -student1 = Student.create!( - user: user1, first_name: 'Ipsum1', middle_name: 'Lorem1', grade: '1st', - last_name: 'Dolor1', birthdate: '2010/11/14', homeroom_teacher: 'Amet1', - gender: 'male', school_name: 'some school name' -) -student2 = Student.create!( - user: user1, first_name: 'Ipsum2', middle_name: 'Lorem2', gender: 'female', - last_name: 'Dolor2', birthdate: '2008/09/04', homeroom_teacher: 'Amet2', - grade: '2nd', school_name: 'some school name' -) -student3 = Student.create!( - user: user1, first_name: 'Ipsum3', middle_name: 'Lorem3', grade: '3rd', - last_name: 'Dolor3', birthdate: '2006/03/25', homeroom_teacher: 'Amet3', - gender: 'male', school_name: 'some school name' -) -student4 = Student.create!( - user: user1, first_name: 'Ipsum4', middle_name: 'Lorem4', grade: '4th', - last_name: 'Dolor4', birthdate: '2005/05/02', homeroom_teacher: 'Amet4', - gender: 'other', school_name: 'some school name' -) -student5 = Student.create!( - user: user1, first_name: 'Ipsum5', middle_name: 'Lorem5', grade: '5th', - last_name: 'Dolor5', birthdate: '2003/03/08', homeroom_teacher: 'Amet5', - gender: 'male', school_name: 'some school name' -) -student6 = Student.create!( - user: user1, first_name: 'Ipsum6', middle_name: 'Lorem6', gender: 'female', - last_name: 'Dolor6', birthdate: '2002/07/23', homeroom_teacher: 'Amet6', - grade: '6th', school_name: 'some school name' -) -student7 = Student.create!( # Student of user2 - user: user2, first_name: 'Ipsum7', middle_name: 'Lorem7', grade: '7th', - last_name: 'Dolor7', birthdate: '2001/11/15', homeroom_teacher: 'Amet7', - gender: 'other', school_name: 'some school name' -) -Student.create!( # Student of user1 without check-in - user: user1, first_name: 'Ipsum8', middle_name: 'Lorem8', - last_name: 'Dolor8', birthdate: '2000/05/02', homeroom_teacher: 'Amet8', - grade: '8st', gender: 'other', school_name: 'some school name' -) - -# Create Students check-ins -CheckIn.create!( # Check-in - Talk to adult - red (today) - user: user1, student: student1, feeling: 'sad', fuel: 'red', - talk_to_adult: true, body_sensations: %w[stuck-or-frozen tired-or-weak empty] -) -CheckIn.create!( # Check-in - Talk to adult - blue (today) - user: user1, student: student2, feeling: 'tired', fuel: 'blue', - talk_to_adult: true, - body_sensations: %w[spacey-or-zoned-out light-or-empty sleepy] -) -CheckIn.create!( # Check-in - Don't talk to adult - blue (today) - user: user1, student: student3, feeling: 'tired', fuel: 'blue', - talk_to_adult: false, - body_sensations: %w[nothing-or-numb heavy-or-full fuzzy-or-unclear] -) -CheckIn.create!( # Check-in - Don't talk to adult - yellow (today) - user: user1, student: student4, feeling: 'silly', fuel: 'yellow', - talk_to_adult: false, body_sensations: %w[comfy-or-at-ease warm] -) -CheckIn.create!( # Check-in - Don't talk to adult - green (today) - user: user1, student: student5, feeling: 'happy', fuel: 'green', - talk_to_adult: false, body_sensations: %w[awake heart-flutters energetic] -) -CheckIn.create!( # Check-in - Talk to adult - blue (last week) - user: user1, student: student6, feeling: 'happy', fuel: 'blue', - talk_to_adult: true, - body_sensations: %w[warm awake], created_at: Date.current.weeks_ago(1) -) -CheckIn.create!( # Check-in - (user2) to check if doesn't appear at user1 - user: user2, student: student7, feeling: 'upset', fuel: 'green', - talk_to_adult: false, - body_sensations: %w[heavy-chest-or-heart sick-to-my-stomache shakey-or-fidgety - hot-or-sweaty] -) - -# Create more today and last week check-ins for student1 -CheckIn.create!( # Check-in - Talk to adult - blue (last week) - user: user1, student: student1, feeling: 'happy', fuel: 'blue', - talk_to_adult: true, - body_sensations: %w[warm awake], created_at: Date.current.weeks_ago(1) -) -CheckIn.create!( # Check-in - Don't talk to adult - blue (today) - user: user1, student: student1, feeling: 'tired', fuel: 'blue', - talk_to_adult: false, - body_sensations: %w[nothing-or-numb heavy-or-full fuzzy-or-unclear] -) -CheckIn.create!( # Check-in - Don't talk to adult - yellow (today) - user: user1, student: student1, feeling: 'silly', fuel: 'yellow', - talk_to_adult: false, body_sensations: %w[comfy-or-at-ease warm] -) - -AdminText.create!(field: 'student_user_welcome_title', text: 'Welcome Student') - -AdminText.create!( - field: 'student_user_welcome_subject', - text: 'Welcome Student' -) - -AdminText.create!( - field: 'student_user_welcome_description', - text: <<-TEXT - You have been subscribed to our system. please click on the link below to change your password - TEXT -) - -AdminText.create!( - field: 'student_user_welcome_link_text', - text: 'Create my password' -) diff --git a/api/schema/db/test.sql b/api/schema/db/test.sql new file mode 100644 index 0000000..a364e21 --- /dev/null +++ b/api/schema/db/test.sql @@ -0,0 +1,22 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.4 +-- Dumped by pg_dump version 12.4 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- PostgreSQL database dump complete +-- + diff --git a/api/schema/db/test_seeded.sql b/api/schema/db/test_seeded.sql new file mode 100644 index 0000000..a364e21 --- /dev/null +++ b/api/schema/db/test_seeded.sql @@ -0,0 +1,22 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.4 +-- Dumped by pg_dump version 12.4 + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- PostgreSQL database dump complete +-- + From 628e68ac5da301a509e85e9e0f8f74d83d54c9f5 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:50:34 -0500 Subject: [PATCH 10/27] Change var to const in app.js --- api/src/app.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/api/src/app.js b/api/src/app.js index 39b1e71..2b60a02 100644 --- a/api/src/app.js +++ b/api/src/app.js @@ -1,19 +1,19 @@ -var createError = require("http-errors"); -var express = require("express"); -var path = require("path"); -var cookieParser = require("cookie-parser"); -var logger = require("morgan"); -var dotenv = require("dotenv"); +const createError = require("http-errors"); +const express = require("express"); +const path = require("path"); +const cookieParser = require("cookie-parser"); +const logger = require("morgan"); +const dotenv = require("dotenv"); -var indexRouter = require("./routes/index"); - -var app = express(); +const indexRouter = require("./routes/index"); // use config/NODE_ENV.env as config dotenv.config({ path: path.resolve(__dirname, `../config/${process.env.NODE_ENV}.env`), }); +const app = express(); + // view engine setup app.set("views", path.join(__dirname, "views")); From 5b69eccc71ebfda997d5410904a4b14b5f109eea Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:50:48 -0500 Subject: [PATCH 11/27] Add example route for accessing db via service --- api/src/routes/index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 21a402c..740b20c 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,9 +1,27 @@ -var express = require("express"); -var router = express.Router(); +const express = require("express"); +const router = express.Router(); + +const { getStudentById } = require("../services/user.js"); + /* GET home page. */ router.get("/", function (req, res) { res.send("Closegap!"); }); +/* GET for testing db. */ +router.get("/students/:studentId", async (req, res) => { + let student; + + try { + student = await getStudentById(parseInt(req.params.studentId)); + } catch (e) { + return res.status(400).json({ + error: "database error", + }); + } + + return res.json(200, student); +}); + module.exports = router; From 2dd63096e189e9ca78a3283dd16787f0a1a8b85a Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:51:57 -0500 Subject: [PATCH 12/27] Add example service for student --- api/src/routes/index.js | 2 +- api/src/services/student.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 api/src/services/student.js diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 740b20c..94ecefb 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,7 +1,7 @@ const express = require("express"); const router = express.Router(); -const { getStudentById } = require("../services/user.js"); +const { getStudentById } = require("../services/student.js"); /* GET home page. */ diff --git a/api/src/services/student.js b/api/src/services/student.js new file mode 100644 index 0000000..8e100cd --- /dev/null +++ b/api/src/services/student.js @@ -0,0 +1,9 @@ +const db = require("../db"); + +const getStudentById = async (id) => { + return db.oneOrNone("SELECT * FROM students WHERE id = $1", [id]); +}; + +module.exports = { + getStudentById, +}; From a2d11fc21d9e2bf07d4c458dd695838adef3040d Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:53:34 -0500 Subject: [PATCH 13/27] Update example route with 404 --- api/src/routes/index.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 94ecefb..32171ee 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -21,7 +21,15 @@ router.get("/students/:studentId", async (req, res) => { }); } - return res.json(200, student); + if (student === null) { + return res.status(404).json({ + error: "no student found with given id", + }); + } + + return res.json(200, { + data: student + }); }); module.exports = router; From 578104c12186c4f4f94abeeeec5e8678875001c2 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Wed, 30 Sep 2020 23:54:18 -0500 Subject: [PATCH 14/27] Add setup script for dev db --- api/infra/setup_dev_db.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100755 api/infra/setup_dev_db.sh diff --git a/api/infra/setup_dev_db.sh b/api/infra/setup_dev_db.sh new file mode 100755 index 0000000..9faf607 --- /dev/null +++ b/api/infra/setup_dev_db.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +ROOT=$(git rev-parse --show-toplevel) + +while true; do + read -p "Seed database with initial data? [Y/n] " yn + case $yn in + "" ) SUFFIX="_seeded"; break;; + [Yy]* ) SUFFIX="_seeded"; break;; + [Nn]* ) SUFFIX=""; break;; + * ) echo "Please answer y or n.";; + esac +done + +createuser -d closegap-api + +createdb -U closegap-api closegap-api_development +createdb -U closegap-api closegap-api_test + +psql closegap-api_development < ${ROOT}/api/schema/db/development${SEEDED}.sql +psql closegap-api_test < ${ROOT}/api/schema/db/test${SEEDED}.sql From 3a6cb73646ac847085b898c3fb11300eb935ec5c Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:06:11 -0500 Subject: [PATCH 15/27] Add db setup instructions in backend readme --- api/README.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/api/README.md b/api/README.md index e5755ec..191eefa 100644 --- a/api/README.md +++ b/api/README.md @@ -2,7 +2,50 @@ This folder contains the backend api of the application. +## Setup DB + +### Install PostgreSQL + +#### macOS + +1. Install [Homebrew](https://brew.sh). + +2. Install postgresql via Homebrew. + + `$ brew install postgresql` + +3. Start postgresql and register it to launch on system start. + + `$ brew services start postgresql` + +#### Windows Subsystem for Linux (WSL) + +1. [Install postgresql.](https://github.com/michaeltreat/Windows-Subsystem-For-Linux-Setup-Guide/blob/master/readmes/installs/PostgreSQL.md) + +2. Start postgresql. + + `$ sudo service postgresql start ` + +### Create database + +Run setup script. Enter `n` if you do not wish to seed with initial data. + +``` +$ infra/setup_dev_db.sh +Seed database with initial data? [Y/n] y +... +``` + ## Install & Run + ```bash yarn && yarn start -``` \ No newline at end of file +``` + +## Test DB access + +``` +$ curl http://localhost:5000/students/1 +{"data":{"id":"1","user_id":"1","first_name":"Ipsum1","middle_name":"Lorem1","last_name":"Dolor1","birthdate":"2010-11-14T06:00:00.000Z","homeroom_teacher":"Amet1","grade":"1st","gender":"male","created_at":"2020-10-01T09:31:50.910Z","updated_at":"2020-10-01T09:31:50.976Z","school_name":"some school name"}}% +``` + From 486528946d7cb73e0e26b3152b3999e24b910804 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:09:49 -0500 Subject: [PATCH 16/27] Update default dev.env config --- api/README.md | 6 ++++++ api/config/dev.env.default | 15 ++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/api/README.md b/api/README.md index 191eefa..1684d20 100644 --- a/api/README.md +++ b/api/README.md @@ -36,6 +36,12 @@ Seed database with initial data? [Y/n] y ... ``` +## Configure App + +``` +$ cp config/dev.env.default config/dev.env +``` + ## Install & Run ```bash diff --git a/api/config/dev.env.default b/api/config/dev.env.default index 092291b..b13f8d1 100644 --- a/api/config/dev.env.default +++ b/api/config/dev.env.default @@ -1,6 +1,11 @@ # PostgreSQL DB Configuration -PGUSER=dbuser -PGHOST=database.server.com -PGPASSWORD=secretpassword -PGDATABASE=mydb -PGPORT=3211 + +PGDATABASE=closegap-api_development + +PGHOST=localhost + +# When unspecified, pg-promise uses system username +PGUSER=closegap-api +PGPASSWORD=closegap-api + +PGPORT=5432 From c69a39bb0d52bd3f088d95da7cbc108ed4703d64 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:10:48 -0500 Subject: [PATCH 17/27] Differentiate dev environment in yarn start --- api/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/package.json b/api/package.json index 44e6469..a7e4083 100644 --- a/api/package.json +++ b/api/package.json @@ -7,7 +7,8 @@ "format:check": "prettier --check \"./**/*.{js,jsx,json,md}\"", "lint": "eslint \"./**/*.js\"", "lint:fix": "eslint --fix \"./**/*.js\"", - "start": "NODE_ENV=dev nodemon ./src/bin/www" + "start": "yarn start:dev", + "start:dev": "NODE_ENV=dev nodemon ./src/bin/www" }, "dependencies": { "cookie-parser": "~1.4.4", From 56df922324e8380f9873f87eb5b76485ffcaa842 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:13:12 -0500 Subject: [PATCH 18/27] Update dev.env.default config --- api/config/dev.env.default | 1 - 1 file changed, 1 deletion(-) diff --git a/api/config/dev.env.default b/api/config/dev.env.default index b13f8d1..d911cf5 100644 --- a/api/config/dev.env.default +++ b/api/config/dev.env.default @@ -4,7 +4,6 @@ PGDATABASE=closegap-api_development PGHOST=localhost -# When unspecified, pg-promise uses system username PGUSER=closegap-api PGPASSWORD=closegap-api From a94ae50ab967bf86fe2b409ff1eebd29a1ad1cf3 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:18:06 -0500 Subject: [PATCH 19/27] Fix linting --- api/README.md | 3 +-- api/src/routes/checkin.js | 2 +- api/src/routes/index.js | 3 +-- api/src/services/student.js | 4 +--- 4 files changed, 4 insertions(+), 8 deletions(-) diff --git a/api/README.md b/api/README.md index 1684d20..dc2ea1d 100644 --- a/api/README.md +++ b/api/README.md @@ -52,6 +52,5 @@ yarn && yarn start ``` $ curl http://localhost:5000/students/1 -{"data":{"id":"1","user_id":"1","first_name":"Ipsum1","middle_name":"Lorem1","last_name":"Dolor1","birthdate":"2010-11-14T06:00:00.000Z","homeroom_teacher":"Amet1","grade":"1st","gender":"male","created_at":"2020-10-01T09:31:50.910Z","updated_at":"2020-10-01T09:31:50.976Z","school_name":"some school name"}}% +{"data":{"id":"1","user_id":"1","first_name":"Ipsum1","middle_name":"Lorem1","last_name":"Dolor1","birthdate":"2010-11-14T06:00:00.000Z","homeroom_teacher":"Amet1","grade":"1st","gender":"male","created_at":"2020-10-01T09:31:50.910Z","updated_at":"2020-10-01T09:31:50.976Z","school_name":"some school name"}}% ``` - diff --git a/api/src/routes/checkin.js b/api/src/routes/checkin.js index bfc3ffa..bb1729f 100644 --- a/api/src/routes/checkin.js +++ b/api/src/routes/checkin.js @@ -3,4 +3,4 @@ const router = express.Router(); // router.post("/") // router.get("/") -// router.delete("/") \ No newline at end of file +// router.delete("/") diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 32171ee..f830e30 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -3,7 +3,6 @@ const router = express.Router(); const { getStudentById } = require("../services/student.js"); - /* GET home page. */ router.get("/", function (req, res) { res.send("Closegap!"); @@ -28,7 +27,7 @@ router.get("/students/:studentId", async (req, res) => { } return res.json(200, { - data: student + data: student, }); }); diff --git a/api/src/services/student.js b/api/src/services/student.js index 8e100cd..22d0803 100644 --- a/api/src/services/student.js +++ b/api/src/services/student.js @@ -1,8 +1,6 @@ const db = require("../db"); -const getStudentById = async (id) => { - return db.oneOrNone("SELECT * FROM students WHERE id = $1", [id]); -}; +const getStudentById = (id) => db.oneOrNone("SELECT * FROM students WHERE id = $1", [id]); module.exports = { getStudentById, From 2f1e0b5beff5b677fa9ae5a5626a9c7e8aa9394c Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:22:00 -0500 Subject: [PATCH 20/27] Fix setup db script not seeding data --- api/infra/setup_dev_db.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/api/infra/setup_dev_db.sh b/api/infra/setup_dev_db.sh index 9faf607..070a65a 100755 --- a/api/infra/setup_dev_db.sh +++ b/api/infra/setup_dev_db.sh @@ -1,12 +1,11 @@ -#!/bin/sh +#!/usr/bin/env bash ROOT=$(git rev-parse --show-toplevel) while true; do read -p "Seed database with initial data? [Y/n] " yn case $yn in - "" ) SUFFIX="_seeded"; break;; - [Yy]* ) SUFFIX="_seeded"; break;; + "" | [Yy]* ) SUFFIX="_seeded"; break;; [Nn]* ) SUFFIX=""; break;; * ) echo "Please answer y or n.";; esac @@ -17,5 +16,5 @@ createuser -d closegap-api createdb -U closegap-api closegap-api_development createdb -U closegap-api closegap-api_test -psql closegap-api_development < ${ROOT}/api/schema/db/development${SEEDED}.sql -psql closegap-api_test < ${ROOT}/api/schema/db/test${SEEDED}.sql +psql closegap-api_development < ${ROOT}/api/schema/db/development${SUFFIX}.sql +psql closegap-api_test < ${ROOT}/api/schema/db/test${SUFFIX}.sql From 6800a250c94c99b332f4f11f62c5f8bd14ea03a3 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:33:13 -0500 Subject: [PATCH 21/27] Export router from checkin route --- api/src/routes/checkin.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/src/routes/checkin.js b/api/src/routes/checkin.js index bb1729f..956c8b4 100644 --- a/api/src/routes/checkin.js +++ b/api/src/routes/checkin.js @@ -4,3 +4,5 @@ const router = express.Router(); // router.post("/") // router.get("/") // router.delete("/") + +module.exports = router; From d59f041f6b78a1cb59980cf48c2fedcb81714d97 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 00:46:43 -0500 Subject: [PATCH 22/27] Run prettier --- api/src/services/student.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/src/services/student.js b/api/src/services/student.js index 22d0803..03cfecb 100644 --- a/api/src/services/student.js +++ b/api/src/services/student.js @@ -1,6 +1,7 @@ const db = require("../db"); -const getStudentById = (id) => db.oneOrNone("SELECT * FROM students WHERE id = $1", [id]); +const getStudentById = (id) => + db.oneOrNone("SELECT * FROM students WHERE id = $1", [id]); module.exports = { getStudentById, From d93a30472604d208feb27e7f36bac5157225c586 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 17:37:13 -0500 Subject: [PATCH 23/27] Use http-status-codes --- api/package.json | 1 + api/src/routes/index.js | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/api/package.json b/api/package.json index a7e4083..65fff08 100644 --- a/api/package.json +++ b/api/package.json @@ -17,6 +17,7 @@ "dotenv": "^8.2.0", "express": "~4.16.1", "http-errors": "~1.6.3", + "http-status-codes": "^2.1.4", "morgan": "~1.9.1", "nodemon": "^2.0.4", "pg": "^8.3.3", diff --git a/api/src/routes/index.js b/api/src/routes/index.js index f830e30..6a5b1b8 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,8 +1,10 @@ const express = require("express"); -const router = express.Router(); +const { StatusCodes } = require("http-status-codes"); const { getStudentById } = require("../services/student.js"); +const router = express.Router(); + /* GET home page. */ router.get("/", function (req, res) { res.send("Closegap!"); @@ -15,18 +17,18 @@ router.get("/students/:studentId", async (req, res) => { try { student = await getStudentById(parseInt(req.params.studentId)); } catch (e) { - return res.status(400).json({ + return res.status(StatusCodes.BAD_REQUEST).json({ error: "database error", }); } if (student === null) { - return res.status(404).json({ + return res.status(StatusCodes.NOT_FOUND).json({ error: "no student found with given id", }); } - return res.json(200, { + return res.json({ data: student, }); }); From 8b5de95c45880f5159ddb7ae96b07e11f6a098ce Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 17:59:58 -0500 Subject: [PATCH 24/27] Add comments to db setup script --- api/infra/setup_dev_db.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/api/infra/setup_dev_db.sh b/api/infra/setup_dev_db.sh index 070a65a..23309de 100755 --- a/api/infra/setup_dev_db.sh +++ b/api/infra/setup_dev_db.sh @@ -1,7 +1,9 @@ #!/usr/bin/env bash +# Get root path of Git repo ROOT=$(git rev-parse --show-toplevel) +# Ask user if they want to seed the database with initial data while true; do read -p "Seed database with initial data? [Y/n] " yn case $yn in @@ -11,10 +13,15 @@ while true; do esac done +# Create user closegap-api, which will be the API's DB user createuser -d closegap-api +# Create development and test DBs createdb -U closegap-api closegap-api_development createdb -U closegap-api closegap-api_test +# Run the SQL schemas. +# schema/db/development_seeded.sql for schema of seeded DB +# schema/db/development.sql for schema of bare DB psql closegap-api_development < ${ROOT}/api/schema/db/development${SUFFIX}.sql psql closegap-api_test < ${ROOT}/api/schema/db/test${SUFFIX}.sql From 869c1d79af34217928a3b2de19e999d30f157aa0 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 18:16:09 -0500 Subject: [PATCH 25/27] Remove checkin route comments --- api/src/routes/checkin.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/api/src/routes/checkin.js b/api/src/routes/checkin.js index 956c8b4..fc8981e 100644 --- a/api/src/routes/checkin.js +++ b/api/src/routes/checkin.js @@ -1,8 +1,4 @@ const express = require("express"); const router = express.Router(); -// router.post("/") -// router.get("/") -// router.delete("/") - module.exports = router; From 6fc2ec27413ae9b00ae45620553df8586565757e Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 18:21:52 -0500 Subject: [PATCH 26/27] Remove return from route --- api/src/routes/index.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 6a5b1b8..792dd09 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,10 +1,9 @@ const express = require("express"); +const router = express.Router(); const { StatusCodes } = require("http-status-codes"); const { getStudentById } = require("../services/student.js"); -const router = express.Router(); - /* GET home page. */ router.get("/", function (req, res) { res.send("Closegap!"); @@ -28,9 +27,7 @@ router.get("/students/:studentId", async (req, res) => { }); } - return res.json({ - data: student, - }); + res.json({ data: student }); }); module.exports = router; From d7d9706e89ef740026815497ad8ac11ce5388fd8 Mon Sep 17 00:00:00 2001 From: Jeffrey Tang Date: Thu, 1 Oct 2020 18:23:32 -0500 Subject: [PATCH 27/27] Fix statusCodes import --- api/src/routes/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/api/src/routes/index.js b/api/src/routes/index.js index 792dd09..d11984d 100644 --- a/api/src/routes/index.js +++ b/api/src/routes/index.js @@ -1,6 +1,6 @@ const express = require("express"); const router = express.Router(); -const { StatusCodes } = require("http-status-codes"); +const statusCodes = require("http-status-codes"); const { getStudentById } = require("../services/student.js"); @@ -16,13 +16,13 @@ router.get("/students/:studentId", async (req, res) => { try { student = await getStudentById(parseInt(req.params.studentId)); } catch (e) { - return res.status(StatusCodes.BAD_REQUEST).json({ + return res.status(statusCodes.BAD_REQUEST).json({ error: "database error", }); } if (student === null) { - return res.status(StatusCodes.NOT_FOUND).json({ + return res.status(statusCodes.NOT_FOUND).json({ error: "no student found with given id", }); }