Skip to content

Commit d7ac4e7

Browse files
Merge pull request #100 from jd-apprentice/development
SonarQube
2 parents 24e9cea + 0a8dd70 commit d7ac4e7

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

src/image/image-repository.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)