GWM : cors changes
This commit is contained in:
parent
0333b8d5ad
commit
dc3a1d824d
@ -3,10 +3,20 @@ const db = require("../models");
|
|||||||
const EstablishmentProduct = db.EstablishmentProduct;
|
const EstablishmentProduct = db.EstablishmentProduct;
|
||||||
const Product = db.Product;
|
const Product = db.Product;
|
||||||
const UnitMaster = db.UnitMaster;
|
const UnitMaster = db.UnitMaster;
|
||||||
|
|
||||||
|
|
||||||
exports.createEstablishmentProduct = async (req, res) => {
|
exports.createEstablishmentProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
req.body.created_by = req.body.created_by || req.user.id;
|
|
||||||
const data = await EstablishmentProduct.create(req.body);
|
const { establishment_id , product_id, action_done_by , created_by } = req.body;
|
||||||
|
|
||||||
|
const data = await EstablishmentProduct.create({
|
||||||
|
establishment_id,
|
||||||
|
product_id,
|
||||||
|
action_done_by,
|
||||||
|
created_by: created_by || req.user?.id || null,
|
||||||
|
});
|
||||||
|
|
||||||
res.status(201).send({'status':"success",'message':"Creation successful",'data': data });
|
res.status(201).send({'status':"success",'message':"Creation successful",'data': data });
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -65,9 +75,17 @@ exports.getEstablishmentProductById = async (req, res) => {
|
|||||||
exports.updateEstablishmentProduct = async (req, res) => {
|
exports.updateEstablishmentProduct = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
req.body.updated_by = req.body.updated_by || req.user.id;
|
const id = req.params.id;
|
||||||
req.body.updated_at = req.body.updated_at || new Date();
|
// whitelist fields
|
||||||
const [updated] = await EstablishmentProduct.update(req.body, { where: { id: req.params.id } });
|
const updateData = {};
|
||||||
|
if (req.body.establishment_id) updateData.establishment_id = req.body.establishment_id;
|
||||||
|
if (req.body.product_id) updateData.product_id = req.body.product_id;
|
||||||
|
if (req.body.action_done_by) updateData.action_done_by = req.body.action_done_by;
|
||||||
|
|
||||||
|
updateData.updated_by = req.body.updated_by || req.user?.id || null;
|
||||||
|
updateData.updated_at = new Date();
|
||||||
|
|
||||||
|
const [updated] = await EstablishmentProduct.update(updateData, { where: { id } });
|
||||||
|
|
||||||
if (!updated) res.status(404).send({'status':"failed",'message':"Record not found",'data': "" });
|
if (!updated) res.status(404).send({'status':"failed",'message':"Record not found",'data': "" });
|
||||||
|
|
||||||
|
|||||||
@ -23,20 +23,22 @@ const authJWT = (req, res, next) => {
|
|||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return res
|
return res
|
||||||
.status(401)
|
.status(403)
|
||||||
.json({ status: "failed", message: "Unauthorized: No token provided" });
|
.json({ status: "failed", message: "Access denied, token missing" });
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('token ='+token)
|
console.log('token ='+token)
|
||||||
|
|
||||||
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
|
||||||
|
if (err) return res.status(401).json({ message: "Invalid or expired token" });
|
||||||
// Attach user info to req for downstream handlers
|
|
||||||
req.user = decoded;
|
req.user = decoded;
|
||||||
next();
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res
|
return res
|
||||||
.status(401)
|
.status(403)
|
||||||
.json({ status: "failed", message: "Unauthorized: Invalid token" });
|
.json({ status: "failed", message: "Unauthorized: Invalid token" });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1245,9 +1245,12 @@ router.get("/download-sample-product-upload-file",[verifySignature, verifyToken]
|
|||||||
* product_id:
|
* product_id:
|
||||||
* type: integer
|
* type: integer
|
||||||
* example: 10
|
* example: 10
|
||||||
* is_active:
|
* action_done_by:
|
||||||
* type: boolean
|
* type: string
|
||||||
* example: true
|
* example: admin_users
|
||||||
|
* created_by:
|
||||||
|
* type: integer
|
||||||
|
* example: 1
|
||||||
* responses:
|
* responses:
|
||||||
* 201:
|
* 201:
|
||||||
* description: EstablishmentProduct created successfully
|
* description: EstablishmentProduct created successfully
|
||||||
@ -1339,9 +1342,12 @@ router.get("/establishment-products/:id",[verifySignature, verifyToken], establi
|
|||||||
* product_id:
|
* product_id:
|
||||||
* type: integer
|
* type: integer
|
||||||
* example: 10
|
* example: 10
|
||||||
* is_active:
|
* action_done_by:
|
||||||
* type: boolean
|
* type: string
|
||||||
* example: false
|
* example: admin_users
|
||||||
|
* updated_by:
|
||||||
|
* type: integer
|
||||||
|
* example: 1
|
||||||
* responses:
|
* responses:
|
||||||
* 200:
|
* 200:
|
||||||
* description: Record updated successfully
|
* description: Record updated successfully
|
||||||
|
|||||||
17
server.js
17
server.js
@ -14,10 +14,25 @@ const app = express();
|
|||||||
|
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
|
||||||
app.use(helmet());
|
app.use(helmet());
|
||||||
app.use(morgan("dev"));
|
app.use(morgan("dev"));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
|
// app.use(cors());
|
||||||
|
const allowedOrigins = [
|
||||||
|
"http://localhost:5173/", //local
|
||||||
|
"http://13.201.47.205:5173/", //dev
|
||||||
|
"http://13.201.47.205:5175/" //uat
|
||||||
|
];
|
||||||
|
app.use(cors({
|
||||||
|
origin: allowedOrigins,
|
||||||
|
credentials: true
|
||||||
|
}));
|
||||||
|
app.use((req, res, next) => {
|
||||||
|
res.header("Access-Control-Allow-Credentials", "true");
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// // Swagger setup
|
// // Swagger setup
|
||||||
// const swaggerOptions = {
|
// const swaggerOptions = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user