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
25 changes: 17 additions & 8 deletions app/(api)/_datalib/_resolvers/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,44 @@ const resolvers = {
Orders.getProducts(parent.id, ctx),
},
Query: {
order: (_: never, args: { id: string }, ctx: ApolloContext) =>
order: (_: never, args: { id: number }, ctx: ApolloContext) =>
Orders.find(args.id, ctx),
orders: (_: never, args: { ids: string[] }, ctx: ApolloContext) =>
Orders.findMany(args.ids, ctx),
orders: (
_: never,
args: {
statuses: string[];
search: string;
offset: number;
limit: number;
},
ctx: ApolloContext
) =>
Orders.findMany(args.statuses, args.search, args.offset, args.limit, ctx),
},
Mutation: {
updateOrder: (
_: never,
args: { id: string; input: OrderInput },
args: { id: number; input: OrderInput },
ctx: ApolloContext
) => Orders.update(args.id, args.input, ctx),
deleteOrder: (_: never, args: { id: string }, ctx: ApolloContext) =>
deleteOrder: (_: never, args: { id: number }, ctx: ApolloContext) =>
Orders.delete(args.id, ctx),
createOrder: (_: never, args: { input: OrderInput }, ctx: ApolloContext) =>
Orders.create(args.input, ctx),
addProductToOrder: (
_: never,
args: { id: string; productToAdd: OrderProductInput },
args: { id: number; productToAdd: OrderProductInput },
ctx: ApolloContext
) => Orders.addProductToOrder(args.id, args.productToAdd, ctx),
removeProductFromOrder: (
_: never,
args: { id: string; product_id: string },
args: { id: number; product_id: string },
ctx: ApolloContext
) => Orders.removeProductFromOrder(args.id, args.product_id, ctx),
editProductQuantity: (
_: never,
args: {
id: string;
id: number;
productToUpdate: OrderProductInput;
},
ctx: ApolloContext
Expand Down
69 changes: 54 additions & 15 deletions app/(api)/_datalib/_services/Orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import revalidateCache from '@actions/revalidateCache';
import prisma from '../_prisma/client';
import { OrderInput, OrderProductInput } from '@datatypes/Order';
import { ApolloContext } from '../apolloServer';
import { Prisma } from '@prisma/client';

export default class Orders {
//CREATE
Expand All @@ -20,8 +21,7 @@ export default class Orders {
}

//READ -> get order and orders, also getProducts using the ProductToOrder table

static async find(id: string, ctx: ApolloContext) {
static async find(id: number, ctx: ApolloContext) {
if (!ctx.isOwner && !ctx.hasValidApiKey) return null;

return prisma.order.findUnique({
Expand All @@ -31,23 +31,62 @@ export default class Orders {
});
}

static async findMany(ids: string[], ctx: ApolloContext) {
static async findMany(
statuses: string[],
search: string,
offset: number,
limit: number,
ctx: ApolloContext
) {
if (!ctx.isOwner && !ctx.hasValidApiKey) return null;

if (!ids) {
return prisma.order.findMany();
if (offset < 0 || limit <= 0) return null;

const whereClause: Prisma.OrderWhereInput = {};

if (statuses && statuses.length > 0) {
whereClause.status = { in: statuses };
}

if (search) {
const searchConditions: Prisma.OrderWhereInput[] = [
{ customer_name: { contains: search, mode: 'insensitive' } },
{ customer_email: { contains: search, mode: 'insensitive' } },
{ customer_phone_num: { contains: search, mode: 'insensitive' } },
];

const searchAsNumber = parseInt(search, 10);
if (!isNaN(searchAsNumber)) {
searchConditions.push({ id: searchAsNumber });
searchConditions.push({
id: {
in: await prisma.order
.findMany({
select: { id: true },
})
.then((orders) =>
orders
.filter((order) => order.id.toString().includes(search))
.map((order) => order.id)
),
},
});
}

whereClause.OR = searchConditions;
}

return prisma.order.findMany({
where: {
id: {
in: ids,
},
where: whereClause,
orderBy: {
created_at: 'desc',
},
skip: offset * limit,
take: limit,
});
}

static async getProducts(order_id: string, ctx: ApolloContext) {
static async getProducts(order_id: number, ctx: ApolloContext) {
if (!ctx.isOwner && !ctx.hasValidApiKey) return null;

const productToOrder = await prisma.productToOrder.findMany({
Expand All @@ -68,7 +107,7 @@ export default class Orders {
}

//UPDATE
static async update(id: string, input: OrderInput, ctx: ApolloContext) {
static async update(id: number, input: OrderInput, ctx: ApolloContext) {
if (!ctx.isOwner && !ctx.hasValidApiKey) return null;

try {
Expand All @@ -87,7 +126,7 @@ export default class Orders {

// these are the services for the mutations we have left
static async addProductToOrder(
id: string,
id: number,
productToAdd: OrderProductInput,
ctx: ApolloContext
) {
Expand Down Expand Up @@ -146,7 +185,7 @@ export default class Orders {
}

static async removeProductFromOrder(
id: string,
id: number,
product_id: string,
ctx: ApolloContext
) {
Expand Down Expand Up @@ -181,7 +220,7 @@ export default class Orders {
}

static async editProductQuantity(
id: string,
id: number,
productToUpdate: OrderProductInput,
ctx: ApolloContext
) {
Expand Down Expand Up @@ -233,7 +272,7 @@ export default class Orders {
}

// DELETE
static async delete(id: string, ctx: ApolloContext) {
static async delete(id: number, ctx: ApolloContext) {
if (!ctx.isOwner && !ctx.hasValidApiKey) return null;

try {
Expand Down
7 changes: 6 additions & 1 deletion app/(api)/_datalib/_typeDefs/Order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ const typeDefs = gql`

type Query {
order(id: ID!): Order
orders(id: [ID]): [Order]
orders(
statuses: [String]
search: String
offset: Int!
limit: Int!
): [Order]
}

type Mutation {
Expand Down
4 changes: 2 additions & 2 deletions app/(api)/_types/Order.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Product } from './Product';

export type Order = {
id: string;
id: number;
customer_name: string;
customer_email: string;
customer_phone_num: string;
Expand All @@ -23,7 +23,7 @@ export type Order = {
export type ProductToOrder = {
product_id: string;
product: Product;
order_id: string;
order_id: number;
order: Order;
quantity: number;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
Warnings:

- The primary key for the `Order` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The `id` column on the `Order` table would be dropped and recreated. This will lead to data loss if there is data in the column.
- The primary key for the `ProductToOrder` table will be changed. If it partially fails, the table could be left without primary key constraint.
- Changed the type of `order_id` on the `ProductToOrder` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required.

*/
-- DropForeignKey
ALTER TABLE "ProductToOrder" DROP CONSTRAINT "ProductToOrder_order_id_fkey";

-- AlterTable
ALTER TABLE "Order" DROP CONSTRAINT "Order_pkey",
DROP COLUMN "id",
ADD COLUMN "id" SERIAL NOT NULL,
ADD CONSTRAINT "Order_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "ProductToOrder" DROP CONSTRAINT "ProductToOrder_pkey",
DROP COLUMN "order_id",
ADD COLUMN "order_id" INTEGER NOT NULL,
ADD CONSTRAINT "ProductToOrder_pkey" PRIMARY KEY ("product_id", "order_id");

-- AddForeignKey
ALTER TABLE "ProductToOrder" ADD CONSTRAINT "ProductToOrder_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "Order"("id") ON DELETE CASCADE ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion prisma/schema/Order.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
model Order {
id String @id @default(uuid())
id Int @id @default(autoincrement())
customer_name String
customer_email String
customer_phone_num String
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema/ProductToOrder.prisma
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
model ProductToOrder {
product_id String
product Product @relation(fields: [product_id], references: [id], onDelete: Cascade)
order_id String
order_id Int
order Order @relation(fields: [order_id], references: [id], onDelete: Cascade)
quantity Int

Expand Down