diff --git a/niceday-api/api/openapi.yaml b/niceday-api/api/openapi.yaml index d9de716..e7088a7 100644 --- a/niceday-api/api/openapi.yaml +++ b/niceday-api/api/openapi.yaml @@ -140,7 +140,31 @@ paths: responses: "200": description: Success + "500": + description: Failed x-swagger-router-controller: ConnectionRequests + /removecontact: + post: + summary: Remove the contact of the user specified in the body + operationId: removeContact + requestBody: + content: + application/json: + schema: + required: + - user_id + type: object + properties: + user_id: + type: string + description: ID of the user to be removed + example: "38527" + responses: + "200": + description: Success + "500": + description: Failed + x-swagger-router-controller: RemoveContact /usertrackers/statuses: post: summary: Enable or disable trackers for a user diff --git a/niceday-api/controllers/RemoveContact.js b/niceday-api/controllers/RemoveContact.js new file mode 100644 index 0000000..7a08ddd --- /dev/null +++ b/niceday-api/controllers/RemoveContact.js @@ -0,0 +1,13 @@ +const utils = require('../utils/writer.jsx'); +const RemoveContact = require('../service/RemoveContactService'); + +// eslint-disable-next-line no-unused-vars +module.exports.removeContact = function removeContact(req, res, next, body) { + RemoveContact.removeContact(req, body) + .then((response) => { + utils.writeJson(res, response); + }) + .catch((response) => { + utils.writeJson(res, utils.respondWithCode(500, response)); + }); +}; diff --git a/niceday-api/index.test.js b/niceday-api/index.test.js index 4469ba8..400d558 100644 --- a/niceday-api/index.test.js +++ b/niceday-api/index.test.js @@ -27,7 +27,7 @@ const MOCK_PENDING_REQUESTS = [ invitationId: MOCK_REQUEST_ID, }, ]; -const MOCK_ACCEPT_REQUESTS = {}; +const MOCK_EMPTY_RESPONSE = {}; // Contains all tests which require a mocked Senseserver describe('Tests on niceday-api server using mocked goalie-js', () => { @@ -74,7 +74,10 @@ describe('Tests on niceday-api server using mocked goalie-js', () => { resolve(MOCK_PENDING_REQUESTS); }), acceptInvitation: () => new Promise((resolve) => { - resolve(MOCK_ACCEPT_REQUESTS); + resolve(MOCK_EMPTY_RESPONSE); + }), + removeContactFromUserContact: () => new Promise((resolve) => { + resolve(MOCK_EMPTY_RESPONSE); }), })), Authentication: jest.fn().mockImplementation(() => ({ @@ -255,7 +258,30 @@ describe('Tests on niceday-api server using mocked goalie-js', () => { }) .then((response) => response.json()) .then((responseBody) => { - expect(responseBody).toEqual(MOCK_ACCEPT_REQUESTS); + expect(responseBody).toEqual(MOCK_EMPTY_RESPONSE); + }) + .catch((error) => { + throw new Error(`Error during fetch: ${error}`); + }); + }); + + it('Test remove contact /removecontact endpoint', () => { + /* + Sends a POST to the /acceptconnection endpoint. + */ + + const urlreq = `http://localhost:${NICEDAY_TEST_SERVERPORT}/removecontact`; + const data = JSON.stringify({ + user_id: NICEDAY_TEST_USER_ID.toString(), + }); + return fetch(urlreq, { + method: 'post', + headers: { 'Content-Type': 'application/json' }, + body: data, + }) + .then((response) => response.json()) + .then((responseBody) => { + expect(responseBody).toEqual(MOCK_EMPTY_RESPONSE); }) .catch((error) => { throw new Error(`Error during fetch: ${error}`); diff --git a/niceday-api/service/RemoveContactService.js b/niceday-api/service/RemoveContactService.js new file mode 100644 index 0000000..be8e178 --- /dev/null +++ b/niceday-api/service/RemoveContactService.js @@ -0,0 +1,30 @@ +const { Contacts, SenseServer } = require('@sense-os/goalie-js'); +require('isomorphic-fetch'); + +const { ENVIRONMENT } = process.env; +let selectedServer; + +if (ENVIRONMENT === 'dev') { + selectedServer = SenseServer.Alpha; +} else { + selectedServer = SenseServer.Production; +} + +const contacts = new Contacts(selectedServer); + +/** + * Get the pending connection requests. + * Returns the JSON as received from the SenseServer. + * @param req - The node.js express request object + * @param body - The node.js express body object. + * */ +exports.removeContact = (req, body) => new Promise((resolve, reject) => { + console.log(req.app.get('token'), req.app.get('therapistId'), body.user_id); + contacts.removeContactFromUserContact(req.app.get('token'), req.app.get('therapistId'), body.user_id) + .then((result) => { + resolve(result); + }) + .catch((error) => { + reject(Error(`Error during contact temoval: ${error}`)); + }); +});