All files / src/controllers/admin-controllers updateProduct.js

60% Statements 3/5
100% Branches 0/0
100% Functions 1/1
60% Lines 3/5

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                          2x 2x 2x            
import ProductsModel from "../../models/Products.js";
 
/**
* Updates a product in the database.
* 
* @param { string } productId - The id of the product to update.
* @param { { [string]: any } } changes - The changes to apply to the product. 
* is an object `keys` = the filed to update, `values` = the new value of the filed
* 
* @return { Promise<boolean | null> } `true` if the product was updated successfully, else `false`.
* if an unexpected Error happened `null` will returned.
*/
export default async function updateProduct(productId, changes) {
    try {
        const { modifiedCount } = await ProductsModel.updateOne({ _id: productId }, { $set: changes });
        return !!modifiedCount;
    } catch (error) {
        console.log(error);
        return null;
    }
}