mirror of https://github.com/xfarrow/blink
create org
This commit is contained in:
parent
190313f1b1
commit
0593b0fb01
|
@ -21,9 +21,14 @@ const port = process.env.API_SERVER_PORT;
|
||||||
// Middleware which parses JSON for POST requests
|
// Middleware which parses JSON for POST requests
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
|
// Register a Person
|
||||||
app.post('/blinkapi/register', api_controller.register);
|
app.post('/blinkapi/register', api_controller.register);
|
||||||
|
// Login
|
||||||
app.post('/blinkapi/login', api_controller.login);
|
app.post('/blinkapi/login', api_controller.login);
|
||||||
|
// Obtain Person's details
|
||||||
app.get('/blinkapi/person/:id', api_controller.verifyToken, api_controller.person);
|
app.get('/blinkapi/person/:id', api_controller.verifyToken, api_controller.person);
|
||||||
|
// Create organization
|
||||||
|
app.post('/blinkapi/organization', api_controller.verifyToken, api_controller.organization);
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|
|
@ -124,6 +124,53 @@ async function person(req, res){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// POST
|
||||||
|
async function organization(req, res){
|
||||||
|
const organizationData = req.body;
|
||||||
|
|
||||||
|
// Ensure that the required fields are present before proceeding
|
||||||
|
if (!organizationData.name) {
|
||||||
|
return res.status(400).json("Invalid request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
// Begin transaction
|
||||||
|
await db.tx(async (t) => {
|
||||||
|
|
||||||
|
// Inserting in the "Organization" table
|
||||||
|
const OrganizationInsertQuery = `
|
||||||
|
INSERT INTO "Organization" (name, location, description, is_hiring)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
RETURNING *`;
|
||||||
|
|
||||||
|
const organizationResult = await t.one(OrganizationInsertQuery, [
|
||||||
|
organizationData.name,
|
||||||
|
organizationData.location,
|
||||||
|
organizationData.description,
|
||||||
|
organizationData.is_hiring
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Inserting in the "OrganizationAdministrator" table
|
||||||
|
const OrganizationAdministratorInsertQuery = `
|
||||||
|
INSERT INTO "OrganizationAdministrator" (id_person, id_organization)
|
||||||
|
VALUES ($1, $2)`;
|
||||||
|
|
||||||
|
await t.none(OrganizationAdministratorInsertQuery, [
|
||||||
|
req.jwt.person_id,
|
||||||
|
organizationResult.id
|
||||||
|
]);
|
||||||
|
|
||||||
|
return res.status(200).json({ "Organization" : organizationResult});
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error){
|
||||||
|
console.error('Error inserting data:', error);
|
||||||
|
res.status(500).json("Internal server error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ======== END API ENDPOINTS ========
|
// ======== END API ENDPOINTS ========
|
||||||
|
|
||||||
async function checkUserCredentials(email, password){
|
async function checkUserCredentials(email, password){
|
||||||
|
@ -178,5 +225,6 @@ module.exports = {
|
||||||
register,
|
register,
|
||||||
login,
|
login,
|
||||||
person,
|
person,
|
||||||
verifyToken
|
verifyToken,
|
||||||
|
organization
|
||||||
};
|
};
|
Loading…
Reference in New Issue