working create grants, getall, and delete with clean methods
This commit is contained in:
@@ -4,21 +4,18 @@ module.exports = models => {
|
|||||||
Donors.hasMany(Grants, { foreignKey: 'donor_id' });
|
Donors.hasMany(Grants, { foreignKey: 'donor_id' });
|
||||||
|
|
||||||
Grants.belongsToMany(Causes, {
|
Grants.belongsToMany(Causes, {
|
||||||
as: 'GrantsCauses',
|
|
||||||
through: 'grants_causes',
|
through: 'grants_causes',
|
||||||
foreignKey: 'grant_id',
|
foreignKey: 'grant_id',
|
||||||
otherKey: 'cause_id'
|
otherKey: 'cause_id'
|
||||||
});
|
});
|
||||||
|
|
||||||
Grants.belongsToMany(Regions, {
|
Grants.belongsToMany(Regions, {
|
||||||
as: 'GrantsRegions',
|
|
||||||
through: 'grants_regions',
|
through: 'grants_regions',
|
||||||
foreignKey: 'grant_id',
|
foreignKey: 'grant_id',
|
||||||
otherKey: 'region_id'
|
otherKey: 'region_id'
|
||||||
});
|
});
|
||||||
|
|
||||||
Grants.belongsToMany(Organizations, {
|
Grants.belongsToMany(Organizations, {
|
||||||
as: 'GrantsOrganizations',
|
|
||||||
through: 'grants_organizations',
|
through: 'grants_organizations',
|
||||||
foreignKey: 'grant_id',
|
foreignKey: 'grant_id',
|
||||||
otherKey: 'organization_id'
|
otherKey: 'organization_id'
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ exports.create = (req, res, next) => {
|
|||||||
.then(donor => {
|
.then(donor => {
|
||||||
// Send created donor to client
|
// Send created donor to client
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
message: 'User created',
|
message: 'Donor created',
|
||||||
donor
|
donor
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,94 +6,61 @@ const { Grants, Causes, Regions, Organizations } = db;
|
|||||||
// Create a grant for certain donor
|
// Create a grant for certain donor
|
||||||
exports.create = (req, res, next) => {
|
exports.create = (req, res, next) => {
|
||||||
const donor_id = req.params.donor_id;
|
const donor_id = req.params.donor_id;
|
||||||
const {
|
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
||||||
name,
|
|
||||||
amount,
|
|
||||||
monthly,
|
|
||||||
causes_ids,
|
|
||||||
regions_ids,
|
|
||||||
organizations_ids
|
|
||||||
} = req.body;
|
|
||||||
|
|
||||||
Grants.create({
|
Grants.create({
|
||||||
donor_id,
|
donor_id,
|
||||||
name,
|
name,
|
||||||
amount,
|
amount,
|
||||||
monthly,
|
monthly,
|
||||||
num_causes: causes_ids.length,
|
num_causes: causes.length,
|
||||||
num_regions: regions_ids.length
|
num_regions: regions.length
|
||||||
})
|
})
|
||||||
.then(grant => {
|
.then(grant => {
|
||||||
const grants_causes_rows = causes_ids.map(cause_id => {
|
return Promise.all([
|
||||||
return { cause_id, grant_id: grant.id };
|
grant.addCauses(causes),
|
||||||
|
grant.addRegions(regions),
|
||||||
|
grant.addOrganizations(organizations)
|
||||||
|
]).then(result => result);
|
||||||
|
})
|
||||||
|
.then(result => {
|
||||||
|
res.status(201).json({
|
||||||
|
message: 'Grant Created'
|
||||||
});
|
});
|
||||||
const grants_regions_rows = regions_ids.map(region_id => {
|
|
||||||
return { region_id, grant_id: grant.id };
|
|
||||||
});
|
|
||||||
|
|
||||||
const grants_organizations_rows = organizations_ids.map(
|
|
||||||
organization_id => {
|
|
||||||
return { organization_id, grant_id: grant.id };
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Promise.all([
|
|
||||||
grants_causes_rows,
|
|
||||||
grants_regions_rows,
|
|
||||||
grants_organizations_rows
|
|
||||||
])
|
|
||||||
.then(results => {
|
|
||||||
Causes.bulkCreate(results[0], { raw: true }).then(causes => {
|
|
||||||
grant.addCauses(causes);
|
|
||||||
// console.log(causes);
|
|
||||||
});
|
|
||||||
console.log(results[1]);
|
|
||||||
Regions.bulkCreate(results[1], { raw: true }).then(regions => {
|
|
||||||
grant.addCauses(regions);
|
|
||||||
// console.log(regions);
|
|
||||||
});
|
|
||||||
Organizations.bulkCreate(results[2], { raw: true }).then(
|
|
||||||
organizations => {
|
|
||||||
grant.addCauses(organizations);
|
|
||||||
// console.log(organizations);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.catch(error => next(error));
|
|
||||||
})
|
})
|
||||||
.catch(error => next(error));
|
.catch(error => next(error));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Find grants with cause and region and charity details by donor_id
|
// Find grants with causes, regions, and organizations by donor_id
|
||||||
exports.findByDonorId = (req, res, next) => {
|
exports.findByDonorId = (req, res, next) => {
|
||||||
Grants.findAll({
|
Grants.findAll({
|
||||||
where: {
|
where: {
|
||||||
donor_id: req.params.donor_id
|
donor_id: req.params.donor_id
|
||||||
}
|
},
|
||||||
}).then(grants => {
|
include: [Causes, Regions, Organizations]
|
||||||
Promise.all(
|
})
|
||||||
grants.map(grant => {
|
.then(grants => {
|
||||||
const causes = grant.getCauses().then(causes => causes),
|
res.status(200).json({
|
||||||
regions = grant.getRegions().then(regions => regions),
|
grants,
|
||||||
organizations = grant
|
number_grants: grants.length
|
||||||
.getOrganizations()
|
});
|
||||||
.then(organizations => organizations);
|
})
|
||||||
return Promise.all([causes, regions, organizations]).then(data => {
|
.catch(error => next(error));
|
||||||
return {
|
};
|
||||||
grant,
|
|
||||||
causes: data[0],
|
exports.delete = (req, res, next) => {
|
||||||
regions: data[1],
|
const grant_id = req.params.grant_id;
|
||||||
organizations: data[2]
|
|
||||||
};
|
Grants.destroy({ where: { id: grant_id } })
|
||||||
});
|
.then(result => {
|
||||||
})
|
var message = 'Already Deleted';
|
||||||
)
|
if (result) {
|
||||||
.then(list_grants => {
|
message = 'Grant Deleted';
|
||||||
res.status(200).json({
|
}
|
||||||
grants: list_grants,
|
res.status(201).json({
|
||||||
number_grants: list_grants.length
|
message,
|
||||||
});
|
result
|
||||||
})
|
});
|
||||||
.catch(error => next(error));
|
})
|
||||||
});
|
.catch(error => next(error));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
|
|||||||
* */
|
* */
|
||||||
router.post('/grants/:donor_id', checkAuth, grants.create);
|
router.post('/grants/:donor_id', checkAuth, grants.create);
|
||||||
|
|
||||||
|
// Delete a grant of a donor
|
||||||
|
router.delete('/grants/:grant_id', checkAuth, grants.delete);
|
||||||
|
|
||||||
// // Retrieve a single Donor by Id
|
// // Retrieve a single Donor by Id
|
||||||
// router.get('/:donor_id', donors.findById);
|
// router.get('/:donor_id', donors.findById);
|
||||||
|
|
||||||
|
|||||||
Generated
+25
@@ -28,6 +28,14 @@
|
|||||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||||
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
|
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
|
||||||
},
|
},
|
||||||
|
"basic-auth": {
|
||||||
|
"version": "2.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
|
||||||
|
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
|
||||||
|
"requires": {
|
||||||
|
"safe-buffer": "5.1.2"
|
||||||
|
}
|
||||||
|
},
|
||||||
"bcrypt-nodejs": {
|
"bcrypt-nodejs": {
|
||||||
"version": "0.0.3",
|
"version": "0.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/bcrypt-nodejs/-/bcrypt-nodejs-0.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/bcrypt-nodejs/-/bcrypt-nodejs-0.0.3.tgz",
|
||||||
@@ -416,6 +424,18 @@
|
|||||||
"moment": ">= 2.9.0"
|
"moment": ">= 2.9.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"morgan": {
|
||||||
|
"version": "1.9.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.9.1.tgz",
|
||||||
|
"integrity": "sha512-HQStPIV4y3afTiCYVxirakhlCfGkI161c76kKFca7Fk1JusM//Qeo1ej2XaMniiNeaZklMVrh3vTtIzpzwbpmA==",
|
||||||
|
"requires": {
|
||||||
|
"basic-auth": "~2.0.0",
|
||||||
|
"debug": "2.6.9",
|
||||||
|
"depd": "~1.1.2",
|
||||||
|
"on-finished": "~2.3.0",
|
||||||
|
"on-headers": "~1.0.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"ms": {
|
"ms": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
@@ -474,6 +494,11 @@
|
|||||||
"ee-first": "1.1.1"
|
"ee-first": "1.1.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"on-headers": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz",
|
||||||
|
"integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c="
|
||||||
|
},
|
||||||
"parseurl": {
|
"parseurl": {
|
||||||
"version": "1.3.2",
|
"version": "1.3.2",
|
||||||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
|
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.18.3",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"jsonwebtoken": "^8.4.0",
|
"jsonwebtoken": "^8.4.0",
|
||||||
|
"morgan": "^1.9.1",
|
||||||
"mysql2": "^1.6.4",
|
"mysql2": "^1.6.4",
|
||||||
"sequelize": "^4.42.0"
|
"sequelize": "^4.42.0"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,9 +3,12 @@ const express = require('express'),
|
|||||||
app = express(),
|
app = express(),
|
||||||
bodyParser = require('body-parser'),
|
bodyParser = require('body-parser'),
|
||||||
port = process.env.PORT,
|
port = process.env.PORT,
|
||||||
db = require('./app/config/db.config.js');
|
db = require('./app/config/db.config.js'),
|
||||||
|
morgan = require('morgan');
|
||||||
|
|
||||||
|
app.use(morgan('dev'));
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
||||||
//handle CORS errors
|
//handle CORS errors
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user