From 29be3b7658d7e9c1669a41dde60ee326195c56c6 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Tue, 17 Oct 2023 11:50:03 +0200 Subject: [PATCH] Delete User --- .../nodejs/BlinkApiUsageExample_Insomnia.json | 1 - backend/apis/nodejs/api.js | 1 + backend/apis/nodejs/api_controller.js | 19 +++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) delete mode 100644 backend/apis/nodejs/BlinkApiUsageExample_Insomnia.json diff --git a/backend/apis/nodejs/BlinkApiUsageExample_Insomnia.json b/backend/apis/nodejs/BlinkApiUsageExample_Insomnia.json deleted file mode 100644 index b300f02..0000000 --- a/backend/apis/nodejs/BlinkApiUsageExample_Insomnia.json +++ /dev/null @@ -1 +0,0 @@ -{"_type":"export","__export_format":4,"__export_date":"2023-10-16T15:40:32.266Z","__export_source":"insomnia.desktop.app:v2023.5.8","resources":[{"_id":"req_cc820f2f3c494733aae2a6d2b026d5d6","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697454087477,"created":1697439750530,"url":"localhost:3000/blinkapi/organization/post/3","name":"DeleteOrganizationPost","description":"","method":"DELETE","body":{"mimeType":"application/json","text":""},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_1cf0a9865fc54eb28b06b007dcfb87b6"},{"name":"User-Agent","value":"insomnia/2023.5.8","id":"pair_e116a62c90714a2fa936db757b86345a"},{"id":"pair_af07de8a9e584d74bf22de772a1a5116","name":"authorization","value":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEwLCJpYXQiOjE2OTc0NTI3NDksImV4cCI6MTY5NzQ1NjM0OX0.1Gx13rTRDVVZItkjVHs_RI4w3sxRXammkJpZcP1VXHE","description":""}],"authentication":{},"metaSortKey":-1697439750530,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_fa2918bf9164449ca7cefd4e3c526847","parentId":null,"modified":1696234728806,"created":1695805230952,"name":"Blink","description":"","scope":"collection","_type":"workspace"},{"_id":"req_28c016dc17ba4d8fb0a1960ec5bdd669","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697461331654,"created":1697443352055,"url":"localhost:3000/blinkapi/organization/post/","name":"CreateOrganizationPost","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"organization_id\" : \"2\",\n\t\"content\" : \"Welcome to Campany Inc. !\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_6e74726d8b9d4be883fb296363f27e4c"},{"name":"User-Agent","value":"insomnia/2023.5.8","id":"pair_e355a0ac56664aaa95d1fba431f09a9c"},{"id":"pair_639ce915c16a4e36b23c5eec89acb7c2","name":"authorization","value":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEwLCJpYXQiOjE2OTc0NTI3NDksImV4cCI6MTY5NzQ1NjM0OX0.1Gx13rTRDVVZItkjVHs_RI4w3sxRXammkJpZcP1VXHE","description":""}],"authentication":{},"metaSortKey":-1697439606468.5,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_881bbb6913074328b0474115cab0d51e","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697461315055,"created":1697439462407,"url":"localhost:3000/blinkapi/organization","name":"CreateOrganization","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"name\" : \"Company Inc.\",\n\t\"location\" : \"Los Angeles/USA\"\n}"},"parameters":[{"id":"pair_fbb851a173414199aabb5612a4afbe79","name":"","value":"","description":""}],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_e97d33e8311c469d9930459e52897eea"},{"name":"User-Agent","value":"insomnia/2023.5.8","id":"pair_d25e174d5f1f4268a348f03b3df16cb7"},{"id":"pair_1718cff2285c40649a4f026ffd5f66aa","name":"authorization","value":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEwLCJpYXQiOjE2OTc0NTI3NDksImV4cCI6MTY5NzQ1NjM0OX0.1Gx13rTRDVVZItkjVHs_RI4w3sxRXammkJpZcP1VXHE","description":""}],"authentication":{},"metaSortKey":-1697439462407,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_e9904f2548cf4032a788812ebc6b7831","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697452849182,"created":1697452790733,"url":"localhost:3000/blinkapi/person/4","name":"GetPerson","description":"","method":"GET","body":{},"parameters":[],"headers":[{"name":"User-Agent","value":"insomnia/2023.5.8","id":"pair_f979eeb78d5d484b87bb2142931bfe88"},{"id":"pair_572e04a4c0f042d0a985e337c1da3cd0","name":"authorization","value":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwZXJzb25faWQiOjEwLCJpYXQiOjE2OTc0NTI3NDksImV4cCI6MTY5NzQ1NjM0OX0.1Gx13rTRDVVZItkjVHs_RI4w3sxRXammkJpZcP1VXHE","description":""}],"authentication":{},"metaSortKey":-1697030905110.25,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_274e462fcfb24a6f8baf77851ab2f4b6","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697461225539,"created":1697439499759,"url":"localhost:3000/blinkapi/login","name":"Login","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"email\" : \"test@test.com\",\n\t\"password\" : \"test\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json"},{"name":"User-Agent","value":"insomnia/2023.5.8"}],"authentication":{},"metaSortKey":-1696622347813.5,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_015720b2942c45a38835ed62b02c5a74","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1697461210911,"created":1695805233220,"url":"localhost:3000/blinkapi/register","name":"Register","description":"","method":"POST","body":{"mimeType":"application/json","text":"{\n\t\"email\" : \"test@test.com\",\n\t\"password\" : \"test\",\n\t\"display_name\" : \"tester\",\n\t\"date_of_birth\" : \"19990706\",\n\t\"available\" : \"true\",\n\t\"place_of_living\" : \"Napoli/ITA\"\n}"},"parameters":[],"headers":[{"name":"Content-Type","value":"application/json","id":"pair_4995c469a16542358ad1b61eb49793f1"},{"name":"User-Agent","value":"insomnia/2023.5.8","id":"pair_85a8fc0b1f9540cf9b89da7b801ec746"}],"authentication":{"type":"apikey","disabled":false,"key":"234","value":"qwded","addTo":"header"},"metaSortKey":-1695805233220,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_bf59e7388e37161461a551db2f40503095407bc3","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1695805230956,"created":1695805230956,"name":"Base Environment","data":{},"dataPropertyOrder":null,"color":null,"isPrivate":false,"metaSortKey":1695805230956,"_type":"environment"},{"_id":"jar_bf59e7388e37161461a551db2f40503095407bc3","parentId":"wrk_fa2918bf9164449ca7cefd4e3c526847","modified":1695805230957,"created":1695805230957,"name":"Default Jar","cookies":[],"_type":"cookie_jar"}]} \ No newline at end of file diff --git a/backend/apis/nodejs/api.js b/backend/apis/nodejs/api.js index 9548a0c..42e1fba 100644 --- a/backend/apis/nodejs/api.js +++ b/backend/apis/nodejs/api.js @@ -20,6 +20,7 @@ app.use(express.json()); // Middleware which parses JSON for POST requests app.post('/blinkapi/register', api_controller.registerPerson); // Register a Person app.post('/blinkapi/login', api_controller.login); // Login app.get('/blinkapi/person/:id', api_controller.verifyToken, api_controller.getPerson); // Obtain Person's details +app.delete('/blinkapi/person/:id', api_controller.verifyToken, api_controller.deletePerson); // Delete a Person app.post('/blinkapi/organization', api_controller.verifyToken, api_controller.createOrganization); // Create 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 diff --git a/backend/apis/nodejs/api_controller.js b/backend/apis/nodejs/api_controller.js index 96a3e73..ff2d6f8 100644 --- a/backend/apis/nodejs/api_controller.js +++ b/backend/apis/nodejs/api_controller.js @@ -117,6 +117,24 @@ async function getPerson(req, res){ } } +// GET +async function deletePerson(req, res){ + if(req.params.id != req.jwt.person_id){ + return res.status(403).json({error: "Forbidden"}); + } + + try { + await knex('Person') + .where({id : req.jwt.person_id}) + .del(); + return res.status(200).json({success: true}); + } catch (error) { + console.log("Error deleting a Person: " + error); + return res.status(500).json({error : "Internal server error"}); + } + +} + // POST async function createOrganization(req, res){ @@ -318,6 +336,7 @@ module.exports = { registerPerson, login, getPerson, + deletePerson, verifyToken, createOrganization, deleteOrganization,