@@ -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 {
0 commit comments