Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/routes/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
50 changes: 44 additions & 6 deletions src/routes/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -218,8 +216,6 @@ router.post('/link/:entity_id', async (req, res) => {
}
});



const ec = {
entityId: entity.id,
contactId: contactToLink.id
Expand All @@ -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;
Expand All @@ -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;
116 changes: 115 additions & 1 deletion swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@
"format" : "uuid"
},
"required" : true,
"description" : "id of the contact"
"description" : "id of the entity"
}, {
"in" : "header",
"name" : "token",
Expand Down Expand Up @@ -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" ],
Expand Down Expand Up @@ -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" ],
Expand Down