Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) subscription schema #15

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 121 additions & 10 deletions app/api/events/subscriptions/[subscriptionId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,50 @@ export async function GET(
const subscriptionData = await prisma.subscription.findUniqueOrThrow({
where: { id: subscriptionId },
include: {
plan: true,
requestedBy: true,
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
appointments: {
include: {
slotOfAppointment: {
include: {
consulteeProfile: true,
consulteeProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -64,20 +101,57 @@ export async function PUT(
feedbackFromConsultee: body.feedbackFromConsultee,
feedbackFromConsultant: body.feedbackFromConsultant,
rating: body.rating,
plan: body.planId
subscriptionPlan: body.planId
? {
connect: { id: body.planId },
}
: undefined,
},
include: {
plan: true,
requestedBy: true,
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
appointments: {
include: {
slotOfAppointment: {
include: {
consulteeProfile: true,
consulteeProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -105,13 +179,50 @@ export async function DELETE(
const subscriptionData = await prisma.subscription.delete({
where: { id: subscriptionId },
include: {
plan: true,
requestedBy: true,
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
appointments: {
include: {
slotOfAppointment: {
include: {
consulteeProfile: true,
consulteeProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
},
Expand Down
66 changes: 61 additions & 5 deletions app/api/events/subscriptions/book/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import authOptions from "@/app/api/auth/[...nextauth]/options";

interface BookSubscriptionRequest {
subscriptionPlanId: string;
tentativeStartDate: string;
tentativeSchedule?: string;
requestNotes?: string;
}

export async function POST(request: Request) {
try {
const session = await getServerSession(authOptions);
Expand All @@ -15,10 +22,20 @@ export async function POST(request: Request) {
tentativeStartDate,
tentativeSchedule,
requestNotes,
} = await request.json();
}: BookSubscriptionRequest = await request.json();

const consultee = await prisma.consulteeProfile.findUnique({
where: { userId: session.user.id },
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
});

if (!consultee) {
Expand All @@ -30,7 +47,20 @@ export async function POST(request: Request) {

const subscriptionPlan = await prisma.subscriptionPlan.findUnique({
where: { id: subscriptionPlanId },
include: { consultantProfile: true },
include: {
consultantProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
});

if (!subscriptionPlan) {
Expand All @@ -45,7 +75,7 @@ export async function POST(request: Request) {

const subscription = await prisma.subscription.create({
data: {
plan: { connect: { id: subscriptionPlanId } },
subscriptionPlan: { connect: { id: subscriptionPlanId } },
requestedBy: { connect: { id: consultee.id } },
startDate: new Date(tentativeStartDate),
endDate: endDate,
Expand All @@ -55,8 +85,34 @@ export async function POST(request: Request) {
requestStatus: "PENDING",
},
include: {
plan: true,
requestedBy: true,
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
});

Expand Down
55 changes: 44 additions & 11 deletions app/api/events/subscriptions/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import prisma from "@/lib/prisma";
import { RequestStatus } from "@prisma/client";
import { Prisma, RequestStatus } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server";

interface UpdateSubscriptionRequest {
id: string;
status: RequestStatus;
}

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const consultantProfileId = searchParams.get("consultantProfileId");
Expand All @@ -17,8 +22,8 @@ export async function GET(request: NextRequest) {
}

try {
const whereClause: any = {
plan: {
const whereClause: Prisma.SubscriptionWhereInput = {
subscriptionPlan: {
consultantProfileId,
},
};
Expand All @@ -31,18 +36,32 @@ export async function GET(request: NextRequest) {
prisma.subscription.findMany({
where: whereClause,
include: {
plan: {
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: true,
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: true,
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
Expand Down Expand Up @@ -75,7 +94,7 @@ export async function GET(request: NextRequest) {

export async function PATCH(request: NextRequest) {
try {
const body = await request.json();
const body: UpdateSubscriptionRequest = await request.json();
const { id, status } = body;

if (!id || !status) {
Expand All @@ -85,26 +104,40 @@ export async function PATCH(request: NextRequest) {
);
}

if (!Object.values(RequestStatus).includes(status as RequestStatus)) {
if (!Object.values(RequestStatus).includes(status)) {
return NextResponse.json({ error: "Invalid status" }, { status: 400 });
}

const subscription = await prisma.subscription.update({
where: { id },
data: { requestStatus: status },
include: {
plan: {
subscriptionPlan: {
include: {
consultantProfile: {
include: {
user: true,
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
},
requestedBy: {
include: {
user: true,
user: {
select: {
id: true,
name: true,
email: true,
image: true,
},
},
},
},
},
Expand Down
Loading
Loading