Skip to content

Commit 7646eed

Browse files
committed
add linking on creation/update of entities
1 parent d5121c1 commit 7646eed

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/routes/entity.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ router.post('/', async (req, res) => {
5454
let message;
5555
try {
5656
if (req.body.name !== undefined) {
57-
let { name, address, phone, email, checkIn } = req.body;
57+
let { name, address, phone, email, checkIn, contacts } = req.body;
5858

5959
if (!checkIn) {
6060
checkIn = {
@@ -66,6 +66,18 @@ router.post('/', async (req, res) => {
6666
}
6767

6868
const entity = await req.context.models.Entity.create({ name, address, email, phone, checkIn });
69+
if (contacts) {
70+
for(const contact of contacts) {
71+
ec = {
72+
entityId: entity.id,
73+
contactId: contact.id
74+
}
75+
if (contact.title) {
76+
ec.relationshipTitle = contact.title;
77+
}
78+
await req.context.models.EntityContact.createIfNew(ec);
79+
}
80+
}
6981

7082
code = 200;
7183
message = entity.id + ' created';
@@ -86,7 +98,7 @@ router.put('/', async (req, res) => {
8698
let message;
8799
try {
88100
if (validator.isUUID(req.body.id)) {
89-
let { id, name, address, phone, email, checkIn } = req.body;
101+
let { id, name, address, phone, email, checkIn, contacts } = req.body;
90102

91103
/** @todo validate emails */
92104
// Validating emails
@@ -133,6 +145,19 @@ router.put('/', async (req, res) => {
133145

134146
await entity.save();
135147

148+
if (contacts) {
149+
for(const contact of contacts) {
150+
ec = {
151+
entityId: entity.id,
152+
contactId: contact.id
153+
}
154+
if (contact.title) {
155+
ec.relationshipTitle = contact.title;
156+
}
157+
await req.context.models.EntityContact.createIfNew(ec);
158+
}
159+
}
160+
136161
code = 200;
137162
message = entity.id + ' updated';
138163
} else {

swagger.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@
478478
}
479479
}
480480
},
481+
"contacts" : {
482+
"type" : "string",
483+
"example" : [{"id":""},{"id":"","title":""}]
484+
},
481485
"description" : {
482486
"type" : "string",
483487
"example" : "Everything for the left handed man, woman, and child!"
@@ -687,6 +691,10 @@
687691
}
688692
}
689693
},
694+
"entities" : {
695+
"type" : "string",
696+
"example" : [{"id":""},{"id":"","title":""}]
697+
},
690698
"description" : {
691699
"type" : "string",
692700
"example" : "Everything for the left handed man, woman, and child!"
@@ -932,10 +940,9 @@
932940
"format" : "uuid",
933941
"example" : "abafa852-ecd0-4d57-9083-85f4dfd9c402"
934942
},
935-
"EntityId" : {
943+
"entities" : {
936944
"type" : "string",
937-
"format" : "uuid",
938-
"example" : "c2fb852c-17fd-4e2b-9ea4-78128af9a5e9"
945+
"example" : [{"id":""},{"id":"","title":""}]
939946
}
940947
}
941948
}
@@ -1102,10 +1109,9 @@
11021109
"format" : "uuid",
11031110
"example" : "abafa852-ecd0-4d57-9083-85f4dfd9c402"
11041111
},
1105-
"EntityId" : {
1112+
"entities" : {
11061113
"type" : "string",
1107-
"format" : "uuid",
1108-
"example" : "c2fb852c-17fd-4e2b-9ea4-78128af9a5e9"
1114+
"example" : [{"id":""},{"id":"","title":""}]
11091115
}
11101116
}
11111117
}

0 commit comments

Comments
 (0)