Refactoring an E-commerce Backend for Multi-Variant Pricing


With over four years of experience as a software quality assurance engineer specialized in API and frontend automation, I have a thorough grasp of the software development lifecycle. This has fuelled my interest in backend programming, prompting me to apply for the HNG 11 internship to hone my abilities while working with like-minded individuals to define application logic. You can visit their internship page or read about hiring from their pool of bright interns here for more information about the HNG Internship.
With that said, let's look at how I refactored an e-commerce application built with ExpressJs, which I'll name "easyBuy" for the sake of this article.
Problem Statement
EasyBuy's original code was made to handle products with just one pricing option. If things were to be sold at various prices depending on variations in colour and size, this became difficult. Different pricing for various product photos and sizes were difficult to present because the old system was unable to handle these differences.
The JSON request for the current implementation is available here.
{
"name": "string",
"status": "string",
"description": "string",
"isLatestProduct": true,
"categoryId": "string",
"collectionId": "string",
"price": 0,
"primaryImage": "string",
"secondImage": "string",
"thirdImage": "string",
}
Refactored Implementation
I utilised a "productVariants" array of objects in the re-engineered implementation. I was able to bind prices with corresponding colours and image. Thanks to this method, each product can have several alternatives, all contained within an array as an object. The product provides a range of these choices upon query, which aids the management and presentation of various product variant costs, colours, and sizes for every product in the systems database.
The JSON request for the current implementation is available here.
{
"name": "string",
"status": "string",
"description": "string",
"isLatestProduct": true,
"categoryId": "string",
"collectionId": "string",
"price": 0,
"productVariants": [
{
"color": "string",
"size": "string",
"price": 0,
"imageUrl": "string",
"isAvailable": true
},
{
"color": "string",
"size": "string",
"price": 0,
"imageUrl": "string",
"isAvailable": true
}
]
}
Other Modules Refactored
Due to these modifications, the order service had to be redesigned. It now requires the "productVariantId" and uses the data in the object to calculate the order price for that product variant
The JSON request for the current implementation is available here.
{
"productId": "string",
"productVariantId": "string"
"unit": 2,
"address": "string"
}
Subscribe to my newsletter
Read articles from Azeez Ibrahim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
