Skip to content

Commit

Permalink
Merge pull request #446 from bounswe/(BKND)-Vendor-Product-Management…
Browse files Browse the repository at this point in the history
…---Update-#319

(bknd) vendor product management   update #319
  • Loading branch information
vlcanunal authored Jan 21, 2021
2 parents ade7e7f + 501c4f4 commit 91cc92b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
10 changes: 10 additions & 0 deletions backend/account_service/constants/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const ErrorMessage = {
MISSING_PARAMETER: "Missing parameter, see documentation for details",
ADDRESS_ALREADY_EXISTS: "Address with the title already exists",
USER_NOT_FOUND: "User not found",
CHECK_UPDATE_PARAMETERS:"Check your update parameters"
};

module.exports.ErrorCode = (message) =>{
const ErrorCodes = {
EMAIL_HAS_BEEN_USED: 400,
COULD_NOT_CREATE_USER: 400,
MISSING_PARAMETER: 400,
USER_NOT_FOUND: 404,

PRODUCT_NOT_FOUND: "Product not found",
};

Expand Down
12 changes: 6 additions & 6 deletions backend/vendor_service/routes/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ module.exports.initialize = (app) => {
});


app.get("/vendor/products", async (request, response) => {
const result = await vendor.getProducts(request.query);
app.patch("/vendor/products/:id", async (request, response) => {
var productId = request.params.id
var changeParameters = request.body;

if ( !!result.success ) {
response.respond(ErrorCode(result.message), result.message);
}
else if (!!result.productList) {
const result = await vendor.updateProduct(productId,changeParameters);

if (!!result.name) {
response.respond(200, "OK", {
result,
});
Expand Down
41 changes: 41 additions & 0 deletions backend/vendor_service/views/vendor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,47 @@ const { ErrorMessage } = require("../constants/error");



module.exports.updateProduct = async (product_id,parameter) => {
try {
var innerParameter = parameter;
if(!!parameter.attributes){
product = await Product.findOne({ _id: ObjectId(product_id) });
product = product.toJSON()

var productAttributes = []
product.productInfos.forEach(function(item){
if(JSON.stringify(item.attributes) == JSON.stringify(parameter.attributes)){
productAttributes.push(parameter)
}else{
productAttributes.push(item)
}

})

innerParameter = {"productInfos": productAttributes};
}

checker = Product.findByIdAndUpdate(product_id, innerParameter,
function (err, docs) {
if (err){
console.log(err)
}
else{
return true
}
});

if(checker){
product = await Product.findOne({ _id: ObjectId(product_id) });
return product
}

} catch (error) {
console.log(error);
return "CHECK_UPDATE_PARAMETERS";
}
};

module.exports.addProducts = async (products) => {
try {
const dbProducts = [];
Expand Down

0 comments on commit 91cc92b

Please sign in to comment.