-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathOrdersControllers.js
More file actions
100 lines (95 loc) · 2.69 KB
/
OrdersControllers.js
File metadata and controls
100 lines (95 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { PrismaClient } from "@prisma/client";
import Stripe from "stripe";
const stripe = new Stripe(
"sk_test_51DpVXWGc9EcLzRLBNKni929hB026lACv6toMfjH1FPtIXfYgIrhXzjolcYzDDl2VwtvmyPF20PJ1JaMUCTNoEwDN00FN8hrRZL"
);
export const createOrder = async (req, res, next) => {
try {
if (req.body.gigId) {
const { gigId } = req.body;
const prisma = new PrismaClient();
const gig = await prisma.gigs.findUnique({
where: { id: parseInt(gigId) },
});
const paymentIntent = await stripe.paymentIntents.create({
amount: gig?.price * 100,
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
await prisma.orders.create({
data: {
paymentIntent: paymentIntent.id,
price: gig?.price,
buyer: { connect: { id: req?.userId } },
gig: { connect: { id: gig?.id } },
},
});
res.status(200).send({
clientSecret: paymentIntent.client_secret,
});
} else {
res.status(400).send("Gig id is required.");
}
} catch (err) {
console.log(err);
return res.status(500).send("Internal Server Error");
}
};
export const confirmOrder = async (req, res, next) => {
try {
if (req.body.paymentIntent) {
const prisma = new PrismaClient();
await prisma.orders.update({
where: { paymentIntent: req.body.paymentIntent },
data: { isCompleted: true },
});
}
} catch (err) {
console.log(err);
return res.status(500).send("Internal Server Error");
}
};
export const getBuyerOrders = async (req, res, next) => {
try {
if (req.userId) {
const prisma = new PrismaClient();
const orders = await prisma.orders.findMany({
where: { gig: {createdBy:{ id: req.userId,}, }, isCompleted: true, },
include: { gig: true },
});
return res.status(200).json({ orders });
}
return res.status(400).send("User id is required.");
} catch (err) {
console.log(err);
return res.status(500).send("Internal Server Error");
}
};
export const getSellerOrders = async (req, res, next) => {
try {
if (req.userId) {
const prisma = new PrismaClient();
const orders = await prisma.orders.findMany({
where: {
gig: {
createdBy: {
id: parseInt(req.userId),
},
},
isCompleted: true,
},
include: {
gig: true,
buyer: true,
},
});
return res.status(200).json({ orders });
}
return res.status(400).send("User id is required.");
} catch (err) {
console.log(err);
return res.status(500).send("Internal Server Error");
}
};