From 0593b0fb01528d5cd7776b3e315af9751cc4a8b3 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Fri, 13 Oct 2023 11:47:53 +0200 Subject: [PATCH] create org --- backend/apis/nodejs/api.js | 5 +++ backend/apis/nodejs/api_controller.js | 50 ++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/backend/apis/nodejs/api.js b/backend/apis/nodejs/api.js index 1493a1e..f9b6b3f 100644 --- a/backend/apis/nodejs/api.js +++ b/backend/apis/nodejs/api.js @@ -21,9 +21,14 @@ const port = process.env.API_SERVER_PORT; // Middleware which parses JSON for POST requests app.use(express.json()); +// Register a Person app.post('/blinkapi/register', api_controller.register); +// Login app.post('/blinkapi/login', api_controller.login); +// Obtain Person's details 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 app.listen(port, () => { diff --git a/backend/apis/nodejs/api_controller.js b/backend/apis/nodejs/api_controller.js index 89c38ff..4b82058 100644 --- a/backend/apis/nodejs/api_controller.js +++ b/backend/apis/nodejs/api_controller.js @@ -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 ======== async function checkUserCredentials(email, password){ @@ -178,5 +225,6 @@ module.exports = { register, login, person, - verifyToken + verifyToken, + organization }; \ No newline at end of file