All files / src/controllers/products-controllers addCommentToProduct.js

72.72% Statements 8/11
50% Branches 4/8
100% Functions 1/1
72.72% Lines 8/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23        1x 1x 1x 1x       1x 1x 1x 1x                
import ProductsCommentsModel from "../../models/ProductsComments.js";
import crypto from "crypto";
 
export default async function addCommentToProduct(productId, theComment) {
    try {
        const commentId = crypto.randomUUID();
        theComment.id = commentId;
        const { modifiedCount, matchedCount } = await ProductsCommentsModel.updateOne(
            { productId },
            { $push: { comments: theComment } }
        );
        Eif (!matchedCount && !modifiedCount) {
            const newProductsCommentsDoc = new ProductsCommentsModel({ productId, comments: [theComment] });
            const response = await newProductsCommentsDoc.save()
            return response ? commentId : false;
        }
        return modifiedCount ? commentId : false;
    } catch (error) {
        console.log(error)
        return null;
    }
}