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

55.55% Statements 5/9
100% Branches 0/0
66.66% Functions 2/3
55.55% Lines 5/9

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 24 25 26                      1x 1x 1x 1x           1x          
import ProductsModel from "../../models/Products.js";
 
/**
* Adds a product to the database by creating an instance of `ProductsModel` and saving it to the database.
* 
* @param theProducts - The Products to add.
* 
* @return { Promise<boolean | null> } `true` if the product was added successfully, `false` if adding failed, 
* `null` if an unexpected error occurred.
*/
export default async function addProduct(theProducts) {
    try {
        const newProduct = new ProductsModel(theProducts);
        const response = await newProduct.save()
            .then(() => true)
            .catch((error) => {
                console.log(error)
                return false
            })
 
        return response;
    } catch (error) {
        console.log(error)
        return null;
    }
}