-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chg: schema related changes for product
- Loading branch information
1 parent
b693e61
commit 63a4a87
Showing
12 changed files
with
197 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
seller/app/modules/product/controllers/product.controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import ProductService from '../v1/services/product.service'; | ||
|
||
const productService = new ProductService(); | ||
|
||
class ProductController { | ||
|
||
async create(req, res, next) { | ||
try { | ||
const data = req.body; | ||
data.organization = req.user.organization | ||
const product = await productService.create(data); | ||
return res.send(product); | ||
|
||
} catch (error) { | ||
console.log('[ProductController] [create] Error -', error); | ||
next(error); | ||
} | ||
} | ||
|
||
|
||
async list(req, res, next) { | ||
try { | ||
const query = req.query; | ||
query.offset = parseInt(query.offset); | ||
query.limit = parseInt(query.limit); | ||
const products = await productService.list(query); | ||
return res.send(products); | ||
|
||
} catch (error) { | ||
console.log('[ProductController] [list] Error -', error); | ||
next(error); | ||
} | ||
} | ||
|
||
async search(req, res, next) { | ||
try { | ||
const query = req.query; | ||
query.offset = 0; | ||
query.limit = 50;//default only 50 products will be sent | ||
const products = await productService.search(query); | ||
return res.send(products); | ||
|
||
} catch (error) { | ||
console.log('[ProductController] [list] Error -', error); | ||
next(error); | ||
} | ||
} | ||
|
||
async get(req, res, next) { | ||
try { | ||
const params = req.params; | ||
const product = await productService.get(params.organizationId); | ||
return res.send(product); | ||
|
||
} catch (error) { | ||
console.log('[ProductController] [get] Error -', error); | ||
next(error); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default ProductController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import mongoose from'mongoose'; | ||
import { uuid } from 'uuidv4'; | ||
const productSchema = new mongoose.Schema({ | ||
_id:{ | ||
type: String, | ||
required:true, | ||
default: () => uuid(), | ||
}, | ||
productCode: {type:String}, | ||
productName: {type:String,required:true}, | ||
MRP: {type:Number}, | ||
retailPrice: {type:Number}, | ||
purchasePrice: {type:Number}, | ||
HSNCode: {type:String}, | ||
GST_Percentage: {type:Number}, | ||
productCategory: {type:String}, | ||
productSubcategory1: {type:String}, | ||
productSubcategory2: {type:String}, | ||
productSubcategory3: {type: String}, | ||
quantity: {type:Number}, | ||
barcode: {type:Number}, | ||
maxAllowedQty: {type:Number}, | ||
packQty:{type:Number}, | ||
UOM: {type:String},//units of measure | ||
length: {type:Number}, | ||
breadth: {type:Number}, | ||
height: {type:Number}, | ||
weight: {type:Number}, | ||
isReturnable: {type:Boolean}, | ||
returnWindow: {type:String}, | ||
isVegetarian: {type:Boolean}, | ||
manufacturerName: {type:String}, | ||
manufacturedDate: {type:String}, | ||
nutritionalInfo: {type:String}, | ||
additiveInfo: {type:String}, | ||
instructions: {type:String}, | ||
isCancellable: {type:Boolean}, | ||
availableOnCod: {type:Boolean}, | ||
longDescription: {type:String}, | ||
description: {type:String}, | ||
organization: { type: String, ref: 'Organization' }, | ||
images: {type:Array}, | ||
updatedAt:{ | ||
type:Number, | ||
default:Date.now() | ||
}, | ||
createdBy:{type:String} | ||
},{ | ||
strict: true, | ||
timestamps:true | ||
}); | ||
|
||
|
||
productSchema.index({name:1}, {unique: false}); | ||
const Product = mongoose.model('Product',productSchema); | ||
module.exports = Product; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import ProductController from '../controllers/product.controller'; | ||
import apiParamsValidator from '../v1/middleware/api.params.validator'; | ||
import productSchema from '../v1/validationSchema/api-params-validation-schema/product.validate.schema'; | ||
import express from 'express'; | ||
import {authentication, authorisation} from "../../../lib/middlewares"; | ||
import {SYSTEM_ROLE} from "../../../lib/utils/constants"; | ||
const router = express.Router(); | ||
|
||
const productController = new ProductController(); | ||
|
||
router.post('/v1/products', | ||
authentication.middleware(), | ||
authorisation.middleware({roles: [SYSTEM_ROLE.ORG_ADMN]}), | ||
apiParamsValidator.middleware({ schema: productSchema.create() }), | ||
productController.create); | ||
|
||
router.get('/v1/products', | ||
authentication.middleware(), | ||
authorisation.middleware({roles: [SYSTEM_ROLE.ORG_ADMN]}), | ||
apiParamsValidator.middleware({ schema: productSchema.list() }), | ||
productController.list, | ||
); | ||
|
||
router.get('/v1/products/search', | ||
productController.search, | ||
); | ||
|
||
router.get('/v1/products/:productId', | ||
authentication.middleware(), | ||
apiParamsValidator.middleware({ schema: productSchema.get() }), | ||
productController.get, | ||
); | ||
|
||
module.exports = router; |
27 changes: 27 additions & 0 deletions
27
seller/app/modules/product/v1/middleware/api.params.validator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { BadRequestParameterError } from '../../../../lib/errors'; | ||
|
||
exports.middleware = (options) => async (req, res, next) => { | ||
try { | ||
let data; | ||
|
||
let { schema } = options; | ||
|
||
// Read data from request body | ||
if (['POST', 'PUT', 'PATCH'].includes(req.method.toUpperCase())) { | ||
data = req.body; | ||
} else { | ||
// Read data from request query params | ||
data = req.params; | ||
} | ||
|
||
const { error } = schema.validate(data); | ||
if (error) { | ||
next(new BadRequestParameterError(error)); | ||
} else { | ||
next(); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
next(err); | ||
} | ||
}; |
2 changes: 2 additions & 0 deletions
2
seller/app/modules/product/v1/validationSchema/api-params-validation-schema/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
// export { default as task } from './task.validation.schema'; |