mirror of
https://github.com/xfarrow/blink
synced 2025-04-16 17:07:48 +02:00
update create organization
This commit is contained in:
parent
9ec2adc622
commit
3f7727f7d5
@ -31,10 +31,11 @@ const jwt = require('jsonwebtoken');
|
|||||||
/**
|
/**
|
||||||
* POST Request
|
* POST Request
|
||||||
*
|
*
|
||||||
* Register a Person
|
* Registers a Person
|
||||||
*
|
*
|
||||||
* @returns The activationLink identifier if
|
* Required field(s): name, email (valid ISO standard), password
|
||||||
* the registration is successful
|
*
|
||||||
|
* @returns The activationlink identifier
|
||||||
*/
|
*/
|
||||||
async function registerPerson(req, res){
|
async function registerPerson(req, res){
|
||||||
|
|
||||||
@ -92,7 +93,9 @@ async function registerPerson(req, res){
|
|||||||
* POST Request
|
* POST Request
|
||||||
*
|
*
|
||||||
* Creates a token if the specified email
|
* Creates a token if the specified email
|
||||||
* and password are valid
|
* and password are valid.
|
||||||
|
*
|
||||||
|
* Required field(s): email, password
|
||||||
*
|
*
|
||||||
* @returns The token
|
* @returns The token
|
||||||
*/
|
*/
|
||||||
@ -116,8 +119,10 @@ async function login(req, res){
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtain a Person's details if the
|
* Obtain a Person's details if the
|
||||||
* specified person is myself or an
|
* Person to retrieve is either myself or an
|
||||||
* enabled Person
|
* enabled Person.
|
||||||
|
*
|
||||||
|
* Required field(s): none
|
||||||
*
|
*
|
||||||
* @returns The Person
|
* @returns The Person
|
||||||
*/
|
*/
|
||||||
@ -129,7 +134,7 @@ async function getPerson(req, res){
|
|||||||
.first();
|
.first();
|
||||||
|
|
||||||
if(user){
|
if(user){
|
||||||
// I am retrieving myself or an enabled user
|
// I am retrieving either myself or an enabled user
|
||||||
if(user.id == req.jwt.person_id || user.enabled){
|
if(user.id == req.jwt.person_id || user.enabled){
|
||||||
delete user['password']; // remove password field for security reasons
|
delete user['password']; // remove password field for security reasons
|
||||||
return res.status(200).send(user);
|
return res.status(200).send(user);
|
||||||
@ -149,7 +154,10 @@ async function getPerson(req, res){
|
|||||||
* Updates a Person's details. If some details are
|
* Updates a Person's details. If some details are
|
||||||
* not present, they shall be ignored.
|
* not present, they shall be ignored.
|
||||||
* To update the password, both the old_password
|
* To update the password, both the old_password
|
||||||
* and new_password field must be specified.
|
* and new_password fields must be specified.
|
||||||
|
*
|
||||||
|
* Required field(s): none. Both old_password and
|
||||||
|
* new_password if updating the password
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async function updatePerson(req, res){
|
async function updatePerson(req, res){
|
||||||
@ -215,9 +223,11 @@ async function updatePerson(req, res){
|
|||||||
/**
|
/**
|
||||||
* GET Request
|
* GET Request
|
||||||
*
|
*
|
||||||
* Deletes a User. An user can only delete
|
* Deletes a Person. An user can only delete
|
||||||
* themselves.
|
* themselves.
|
||||||
*
|
*
|
||||||
|
* Required field(s): none
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
async function deletePerson(req, res) {
|
async function deletePerson(req, res) {
|
||||||
try {
|
try {
|
||||||
@ -232,7 +242,15 @@ async function deletePerson(req, res) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST
|
/**
|
||||||
|
* POST Request
|
||||||
|
*
|
||||||
|
* Creates an Organization and its Administrator.
|
||||||
|
*
|
||||||
|
* Required field(s): name
|
||||||
|
*
|
||||||
|
* @returns the inserted organization
|
||||||
|
*/
|
||||||
async function createOrganization(req, res){
|
async function createOrganization(req, res){
|
||||||
|
|
||||||
// Ensure that the required fields are present before proceeding
|
// Ensure that the required fields are present before proceeding
|
||||||
@ -241,7 +259,7 @@ async function createOrganization(req, res){
|
|||||||
}
|
}
|
||||||
|
|
||||||
try{
|
try{
|
||||||
await knex.transaction(async (trx) => {
|
const insertedOrganization = await knex.transaction(async (trx) => {
|
||||||
|
|
||||||
// We have to insert either both in Organization and in OrganizationAdministrator
|
// We have to insert either both in Organization and in OrganizationAdministrator
|
||||||
// or in neither
|
// or in neither
|
||||||
@ -251,8 +269,7 @@ async function createOrganization(req, res){
|
|||||||
location: req.body.location,
|
location: req.body.location,
|
||||||
description: req.body.description,
|
description: req.body.description,
|
||||||
is_hiring: req.body.is_hiring,
|
is_hiring: req.body.is_hiring,
|
||||||
})
|
}, '*');
|
||||||
.returning('*');
|
|
||||||
|
|
||||||
// Inserting in the "OrganizationAdministrator" table
|
// Inserting in the "OrganizationAdministrator" table
|
||||||
await trx('OrganizationAdministrator')
|
await trx('OrganizationAdministrator')
|
||||||
@ -260,11 +277,9 @@ async function createOrganization(req, res){
|
|||||||
id_person: req.jwt.person_id,
|
id_person: req.jwt.person_id,
|
||||||
id_organization: organizationResult[0].id,
|
id_organization: organizationResult[0].id,
|
||||||
});
|
});
|
||||||
|
return organizationResult[0];
|
||||||
await trx.commit();
|
|
||||||
|
|
||||||
return res.status(200).json({ Organization: organizationResult[0] });
|
|
||||||
});
|
});
|
||||||
|
return res.status(200).json({ Organization: insertedOrganization });
|
||||||
}
|
}
|
||||||
catch (error){
|
catch (error){
|
||||||
console.error('Error creating Organization:', error);
|
console.error('Error creating Organization:', error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user