@@ -131,7 +131,6 @@ router.param('variantText', async (req, res, next, ident) => {
131131 try {
132132 result = await db . models . variantText . findOne ( {
133133 where : { ident} ,
134- attributes : variantTextPublicAttributes ,
135134 include : variantTextPublicInclude ,
136135 } ) ;
137136 } catch ( error ) {
@@ -168,7 +167,16 @@ router.route('/:variantText([A-z0-9-]{36})')
168167 return res . json ( req . variantText . view ( 'public' ) ) ;
169168 } )
170169 . put ( async ( req , res ) => {
171- const requestedProjectIdents = req . body . projects || ( req . body . project ? [ req . body . project ] : [ ] ) ;
170+ const requestedProjectIdentsRaw = req . body . projects || ( req . body . project ? [ req . body . project ] : [ ] ) ;
171+ const requestedProjectIdents = ( Array . isArray ( requestedProjectIdentsRaw ) ? requestedProjectIdentsRaw : [ requestedProjectIdentsRaw ] )
172+ . map ( ( project ) => {
173+ if ( typeof project === 'string' ) {
174+ return project ;
175+ }
176+
177+ return project ?. ident ;
178+ } )
179+ . filter ( ( ident ) => { return Boolean ( ident ) ; } ) ;
172180
173181 if ( requestedProjectIdents . length && ! hasProjectAccessForAll ( req . user , requestedProjectIdents ) ) {
174182 logger . error ( `user ${ req . user . username } does not have access to variant text projects ${ requestedProjectIdents . join ( ', ' ) } ` ) ;
@@ -177,6 +185,29 @@ router.route('/:variantText([A-z0-9-]{36})')
177185 } ) ;
178186 }
179187
188+ let requestedProjectIds ;
189+ if ( requestedProjectIdents . length ) {
190+ const projects = await db . models . project . findAll ( {
191+ where : {
192+ ident : requestedProjectIdents ,
193+ } ,
194+ } ) ;
195+
196+ if ( projects . length !== requestedProjectIdents . length ) {
197+ const foundProjectIdents = projects . map ( ( project ) => { return project . ident ; } ) ;
198+ const missingProjectIdent = requestedProjectIdents . find ( ( ident ) => {
199+ return ! foundProjectIdents . includes ( ident ) ;
200+ } ) ;
201+
202+ logger . error ( `Unable to find project ${ missingProjectIdent } ` ) ;
203+ return res . status ( HTTP_STATUS . NOT_FOUND ) . json ( {
204+ error : { message : 'Unable to find project' } ,
205+ } ) ;
206+ }
207+
208+ requestedProjectIds = projects . map ( ( project ) => { return project . id ; } ) ;
209+ }
210+
180211 const variantTextBody = { ...req . body } ;
181212 delete variantTextBody . project ;
182213 delete variantTextBody . projects ;
@@ -200,8 +231,8 @@ router.route('/:variantText([A-z0-9-]{36})')
200231 await req . variantText . update ( variantTextBody , { userId : req . user . id } ) ;
201232 }
202233
203- if ( Array . isArray ( req . body . projectIds ) ) {
204- await req . variantText . setProjects ( req . body . projectIds ) ;
234+ if ( Array . isArray ( requestedProjectIds ) ) {
235+ await req . variantText . setProjects ( requestedProjectIds ) ;
205236 }
206237
207238 const updatedVariantText = await db . models . variantText . findOne ( {
0 commit comments