Skip to content

Commit

Permalink
refactor #11: Abstracted depopulateObject
Browse files Browse the repository at this point in the history
  • Loading branch information
Boosmith committed Sep 1, 2019
1 parent 149da85 commit fa0e8f0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
19 changes: 19 additions & 0 deletions src/api/apiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ export function handleError(error) {
console.error("API call failed. " + error);
throw error;
}

export function depopulateObject(obj, ...populatedProperties) {
const depopulatedObject = obj;

populatedProperties.forEach(property => {
let targetProp = obj[property];
if (!Array.isArray(targetProp)) {
depopulatedObject[property] = targetProp._id;
} else {
let depopulated = [];
depopulatedObject[property].forEach(item => {
depopulated.push(item._id);
});
depopulatedObject[property] = depopulated;
}
return;
});
return depopulatedObject;
}
14 changes: 6 additions & 8 deletions src/api/cardApi.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { handleResponse, handleError } from "./apiUtils";
import {
depopulateMongooseObject,
handleResponse,
handleError
} from "./apiUtils";
import { bearerHeaders } from "../tools/auth";
const baseUrl = process.env.REACT_APP_API_URL + "/api/cards/";

export function depopulateCard(card) {
card.owner = card.owner._id;
let members = [];
card.members.forEach(member => {
members.push(member._id);
});
card.members = members;
return card;
return depopulateMongooseObject(card, "owner", "members");
}

export function getCards() {
Expand Down
5 changes: 3 additions & 2 deletions src/api/cardApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ it("should clean up referenced members", function() {
createdDate: "2019-08-31T16:39:28.780Z",
modifiedDate: "2019-08-31T16:39:28.780Z"
};

expect(depopulateCard(card).members).toEqual([
const depopulatedCard = depopulateCard(card);
expect(depopulatedCard.members).toEqual([
"5d42104a7979bd071c129606",
"5d42104a7979bd071c129609"
]);
expect(depopulatedCard.owner).toEqual("5d42e5d5e1e3d731df26010a");
});

0 comments on commit fa0e8f0

Please sign in to comment.