Skip to content

Commit e79731e

Browse files
committed
get by id purchases and updatePurchaseState fixed
1 parent a60de15 commit e79731e

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

server/Controllers/purchasesController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ const purchaseController = {
1111
};
1212

1313
function createPurchase(req, res, next) {
14-
console.log(req.body);
14+
1515
purchaseService
1616
.createPurchase(
1717
req.user.id,
1818
req.body.cartId,
1919
req.body.amount,
20-
req.body.address,
20+
req.body.shippingAddress,
2121
)
2222
.then((purchase) => res.json(purchase))
2323
.catch((error) => next(error));

server/Models/purchasesModel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const PurchaseSchema = new mongoose.Schema(
1616
shippingAddress: {
1717
type: Object,
1818
required: true,
19-
maxLength: 100,
19+
maxLength: 500,
2020
minlength: 50,
2121
},
2222
shippingStatus: {

server/Routes/purchasesRoutes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ router.get(
2020
);
2121

2222
router.delete(
23-
'/purchases/:purchaseId',
23+
'/purchases/:id',
2424
authMiddleware,
2525
purchaseController.cleanPurchase,
2626
);
2727

2828
router.put(
29-
'/purchases/:purchaseId',
29+
'/purchases/:id',
3030
authMiddleware,
3131
adminCheck,
3232
purchaseController.updatePurchaseState,

server/View/productView.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ function isValidObjectId(id) {
2525
}
2626

2727
async function getProductById(productId) {
28-
console.log(productId);
2928
if (productId && isValidObjectId(productId)) {
3029
const product = await Product.findById(productId);
3130
return product;
3231
} else {
33-
throw new NotFoundError('Product Not found');
32+
throw new NotFoundError(`Product with id ${productId} Not found`);
3433
}
3534
}
3635

server/View/purchasesView.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Purchase from '../Models/purchasesModel.js';
22
import StripePaymentProvider from '../helpers/stripe.js';
3+
import { NotFoundError } from '../helpers/errorHandler.js';
34
const paymentProvider = new StripePaymentProvider();
45

56
const purchaseService = {
@@ -12,23 +13,42 @@ const purchaseService = {
1213
processPayment,
1314
};
1415

16+
async function findPurchaseById(id) {
17+
const purchase = await Purchase.findById(id);
18+
if (purchase) {
19+
return purchase;
20+
} else {
21+
throw new NotFoundError(`Purchase with id ${id} Not found`);
22+
}
23+
}
24+
1525
async function createPurchase(userId, cartId, amount, shippingAddress) {
1626
const purchase = new Purchase({
1727
userId,
1828
cartId,
1929
amount,
2030
shippingAddress,
2131
});
22-
32+
2333
return await purchase.save();
2434
}
2535

2636
async function getAllPurchase(page, size) {
2737
// get a pagination with purchases instead all purchases
28-
const pageNumber = parseInt(page) || 1;
29-
const pageSize = parseInt(size) || 10;
30-
const skipCount = (pageNumber - 1) * pageSize;
31-
return await Purchase.find().skip(skipCount).limit(pageSize);
38+
const actualPage = parseInt(page) || 1;
39+
const pageSize = parseInt(size) || 8;
40+
const skipCount = (actualPage - 1) * pageSize;
41+
42+
const totalCount = await Purchase.countDocuments();
43+
const totalPages = Math.ceil(totalCount / pageSize);
44+
45+
const products = await Purchase.aggregate([
46+
{ $sample: { size: totalCount } },
47+
{ $skip: skipCount },
48+
{ $limit: pageSize },
49+
]);
50+
51+
return { products, totalPages };
3252
}
3353

3454
async function getUserPurchase(id) {
@@ -60,17 +80,20 @@ async function getMonthly() {
6080
}
6181

6282
async function cleanPurchase(id) {
83+
await findPurchaseById(id);
6384
return await Purchase.findByIdAndDelete(id);
6485
}
6586

6687
async function updatePurchaseState(id, status) {
88+
await findPurchaseById(id);
6789
const newStatus = await Purchase.findOneAndUpdate(
6890
{
69-
purchaseId: id,
91+
_id: id,
7092
},
7193
{ shippingStatus: status },
7294
{ new: true },
7395
);
96+
console.log(newStatus);
7497
return newStatus;
7598
}
7699

0 commit comments

Comments
 (0)