Skip to content

Commit

Permalink
Merge pull request #9 from tautf/flo/milestone-1.2.0
Browse files Browse the repository at this point in the history
refactor: issues for 1.2.0
  • Loading branch information
tautf authored Feb 9, 2024
2 parents 0dba593 + 275d451 commit ae3b585
Show file tree
Hide file tree
Showing 18 changed files with 1,090 additions and 499 deletions.
1,052 changes: 619 additions & 433 deletions package-lock.json

Large diffs are not rendered by default.

41 changes: 20 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@
"start:migrate:prod": "npx prisma migrate deploy && node server.js"
},
"dependencies": {
"@headlessui/react": "^1.7.17",
"@heroicons/react": "^2.0.18",
"@headlessui/react": "^1.7.18",
"@heroicons/react": "^2.1.1",
"@nextui-org/react": "^2.2.9",
"@prisma/client": "^5.3.1",
"autoprefixer": "10.4.16",
"@prisma/client": "^5.9.1",
"autoprefixer": "10.4.17",
"dayjs": "^1.11.10",
"framer-motion": "^10.16.7",
"next": "^14.0.3",
"next-auth": "^4.24.5",
"framer-motion": "^11.0.3",
"next": "^14.1.0",
"next-themes": "^0.2.1",
"postcss": "^8.4.31",
"postcss": "^8.4.35",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.5",
"tailwindcss": "^3.4.1",
"validator": "^13.11.0",
"zod": "^3.22.4"
},
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@types/node": "^20.10.0",
"@types/react": "18.2.39",
"@types/react-dom": "18.2.17",
"@types/validator": "^13.11.7",
"eslint": "^8.50.0",
"eslint-config-next": "^13.5.2",
"husky": "^8.0.3",
"prettier": "^3.1.0",
"prisma": "^5.3.1",
"typescript": "^5.2.2"
"@commitlint/cli": "^18.6.0",
"@commitlint/config-conventional": "^18.6.0",
"@types/node": "^20.11.7",
"@types/react": "^18.2.55",
"@types/react-dom": "^18.2.19",
"@types/validator": "^13.11.9",
"eslint": "^8.56.0",
"eslint-config-next": "^14.1.0",
"husky": "^9.0.10",
"prettier": "^3.2.5",
"prisma": "^5.9.1",
"typescript": "^5.3.3"
}
}
2 changes: 2 additions & 0 deletions prisma/migrations/20240204214628_mig_20240204/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Product" ADD COLUMN "replacedAt" TIMESTAMP(3);
2 changes: 2 additions & 0 deletions prisma/migrations/20240204215225_mig_202402042/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Product" ADD COLUMN "renewedAt" TIMESTAMP(3);
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ model Product {
updatedAt DateTime @updatedAt
productType ProductType @relation(fields: [productTypeId], references: [id])
productTypeId Int
renewedAt DateTime?
replacedById Int?
replacedAt DateTime?
}

