Skip to content

Commit

Permalink
#100 backend
Browse files Browse the repository at this point in the history
  • Loading branch information
fdhhhdjd committed Oct 31, 2022
1 parent 92ff715 commit 6a6b6b4
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 204 deletions.
1 change: 0 additions & 1 deletion backend/src/v1/user_api/controllers/carousel.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { returnReasons } = require("../../middlewares/handleError");
const {
handleGetallCarousel,
} = require("../services/carousel.service/carousel.service");
const HELPER = require("../../utils/helper");
const carouselCtrl = {
getallCarousel: async (req, res) => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = {
{ $sort: { latest: -1 } },
]);

await set(
set(
"carousel",
JSON.stringify(carousels),
CONSTANTS._1_DAYS_REDIS + number_random
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
const number_random = HELPER.randomNumber();

const category = await Category.find();
await set(
set(
"categories",
JSON.stringify(category),
CONSTANTS._1_DAYS_REDIS + number_random
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
status: 400,
success: false,
element: {
msg: "Please fill in full infomation",
msg: "Please fill in full information",
},
};
}
Expand All @@ -30,30 +30,40 @@ module.exports = {
},
};
}
await sendFeedback(
fullname,
email,
subject,
content,
read_at,
time_log_gmt7_string
);

await RedisPub(
"user_feedback",
JSON.stringify({
email,
return Promise.all([
sendFeedback(
fullname,
content,
email,
subject,
})
);
return {
status: 200,
success: true,
element: {
msg: "Send Feedback Successfully !!",
},
};
content,
read_at,
time_log_gmt7_string
),
RedisPub(
"user_feedback",
JSON.stringify({
email,
fullname,
content,
subject,
})
)
]).then(result => {
return {
status: 200,
success: true,
element: {
msg: "Send Feedback Successfully !!",
},
};
}).catch((err) => {
return {
status: 400,
success: true,
element: {
msg: "Send Feedback Fail !!",
},
};
})
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
},
handleDeleteFlagOrders: async ({ order_id }) => {
try {
await Payments.findOneAndUpdate(
Payments.findOneAndUpdate(
{ _id: order_id },
{
deleteAt: CONSTANTS.DELETED_ENABLE,
Expand All @@ -41,7 +41,13 @@ module.exports = {
},
};
} catch (error) {
return res.status(500).json({ msg: err.message });
return {
status: 503,
success: false,
element: {
msg: "Server busy !!",
},
};
}
},
handleDetailOrders: async ({ order_id }) => {
Expand All @@ -55,7 +61,13 @@ module.exports = {
},
};
} catch (error) {
return res.status(500).json({ msg: err.message });
return {
status: 503,
success: false,
element: {
msg: "Server busy !!",
},
};
}
},
};
230 changes: 130 additions & 100 deletions backend/src/v1/user_api/services/payment.service/payment.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,125 +10,155 @@ const Products = require("../../../models/ProductModel");
const STORAGE = require("../../../utils/storage");
module.exports = {
handlePaymentTotal: async ({ user_id }) => {
if (!user_id) {
try {
if (!user_id) {
return {
status: 400,
success: false,
element: {
msg: "User Fail !",
},
};
}
const data = await hgetall(user_id);
const quantity_sum = await sumQuantity({ user_id });

var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
quantity_sum,
});
}
let total = 0;
let total_apply_voucher = 0;
const voucher = await get(`voucher_userId:${user_id}`);
for (let i = 0; i < cart.length; i++) {
total += cart[i].cart[0].price * cart[i].quantity_sum;
}
total_apply_voucher = (total * JSON.parse(voucher)) / 100;
return {
status: 200,
success: true,
element: {
total,
voucher: JSON.parse(voucher),
total_apply_voucher,
},
};
} catch (error) {
return {
status: 400,
status: 503,
success: false,
element: {
msg: "User Fail !",
msg: "Server busy !!",
},
};
}
const data = await hgetall(user_id);
const quantity_sum = await sumQuantity({ user_id });

