Skip to content

Commit

Permalink
get by id purchases and updatePurchaseState fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Chmod351 committed Jun 28, 2023
1 parent a60de15 commit e79731e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
4 changes: 2 additions & 2 deletions server/Controllers/purchasesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const purchaseController = {
};

function createPurchase(req, res, next) {
console.log(req.body);

purchaseService
.createPurchase(
req.user.id,
req.body.cartId,
req.body.amount,
req.body.address,
req.body.shippingAddress,
)
.then((purchase) => res.json(purchase))
.catch((error) => next(error));
Expand Down
2 changes: 1 addition & 1 deletion server/Models/purchasesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const PurchaseSchema = new mongoose.Schema(
shippingAddress: {
type: Object,
required: true,
maxLength: 100,
maxLength: 500,
minlength: 50,
},
shippingStatus: {
Expand Down
4 changes: 2 additions & 2 deletions server/Routes/purchasesRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ router.get(
);

router.delete(
'/purchases/:purchaseId',
'/purchases/:id',
authMiddleware,
purchaseController.cleanPurchase,
);

router.put(
'/purchases/:purchaseId',
'/purchases/:id',
authMiddleware,
adminCheck,
purchaseController.updatePurchaseState,
Expand Down
3 changes: 1 addition & 2 deletions server/View/productView.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ function isValidObjectId(id) {
}

async function getProductById(productId) {
console.log(productId);
if (productId && isValidObjectId(productId)) {
const product = await Product.findById(productId);
return product;
} else {
throw new NotFoundError('Product Not found');
throw new NotFoundError(`Product with id ${productId} Not found`);
}
}

Expand Down
35 changes: 29 additions & 6 deletions server/View/purchasesView.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Purchase from '../Models/purchasesModel.js';
import StripePaymentProvider from '../helpers/stripe.js';
import { NotFoundError } from '../helpers/errorHandler.js';
const paymentProvider = new StripePaymentProvider();

const purchaseService = {
Expand All @@ -12,23 +13,42 @@ const purchaseService = {
processPayment,
};

async function findPurchaseById(id) {
const purchase = await Purchase.findById(id);
if (purchase) {
return purchase;
} else {
throw new NotFoundError(`Purchase with id ${id} Not found`);
}
}

async function createPurchase(userId, cartId, amount, shippingAddress) {
const purchase = new Purchase({
userId,
cartId,
amount,
shippingAddress,
});

return await purchase.save();
}

async function getAllPurchase(page, size) {
// get a pagination with purchases instead all purchases
const pageNumber = parseInt(page) || 1;
const pageSize = parseInt(size) || 10;
const skipCount = (pageNumber - 1) * pageSize;
return await Purchase.find().skip(skipCount).limit(pageSize);
const actualPage = parseInt(page) || 1;
const pageSize = parseInt(size) || 8;
const skipCount = (actualPage - 1) * pageSize;

const totalCount = await Purchase.countDocuments();
const totalPages = Math.ceil(totalCount / pageSize);

const products = await Purchase.aggregate([
{ $sample: { size: totalCount } },
{ $skip: skipCount },
{ $limit: pageSize },
]);

return { products, totalPages };
}

async function getUserPurchase(id) {
Expand Down Expand Up @@ -60,17 +80,20 @@ async function getMonthly() {
}

async function cleanPurchase(id) {
await findPurchaseById(id);
return await Purchase.findByIdAndDelete(id);
}

async function updatePurchaseState(id, status) {
await findPurchaseById(id);
const newStatus = await Purchase.findOneAndUpdate(
{
purchaseId: id,
_id: id,
},
{ shippingStatus: status },
{ new: true },
);
console.log(newStatus);
return newStatus;
}

Expand Down

0 comments on commit e79731e

Please sign in to comment.