update organization

This commit is contained in:
xfarrow 2023-10-19 15:53:45 +02:00
parent bb1fbfa1f2
commit 9c880af4d8
2 changed files with 43 additions and 1 deletions

View File

@ -27,6 +27,7 @@ app.put('/blinkapi/person/:id', api_controller.verifyToken, api_controller.updat
app.delete('/blinkapi/person/delete', api_controller.verifyToken, api_controller.deletePerson); // Delete a Person app.delete('/blinkapi/person/delete', api_controller.verifyToken, api_controller.deletePerson); // Delete a Person
app.post('/blinkapi/organization', api_controller.verifyToken, api_controller.createOrganization); // Create organization app.post('/blinkapi/organization', api_controller.verifyToken, api_controller.createOrganization); // Create organization
app.get('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.getOrganization); // Get Organization data app.get('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.getOrganization); // Get Organization data
app.put('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.updateOrganization); // Update organization
app.delete('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.deleteOrganization); // Delete organization app.delete('/blinkapi/organization/:id', api_controller.verifyToken, api_controller.deleteOrganization); // Delete organization
app.post('/blinkapi/organization/post', api_controller.verifyToken, api_controller.createOrganizationPost); // Create a organization's post app.post('/blinkapi/organization/post', api_controller.verifyToken, api_controller.createOrganizationPost); // Create a organization's post
app.delete('/blinkapi/organization/post/:id', api_controller.verifyToken, api_controller.deleteOrganizationPost); // Delete a organization's post app.delete('/blinkapi/organization/post/:id', api_controller.verifyToken, api_controller.deleteOrganizationPost); // Delete a organization's post

View File

@ -128,6 +128,10 @@ async function updatePerson(req, res){
return res.status(403).json({ error : "Forbidden"}); return res.status(403).json({ error : "Forbidden"});
} }
if(!req.body.display_name || req.body.display_name.trim().length === 0){
return res.status(400).json({ error : "Invalid request"});
}
try { try {
await knex('Person') await knex('Person')
.where('id', req.params.id) .where('id', req.params.id)
@ -168,7 +172,7 @@ async function createOrganization(req, res){
} }
try{ try{
knex.transaction(async (trx) => { await knex.transaction(async (trx) => {
const organizationResult = await trx('Organization') const organizationResult = await trx('Organization')
.insert({ .insert({
name: req.body.name, name: req.body.name,
@ -196,6 +200,42 @@ async function createOrganization(req, res){
} }
} }
// PUT
async function updateOrganization(req, res){
if(!req.body.name || req.body.name.trim().length === 0){
return res.status(400).json({ error : "Invalid request"});
}
try {
await knex.transaction(async (trx) => {
// Check if the current user is a organization's administrator
const isOrganizationAdmin = await trx('OrganizationAdministrator')
.where('id_person', req.jwt.person_id)
.where('id_organization', req.params.id)
.select('*')
.first();
if(!isOrganizationAdmin){
return res.status(403).json({error : "Forbidden"});
}
await knex('Organization')
.where('id', req.params.id)
.update({
name: req.body.name,
location: req.body.location,
description: req.body.description,
is_hiring: req.body.is_hiring
});
return res.status(200).json({ success : "true"});
});
}
catch (error) {
console.log(error);
return res.status(500).json({error : "Internal server error"});
}
}
// DELETE // DELETE
async function deleteOrganization(req, res){ async function deleteOrganization(req, res){
const organizationIdToDelete = req.params.id; const organizationIdToDelete = req.params.id;
@ -430,6 +470,7 @@ module.exports = {
verifyToken, verifyToken,
createOrganization, createOrganization,
getOrganization, getOrganization,
updateOrganization,
deleteOrganization, deleteOrganization,
createOrganizationPost, createOrganizationPost,
deleteOrganizationPost, deleteOrganizationPost,