diff --git a/src/api/cardApi.js b/src/api/cardApi.js index 5d4a5c3..a8fa3ea 100755 --- a/src/api/cardApi.js +++ b/src/api/cardApi.js @@ -2,6 +2,16 @@ import { 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; +} + export function getCards() { return fetch(baseUrl, bearerHeaders()) .then(handleResponse) @@ -18,7 +28,7 @@ export function saveCard(Card) { return fetch(baseUrl + (Card._id || ""), { method: Card._id ? "PUT" : "POST", headers: { "content-type": "application/json" }, - body: JSON.stringify(Card) + body: JSON.stringify(depopulateCard(Card)) }) .then(handleResponse) .catch(handleError); diff --git a/src/api/cardApi.test.js b/src/api/cardApi.test.js new file mode 100644 index 0000000..0fddc67 --- /dev/null +++ b/src/api/cardApi.test.js @@ -0,0 +1,40 @@ +import { depopulateCard } from "./cardApi"; + +it("should clean up referenced members", function() { + const card = { + _id: "5d6aa2c0b3c3b66d9e80ceb6", + number: "#8", + title: "hold how", + description: + "Id fugiat et aliqua nisi. Ea laborum dolor exercitation sit. Veniam ea dolore ad est ad anim excepteur. Eiusmod magna aliquip duis. Anim deserunt dolor eu velit eiusmod aliquip eu consectetur Lorem.", + status: "", + board: "", + comments: "", + activity: "", + attachments: "", + owner: { + _id: "5d42e5d5e1e3d731df26010a", + firstName: "Andrew", + lastName: "Smith" + }, + members: [ + { + _id: "5d42104a7979bd071c129606", + firstName: "Dale", + lastName: "Fitzgerald" + }, + { + _id: "5d42104a7979bd071c129609", + firstName: "Adeline", + lastName: "Fitzgerald" + } + ], + createdDate: "2019-08-31T16:39:28.780Z", + modifiedDate: "2019-08-31T16:39:28.780Z" + }; + + expect(depopulateCard(card).members).toEqual([ + "5d42104a7979bd071c129606", + "5d42104a7979bd071c129609" + ]); +});