Fix to purchase models after unique purchases update

This commit is contained in:
austinkelsay 2024-10-12 13:43:35 -05:00
parent 823fff5c96
commit 173632deb8
2 changed files with 14 additions and 33 deletions

View File

@ -83,21 +83,9 @@ export const addResourcePurchaseToUser = async (userId, purchaseData) => {
where: { id: userId },
data: {
purchased: {
upsert: {
where: {
userId_courseId_resourceId: {
userId: userId,
courseId: null,
resourceId: purchaseData.resourceId,
},
},
create: {
resourceId: purchaseData.resourceId,
amountPaid: purchaseData.amountPaid,
},
update: {
amountPaid: purchaseData.amountPaid,
},
create: {
resourceId: purchaseData.resourceId,
amountPaid: purchaseData.amountPaid,
},
},
},
@ -116,21 +104,9 @@ export const addCoursePurchaseToUser = async (userId, purchaseData) => {
where: { id: userId },
data: {
purchased: {
upsert: {
where: {
userId_courseId_resourceId: {
userId: userId,
courseId: purchaseData.courseId,
resourceId: null,
},
},
create: {
courseId: purchaseData.courseId,
amountPaid: purchaseData.amountPaid,
},
update: {
amountPaid: purchaseData.amountPaid,
},
create: {
courseId: purchaseData.courseId,
amountPaid: purchaseData.amountPaid,
},
},
},

View File

@ -13,17 +13,22 @@ export default async function handler(req, res) {
try {
const { userId, resourceId, amountPaid } = req.body;
if (!userId || !resourceId || !amountPaid) {
return res.status(400).json({ error: 'Missing required fields' });
}
const updatedUser = await addResourcePurchaseToUser(userId, {
resourceId,
amountPaid: parseInt(amountPaid, 10) // Ensure amountPaid is an integer
amountPaid: parseInt(amountPaid, 10)
});
res.status(200).json(updatedUser);
} catch (error) {
res.status(500).json({ error: error.message });
console.error('Error in resource purchase:', error);
res.status(500).json({ error: 'An error occurred while processing the purchase' });
}
} else {
res.setHeader('Allow', ['POST']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}
}