Skip to content
Merged
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
32 changes: 26 additions & 6 deletions src/app/api/distributions/admin/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NextRequest } from "next/server";
import { auth } from "@/auth";
import { AuthenticationError, AuthorizationError } from "@/util/errors";
import { errorResponse } from "@/util/errors";
import { AuthenticationError, AuthorizationError, ArgumentError, errorResponse } from "@/util/errors";
import UserService from "@/services/userService";
import DistributionService from "@/services/distributionService";
import { NextResponse } from "next/server";
import { tableParamsSchema } from "@/schema/tableParams";

export async function GET(request: NextRequest) {
try {
Expand All @@ -20,13 +20,33 @@ export async function GET(request: NextRequest) {
const url = new URL(request.url);
const completed = url.searchParams.get("completed");

const tableParams = tableParamsSchema.safeParse({
filters: url.searchParams.get("filters"),
page: url.searchParams.get("page"),
pageSize: url.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

if (completed === "true") {
const signoffs = await DistributionService.getCompletedSignOffs();
return NextResponse.json({ signoffs });
const signoffs = await DistributionService.getCompletedSignOffs(
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
return NextResponse.json(signoffs);
}

const partnerAllocationsWithVisibility = await DistributionService.getPartnerAllocationSummaries();
return NextResponse.json({ data: partnerAllocationsWithVisibility });
const partnerAllocationsWithVisibility = await DistributionService.getPartnerAllocationSummaries(
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
return NextResponse.json(partnerAllocationsWithVisibility);
} catch (error) {
return errorResponse(error);
}
Expand Down
22 changes: 20 additions & 2 deletions src/app/api/donorOffers/[donorOfferId]/edit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AuthorizationError,
errorResponse,
} from "@/util/errors";
import { tableParamsSchema } from "@/schema/tableParams";

const paramSchema = z.object({
donorOfferId: z
Expand All @@ -19,7 +20,7 @@ const paramSchema = z.object({
});

export async function GET(
_: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ donorOfferId: string }> }
) {
try {
Expand All @@ -38,7 +39,24 @@ export async function GET(
throw new ArgumentError(parsed.error.message);
}

const result = await DonorOfferService.getDonorOfferForEdit(parsed.data.donorOfferId);
const tableParams = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

const result = await DonorOfferService.getDonorOfferForEdit(
parsed.data.donorOfferId,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
return NextResponse.json(result);
} catch (error) {
return errorResponse(error);
Expand Down
30 changes: 27 additions & 3 deletions src/app/api/donorOffers/[donorOfferId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
errorResponse,
ok,
} from "@/util/errors";
import { tableParamsSchema } from "@/schema/tableParams";

const paramSchema = z.object({
donorOfferId: z
Expand All @@ -35,7 +36,7 @@ const postBodySchema = z.object({
});

export async function GET(
_: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ donorOfferId: string }> }
) {
try {
Expand All @@ -51,12 +52,35 @@ export async function GET(
throw new ArgumentError(parsed.error.message);
}

const tableParams = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

let result;

if (session.user.type === "PARTNER") {
result = await DonorOfferService.getPartnerDonorOfferDetails(parsed.data.donorOfferId, session.user.id);
result = await DonorOfferService.getPartnerDonorOfferDetails(
parsed.data.donorOfferId,
session.user.id,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
} else if (UserService.isStaff(session.user.type)) {
result = await DonorOfferService.getAdminDonorOfferDetails(parsed.data.donorOfferId);
result = await DonorOfferService.getAdminDonorOfferDetails(
parsed.data.donorOfferId,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
} else {
throw new AuthorizationError("Unauthorized user type");
}
Expand Down
24 changes: 21 additions & 3 deletions src/app/api/donorOffers/items/[itemId]/lineItems/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AuthorizationError,
errorResponse,
} from "@/util/errors";
import { tableParamsSchema } from "@/schema/tableParams";

const paramSchema = z.object({
itemId: z
Expand All @@ -19,7 +20,7 @@ const paramSchema = z.object({
});

export async function GET(
_: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ itemId: string }> }
) {
try {
Expand All @@ -39,8 +40,25 @@ export async function GET(
throw new ArgumentError(parsed.error.message);
}

const result = await DonorOfferService.getItemLineItems(parsed.data.itemId);

const tableParams = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

const result = await DonorOfferService.getItemLineItems(
parsed.data.itemId,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);

return Response.json(result);
} catch (error) {
return errorResponse(error);
Expand Down
24 changes: 21 additions & 3 deletions src/app/api/donorOffers/items/[itemId]/requests/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
AuthorizationError,
errorResponse,
} from "@/util/errors";
import { tableParamsSchema } from "@/schema/tableParams";

const paramSchema = z.object({
itemId: z
Expand All @@ -19,7 +20,7 @@ const paramSchema = z.object({
});

export async function GET(
_: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ itemId: string }> }
) {
try {
Expand All @@ -39,8 +40,25 @@ export async function GET(
throw new ArgumentError(parsed.error.message);
}

const result = await DonorOfferService.getItemRequests(parsed.data.itemId);

const tableParams = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

const result = await DonorOfferService.getItemRequests(
parsed.data.itemId,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);

return Response.json(result);
} catch (error) {
return errorResponse(error);
Expand Down
31 changes: 27 additions & 4 deletions src/app/api/donorOffers/route.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import { NextResponse } from "next/server";
import { NextRequest, NextResponse } from "next/server";

import { auth } from "@/auth";
import DonorOfferService from "@/services/donorOfferService";
import UserService from "@/services/userService";
import {
AuthenticationError,
AuthorizationError,
ArgumentError,
errorResponse,
} from "@/util/errors";
import { tableParamsSchema } from "@/schema/tableParams";

export async function GET() {
export async function GET(request: NextRequest) {
try {
const session = await auth();
if (!session?.user) {
throw new AuthenticationError("Session required");
}

const parsed = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!parsed.success) {
throw new ArgumentError(parsed.error.message);
}

const { filters, page, pageSize } = parsed.data;

let result;

if (session.user.type === "PARTNER") {
result = await DonorOfferService.getPartnerDonorOffers(parseInt(session.user.id));
result = await DonorOfferService.getPartnerDonorOffers(
parseInt(session.user.id),
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
} else if (UserService.isStaff(session.user.type)) {
result = await DonorOfferService.getAdminDonorOffers();
result = await DonorOfferService.getAdminDonorOffers(
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);
} else {
throw new AuthorizationError("Must be STAFF, ADMIN, or SUPER_ADMIN");
}
Expand Down
24 changes: 21 additions & 3 deletions src/app/api/items/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AuthenticationError, AuthorizationError, ArgumentError } from "@/util/e
import { NextResponse, NextRequest } from "next/server";
import { ItemFormSchema } from "@/schema/itemForm";
import UserService from "@/services/userService";
import { tableParamsSchema } from "@/schema/tableParams";

export async function POST(request: NextRequest): Promise<NextResponse> {
try {
Expand All @@ -31,7 +32,7 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
}
}

export async function GET(): Promise<NextResponse> {
export async function GET(request: NextRequest): Promise<NextResponse> {
try {
const session = await auth();
if (!session?.user) {
Expand All @@ -42,8 +43,25 @@ export async function GET(): Promise<NextResponse> {
throw new AuthorizationError("Must be ADMIN or SUPER_ADMIN");
}

const items = await ItemService.getAllItems();
return NextResponse.json(items, { status: 200 });
const parsed = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
includeInvites: request.nextUrl.searchParams.get("includeInvites"),
});

if (!parsed.success) {
throw new ArgumentError(parsed.error.message);
}

const { filters, page, pageSize } = parsed.data;

const { items, total } = await ItemService.getAllItems(
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined
);
return NextResponse.json({items, total}, { status: 200 });
} catch (error) {
return errorResponse(error);
}
Expand Down
22 changes: 20 additions & 2 deletions src/app/api/shippingStatus/[partnerId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ShipmentStatus } from "@prisma/client";
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import UserService from "@/services/userService";
import { tableParamsSchema } from "@/schema/tableParams";

const updateShippingStatusSchema = z.object({
donorShippingNumber: z.string(),
Expand All @@ -14,7 +15,7 @@ const updateShippingStatusSchema = z.object({
});

export async function GET(
_: NextRequest,
request: NextRequest,
{ params }: { params: Promise<{ partnerId: string }> }
) {
try {
Expand All @@ -32,7 +33,24 @@ export async function GET(
throw new ArgumentError("Invalid partner ID");
}

const result = await ShippingStatusService.getPartnerShippingStatuses(partnerId);
const tableParams = tableParamsSchema.safeParse({
filters: request.nextUrl.searchParams.get("filters"),
page: request.nextUrl.searchParams.get("page"),
pageSize: request.nextUrl.searchParams.get("pageSize"),
});

if (!tableParams.success) {
throw new ArgumentError(tableParams.error.message);
}

const { filters, page, pageSize } = tableParams.data;

const result = await ShippingStatusService.getPartnerShippingStatuses(
partnerId,
filters ?? undefined,
page ?? undefined,
pageSize ?? undefined,
);

return NextResponse.json(result, { status: 200 });
} catch (error) {
Expand Down
Loading
Loading