Skip to content

Commit

Permalink
Back-end:
Browse files Browse the repository at this point in the history
- creating a new group is now using the best match algorithm.
- updated tests
issue: #528 #537
  • Loading branch information
alonttal committed Jun 9, 2018
1 parent 2022a1e commit 04d51c2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"supertest": "^3.1.0"
},
"scripts": {
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=html --reporter=text _mocha --recursive tests/server/models/apartment.test.js --exit",
"test": "export NODE_ENV=test || SET \"NODE_ENV=test\" && nyc --reporter=html --reporter=text _mocha --recursive tests/server/server.test.js --exit",
"start": "node server/server.js",
"coverage": "nyc report --reporter=lcov > coverage.lcov",
"view": "node views/build/build.js && node server/server.js"
Expand Down
3 changes: 0 additions & 3 deletions server/models/apartment.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,6 @@ ApartmentSchema.methods.addInterestedUser = function (_interestedID) {
const apartment = this;

apartment._interested.push(_interestedID);
if (apartment.isTimeToOpenGroup()) {
apartment.createGroup(_interestedID);
}
};

/**
Expand Down
23 changes: 18 additions & 5 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1398,9 +1398,11 @@ app.get('/apartments/:id/groups', async (req, res) => {
*
* Create a new group.
* The request should include:
* - String id (which should be a valid ObjectId).
* - String id (which should be a valid ObjectId) - in this case the route will try to
* build the best group for the id.
* or
* - list of String ids (which should be valid ObjectId's)
* - list of String ids (which should be valid ObjectId's) - in this case the route will
* try to build a group based on the ids in the list
* The route will return the updated apartment (with the added group).
* The route might fail if creating the new group failed.
*/
Expand All @@ -1415,7 +1417,18 @@ app.post('/apartments/:id/groups', authenticate, async (req, res) => {
}
// now we know that all ids are valid so we can try to add this group
let apartment = await Apartment.findById(req.params.id);
apartment = await apartment.createGroup(body.id);
if (_.isArray(body.id)) { // add the group as is.
apartment = await apartment.createGroup(body.id);
} else if (_.isString(body.id) && apartment.isTimeToOpenGroup()) { // find best group to build if it is time to open a group
const ids = (await req.user.getBestMatchingUsers(apartment._interested))
.map($ => $._id)
.filter($ => !$.equals(req.user._id))
.slice(0, apartment.requiredRoommates);
ids[ids.length - 1] = req.user._id.toHexString(); // set the requesting member to be in the group
apartment = await apartment.createGroup(ids);
} else {
return res.status(BAD_REQUEST).send(errors.groupCreationFailed);
}
return res.send({ apartment });
} catch (error) {
return res.status(BAD_REQUEST).send(error);
Expand All @@ -1440,9 +1453,9 @@ app.patch('/apartments/:id/groups', authenticate, async (req, res) => {
return res.status(BAD_REQUEST).send(errors.apartmentNotFound);
}
apartment = await apartment.updateMemberStatus(body.id, req.user._id, body.status);
res.send({ apartment });
return res.send({ apartment });
} catch (error) {
res.status(BAD_REQUEST).send(error);
return res.status(BAD_REQUEST).send(error);
}
});
/**
Expand Down
3 changes: 0 additions & 3 deletions tests/seed/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ const apartment1User2VisitId = new ObjectID();

const user1Notification1Id = new ObjectID();

const group1Id = new ObjectID();
const group2Id = new ObjectID();

const apartment1 = new Apartment({
_id: apartment1Id,
_createdBy: new ObjectID(),
Expand Down
4 changes: 3 additions & 1 deletion tests/server/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,7 @@ describe('#Server Tests', () => {
}
});
});
it('should create a new group when sending a single member', (done) => {
it('should create a new best group when sending a single member', (done) => {
const apartmentId = apartments[0]._id.toHexString();
const id = users[1]._id.toHexString();
request(app)
Expand All @@ -2596,6 +2596,8 @@ describe('#Server Tests', () => {
const apartment = await Apartment.findById(apartmentId);
expect(apartment.groups.length).toBe(1);
expect(apartment.groups[0].members.length).toBe(2);
expect(apartment.groups[0].members[0].id.toHexString()).toBe(users[0]._id.toHexString());
expect(apartment.groups[0].members[1].id.toHexString()).toBe(users[1]._id.toHexString());
return done();
} catch (e) {
return done(e);
Expand Down

0 comments on commit 04d51c2

Please sign in to comment.