From 05a1003be2647def535e781e3138947dd06571ed Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Wed, 13 Mar 2019 23:53:07 -0700 Subject: [PATCH] cleanup and working seed data --- app/config/associate.js | 3 - app/config/db.config.js | 3 - .../organization/organization.controller.js | 12 +- docs/migrations | 18 +++ models/orgBank.model.js | 10 -- models/organization.model.js | 2 +- seeders/20190122051334-base-user.js | 3 +- seeders/20190122053544-base-orgs.js | 125 +++++++++++------- seeders/20190122054316-base-grants.js | 65 --------- server.js | 21 ++- 10 files changed, 119 insertions(+), 143 deletions(-) create mode 100644 docs/migrations delete mode 100644 models/orgBank.model.js delete mode 100644 seeders/20190122054316-base-grants.js diff --git a/app/config/associate.js b/app/config/associate.js index b5f129d..ee12ef2 100644 --- a/app/config/associate.js +++ b/app/config/associate.js @@ -7,7 +7,6 @@ module.exports = models => { Organization, Charge, PaymentPlan, - OrgBank, GrantsOrganizations } = models; @@ -41,6 +40,4 @@ module.exports = models => { Grant.hasOne(PaymentPlan, { foreignKey: 'grant_id' }); - - Organization.hasOne(OrgBank); }; diff --git a/app/config/db.config.js b/app/config/db.config.js index b0cb39e..afc69cb 100644 --- a/app/config/db.config.js +++ b/app/config/db.config.js @@ -7,7 +7,6 @@ const Sequelize = require('sequelize'), UserModel = require('../../models/user.model.js'), PaymentPlanModel = require('../../models/paymentplan.model'), ChargeModel = require('../../models/charge.model'), - OrgBankModel = require('../../models/orgBank.model'), GrantsOrganizationsModel = require('../../models/grantsOrganizations.model'), associate = require('./associate'); @@ -46,7 +45,6 @@ const Organization = OrganizationModel(sequelize, Sequelize); const User = UserModel(sequelize, Sequelize); const Charge = ChargeModel(sequelize, Sequelize); const PaymentPlan = PaymentPlanModel(sequelize, Sequelize); -const OrgBank = OrgBankModel(sequelize, Sequelize); const GrantsOrganizations = GrantsOrganizationsModel(sequelize, Sequelize); db.Donor = Donor; db.Grant = Grant; @@ -56,7 +54,6 @@ db.Organization = Organization; db.User = User; db.Charge = Charge; db.PaymentPlan = PaymentPlan; -db.OrgBank = OrgBank; db.GrantsOrganizations = GrantsOrganizations; // make associations with created models diff --git a/app/controllers/organization/organization.controller.js b/app/controllers/organization/organization.controller.js index 558ed34..4537edc 100644 --- a/app/controllers/organization/organization.controller.js +++ b/app/controllers/organization/organization.controller.js @@ -15,16 +15,16 @@ exports.create = async (req, res, next) => { address, city, - state, country, + state, zip, primary_cause, primary_region, ein, - estimate_assets, - estimate_year_operating_cost, + estimate_asset_value, + estimate_yearly_operating_cost, isNonprofit, @@ -82,16 +82,16 @@ exports.create = async (req, res, next) => { address, city, - state, country, + state, zip, primary_cause, primary_region, ein, - estimate_assets, - estimate_year_operating_cost, + estimate_asset_value, + estimate_yearly_operating_cost, isNonprofit, diff --git a/docs/migrations b/docs/migrations new file mode 100644 index 0000000..e921fd8 --- /dev/null +++ b/docs/migrations @@ -0,0 +1,18 @@ +Insert seed data: +- 3 orgs (password: test) +- 1 donor (password: test) +- basic causes +- basic regions +- 1 charity bundle + +`npx sequelize db:seed:all` + + +Revert seed data insert: + +`npx sequelize db:seed:undo:all` + + +Drop all tables: + +`npm run drop:local:{operating_system}` where `operating_system` is `mac` or `windows` \ No newline at end of file diff --git a/models/orgBank.model.js b/models/orgBank.model.js deleted file mode 100644 index c6af5f4..0000000 --- a/models/orgBank.model.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = (sequelize, DataTypes) => { - const OrgBank = sequelize.define('org_bank', { - id: { - type: DataTypes.INTEGER, - primaryKey: true, - autoIncrement: true - } - }); - return OrgBank; -}; diff --git a/models/organization.model.js b/models/organization.model.js index d22eb58..447db1d 100644 --- a/models/organization.model.js +++ b/models/organization.model.js @@ -74,7 +74,7 @@ module.exports = (sequelize, DataTypes) => { type: DataTypes.BIGINT, defaultValue: 0 }, - estimate_year_operating_cost: { + estimate_yearly_operating_cost: { type: DataTypes.BIGINT, defaultValue: 0 }, diff --git a/seeders/20190122051334-base-user.js b/seeders/20190122051334-base-user.js index 32167bd..6c8b8ff 100644 --- a/seeders/20190122051334-base-user.js +++ b/seeders/20190122051334-base-user.js @@ -4,7 +4,6 @@ const bcrypt = require('bcrypt-nodejs'); // DOESNT WORK AS NEED TO HASH PASSWORD TO DB module.exports = { up: async (queryInterface, Sequelize) => { - var hash = bcrypt.hashSync('test', null); return queryInterface.bulkInsert('donors', [ @@ -27,7 +26,7 @@ module.exports = { down: (queryInterface, Sequelize) => { return queryInterface.bulkDelete('donors', { - email: 'patel.arjun50@gmail.com' + email: 'patelarjun50@gmail.com' }); } }; diff --git a/seeders/20190122053544-base-orgs.js b/seeders/20190122053544-base-orgs.js index c507974..185d54a 100644 --- a/seeders/20190122053544-base-orgs.js +++ b/seeders/20190122053544-base-orgs.js @@ -1,7 +1,10 @@ 'use strict'; +const bcrypt = require('bcrypt-nodejs'); module.exports = { - up: (queryInterface, Sequelize) => { + up: async (queryInterface, Sequelize) => { + var hashedPass = await bcrypt.hashSync('test', null); + return queryInterface.bulkInsert('organizations', [ { name: 'Gopala Goshala', @@ -18,7 +21,7 @@ module.exports = { zip: '92604', primary_cause: 'Animal', - primary_region: 'North India', + primary_region: 'South Asia', ein: '63-6088665', estimate_asset_value: 200000, @@ -26,56 +29,82 @@ module.exports = { isNonprofit: true, - primary_contact_phone, - primary_contact_first_name, - primary_contact_last_name - }, - { - name, - email, - password: hashedPass, //hashed password - short_description, - primary_cause, - primary_region, - ein, - primary_contact_first_name, - primary_contact_last_name, - phone, - country, - address, - city, - zip, - estimate_assets, - estimate_year_operating_cost - }, - { - name, - email, - password: hashedPass, //hashed password - short_description, - primary_cause, - primary_region, - ein, - primary_contact_first_name, - primary_contact_last_name, - phone, - country, - address, - city, - zip, - estimate_assets, - estimate_year_operating_cost + primary_contact_phone: '9493873423', + primary_contact_first_name: 'Kamlesh', + primary_contact_last_name: 'Patel', + + created_at: new Date(), + updated_at: new Date() } + // , + // { + // name: 'The Pollination Project', + // short_description: + // 'Every day of the year, we give a grant to changemakers across the globe.', + + // primary_contact_email: 'ajay@gmail.com', + // password: hashedPass, + + // address: '2031 Carry Drive', + // city: 'San Francisco', + // country: 'USA', + // state: 'CA', + // zip: '92604', + + // primary_cause: 'Animal', + // primary_region: 'South Asia', + + // ein: '63-6088665', + // estimate_asset_value: 200000, + // estimate_yearly_operating_cost: 40000, + + // isNonprofit: true, + + // primary_contact_phone: '6254581254', + // primary_contact_first_name: 'Ajay', + // primary_contact_last_name: 'Datta', + + // created_at: new Date(), + // updated_at: new Date() + // }, + // { + // name: 'Cancer Global Perspective', + // short_description: + // 'Your tax-deductible donation funds lifesaving research, treatment and care — and would mean so much to someone fighting cancer. Please donate today.', + + // primary_contact_email: 'cancersupport@gmail.com', + // password: hashedPass, + + // address: '82912 Larry Page Drive', + // city: 'Holmes', + // country: 'USA', + // state: 'AZ', + // zip: '92604', + + // primary_cause: 'Animal', + // primary_region: 'South Asia', + + // ein: '63-6088665', + // estimate_asset_value: 200000, + // estimate_yearly_operating_cost: 40000, + + // isNonprofit: true, + + // primary_contact_phone: '6254581254', + // primary_contact_first_name: 'Leo', + // primary_contact_last_name: 'Rosen', + + // created_at: new Date(), + // updated_at: new Date() + // } ]); }, down: (queryInterface, Sequelize) => { - return queryInterface.bulkDelete('organizations', { - [Sequelize.Op.or]: [ - { name: 'Gopala Goshala' }, - { name: 'Water Organization India' }, - { name: "Pete's Animals" } - ] - }); + return queryInterface.bulkDelete('organizations', [ + { primary_contact_email: 'kcp1982@yahoo.com' }, + { primary_contact_email: 'ajay@gmail.com' }, + { primary_contact_email: 'cancersupport@gmail.com' } + ]); } }; diff --git a/seeders/20190122054316-base-grants.js b/seeders/20190122054316-base-grants.js deleted file mode 100644 index cec55c4..0000000 --- a/seeders/20190122054316-base-grants.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -module.exports = { - up: (queryInterface, Sequelize) => { - var sequelize = queryInterface.sequelize; - const causes = [1, 2], - regions = [1], - organizations = [1, 2, 3]; - - return Promise.all([ - sequelize.query( - `INSERT INTO grants (id, name, amount, monthly, last_payment, num_causes, num_regions, created_at, updated_at, donor_id) - VALUES (1, 'Max Impact in the South', 1000, 1, :last_payment, 2, 1, :created_at, :updated_at, (select id from donors where email='patel.arjun50@gmail.com'))`, - { - replacements: { - last_payment: new Date(), - created_at: new Date(), - updated_at: new Date() - }, - type: sequelize.QueryTypes.INSERT - } - ), - sequelize.query( - `INSERT INTO grants_causes (grant_id, cause_id, created_at, updated_at) - VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), - ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, - { - replacements: { - created_at: new Date(), - updated_at: new Date() - }, - type: sequelize.QueryTypes.INSERT - } - ), - sequelize.query( - `INSERT INTO grants_regions (grant_id, cause_id, created_at, updated_at) - VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), - ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, - { - replacements: { - created_at: new Date(), - updated_at: new Date() - }, - type: sequelize.QueryTypes.INSERT - } - ), - sequelize.query( - `INSERT INTO grants_causes (grant_id, cause_id, created_at, updated_at) - VALUES ((select id from grants where id=1), select id from causes where name='Animal Care', :created_at, :updated_at), - ((select id from grants where id=1), select id from causes where name='Water', :created_at, :updated_at)`, - { - replacements: { - created_at: new Date(), - updated_at: new Date() - }, - type: sequelize.QueryTypes.INSERT - } - ) - ]); - }, - - down: (queryInterface, Sequelize) => { - return queryInterface.bulkDelete('grants', null, {}); - } -}; diff --git a/server.js b/server.js index b1a2bc4..9442f45 100644 --- a/server.js +++ b/server.js @@ -30,7 +30,9 @@ app.use((req, res, next) => { db.sequelize .authenticate() .then(() => { - console.log('Connection has been established successfully: ' + process.env.NODE_ENV); + console.log( + 'Connection has been established successfully: ' + process.env.NODE_ENV + ); }) .catch(err => { console.error('Unable to connect to the database:', err); @@ -38,9 +40,18 @@ db.sequelize //force: true will drop the table if it already exists const drop_tables = process.env.DROP_TABLES || false; -db.sequelize.sync({ force: drop_tables }).then(() => { - console.log(`Drop and Resync with { force: ${drop_tables} }`); -}); +if (drop_tables) { + db.sequelize + .query('SET FOREIGN_KEY_CHECKS = 0', null, { raw: true }) + .then(function() { + db.sequelize.sync({ force: true }).then(function() { + console.log('Dropped all of the tables'); + }); + }); +} else + db.sequelize.sync({ force: false }).then(() => { + console.log(`Drop and Resync with { force: ${drop_tables} }`); + }); //main route for api; perhaps for api docs frontend app.get('/', function(req, res, next) { @@ -87,7 +98,7 @@ app.use((error, req, res, next) => { var server = app.listen(port, function() { var host = server.address(); var port = server.address().port; - + //server is successful console.log(`App listening at port: ${port}`); });