Skip to content

Commit

Permalink
Merge pull request idurar#588 from idurar/feat/add-order-api
Browse files Browse the repository at this point in the history
add order api and page
  • Loading branch information
salahlalami authored Oct 22, 2023
2 parents 1020c9b + f84d899 commit 86f9d43
Show file tree
Hide file tree
Showing 30 changed files with 785 additions and 25 deletions.
2 changes: 2 additions & 0 deletions backend/controllers/appControllers/inventoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const createCRUDController = require('@/controllers/middlewaresControllers/createCRUDController');
module.exports = createCRUDController('Inventory');
2 changes: 2 additions & 0 deletions backend/controllers/appControllers/kycController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const createCRUDController = require('@/controllers/middlewaresControllers/createCRUDController');
module.exports = createCRUDController('Kyc');
2 changes: 2 additions & 0 deletions backend/controllers/appControllers/orderController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const createCRUDController = require('@/controllers/middlewaresControllers/createCRUDController');
module.exports = createCRUDController('Order');
3 changes: 2 additions & 1 deletion backend/middlewares/setFilePathToBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module.exports = (req, res, next) => {
console.log('🚀 ~ file: setFilePathToBody.js:4 ~ req.file:', req.file);
if (req.file) {
req.body.photo = req.file.path;
console.log('🚀 ~ file: setFilePathToBody.js:5 ~ req.file:', req.file);
req.body.filePath = req.file.path;
console.log('🚀 ~ file: setFilePathToBody.js:6 ~ req.file.path:', req.file.path);
}
// if (req.files) {
Expand Down
34 changes: 34 additions & 0 deletions backend/models/appModels/Inventory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const inventorySchema = new mongoose.Schema({
removed: {
type: Boolean,
default: false,
},
enabled: {
type: Boolean,
default: true,
},
product: {
type: String,
trim: true,
required: true,
},
quantity: {
type: Number,
required: true,
min: 0, // Ensure non-negative numbers
},
unitPrice: {
type: Number,
required: true,
min: 0, // Ensure non-negative numbers
},
created: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model('Inventory', inventorySchema);
38 changes: 38 additions & 0 deletions backend/models/appModels/Kyc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const kycSchema = new mongoose.Schema({
removed: {
type: Boolean,
default: false,
},
enabled: {
type: Boolean,
default: true,
},
created: {
type: Date,
default: Date.now,
},
// Fields for shipping
name: {
type: String,
trim: true,
required: true,
},
address: {
type: String,
trim: true,
required: true,
},
contact: {
type: String,
required: true,
},
filePath: {
type: String,
trim: true,
},
});

module.exports = mongoose.model('Kyc', kycSchema);
47 changes: 47 additions & 0 deletions backend/models/appModels/Order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const orderSchema = new mongoose.Schema({
removed: {
type: Boolean,
default: false,
},
enabled: {
type: Boolean,
default: true,
},
created: {
type: Date,
default: Date.now,
},
// Fields for shipping
orderId: {
type: String,
trim: true,
required: true,
},
products: {
type: String, // Consider changing this to an array of objects if you have multiple products
trim: true,
required: true,
},
quantity: {
type: Number,
required: true,
},
price: {
type: Number,
required: true,
},
status: {
type: String,
enum: ['pending', 'shipped', 'delivered', 'cancelled'],
required: true,
},
notes: {
type: String,
trim: true,
},
});

module.exports = mongoose.model('Order', orderSchema);
Binary file added backend/public/uploads/kyc/1697989633969.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697990168699.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991127114.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991302201.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991309718.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991461479.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991486063.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/public/uploads/kyc/1697991872524.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 55 additions & 1 deletion backend/routes/appRoutes/appApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { catchErrors } = require('@/handlers/errorHandlers');

const router = express.Router();

const multer = require('multer');
const path = require('path');
const setFilePathToBody = require('@/middlewares/setFilePathToBody');

const employeeController = require('@/controllers/appControllers/employeeController');
const paymentModeController = require('@/controllers/appControllers/paymentModeController');
const taxController = require('@/controllers/appControllers/taxController');
Expand All @@ -17,9 +21,12 @@ const supplierOrderController = require('@/controllers/appControllers/supplierOr
const expenseController = require('@/controllers/appControllers/expenseController');
const expenseCategoryController = require('@/controllers/appControllers/expenseCategoryController');
const paymentInvoiceController = require('@/controllers/appControllers/paymentInvoiceController');

const orderController = require('@/controllers/appControllers/orderController');
const offerController = require('@/controllers/appControllers/offerController');

const kycController = require('@/controllers/appControllers/kycController');
const inventoryController = require('@/controllers/appControllers/inventoryController');

// //_________________________________ API for employees_____________________
router.route('/employee/create').post(catchErrors(employeeController.create));
router.route('/employee/read/:id').get(catchErrors(employeeController.read));
Expand Down Expand Up @@ -165,4 +172,51 @@ router.route('/offer/filter').get(catchErrors(offerController.filter));
router.route('/offer/pdf/:id').get(catchErrors(offerController.generatePDF));
router.route('/offer/summary').get(catchErrors(offerController.summary));

// //_________________________________________________________________API for Order________________

router.route('/order/create').post(catchErrors(orderController.create));
router.route('/order/read/:id').get(catchErrors(orderController.read));
router.route('/order/update/:id').patch(catchErrors(orderController.update));
router.route('/order/delete/:id').delete(catchErrors(orderController.delete));
router.route('/order/search').get(catchErrors(orderController.search));
router.route('/order/list').get(catchErrors(orderController.list));
router.route('/order/filter').get(catchErrors(orderController.filter));

// //_________________________________________________________________API for Inventory

router.route('/inventory/create').post(catchErrors(inventoryController.create));
router.route('/inventory/read/:id').get(catchErrors(inventoryController.read));
router.route('/inventory/update/:id').patch(catchErrors(inventoryController.update));
router.route('/inventory/delete/:id').delete(catchErrors(inventoryController.delete));
router.route('/inventory/search').get(catchErrors(inventoryController.search));
router.route('/inventory/list').get(catchErrors(inventoryController.list));
router.route('/inventory/filter').get(catchErrors(inventoryController.filter));

// //_________________________________________________________________API for Kyc________________

const kycFileStorage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/kyc');
},
filename: function (req, file, cb) {
console.log('🚀 ~ file: appApi.js:182 ~ file:', file);
cb(null, Date.now() + path.extname(file.originalname));
},
});
const kycFileUpload = multer({ storage: kycFileStorage });

router
.route('/kyc/create')
.post(kycFileUpload.single('file'), setFilePathToBody, catchErrors(kycController.create));
router
.route('/kyc/update/:id')
.patch(kycFileUpload.single('file'), setFilePathToBody, catchErrors(kycController.update));

router.route('/kyc/read/:id').get(catchErrors(kycController.read));

router.route('/kyc/delete/:id').delete(catchErrors(kycController.delete));
router.route('/kyc/search').get(catchErrors(kycController.search));
router.route('/kyc/list').get(catchErrors(kycController.list));
router.route('/kyc/filter').get(catchErrors(kycController.filter));

module.exports = router;
6 changes: 6 additions & 0 deletions frontend/src/app/Navigation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import history from '@/utils/history';
import {
SettingOutlined,
CustomerServiceOutlined,
ShopOutlined,
ShoppingCartOutlined,
InboxOutlined,
FileTextOutlined,
FileSyncOutlined,
DashboardOutlined,
Expand All @@ -26,6 +29,9 @@ const SIDEBAR_MENU = [
{ key: '/lead', icon: <UserAddOutlined />, title: 'Lead' },
{ key: '/offer', icon: <FileOutlined />, title: 'Offer' },
{ key: '/customer', icon: <CustomerServiceOutlined />, title: 'Customer' },
// { key: '/order', icon: <ShopOutlined />, title: 'Order' },
// { key: '/inventory', icon: <InboxOutlined />, title: 'Inventory' },
// { key: '/kyc', icon: <ShoppingCartOutlined />, title: 'Kyc' },
{ key: '/invoice', icon: <FileTextOutlined />, title: 'Invoice' },
{ key: '/quote', icon: <FileSyncOutlined />, title: 'Quote' },
{ key: '/payment/invoice', icon: <CreditCardOutlined />, title: 'Payment Invoice' },
Expand Down
11 changes: 7 additions & 4 deletions frontend/src/components/CreateForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,26 @@ import { selectCreatedItem } from '@/redux/crud/selectors';
import { Button, Form } from 'antd';
import Loading from '@/components/Loading';

export default function CreateForm({ config, formElements }) {
export default function CreateForm({ config, formElements, withUpload = false }) {
let { entity } = config;
const dispatch = useDispatch();
const { isLoading, isSuccess } = useSelector(selectCreatedItem);
const { crudContextAction } = useCrudContext();
const { panel, collapsedBox, readBox } = crudContextAction;
const [form] = Form.useForm();
const onSubmit = (fieldsValue) => {
console.log('🚀 ~ file: index.jsx ~ line 19 ~ onSubmit ~ fieldsValue', fieldsValue);

// Manually trim values before submission

if (fieldsValue.file && withUpload) {
fieldsValue.file = fieldsValue.file[0].originFileObj;
}

const trimmedValues = Object.keys(fieldsValue).reduce((acc, key) => {
acc[key] = typeof fieldsValue[key] === 'string' ? fieldsValue[key].trim() : fieldsValue[key];
return acc;
}, {});

dispatch(crud.create({ entity, jsonData: trimmedValues }));
dispatch(crud.create({ entity, jsonData: trimmedValues, withUpload }));
};

useEffect(() => {
Expand Down
14 changes: 10 additions & 4 deletions frontend/src/components/UpdateForm/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { selectCurrentItem } from '@/redux/crud/selectors';
import { Button, Form } from 'antd';
import Loading from '@/components/Loading';

export default function UpdateForm({ config, formElements }) {
export default function UpdateForm({ config, formElements, withUpload = false }) {
let { entity } = config;
const dispatch = useDispatch();
const { current, isLoading, isSuccess } = useSelector(selectUpdatedItem);
Expand All @@ -31,10 +31,16 @@ export default function UpdateForm({ config, formElements }) {
const [form] = Form.useForm();

const onSubmit = (fieldsValue) => {
console.log('🚀 ~ file: index.jsx:34 ~ onSubmit ~ fieldsValue:', fieldsValue);

const id = current._id;
dispatch(crud.update({ entity, id, jsonData: fieldsValue }));

if (fieldsValue.file && withUpload) {
fieldsValue.file = fieldsValue.file[0].originFileObj;
}
const trimmedValues = Object.keys(fieldsValue).reduce((acc, key) => {
acc[key] = typeof fieldsValue[key] === 'string' ? fieldsValue[key].trim() : fieldsValue[key];
return acc;
}, {});
dispatch(crud.update({ entity, id, jsonData: trimmedValues, withUpload }));
};
useEffect(() => {
if (current) {
Expand Down
55 changes: 55 additions & 0 deletions frontend/src/forms/InventoryForm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { Form, Input, InputNumber } from 'antd';

export default function InventoryForm() {
// Renamed to InventoryForm for clarity
return (
<>
<Form.Item
label="Product"
name="product"
rules={[
{
required: true,
message: 'Please input Product name!',
},
]}
>
<Input />
</Form.Item>

<Form.Item
label="Quantity"
name="quantity"
rules={[
{
required: true,
message: 'Please input Quantity!',
type: 'number',
min: 0, // Ensure non-negative numbers
},
]}
>
<InputNumber />
</Form.Item>

<Form.Item
label="Unit Price"
name="unitPrice"
rules={[
{
required: true,
message: 'Please input Unit Price!',
type: 'number',
min: 0, // Ensure non-negative numbers
},
]}
>
<InputNumber
formatter={(value) => `$ ${value}`} // Optional: format value as currency
parser={(value) => value.replace(/\$\s?|(,*)/g, '')} // Optional: parse input as number
/>
</Form.Item>
</>
);
}
Loading

0 comments on commit 86f9d43

Please sign in to comment.