@@ -53,13 +53,17 @@ router.post('/add-device', upload.single('avatar'), userMw.one, async (req, res)
5353 fs . unlink ( req . file . path , ( ) => { console . log ( 'Temp file deleted' ) } ) ;
5454 }
5555 const existingInitials = await Device . findOne ( { initials : req . body . initials } ) ;
56- if ( existingInitials ) return res . send ( 'Initials Invalid' ) ;
56+ if ( existingInitials ) {
57+ res . header ( 'HX-Trigger' , 'invalidInitials' ) ;
58+ return res . status ( 400 ) . send ( 'Initials Invalid' ) ;
59+ }
5760 const device = await Device . create ( req . body . deviceName , req . body . initials , card , req . User ) ;
5861 if ( device ) {
5962 if ( req . envSettings . mqttEnabled ) publishDeviceCards ( req . User . username , req . User . friends , [ device ] ) ;
6063 }
61- const userDevices = await Device . getByUserId ( req . User . _id ) ;
62- res . render ( 'user-devices.html' , { userDevices } ) ;
64+ req . pageData . userDevices = await Device . getByUserId ( req . User . _id ) ;
65+ res . header ( 'HX-Trigger' , 'deviceSave' ) ;
66+ res . render ( 'user-devices.html' , req . pageData ) ;
6367} ) ;
6468
6569router . get ( '/edit-device/:deviceId' , userMw . one , devMw . one , async ( req , res ) => {
@@ -82,11 +86,17 @@ router.post('/edit-device/:deviceId', upload.single('avatar'), userMw.one, devMw
8286 fs . unlink ( req . file . path , ( ) => { console . log ( 'Temp file deleted' ) } ) ;
8387 }
8488 if ( req . Device && req . Device . userId === req . User . _id ) {
89+ const existingInitials = await Device . findOne ( { initials : req . body . initials } ) ;
90+ if ( existingInitials && existingInitials . _id !== req . Device . _id ) {
91+ res . header ( 'HX-Trigger' , 'invalidInitials' ) ;
92+ return res . status ( 400 ) . send ( 'Initials Invalid' ) ;
93+ }
8594 req . Device = await req . Device . update ( req . body . deviceName , req . body . initials , card , req . User ) ;
8695 await CardSeen . update ( { deviceId : req . params . deviceId } , { $set : { seen : false } } ) ; // Force friends to re-download card data (HTTP mode)
8796 if ( req . envSettings . mqttEnabled ) publishDeviceCards ( req . User . username , req . User . friends , [ req . Device ] ) ; // Send card (MQTT mode)
8897 }
8998 req . pageData . userDevices = await Device . getByUserId ( req . User . _id ) ;
99+ res . header ( 'HX-Trigger' , 'deviceSave' ) ;
90100 res . render ( 'user-devices.html' , req . pageData ) ;
91101} ) ;
92102
@@ -96,8 +106,8 @@ router.get('/delete-device/:deviceId', userMw.one, devMw.one, async (req, res) =
96106 await req . Device . remove ( ) ;
97107 await CardSeen . remove ( { deviceId : req . params . deviceId } , { multi : true } ) ;
98108 }
99- const userDevices = await Device . getByUserId ( req . User . _id ) ;
100- res . render ( 'user-devices.html' , { userDevices } ) ;
109+ req . pageData . userDevices = await Device . getByUserId ( req . User . _id ) ;
110+ res . render ( 'user-devices.html' , req . pageData ) ;
101111} ) ;
102112
103113// !!!! friends !!!!
0 commit comments