var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
quantity_sum,
});
}
let total = 0;
let total_apply_voucher = 0;
const voucher = await get(`voucher_userId:${user_id}`);
for (let i = 0; i < cart.length; i++) {
total += cart[i].cart[0].price * cart[i].quantity_sum;
}
total_apply_voucher = (total * JSON.parse(voucher)) / 100;
return {
status: 200,
success: true,
element: {
total,
voucher: JSON.parse(voucher),
total_apply_voucher,
},
};
},
handleCheckInStock: async ({ user_id }) => {
const data = await hgetall(user_id);
var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
});
}
let stockAvailable = true;
let outOfStock = [];
for (let i = 0; i < cart.length; i++) {
if (cart[i].cart[0].countInStock === 0) {
stockAvailable = false;
outOfStock.push({
outOfStock: cart[i].cart[0],
try {
const data = await hgetall(user_id);
var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
});
}
let stockAvailable = true;
let outOfStock = [];
for (let i = 0; i < cart.length; i++) {
if (cart[i].cart[0].countInStock === 0) {
stockAvailable = false;
outOfStock.push({
outOfStock: cart[i].cart[0],
});
}
}
return {
status: 200,
success: true,
element: {
stockAvailable,
outOfStock,
},
};
} catch (error) {
return {
status: 503,
success: false,
element: {
msg: "Server busy !!",
},
};
}
return {
status: 200,
success: true,
element: {
stockAvailable,
outOfStock,
},
};
},
handlePaymentPaypal: async ({ user_id, paymentID, address }) => {
const data = await hgetall(user_id);
var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
quantity: data[key],
try {
const data = await hgetall(user_id);
var cart = [];
for (var key in data) {
cart.push({
cart: await Products.find({ _id: key }),
quantity: data[key],
});
}
let total = 0;
let total_apply_voucher = 0;
const voucher = await get(`voucher_userId:${user_id}`);
for (let i = 0; i < cart.length; i++) {
total += cart[i].cart[0].price * cart[i].quantity;
}
total_apply_voucher = (total * JSON.parse(voucher)) / 100;
const { success, element } = await createPayment({
user_id,
cart,
paymentID,
address,
total,
total_apply_voucher,
voucher: JSON.parse(voucher),
});
}
let total = 0;
let total_apply_voucher = 0;
const voucher = await get(`voucher_userId:${user_id}`);
for (let i = 0; i < cart.length; i++) {
total += cart[i].cart[0].price * cart[i].quantity;
}
total_apply_voucher = (total * JSON.parse(voucher)) / 100;
const { success, element } = await createPayment({
user_id,
cart,
paymentID,
address,
total,
total_apply_voucher,
voucher: JSON.parse(voucher),
});
if (!success) {
if (!success) {
return {
status: 400,
success: false,
element: {
msg: "Payment Paypal Fail !!!",
},
};
}
RedisPub(
"user_payment_success",
JSON.stringify({
email: element?.email,
name: element?.name,
})
);
for (let i = 0; i < cart.length; i++) {
STORAGE.sold(cart[i].cart[0]._id, cart[i].quantity, cart[i].cart[0].sold);
STORAGE.stock(
cart[i].cart[0]._id,
cart[i].quantity,
cart[i].cart[0].countInStock
);
}
del(`cartUserId:${user_id}`);
return {
status: 400,
status: 200,
success: true,
element: {
msg: "Payment Paypal Success !!",
},
};
} catch (error) {
return {
status: 503,
success: false,
element: {
msg: "Payment Paypal Fail !!!",
msg: "Server busy !!",
},
};
}
await RedisPub(
"user_payment_success",
JSON.stringify({
email: element?.email,
name: element?.name,
})
);
for (let i = 0; i < cart.length; i++) {
STORAGE.sold(cart[i].cart[0]._id, cart[i].quantity, cart[i].cart[0].sold);
STORAGE.stock(
cart[i].cart[0]._id,
cart[i].quantity,
cart[i].cart[0].countInStock
);
}
await del(`cartUserId:${user_id}`);
return {
status: 200,
success: true,
element: {
msg: "Payment Paypal Success !!",
},
};
},
};
Loading

0 comments on commit 6a6b6b4

Please sign in to comment.