Skip to content

Commit 5ca2d0d

Browse files
authored
Merge pull request #140 from CodeForBaltimore/c-w-allen/issue-135
Unlink contacts from entity endpoint
2 parents 82f6702 + ca61876 commit 5ca2d0d

File tree

3 files changed

+201
-8
lines changed

3 files changed

+201
-8
lines changed

src/routes/contact.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,48 @@ router.post('/link/:contact_id', async (req, res) => {
206206

207207
await req.context.models.EntityContact.createIfNew(ec);
208208
}
209-
message = `Link successful/already exists for contact with ID ${contact.id}`;
209+
message = `Linking successful/already exists for contact with ID ${contact.id}`;
210+
code = 200;
211+
} else {
212+
code = 422;
213+
}
214+
} catch (e) {
215+
console.error(e);
216+
code = 500;
217+
}
218+
219+
return utils.response(res, code, message);
220+
});
221+
222+
// unlinks contact with list of entities
223+
router.post('/unlink/:contact_id', async (req, res) => {
224+
let code;
225+
let message;
226+
try {
227+
if (validator.isUUID(req.params.contact_id)) {
228+
const contact = await req.context.models.Contact.findOne({
229+
where: {
230+
id: req.params.contact_id
231+
}
232+
});
233+
for(const entity of req.body.entities) {
234+
const entityToUnLink = await req.context.models.Entity.findOne({
235+
where: {
236+
id: entity.id
237+
}
238+
});
239+
240+
// ideally only one of these should exist
241+
const ec = await req.context.models.EntityContact.findOne({
242+
where: {
243+
entityId: entityToUnLink.id,
244+
contactId: contact.id
245+
}
246+
});
247+
248+
await ec.destroy();
249+
}
250+
message = `Unlinking successful for contact with ID ${contact.id}`;
210251
code = 200;
211252
} else {
212253
code = 422;

src/routes/entity.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ router.get('/:entity_id', async (req, res) => {
5252
router.post('/', async (req, res) => {
5353
let code;
5454
let message;
55-
let ec;
5655
try {
5756
if (req.body.name !== undefined) {
5857
let { name, address, phone, email, checkIn, contacts } = req.body;
@@ -69,7 +68,7 @@ router.post('/', async (req, res) => {
6968
const entity = await req.context.models.Entity.create({ name, address, email, phone, checkIn });
7069
if (contacts) {
7170
for(const contact of contacts) {
72-
ec = {
71+
const ec = {
7372
entityId: entity.id,
7473
contactId: contact.id
7574
}
@@ -97,7 +96,6 @@ router.post('/', async (req, res) => {
9796
router.put('/', async (req, res) => {
9897
let code;
9998
let message;
100-
let ec;
10199
try {
102100
if (validator.isUUID(req.body.id)) {
103101
let { id, name, address, phone, email, checkIn, contacts } = req.body;
@@ -218,8 +216,6 @@ router.post('/link/:entity_id', async (req, res) => {
218216
}
219217
});
220218

221-
222-
223219
const ec = {
224220
entityId: entity.id,
225221
contactId: contactToLink.id
@@ -231,7 +227,7 @@ router.post('/link/:entity_id', async (req, res) => {
231227

232228
await req.context.models.EntityContact.createIfNew(ec);
233229
}
234-
message = `Link successful/already exists for entity with ID ${entity.id}`;
230+
message = `Linking successful/already exists for entity with ID ${entity.id}`;
235231
code = 200;
236232
} else {
237233
code = 422;
@@ -244,4 +240,46 @@ router.post('/link/:entity_id', async (req, res) => {
244240
return utils.response(res, code, message);
245241
});
246242

243+
// unlinks entity with list of contacts
244+
router.post('/unlink/:entity_id', async (req, res) => {
245+
let code;
246+
let message;
247+
try {
248+
if (validator.isUUID(req.params.entity_id)) {
249+
const entity = await req.context.models.Entity.findOne({
250+
where: {
251+
id: req.params.entity_id
252+
}
253+
});
254+
255+
for(const contact of req.body.contacts) {
256+
const contactToUnLink = await req.context.models.Contact.findOne({
257+
where: {
258+
id: contact.id
259+
}
260+
});
261+
262+
// ideally only one of these should exist
263+
const ec = await req.context.models.EntityContact.findOne({
264+
where: {
265+
entityId: entity.id,
266+
contactId: contactToUnLink.id
267+
}
268+
});
269+
270+
await ec.destroy();
271+
}
272+
message = `Unlinking successful for entity with ID ${entity.id}`;
273+
code = 200;
274+
} else {
275+
code = 422;
276+
}
277+
} catch (e) {
278+
console.error(e);
279+
code = 500;
280+
}
281+
282+
return utils.response(res, code, message)
283+
});
284+
247285
export default router;

swagger.json

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@
966966
"format" : "uuid"
967967
},
968968
"required" : true,
969-
"description" : "id of the contact"
969+
"description" : "id of the entity"
970970
}, {
971971
"in" : "header",
972972
"name" : "token",
@@ -1010,6 +1010,63 @@
10101010
}
10111011
}
10121012
},
1013+
"/entity/unlink/{entity_id}" : {
1014+
"post" : {
1015+
"tags" : [ "entity" ],
1016+
"summary" : "unlinks an entity from a list of given contacts",
1017+
"description" : "By passing the entity id and list of contacts, you can unlink the entity from each contact.",
1018+
"parameters" : [ {
1019+
"in" : "path",
1020+
"name" : "entity_id",
1021+
"schema" : {
1022+
"type" : "string",
1023+
"format" : "uuid"
1024+
},
1025+
"required" : true,
1026+
"description" : "id of the entity"
1027+
}, {
1028+
"in" : "header",
1029+
"name" : "token",
1030+
"required" : true,
1031+
"schema" : {
1032+
"type" : "string",
1033+
"example" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE1ODA3NTM0MDUsImV4cCI6MTU4MDgzOTgwNX0.Q6W7Vo6By35yjZBeLKkk96s8LyqIE2G39AG1H3LRD9M"
1034+
}
1035+
} ],
1036+
"requestBody" : {
1037+
"description" : "The body of the payload",
1038+
"required" : true,
1039+
"content" : {
1040+
"application/json" : {
1041+
"schema" : {
1042+
"type" : "object",
1043+
"required" : [ "contacts" ],
1044+
"properties" : {
1045+
"contacts" : {
1046+
"type" : "string",
1047+
"example" : [{"id":""},{"id":""}]
1048+
}
1049+
}
1050+
}
1051+
}
1052+
}
1053+
},
1054+
"responses" : {
1055+
"200" : {
1056+
"description" : "Successful/already exists"
1057+
},
1058+
"401" : {
1059+
"description" : "Unauthorized"
1060+
},
1061+
"422" : {
1062+
"description" : "Invalid input"
1063+
},
1064+
"500" : {
1065+
"description" : "Server error"
1066+
}
1067+
}
1068+
}
1069+
},
10131070
"/contact" : {
10141071
"post" : {
10151072
"tags" : [ "contact" ],
@@ -1424,6 +1481,63 @@
14241481
}
14251482
}
14261483
},
1484+
"/contact/unlink/{contact_id}" : {
1485+
"post" : {
1486+
"tags" : [ "contact" ],
1487+
"summary" : "unlinks a contact from a list of given entities",
1488+
"description" : "By passing the contact id and list of entities, you can unlink the contact from each entity.",
1489+
"parameters" : [ {
1490+
"in" : "path",
1491+
"name" : "contact_id",
1492+
"schema" : {
1493+
"type" : "string",
1494+
"format" : "uuid"
1495+
},
1496+
"required" : true,
1497+
"description" : "id of the contact"
1498+
}, {
1499+
"in" : "header",
1500+
"name" : "token",
1501+
"required" : true,
1502+
"schema" : {
1503+
"type" : "string",
1504+
"example" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE1ODA3NTM0MDUsImV4cCI6MTU4MDgzOTgwNX0.Q6W7Vo6By35yjZBeLKkk96s8LyqIE2G39AG1H3LRD9M"
1505+
}
1506+
} ],
1507+
"requestBody" : {
1508+
"description" : "The body of the payload",
1509+
"required" : true,
1510+
"content" : {
1511+
"application/json" : {
1512+
"schema" : {
1513+
"type" : "object",
1514+
"required" : [ "entities" ],
1515+
"properties" : {
1516+
"entities" : {
1517+
"type" : "string",
1518+
"example" : [{"id":""},{"id":""}]
1519+
}
1520+
}
1521+
}
1522+
}
1523+
}
1524+
},
1525+
"responses" : {
1526+
"200" : {
1527+
"description" : "Successful/already exists"
1528+
},
1529+
"401" : {
1530+
"description" : "Unauthorized"
1531+
},
1532+
"422" : {
1533+
"description" : "Invalid input"
1534+
},
1535+
"500" : {
1536+
"description" : "Server error"
1537+
}
1538+
}
1539+
}
1540+
},
14271541
"/csv/{model_type}" : {
14281542
"get" : {
14291543
"tags" : [ "csv" ],

0 commit comments

Comments
 (0)