@@ -85,7 +85,7 @@ router.post('/', async (req, res) => {
8585
8686 const entity = await req . context . models . Entity . create ( { name, type, address, email, phone, checkIn } )
8787 if ( contacts ) {
88- for ( const contact of contacts ) {
88+ for ( const contact of contacts ) {
8989 const ec = {
9090 entityId : entity . id ,
9191 contactId : contact . id
@@ -174,7 +174,7 @@ router.put('/', async (req, res) => {
174174 await entity . save ( )
175175
176176 if ( contacts ) {
177- for ( const contact of contacts ) {
177+ for ( const contact of contacts ) {
178178 const ec = {
179179 entityId : entity . id ,
180180 contactId : contact . id
@@ -190,7 +190,7 @@ router.put('/', async (req, res) => {
190190 } else {
191191 response . setCode ( 400 )
192192 }
193-
193+
194194 } catch ( e ) {
195195 console . error ( e )
196196 response . setCode ( 500 )
@@ -211,7 +211,7 @@ router.delete('/:entity_id', async (req, res) => {
211211 } )
212212 await entity . destroy ( )
213213
214-
214+
215215 response . setMessage ( req . params . entity_id + ' deleted' )
216216 } else {
217217 response . setCode ( 400 )
@@ -234,7 +234,7 @@ router.post('/link/:entity_id', async (req, res) => {
234234 id : req . params . entity_id
235235 }
236236 } )
237- for ( const contact of req . body . contacts ) {
237+ for ( const contact of req . body . contacts ) {
238238 const contactToLink = await req . context . models . Contact . findOne ( {
239239 where : {
240240 id : contact . id
@@ -253,7 +253,7 @@ router.post('/link/:entity_id', async (req, res) => {
253253 await req . context . models . EntityContact . createIfNew ( ec )
254254 }
255255 response . setMessage ( `Linking successful/already exists for entity with ID ${ entity . id } ` )
256-
256+
257257 } else {
258258 response . setCode ( 400 )
259259 }
@@ -276,7 +276,8 @@ router.post('/unlink/:entity_id', async (req, res) => {
276276 }
277277 } )
278278
279- for ( const contact of req . body . contacts ) {
279+ let i = 0
280+ for ( const contact of req . body . contacts ) {
280281 const contactToUnLink = await req . context . models . Contact . findOne ( {
281282 where : {
282283 id : contact . id
@@ -291,10 +292,18 @@ router.post('/unlink/:entity_id', async (req, res) => {
291292 }
292293 } )
293294
294- await ec . destroy ( )
295+ if ( ec != null ) {
296+ await ec . destroy ( )
297+ i ++
298+ }
299+ }
300+
301+ if ( i > 0 ) {
302+ response . setMessage ( `Unlinking successful for entity with ID ${ entity . id } ` )
303+ } else {
304+ response . setCode ( 400 )
305+ response . setMessage ( 'Bad link sent' )
295306 }
296- response . setMessage ( `Unlinking successful for entity with ID ${ entity . id } ` )
297-
298307 } else {
299308 response . setCode ( 400 )
300309 }
0 commit comments