From e8de63bb96ed30f487a7f373d3f1387ab94579d0 Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Tue, 2 Apr 2019 11:08:08 -0700 Subject: [PATCH] properly adding in project retrieval with organization --- app/controllers/index.js | 21 ++++++------ app/controllers/organization.controller.js | 37 ++++++++++++++++------ app/controllers/project.controller.js | 15 +++++++++ app/routes/organization.route.js | 28 ++++++++++------ models/Project.js | 4 +++ 5 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 app/controllers/project.controller.js diff --git a/app/controllers/index.js b/app/controllers/index.js index 2a3899d..8a45019 100644 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -1,12 +1,13 @@ module.exports = { - donor: require("./donor.controller"), - grant: require("./grant.controller"), - organization: require("./organization.controller"), - stripe: require("./stripe.controller"), - sendgrid: require("./sendgrid.controller"), - region: require("./region.controller"), - cause: require("./cause.controller"), - user: require("./user.controller"), - auth: require("./auth.controller"), - charge: require("./charge.controller") + donor: require('./donor.controller'), + grant: require('./grant.controller'), + organization: require('./organization.controller'), + stripe: require('./stripe.controller'), + sendgrid: require('./sendgrid.controller'), + region: require('./region.controller'), + cause: require('./cause.controller'), + user: require('./user.controller'), + auth: require('./auth.controller'), + charge: require('./charge.controller'), + project: require('./project.controller') }; diff --git a/app/controllers/organization.controller.js b/app/controllers/organization.controller.js index 4c24db2..c2510d7 100644 --- a/app/controllers/organization.controller.js +++ b/app/controllers/organization.controller.js @@ -181,15 +181,34 @@ exports.createOrganization = async (req, res, next) => { }; // FETCH all organizations -exports.getAllOrganizations = (req, res, next) => { - Organization.findAll() - .then(organizations => { - res.status(200).json({ - organizations, - number_items: organizations.length - }); - }) - .catch(error => next(error)); +exports.getAllOrganizations = async (req, res, next) => { + const QUERY = ` + select + o.*, + p.id as project_id, + p.title, + p.description, + p.isComplete + from organizations as o + left join ( + SELECT * from projects + where id in ( + select + max(id) + from projects + group by organization_id + ) + ) + as p on p.organization_id = o.id + `; + const organizations = await db.sequelize.query(QUERY, { + type: db.Sequelize.QueryTypes.SELECT + }); + + return res.status(200).json({ + organizations, + number_items: organizations.length + }); }; // FETCH depending on the given search diff --git a/app/controllers/project.controller.js b/app/controllers/project.controller.js new file mode 100644 index 0000000..5062031 --- /dev/null +++ b/app/controllers/project.controller.js @@ -0,0 +1,15 @@ +const db = require('../../models'), + errorMaker = require('../helpers/error.maker'), + textCleaner = require('../helpers/text_cleaner'); + +const { Project } = db; + +exports.createProject = async (req, res, next) => { + const { org_id } = req.user.id; + + var project = await db.sequelize.query(` + SELECT * + FROM project + WHERE organization_id = :org_id + ORDER BY created_at `); +}; diff --git a/app/routes/organization.route.js b/app/routes/organization.route.js index 18fe4b5..6c650f5 100644 --- a/app/routes/organization.route.js +++ b/app/routes/organization.route.js @@ -1,23 +1,31 @@ -const express = require("express"), - router = express.Router(), - checkAuth = require("../middleware/check-auth"), - roles = require("../helpers/roles"); +const express = require('express'), + router = express.Router(), + checkAuth = require('../middleware/check-auth'), + roles = require('../helpers/roles'); -const { organization, cause, region, stripe } = require("../controllers"); +const { + organization, + cause, + region, + stripe, + project +} = require('../controllers'); // Charity signup route -router.post("/", organization.createOrganization); +router.post('/', organization.createOrganization); // GET Activate org's stripe connected account -router.get("/stripe/connect", stripe.activateStripeAccount); +router.get('/stripe/connect', stripe.activateStripeAccount); // GET stripe express ui account link router.get( - "/stripe/link", - checkAuth(roles.ORGANIZATION), - stripe.getExpressUILink + '/stripe/link', + checkAuth(roles.ORGANIZATION), + stripe.getExpressUILink ); +router.post('/project', checkAuth(roles.ORGANIZATION), project.createProject); + // // Add cause // router.post('/cause', checkAuth(roles.ORGANIZATION), cause.createCause); diff --git a/models/Project.js b/models/Project.js index 0d1fd00..5ef7b5e 100644 --- a/models/Project.js +++ b/models/Project.js @@ -20,6 +20,10 @@ module.exports = (sequelize, DataTypes) => { defaultValue: 0 }, + title: { + type: DataTypes.STRING(1000), + allowNull: false + }, description: DataTypes.STRING, isComplete: { type: DataTypes.BOOLEAN,