Skip to content

Commit

Permalink
change res to json
Browse files Browse the repository at this point in the history
  • Loading branch information
MostafaRastegar committed Jun 19, 2020
1 parent 24c5ca4 commit 679ceb3
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ API_URL=/api/v1
MONGO_USERNAME=gateway
MONGO_PASSWORD=secret
MONGO_PORT=27017
MONGO_DB=gateway
MONGO_DB=gateway
MONGO_HOSTNAME=localhost
33 changes: 23 additions & 10 deletions controller/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ const MongoClient = require("mongodb").MongoClient;
const assert = require("assert");
const dotenv = require("dotenv");
dotenv.config();
const { MONGO_DB, MONGO_USERNAME, MONGO_PASSWORD, MONGO_HOSTNAME, MONGO_PORT} = process.env;
const dbUrl = `mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;

const {
MONGO_DB,
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_HOSTNAME,
MONGO_PORT,
} = process.env;
const checkedMongoUserPass =
MONGO_USERNAME && MONGO_PASSWORD
? MONGO_USERNAME + ":" + MONGO_PASSWORD + "@"
: "";
const dbUrl = `mongodb://${checkedMongoUserPass}${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource=admin`;
const mrConnect = (callFunc) => {
MongoClient.connect(dbUrl, function (err, client) {
assert.equal(null, err);
Expand Down Expand Up @@ -44,7 +53,7 @@ const mrInitCollections = () => {
refId: "e363b21d-c8fa-445e-8c8d-d6b2c1250c8e",
saleOrderId: "3355",
saleReferenceId: 9012415,
resCode: "0"
resCode: "0",
};
mrCheckAndInsert(db, dbCollections, inputUserData, "users");
mrCheckAndInsert(db, dbCollections, inputDataTransaction, "transactions");
Expand Down Expand Up @@ -75,12 +84,16 @@ const mrInsertOne = (collectionName, input, callFunc) => {
};

const mrUpdate = (collectionName, input, callFunc) => {
const { refId,resCode } = input;
const { refId, resCode } = input;
mrConnect((db, client) => {
db.collection(collectionName).update({"refId": refId},{$set: { "resCode": resCode}}, (findErr, addResult) => {
if (findErr) throw findErr;
callFunc(input);
});
db.collection(collectionName).update(
{ refId: refId },
{ $set: { resCode: resCode } },
(findErr, addResult) => {
if (findErr) throw findErr;
callFunc(input);
}
);
client.close();
});
};
Expand All @@ -91,5 +104,5 @@ module.exports = {
mrInsertOne,
mrInitCollections,
mrCheckAndInsert,
mrUpdate
mrUpdate,
};
9 changes: 4 additions & 5 deletions controller/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PaymentController {
terminalId,
});
if (!user) {
return res.status(404).send({
return res.status(404).json({
success: "false",
message: "Username or userPassword incorrect",
data: {},
Expand All @@ -51,7 +51,7 @@ class PaymentController {
saleReferenceId: Math.floor(Math.random() * 10000000),
};
mrInsertOne("transactions", inputData, (data) => {
return res.status(200).send({
return res.status(200).json({
success: "true",
message: "Create refId successfully",
data: {
Expand All @@ -71,8 +71,7 @@ class PaymentController {
return item.refId === refId;
});
if (!transaction) {
res.send("");
return res.status(404).send({
return res.status(404).json({
success: "false",
message: "transaction not found",
data: {},
Expand All @@ -90,7 +89,7 @@ class PaymentController {
};
postCompletePayment = (req, res) => {
const inputData = req.body;
// inputData is {refId, refCode}
// inputData is {refId, resCode}
mrUpdate("transactions", inputData, (transData) => {
mrFindAll("transactions", (data) => {
const transaction = data.find((item) => {
Expand Down
38 changes: 19 additions & 19 deletions controller/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,55 @@ class UserController {
title: "Add Collections to database successfully",
},
});
}
getAllUsers = (req, res) => {
};
getAllUsers = (req, res) => {
mrFindAll("users", (data) => {
return res.status(200).send({
return res.status(200).json({
success: "true",
message: "users retrieved successfully",
data,
});
});
}
};

getAllTransaction = (req, res) => {
getAllTransaction = (req, res) => {
mrFindAll("transactions", (data) => {
return res.status(200).send({
return res.status(200).json({
success: "true",
message: "transactions retrieved successfully",
data,
});
});
}
};

getOneUser = (data,userParams) => {
const user = data.find((userItem) => (
getOneUser = (data, userParams) => {
const user = data.find(
(userItem) =>
userItem.userName === userParams.userName &&
userItem.userPassword === userParams.userPassword &&
userItem.terminalId === parseInt(userParams.terminalId)
)
);
return user;
}
};

loginUser = (req, res) => {
loginUser = (req, res) => {
const userParams = req.body;
mrFindAll("users", (data) => {
const user = this.getOneUser(data, userParams);
if (!user) {
return res.status(404).send({
success: 'false',
message: 'user not found',
return res.status(404).json({
success: "false",
message: "user not found",
data: [],
});
}
return res.status(200).send({
success: 'true',
message: 'user retrieved successfully',
return res.status(200).json({
success: "true",
message: "user retrieved successfully",
data: user,
});
});
}
};
}

const userController = new UserController();
Expand Down
103 changes: 22 additions & 81 deletions postman/mr-gateway.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"method": "POST",
"header": [
{
"warning": "This is a duplicate header and will be overridden by the Content-Type header generated by Postman.",
"key": "Content-Type",
"value": "application/json"
}
Expand All @@ -35,80 +36,32 @@
"response": []
},
{
"name": "payRequet",
"name": "payRequest",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
"value": "application/json"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "orderId",
"value": "3355",
"type": "text"
},
{
"key": "callBackUrl",
"value": "{{callBackUrl}}",
"type": "text"
},
{
"key": "amount",
"value": "650000",
"type": "text"
},
{
"key": "userName",
"value": "mr_gateway",
"type": "text"
},
{
"key": "userPassword",
"value": "mr_gateway_123456",
"type": "text"
},
{
"key": "terminalId",
"value": "442530",
"type": "text"
},
{
"key": "localDate",
"value": "20201606",
"type": "text"
},
{
"key": "localTime",
"value": "101920",
"type": "text"
},
{
"key": "payerId",
"value": "10",
"type": "text"
},
{
"key": "additionalData",
"value": "this is for test",
"type": "text"
}
],
"mode": "raw",
"raw": "{\n \"orderId\": 3355,\n \"callBackUrl\": \"http://localhost:4001\",\n \"amount\": 650000,\n \"userName\": \"mr_gateway\",\n \"userPassword\": \"mr_gateway_123456\",\n \"terminalId\": 442530,\n \"localDate\": 20201606,\n \"localTime\": 101920,\n \"payerId\": 10,\n \"additionalData\": \"this is for test\"\n}",
"options": {
"raw": {
"language": "json"
},
"urlencoded": {}
}
},
"url": {
"raw": "{{serverUrl}}/payRequet",
"raw": "{{serverUrl}}/payRequest",
"host": [
"{{serverUrl}}"
],
"path": [
"payRequet"
"payRequest"
]
}
},
Expand All @@ -121,19 +74,16 @@
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
"value": "application/json"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "refId",
"value": "8c5e2874-2911-4471-a2cd-3f7a4b2a162c",
"type": "text"
}
],
"mode": "raw",
"raw": "{\n \"refId\":\"8c5e2874-2911-4471-a2cd-3f7a4b2a162c\"\n}",
"options": {
"raw": {
"language": "json"
},
"urlencoded": {}
}
},
Expand All @@ -156,25 +106,16 @@
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
"value": "application/json"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "resCode",
"value": "17",
"description": "0 = success , 17 = failed",
"type": "text"
},
{
"key": "refId",
"value": "8c5e2874-2911-4471-a2cd-3f7a4b2a162c",
"type": "text"
}
],
"mode": "raw",
"raw": "{\n \"resCode\": 17,\n \"refId\": \"8c5e2874-2911-4471-a2cd-3f7a4b2a162c\"\n}",
"options": {
"raw": {
"language": "json"
},
"urlencoded": {}
}
},
Expand Down
2 changes: 1 addition & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ router.get(`${process.env.API_URL}/transactions`, userController.getAllTransacti
router.post(`${process.env.API_URL}/login`, userController.loginUser);

// Post Pay request from user website
router.post(`${process.env.API_URL}/payRequet`, paymentController.postPayRequest);
router.post(`${process.env.API_URL}/payRequest`, paymentController.postPayRequest);

// Post Pay Action from user => success or failed
router.post(`${process.env.API_URL}/payAction`, paymentController.postPayAction);
Expand Down

0 comments on commit 679ceb3

Please sign in to comment.