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' });
|
||||
|
||||
Grants.belongsToMany(Causes, {
|
||||
as: 'GrantsCauses',
|
||||
through: 'grants_causes',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'cause_id'
|
||||
});
|
||||
|
||||
Grants.belongsToMany(Regions, {
|
||||
as: 'GrantsRegions',
|
||||
through: 'grants_regions',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'region_id'
|
||||
});
|
||||
|
||||
Grants.belongsToMany(Organizations, {
|
||||
as: 'GrantsOrganizations',
|
||||
through: 'grants_organizations',
|
||||
foreignKey: 'grant_id',
|
||||
otherKey: 'organization_id'
|
||||
|
||||
@@ -38,7 +38,7 @@ exports.create = (req, res, next) => {
|
||||
.then(donor => {
|
||||
// Send created donor to client
|
||||
return res.status(201).json({
|
||||
message: 'User created',
|
||||
message: 'Donor created',
|
||||
donor
|
||||
});
|
||||
})
|
||||
|
||||
@@ -6,94 +6,61 @@ const { Grants, Causes, Regions, Organizations } = db;
|
||||
// Create a grant for certain donor
|
||||
exports.create = (req, res, next) => {
|
||||
const donor_id = req.params.donor_id;
|
||||
const {
|
||||
name,
|
||||
amount,
|
||||
monthly,
|
||||
causes_ids,
|
||||
regions_ids,
|
||||
organizations_ids
|
||||
} = req.body;
|
||||
const { name, amount, monthly, causes, regions, organizations } = req.body;
|
||||
|
||||
Grants.create({
|
||||
donor_id,
|
||||
name,
|
||||
amount,
|
||||
monthly,
|
||||
num_causes: causes_ids.length,
|
||||
num_regions: regions_ids.length
|
||||
num_causes: causes.length,
|
||||
num_regions: regions.length
|
||||
})
|
||||
.then(grant => {
|
||||
const grants_causes_rows = causes_ids.map(cause_id => {
|
||||
return { cause_id, grant_id: grant.id };
|
||||
return Promise.all([
|
||||
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));
|
||||
};
|
||||
|
||||
// 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) => {
|
||||
Grants.findAll({
|
||||
where: {
|
||||
donor_id: req.params.donor_id
|
||||
}
|
||||
}).then(grants => {
|
||||
Promise.all(
|
||||
grants.map(grant => {
|
||||
const causes = grant.getCauses().then(causes => causes),
|
||||
regions = grant.getRegions().then(regions => regions),
|
||||
organizations = grant
|
||||
.getOrganizations()
|
||||
.then(organizations => organizations);
|
||||
return Promise.all([causes, regions, organizations]).then(data => {
|
||||
return {
|
||||
grant,
|
||||
causes: data[0],
|
||||
regions: data[1],
|
||||
organizations: data[2]
|
||||
};
|
||||
});
|
||||
})
|
||||
)
|
||||
.then(list_grants => {
|
||||
res.status(200).json({
|
||||
grants: list_grants,
|
||||
number_grants: list_grants.length
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
});
|
||||
},
|
||||
include: [Causes, Regions, Organizations]
|
||||
})
|
||||
.then(grants => {
|
||||
res.status(200).json({
|
||||
grants,
|
||||
number_grants: grants.length
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
exports.delete = (req, res, next) => {
|
||||
const grant_id = req.params.grant_id;
|
||||
|
||||
Grants.destroy({ where: { id: grant_id } })
|
||||
.then(result => {
|
||||
var message = 'Already Deleted';
|
||||
if (result) {
|
||||
message = 'Grant Deleted';
|
||||
}
|
||||
res.status(201).json({
|
||||
message,
|
||||
result
|
||||
});
|
||||
})
|
||||
.catch(error => next(error));
|
||||
};
|
||||
|
||||
@@ -21,6 +21,9 @@ router.get('/grants/:donor_id', checkAuth, grants.findByDonorId);
|
||||
* */
|
||||
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
|
||||
// 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",
|
||||
"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": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/bcrypt-nodejs/-/bcrypt-nodejs-0.0.3.tgz",
|
||||
@@ -416,6 +424,18 @@
|
||||
"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": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
@@ -474,6 +494,11 @@
|
||||
"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": {
|
||||
"version": "1.3.2",
|
||||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
"body-parser": "^1.18.3",
|
||||
"express": "^4.16.4",
|
||||
"jsonwebtoken": "^8.4.0",
|
||||
"morgan": "^1.9.1",
|
||||
"mysql2": "^1.6.4",
|
||||
"sequelize": "^4.42.0"
|
||||
},
|
||||
|
||||
@@ -3,9 +3,12 @@ const express = require('express'),
|
||||
app = express(),
|
||||
bodyParser = require('body-parser'),
|
||||
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.urlencoded({ extended: true }));
|
||||
|
||||
//handle CORS errors
|
||||
app.use((req, res, next) => {
|
||||
|
||||
Reference in New Issue
Block a user