model ProductType {
Expand Down
189 changes: 189 additions & 0 deletions src/app/actions/products/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const addProduct = cache(async (form: FormData): Promise<number> => {
export const updateProduct = cache(
async (productToUpdate: any): Promise<void> => {
productToUpdate.updatedAt = dayjs().toDate();
productToUpdate.renewedAt = dayjs().toDate();
await prisma.product.update({
where: { id: productToUpdate.id },
data: productToUpdate,
Expand All @@ -99,6 +100,7 @@ export const replaceProduct = cache(
where: { id },
data: {
replacedById: newProductId,
replacedAt: dayjs().toDate(),
},
});
revalidatePath('/');
Expand All @@ -107,6 +109,12 @@ export const replaceProduct = cache(
},
);

export const deleteProduct = cache(async (id: number): Promise<void> => {
await prisma.product.delete({ where: { id } });
revalidatePath('/');
revalidatePath('/products');
});

/**
* Retrieves the top 5 products that need to be replaced
* @returns top 5 products that need to be replaced
Expand Down Expand Up @@ -196,6 +204,187 @@ export const getToReplaceAndInvestIn90Days = cache(
},
);

export const getToReplaceAndInvestInCurrentQuarter = cache(
async (): Promise<{
productsCurrentQuarter: Product[];
sumCurrentQuarter: number;
}> => {
const startOfYear =
dayjs().month() >= 9
? dayjs().month(9).startOf('month')
: dayjs().subtract(1, 'year').month(9).startOf('month');
const endOfYear =
dayjs().month() >= 9
? dayjs().add(1, 'year').month(8).endOf('month')
: dayjs().month(8).endOf('month');

const products = await prisma.product.findMany({
where: {
replacedById: {
not: null,
},
replacedAt: {
gte: startOfYear.toDate(),
lte: endOfYear.toDate(),
},
},
});

return {
productsCurrentQuarter: products,
sumCurrentQuarter: products.reduce(
(acc, product) => acc + product.price,
0,
),
};
},
);

export const getToReplaceAndInvestInNextQuarter = cache(
async (): Promise<{
productsNextQuarter: Product[];
sumNextQuarter: number;
}> => {
const products = await prisma.product.findMany({
where: {
renewalDate: {
gte: new Date(
new Date().getFullYear(),
new Date().getMonth() + 3,
new Date().getDate(),
),
lte: new Date(
new Date().getFullYear(),
new Date().getMonth() + 6,
new Date().getDate(),
),
},
replacedById: null,
},
});

return {
productsNextQuarter: products,
sumNextQuarter: products.reduce(
(acc, product) => acc + product.price,
0,
),
};
},
);

export const getToReplaceAndInvestInCurrentBusinessYear = cache(
async (): Promise<{
productsCurrentYear: Product[];
sumCurrentYear: number;
}> => {
const startOfYear =
dayjs().month() >= 9
? dayjs().month(9).startOf('month')
: dayjs().subtract(1, 'year').month(9).startOf('month');
const endOfYear =
dayjs().month() >= 9
? dayjs().add(1, 'year').month(8).endOf('month')
: dayjs().month(8).endOf('month');

const products = await prisma.product.findMany({
where: {
renewalDate: {
gte: startOfYear.toDate(),
lte: endOfYear.toDate(),
},
replacedById: null,
},
});

return {
productsCurrentYear: products,
sumCurrentYear: products.reduce(
(acc, product) => acc + product.price,
0,
),
};
},
);

export const getToReplaceAndInvestInNextBusinessYear = cache(
async (): Promise<{
productsNextYear: Product[];
sumNextYear: number;
}> => {
const startOfNextYear =
dayjs().month() >= 9
? dayjs().add(1, 'year').month(9).startOf('month')
: dayjs().month(9).startOf('month');
const endOfNextYear =
dayjs().month() >= 9
? dayjs().add(2, 'year').month(8).endOf('month')
: dayjs().add(1, 'year').month(8).endOf('month');

const products = await prisma.product.findMany({
where: {
renewalDate: {
gte: startOfNextYear.toDate(),
lte: endOfNextYear.toDate(),
},
replacedById: null,
},
});

return {
productsNextYear: products,
sumNextYear: products.reduce(
(acc, product) => acc + product.price,
0,
),
};
},
);

export const getReplacedProductsInCurrentBusinessYear = cache(
async (): Promise<{
replacedProductsCurrentYear: Product[];
sumReplacedCurrentYear: number;
}> => {
const startOfYear =
dayjs().month() >= 9
? dayjs().month(9).startOf('month')
: dayjs().subtract(1, 'year').month(9).startOf('month');
const endOfYear =
dayjs().month() >= 9
? dayjs().add(1, 'year').month(8).endOf('month')
: dayjs().month(8).endOf('month');

const products = await prisma.product.findMany({
where: {
replacedById: {
not: null,
},
replacedAt: {
gte: startOfYear.toDate(),
lte: endOfYear.toDate(),
},
OR: [
{
renewedAt: {
gte: startOfYear.toDate(),
lte: endOfYear.toDate(),
},
},
],
},
});

return {
replacedProductsCurrentYear: products,
sumReplacedCurrentYear: products.reduce(
(acc, product) => acc + product.price,
0,
),
};
},
);

const createProductObject = (formData: FormData): any => {
const productSchema = z.object({
name: z.string().min(5, {
Expand Down
21 changes: 21 additions & 0 deletions src/app/components/controlling-dashboard-card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';

import type { ICard } from '../page';

type Props = {
card: ICard;
className: string;
};
export default function DashboardCard({ card, className }: Props) {
return (
<div
className={`${className} block lg:m-3 m-2 w-56 h-56 ${card.bgColor} rounded-2xl shadow-lg relative hover:scale-105 transition ease-in`}
>
<h1 className="ml-5 mt-5 text-xl font-extrabold">{card.header}</h1>
<p className="mx-5 lg:text-md text-sm">{card.subHeader}</p>
<h2 className="p-2 lg:text-3xl text-xl font-extrabold absolute bottom-1 right-1 hover:scale-110 transition ease-in">
{card.text}
</h2>
</div>
);
}
2 changes: 1 addition & 1 deletion src/app/components/dashboard-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Props = {
export default function DashboardCard({ card, className }: Props) {
return (
<div
className={`${className} block lg:m-3 m-2 w-56 h-56 ${card.bgColor} rounded-2xl shadow-lg relative hover:scale-105 transition ease-in`}
className={`${className} block lg:m-3 m-2 w-56 h-56 ${card.bgColor} rounded-xl shadow-lg relative hover:scale-105 transition ease-in`}
>
<h1 className="ml-5 mt-5 text-xl font-extrabold">{card.header}</h1>
<p className="mx-5 lg:text-md text-sm">{card.subHeader}</p>
Expand Down
Loading

0 comments on commit ae3b585

Please sign in to comment.