Skip to content

Commit

Permalink
Add product endpoints for variable products
Browse files Browse the repository at this point in the history
  • Loading branch information
bewake24 committed Nov 18, 2024
1 parent d17a10a commit b003661
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 59 deletions.
83 changes: 39 additions & 44 deletions controllers/product.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ const {
MONGOOSE_DUPLICATE_KEY,
MONGOOSE_VALIDATION_ERROR,
MONGOOSE_CAST_ERROR,
VARIABLE,
} = require("../constants/models.constants");
const Product = require("../model/product.model");
const invalidFieldMessage = require("../utils/invalidFieldMessage");
const asyncHandler = require("../utils/asyncHandler");
const ApiResponse = require("../utils/ApiResponse");
const Category = require("../model/category.model");
const Tag = require("../model/tag.model");
const Attribute = require("../model/attribute.model");

// Resolve categories/tags: can't use map because it doesn't wait and hence we get promise pending
const resolveItems = async (items, model) => {
return await Promise.all(
items.split(",").map(async (item) => {
const existingItem = await model.findOne({ name: item });
return existingItem
? existingItem._id
: (await model.create({ name: item }))._id;
})
);
};

const addAProduct = asyncHandler(async (req, res) => {
try {
Expand All @@ -23,40 +37,26 @@ const addAProduct = asyncHandler(async (req, res) => {
publishDate,
} = req.body;

let { categories, tags } = req.body;
let { categories, tags, attributes } = req.body;

const attribute = productType === SIMPLE ? req.body.attribute : null;
if (productType === SIMPLE && attributes) {
return ApiResponse.validationError(
res,
"Simple products cannot have attributes",
{ attributes: "Attributes are not allowed for simple products" },
400
);
}

const thumbnail = req.files.thumbnail[0].filename;
const gallery = req.files.gallery.map((file) => file.filename);

categories = categories.split(",");
tags = tags.split(",");

// Resolve categories: can't use map because it doesn't wait and hence we get promise pending
const addedCategories = await Promise.all(
categories.map(async (category) => {
let productCategory = await Category.findOne({ name: category });
if (!productCategory) {
productCategory = await Category.create({ name: category }); // Ensure await here
}
return productCategory._id;
})
);

// Resolve tags
const addedTags = await Promise.all(
tags.map(async (tag) => {
let productTag = await Tag.findOne({ name: tag });
if (!productTag) {
productTag = await Tag.create({ name: tag }); // Ensure await here
}
return productTag._id;
})
);

console.log(addedCategories);
console.log(addedTags);
const addedCategories = await resolveItems(categories, Category);
const addedTags = await resolveItems(tags, Tag);
const addedAttributes =
productType === VARIABLE
? await resolveItems(attributes, Attribute)
: undefined;

const product = await Product.create({
name,
Expand All @@ -68,7 +68,7 @@ const addAProduct = asyncHandler(async (req, res) => {
tags: addedTags,
productStatus,
publishDate,
attribute,
attributes: addedAttributes, // Save only if VARIABLE, otherwise undefined
thumbnail,
gallery,
});
Expand Down Expand Up @@ -136,31 +136,26 @@ const updateAProduct = asyncHandler(async (req, res) => {
"gallery",
];

// Filter only allowed fields
const fieldsToUpdate = Object.keys(updates).reduce((acc, key) => {
if (allowedUpdates.includes(key)) {
acc[key] = updates[key];
}
return acc;
}, {});

// Check if product exists
const product = await Product.findById(id);
if (!product) {
return ApiResponse.notFound(res, "Product not found", 404);
}

// Process categories and tags
const resolveItems = async (items, model) => {
return await Promise.all(
items.split(",").map(async (item) => {
const existingItem = await model.findOne({ name: item });
return existingItem
? existingItem._id
: (await model.create({ name: item }))._id;
})
if (product.productType === SIMPLE && req.body.attributes) {
return ApiResponse.validationError(
res,
"Simple products cannot have attributes",
{ attributes: "Attributes are not allowed for simple products" },
400
);
};
}

if (categories) {
fieldsToUpdate.categories = await resolveItems(categories, Category);
Expand All @@ -170,8 +165,8 @@ const updateAProduct = asyncHandler(async (req, res) => {
}

// Handle attributes for SIMPLE products
if (fieldsToUpdate.productType === SIMPLE) {
fieldsToUpdate.attributes = attributes || null;
if (fieldsToUpdate.productType === VARIABLE) {
fieldsToUpdate.attributes = await resolveItems(attributes, Attribute);
}

// Update the product
Expand Down
15 changes: 0 additions & 15 deletions model/product.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,6 @@ const productSchema = new mongoose.Schema(
ref: "Variation",
},
],
validate: {
validator: function (value) {
if (this.productType === SIMPLE && value.length > 0) {
return false; // Invalid if SIMPLE product has any variations
}
if (this.productType === VARIABLE && value.length < 1) {
return false; // Invalid if VARIABLE product has no variations
}
return true;
},
message: (props) =>
props.instance.productType === SIMPLE
? "A SIMPLE product must not have any variations."
: "A VARIABLE product must have at least one variation.",
},
},
publishDate: {
type: Date,
Expand Down

0 comments on commit b003661

Please sign in to comment.