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,22 @@ 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+
19+ const sanitizedTagId = image . tag . tag_id ?. toString ( ) ;
20+ if ( ! Types . ObjectId . isValid ( image . tag . tag_id ) ) {
2121 throw new Error ( 'Invalid tag id' ) ;
2222 }
2323
24- const tagExists = await Tag . findOne ( { tag_id : sanitizedTagId } ) ;
25- const _idTag = tagExists ?. _id ;
24+ const tagExists = await Tag . findById ( { tag_id : { $eq : sanitizedTagId } } ) ;
25+
26+ if ( ! tagExists ) {
27+ rollbar . error ( 'Tag not found' ) ;
28+ throw new Error ( 'Tag not found' ) ;
29+ }
2630
2731 return Image . create ( {
2832 ...image ,
29- tag : _idTag ?? image . tag , // Use validated tag or fallback
33+ tag : tagExists . _id ,
3034 } ) ;
3135 }
3236
You can’t perform that action at this time.
0 commit comments