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

Finish App Router Migration #615

Merged
merged 11 commits into from
Nov 11, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ venv
npm-debug.log*
yarn-debug.log*
yarn-error.log*
next/config-overrides.js

notes.txt
flask/.virtualenv
Expand Down
40 changes: 40 additions & 0 deletions next/jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"paths": {
"@/*": [
"./*",
"./src/*",
]
}
},
"include": [
"next-env.d.ts",
"**/*.js",
"**/*.jsx",
],
"exclude": [
"node_modules"
]
}
141 changes: 110 additions & 31 deletions next/lib/data.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import { sql } from "@vercel/postgres";

'use server'
import { PrismaClient } from '@prisma/client';
import { determineBackendUrl } from '@/src/utils/backendrouter';
import { isOddReleaseWeek, busy_sleep } from '@/src/utils/time';
import * as Sentry from '@sentry/nextjs';

const prisma = new PrismaClient();

// export type Product = {
// id: number;
// description: string;
// descriptionfull: string;
// price: number;
// img: string;
// imgcropped: string;
// reviews: Review[] | [];
// }

// export type Review = {
// id: number;
// productid: number;
// rating: number;
// customerid: string;
// description: string;
// created: string;
// }


export default async function getProducts() {
try {
Expand All @@ -47,20 +30,116 @@ export default async function getProducts() {
console.error("Database Error:", error)
// do sentry stuff
}
}

export async function getProduct(index) {
const i = Number(index);
try {
console.log("Fetching product...");
console.log(i);
const product = await prisma.products.findUnique({
where: { id: i }
});

const product_reviews = await prisma.reviews.findMany({
where: { id: i },
});

product.reviews = product_reviews;
return product;
} catch (error) {
console.error("Database Error:", error);
}
}

async function getReview(i) {
try {
const data = await sql`SELECT * from reviews WHERE productId=${i}`;
console.log(data.rows);
return data.rows;
} catch (error) {
console.error("Db error", error);
export async function checkoutAction(cart) {
console.log("cart ", cart);
const inventory = await getInventory(cart);

console.log("> /checkout inventory", inventory)


if (inventory.length === 0 || cart.quantities.length === 0) {
const error = new Error("Not enough inventory for product")
Sentry.captureException(error);
throw error;
}

for (let inventoryItem of inventory) {
let id = inventoryItem.id;
console.log(inventoryItem.count, cart.quantities[id]);
if (inventoryItem.count < cart.quantities[id] || cart.quantities[id] >= inventoryItem.count) {
const error = new Error("Not enough inventory for product")
Sentry.captureException(error);
throw error;
}
}

return {status : 200, message: "success"}
}

export async function getProductsPrisma() {
const products = await prisma.products.findMany();
console.log("prisma", products);

export async function getInventory(cart) {
console.log("> getInventory");

const quantities = cart['quantities'];
console.log("> quantities", quantities);

let productIds = []

Object.entries(quantities).forEach(([key, value]) => productIds.push(Number(key)));
console.log("> product ids", productIds);
let inventory;
try {
inventory = await prisma.inventory.findMany({
where: { id : { in : productIds } }
});
} catch (error) {
console.log("Database Error:", error);
}
console.log("inventory: ", inventory);
return inventory;
}



export async function getAbout(backend) {
if (!isOddReleaseWeek()) {
// can't have async sleep in a constructor
busy_sleep(Math.random(25) + 100);
}

const url = determineBackendUrl(backend);
// Http requests to make in parallel, so the Transaction has more Spans
let request1 = fetch(url + '/api', {
method: 'GET',
});
let request2 = fetch(url + '/organization', {
method: 'GET',
});
let request3 = fetch(url + '/connect', {
method: 'GET',
});

// Need Safari13 in tests/config.py in order for this modern javascript to work in Safari Browser
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled#browser_compatibility
// let response = await Promise.allSettled([request1, request2, request3])

const [response1, response2, response3] = await Promise.all([request1, request2, request3]);

console.log([response1, response2, response3]);
const responses = [response1, response2, response3];
// Error Handling
responses.forEach((r) => {
if (!r.ok) {
Sentry.withScope((scope) => {
scope.setContext('response', r);
Sentry.captureException(
new Error(
r.status + ' - ' + (r.statusText || 'Server Error for API')
)
);
});
}
});
}
7 changes: 1 addition & 6 deletions next/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { withSentryConfig } from '@sentry/nextjs';
/** @type {import('next').NextConfig} */
const nextConfig = {
distDir: './dist', // Changes the build output directory to `./dist/`.
// This should be removed during a refactor, following these directions:
// https://nextjs.org/docs/messages/missing-suspense-with-csr-bailout
experimental: {
missingSuspenseWithCSRBailout: false,
},
};

export default withSentryConfig(nextConfig, {
Expand Down Expand Up @@ -47,5 +42,5 @@ export default withSentryConfig(nextConfig, {
// See the following for more information:
// https://docs.sentry.io/product/crons/
// https://vercel.com/docs/cron-jobs
automaticVercelMonitors: true,
automaticVercelMonitors: false,
});
Loading
Loading