File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,18 +15,21 @@ class ImageRepository {
1515 * @return { Promise<ImageProp> } - A new image created
1616 */
1717 async create ( image : ImageProp ) : Promise < ImageProp > {
18- const sanitizedTagId = image . tag . toString ( ) . trim ( ) ;
19- if ( ! Types . ObjectId . isValid ( sanitizedTagId ) ) {
20- rollbar . error ( 'Invalid tag id' ) ;
18+ const sanitizedTagId = image . tag . tag_id ?. toString ( ) ;
19+ if ( ! Types . ObjectId . isValid ( image . tag . tag_id ) ) {
2120 throw new Error ( 'Invalid tag id' ) ;
2221 }
2322
24- const tagExists = await Tag . findOne ( { tag_id : sanitizedTagId } ) ;
25- const _idTag = tagExists ?. _id ;
23+ const tagExists = await Tag . findById ( { tag_id : { $eq : sanitizedTagId } } ) ;
24+
25+ if ( ! tagExists ) {
26+ rollbar . error ( 'Tag not found' ) ;
27+ throw new Error ( 'Tag not found' ) ;
28+ }
2629
2730 return Image . create ( {
2831 ...image ,
29- tag : _idTag ?? image . tag , // Use validated tag or fallback
32+ tag : tagExists . _id ,
3033 } ) ;
3134 }
3235
You can’t perform that action at this time.
0 commit comments