Skip to content

Commit

Permalink
chg: schema related changes for product
Browse files Browse the repository at this point in the history
  • Loading branch information
wemakshaychavan committed Feb 24, 2023
1 parent b693e61 commit 63a4a87
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 18 deletions.
10 changes: 2 additions & 8 deletions seller-app-api/services/category.service.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import HttpRequest from '../utils/HttpRequest';
var config = require('../lib/config');
// const strapiAccessToken = '1465d1ca50726c39d0a764ba345121bc594f4923367de7d8ce57c779c0b3a3fd64eecbd4268e5e8818a57068f0f48b1b7d3a4ec20cfeb55e48bf902283c318b8b9fbe7f6fd9e86e813eab18acbd38075a2389bd5e5eb73fe1ba9607d3f9e7b00a5cc46c8dcf617734f52ec0e91b90d167a180bba4ed1f0a7d7ad026f28c5aad2'
const strapiAccessToken = config.get("strapi").apiToken
const strapiURI = config.get("strapi").serverUrl

const strapiURI = config.get("seller").serverUrl
class CategoryService {

async list(){

let headers = {};
headers['Authorization'] = `Bearer ${strapiAccessToken}`;

let httpRequest = new HttpRequest(
strapiURI,
Expand All @@ -26,7 +25,6 @@ class CategoryService {
async get(id){

let headers = {};
headers['Authorization'] = `Bearer ${strapiAccessToken}`;

let httpRequest = new HttpRequest(
strapiURI,
Expand All @@ -44,8 +42,6 @@ class CategoryService {
async update(data,id){

let headers = {};
headers['Authorization'] = `Bearer ${strapiAccessToken}`;

console.log(data)
let httpRequest = new HttpRequest(
strapiURI,
Expand All @@ -63,8 +59,6 @@ class CategoryService {
async create(data){

let headers = {};
headers['Authorization'] = `Bearer ${strapiAccessToken}`;

let httpRequest = new HttpRequest(
strapiURI,
'/api/categories',
Expand Down
2 changes: 0 additions & 2 deletions seller-app-api/services/logistics.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import HttpRequest from "../utils/HttpRequest";
import {getProducts, getSelect, getInit, getConfirm,getTrack,getSupport} from "../utils/schemaMapping";
import {sequelize, Sequelize,InitRequest, ConfirmRequest, SelectRequest} from '../models'

const strapiAccessToken = config.get("strapi").apiToken
const strapiURI = config.get("strapi").serverUrl
const BPP_ID = config.get("sellerConfig").BPP_ID
const BPP_URI = config.get("sellerConfig").BPP_URI
const sellerPickupLocation = config.get("sellerConfig").sellerPickupLocation
Expand Down
4 changes: 3 additions & 1 deletion seller/app/lib/utils/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const MESSAGES = {
'Unathorized access. Please login again to continue',
ORGANIZATION_NOT_EXISTS: 'We weren\'t able to find an organization with this details',
ORGANIZATION_ALREADY_EXISTS:
'An organization with this name already exists. Please select a new one, or login to the existing organization',
'An organization with this name already exists.',
PRODUCT_ALREADY_EXISTS:
'A product with this name already exists.',
ROLE_NOT_EXISTS: 'We couldn\'t find the role with given name',
ROLE_ALREADY_EXISTS: 'A Role with the given name already exists',
FORGOT_PASSWORD_SUCCESS: 'A temporary password is sent to your email address',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class AuthenticationController {
**/
login(req, res, next) {
const data = req.body;

authenticationService
.login(req.user, data)
.then(({ user, token }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OrganizationController {
return res.send(organization);

} catch (error) {
console.log('[OrganizationController] [create] Error -', error);
console.log('[ProductController] [create] Error -', error);
next(error);
}
}
Expand All @@ -39,7 +39,7 @@ class OrganizationController {
return res.send(organizations);

} catch (error) {
console.log('[OrganizationController] [list] Error -', error);
console.log('[ProductController] [list] Error -', error);
next(error);
}
}
Expand All @@ -57,7 +57,7 @@ class OrganizationController {
return res.send(organizations);

} catch (error) {
console.log('[OrganizationController] [get] Error -', error);
console.log('[ProductController] [get] Error -', error);
next(error);
}
}
Expand All @@ -70,7 +70,7 @@ class OrganizationController {
return res.send(organizations);

} catch (error) {
console.log('[OrganizationController] [get] Error -', error);
console.log('[ProductController] [get] Error -', error);
next(error);
}
}
Expand All @@ -82,7 +82,7 @@ class OrganizationController {
return res.send(organizations);

} catch (error) {
console.log('[OrganizationController] [get] Error -', error);
console.log('[ProductController] [get] Error -', error);
next(error);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class AuthenticationService {
const tokenPayload = {
user: {
id: currentUser.id,
role:currentUser.role
role:currentUser.role,
organization:currentUser?.organization?._id
},
lastLoginAt: loginTimestamp,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class OrganizationService {
};
return organizationData;
} catch (err) {
console.log('[OrganizationService] [getAll] Error in getting all organization ',err);
console.log('[ProductService] [getAll] Error in getting all organization ',err);
throw err;
}
}
Expand Down
63 changes: 63 additions & 0 deletions seller/app/modules/product/controllers/product.controller.js
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;
56 changes: 56 additions & 0 deletions seller/app/modules/product/models/product.model.js
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;
35 changes: 35 additions & 0 deletions seller/app/modules/product/routes/product.route.js
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 seller/app/modules/product/v1/middleware/api.params.validator.js
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);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// export { default as task } from './task.validation.schema';

0 comments on commit 63a4a87

Please sign in to comment.