diff --git a/src/routes/contact.js b/src/routes/contact.js index 83e1e6c8..2fd5ec5c 100644 --- a/src/routes/contact.js +++ b/src/routes/contact.js @@ -206,7 +206,48 @@ router.post('/link/:contact_id', async (req, res) => { await req.context.models.EntityContact.createIfNew(ec); } - message = `Link successful/already exists for contact with ID ${contact.id}`; + message = `Linking successful/already exists for contact with ID ${contact.id}`; + code = 200; + } else { + code = 422; + } + } catch (e) { + console.error(e); + code = 500; + } + + return utils.response(res, code, message); +}); + +// unlinks contact with list of entities +router.post('/unlink/:contact_id', async (req, res) => { + let code; + let message; + try { + if (validator.isUUID(req.params.contact_id)) { + const contact = await req.context.models.Contact.findOne({ + where: { + id: req.params.contact_id + } + }); + for(const entity of req.body.entities) { + const entityToUnLink = await req.context.models.Entity.findOne({ + where: { + id: entity.id + } + }); + + // ideally only one of these should exist + const ec = await req.context.models.EntityContact.findOne({ + where: { + entityId: entityToUnLink.id, + contactId: contact.id + } + }); + + await ec.destroy(); + } + message = `Unlinking successful for contact with ID ${contact.id}`; code = 200; } else { code = 422; diff --git a/src/routes/entity.js b/src/routes/entity.js index 3de56911..90db55c1 100644 --- a/src/routes/entity.js +++ b/src/routes/entity.js @@ -52,7 +52,6 @@ router.get('/:entity_id', async (req, res) => { router.post('/', async (req, res) => { let code; let message; - let ec; try { if (req.body.name !== undefined) { let { name, address, phone, email, checkIn, contacts } = req.body; @@ -69,7 +68,7 @@ router.post('/', async (req, res) => { const entity = await req.context.models.Entity.create({ name, address, email, phone, checkIn }); if (contacts) { for(const contact of contacts) { - ec = { + const ec = { entityId: entity.id, contactId: contact.id } @@ -97,7 +96,6 @@ router.post('/', async (req, res) => { router.put('/', async (req, res) => { let code; let message; - let ec; try { if (validator.isUUID(req.body.id)) { let { id, name, address, phone, email, checkIn, contacts } = req.body; @@ -218,8 +216,6 @@ router.post('/link/:entity_id', async (req, res) => { } }); - - const ec = { entityId: entity.id, contactId: contactToLink.id @@ -231,7 +227,7 @@ router.post('/link/:entity_id', async (req, res) => { await req.context.models.EntityContact.createIfNew(ec); } - message = `Link successful/already exists for entity with ID ${entity.id}`; + message = `Linking successful/already exists for entity with ID ${entity.id}`; code = 200; } else { code = 422; @@ -244,4 +240,46 @@ router.post('/link/:entity_id', async (req, res) => { return utils.response(res, code, message); }); +// unlinks entity with list of contacts +router.post('/unlink/:entity_id', async (req, res) => { + let code; + let message; + try { + if (validator.isUUID(req.params.entity_id)) { + const entity = await req.context.models.Entity.findOne({ + where: { + id: req.params.entity_id + } + }); + + for(const contact of req.body.contacts) { + const contactToUnLink = await req.context.models.Contact.findOne({ + where: { + id: contact.id + } + }); + + // ideally only one of these should exist + const ec = await req.context.models.EntityContact.findOne({ + where: { + entityId: entity.id, + contactId: contactToUnLink.id + } + }); + + await ec.destroy(); + } + message = `Unlinking successful for entity with ID ${entity.id}`; + code = 200; + } else { + code = 422; + } + } catch (e) { + console.error(e); + code = 500; + } + + return utils.response(res, code, message) +}); + export default router; diff --git a/swagger.json b/swagger.json index 852af927..46d37fb0 100644 --- a/swagger.json +++ b/swagger.json @@ -966,7 +966,7 @@ "format" : "uuid" }, "required" : true, - "description" : "id of the contact" + "description" : "id of the entity" }, { "in" : "header", "name" : "token", @@ -1010,6 +1010,63 @@ } } }, + "/entity/unlink/{entity_id}" : { + "post" : { + "tags" : [ "entity" ], + "summary" : "unlinks an entity from a list of given contacts", + "description" : "By passing the entity id and list of contacts, you can unlink the entity from each contact.", + "parameters" : [ { + "in" : "path", + "name" : "entity_id", + "schema" : { + "type" : "string", + "format" : "uuid" + }, + "required" : true, + "description" : "id of the entity" + }, { + "in" : "header", + "name" : "token", + "required" : true, + "schema" : { + "type" : "string", + "example" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE1ODA3NTM0MDUsImV4cCI6MTU4MDgzOTgwNX0.Q6W7Vo6By35yjZBeLKkk96s8LyqIE2G39AG1H3LRD9M" + } + } ], + "requestBody" : { + "description" : "The body of the payload", + "required" : true, + "content" : { + "application/json" : { + "schema" : { + "type" : "object", + "required" : [ "contacts" ], + "properties" : { + "contacts" : { + "type" : "string", + "example" : [{"id":""},{"id":""}] + } + } + } + } + } + }, + "responses" : { + "200" : { + "description" : "Successful/already exists" + }, + "401" : { + "description" : "Unauthorized" + }, + "422" : { + "description" : "Invalid input" + }, + "500" : { + "description" : "Server error" + } + } + } + }, "/contact" : { "post" : { "tags" : [ "contact" ], @@ -1424,6 +1481,63 @@ } } }, + "/contact/unlink/{contact_id}" : { + "post" : { + "tags" : [ "contact" ], + "summary" : "unlinks a contact from a list of given entities", + "description" : "By passing the contact id and list of entities, you can unlink the contact from each entity.", + "parameters" : [ { + "in" : "path", + "name" : "contact_id", + "schema" : { + "type" : "string", + "format" : "uuid" + }, + "required" : true, + "description" : "id of the contact" + }, { + "in" : "header", + "name" : "token", + "required" : true, + "schema" : { + "type" : "string", + "example" : "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InRlc3QiLCJpYXQiOjE1ODA3NTM0MDUsImV4cCI6MTU4MDgzOTgwNX0.Q6W7Vo6By35yjZBeLKkk96s8LyqIE2G39AG1H3LRD9M" + } + } ], + "requestBody" : { + "description" : "The body of the payload", + "required" : true, + "content" : { + "application/json" : { + "schema" : { + "type" : "object", + "required" : [ "entities" ], + "properties" : { + "entities" : { + "type" : "string", + "example" : [{"id":""},{"id":""}] + } + } + } + } + } + }, + "responses" : { + "200" : { + "description" : "Successful/already exists" + }, + "401" : { + "description" : "Unauthorized" + }, + "422" : { + "description" : "Invalid input" + }, + "500" : { + "description" : "Server error" + } + } + } + }, "/csv/{model_type}" : { "get" : { "tags" : [ "csv" ],