Skip to content

Commit

Permalink
Merge pull request #18 from PerfectFit-project/354-disconnect-user
Browse files Browse the repository at this point in the history
new endpoint for disconnecting user
  • Loading branch information
wbaccinelli authored Jul 14, 2023
2 parents 0bd41ec + 16ea922 commit 0363880
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
24 changes: 24 additions & 0 deletions niceday-api/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions niceday-api/controllers/RemoveContact.js
Original file line number Diff line number Diff line change
@@ -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));
});
};
32 changes: 29 additions & 3 deletions niceday-api/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(() => ({
Expand Down Expand Up @@ -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}`);
Expand Down
30 changes: 30 additions & 0 deletions niceday-api/service/RemoveContactService.js
Original file line number Diff line number Diff line change
@@ -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}`));
});
});

0 comments on commit 0363880

Please sign in to comment.