Fix duplicate purchases bug

This commit is contained in:
austinkelsay 2024-09-24 09:36:28 -05:00
parent 4f98ea3656
commit ca77e2674f
6 changed files with 53 additions and 12 deletions

View File

@ -226,6 +226,9 @@ CREATE UNIQUE INDEX "Course_noteId_key" ON "Course"("noteId");
-- CreateIndex -- CreateIndex
CREATE UNIQUE INDEX "UserLesson_userId_lessonId_key" ON "UserLesson"("userId", "lessonId"); CREATE UNIQUE INDEX "UserLesson_userId_lessonId_key" ON "UserLesson"("userId", "lessonId");
-- CreateIndex
CREATE UNIQUE INDEX "Purchase_userId_courseId_key" ON "Purchase"("userId", "courseId");
-- CreateIndex -- CreateIndex
CREATE UNIQUE INDEX "UserCourse_userId_courseId_key" ON "UserCourse"("userId", "courseId"); CREATE UNIQUE INDEX "UserCourse_userId_courseId_key" ON "UserCourse"("userId", "courseId");

View File

@ -184,16 +184,18 @@ model UserLesson {
} }
model Purchase { model Purchase {
id String @id @default(uuid()) id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
userId String userId String
course Course? @relation(fields: [courseId], references: [id])
courseId String? courseId String?
resource Resource? @relation(fields: [resourceId], references: [id])
resourceId String? resourceId String?
amountPaid Int amountPaid Int
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
course Course? @relation(fields: [courseId], references: [id])
resource Resource? @relation(fields: [resourceId], references: [id])
@@unique([userId, courseId])
} }
model UserCourse { model UserCourse {

View File

@ -33,7 +33,7 @@ const ProgressListItem = ({ dTag, category }) => {
const encodeNaddr = () => { const encodeNaddr = () => {
return nip19.naddrEncode({ return nip19.naddrEncode({
pubkey: event.pubkey, pubkey: event.pubkey,
identifier: dTag, identifier: event.id,
kind: 30004, kind: 30004,
relayUrls: appConfig.defaultRelayUrls relayUrls: appConfig.defaultRelayUrls
}) })

View File

@ -2,10 +2,13 @@ import React, { useEffect, useState } from "react";
import { useNDKContext } from "@/context/NDKContext"; import { useNDKContext } from "@/context/NDKContext";
import { parseEvent } from "@/utils/nostr"; import { parseEvent } from "@/utils/nostr";
import { ProgressSpinner } from "primereact/progressspinner"; import { ProgressSpinner } from "primereact/progressspinner";
import { nip19 } from "nostr-tools";
import appConfig from "@/config/appConfig";
const PurchasedListItem = ({ eventId, category }) => { const PurchasedListItem = ({ eventId, category }) => {
const { ndk } = useNDKContext(); const { ndk } = useNDKContext();
const [event, setEvent] = useState(null); const [event, setEvent] = useState(null);
const [naddr, setNaddr] = useState(null);
useEffect(() => { useEffect(() => {
const fetchEvent = async () => { const fetchEvent = async () => {
@ -24,7 +27,23 @@ const PurchasedListItem = ({ eventId, category }) => {
fetchEvent(); fetchEvent();
}, [eventId, ndk]); }, [eventId, ndk]);
return !event || !ndk ? <ProgressSpinner className="w-[40px] h-[40px]" /> : (<a className="text-blue-500 underline hover:text-blue-600" href={category === "courses" ? `/courses/${event.id}` : `/details/${event.id}`}>{event.title}</a>); useEffect(() => {
if (event) {
console.log("event", event);
encodeNaddr();
}
}, [event]);
const encodeNaddr = () => {
setNaddr(nip19.naddrEncode({
pubkey: event.pubkey,
identifier: event.id,
kind: event.kind,
relayUrls: appConfig.defaultRelayUrls
}))
}
return !event || !ndk ? <ProgressSpinner className="w-[40px] h-[40px]" /> : (<a className="text-blue-500 underline hover:text-blue-600" href={category === "courses" ? `/course/${naddr}` : `/details/${naddr}`}>{event.title}</a>);
} }
export default PurchasedListItem; export default PurchasedListItem;

View File

@ -248,7 +248,6 @@ const UserSettings = () => {
<Column field="amountPaid" header="Cost"></Column> <Column field="amountPaid" header="Cost"></Column>
<Column <Column
body={(rowData) => { body={(rowData) => {
console.log("rowData", rowData);
return <PurchasedListItem eventId={rowData?.resource?.noteId || rowData?.course?.noteId} category={rowData?.course ? "courses" : "resources"} /> return <PurchasedListItem eventId={rowData?.resource?.noteId || rowData?.course?.noteId} category={rowData?.course ? "courses" : "resources"} />
}} }}
header="Name" header="Name"

View File

@ -104,9 +104,27 @@ export const addCoursePurchaseToUser = async (userId, purchaseData) => {
where: { id: userId }, where: { id: userId },
data: { data: {
purchased: { purchased: {
create: { upsert: {
courseId: purchaseData.courseId, where: {
amountPaid: purchaseData.amountPaid, userId_courseId: {
userId: userId,
courseId: purchaseData.courseId,
},
},
create: {
courseId: purchaseData.courseId,
amountPaid: purchaseData.amountPaid,
},
update: {
amountPaid: purchaseData.amountPaid,
},
},
},
},
include: {
purchased: {
include: {
course: true,
}, },
}, },
}, },