-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from vimalsonara/chart
API integration last seven days chart
- Loading branch information
Showing
7 changed files
with
221 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { getServerSession } from "next-auth"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { authOptions } from "../../auth/[...nextauth]/route"; | ||
import { collection, getDocs } from "firebase/firestore"; | ||
import { db } from "@/lib/firebaseConfig"; | ||
import { Purchase } from "@/types/types"; | ||
|
||
export async function GET(req: NextRequest) { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (session) { | ||
const { searchParams } = new URL(req.url); | ||
const date = searchParams.get("today"); | ||
const purchaseRef = collection(db, "purchaseEntries"); | ||
const purchaseList: Purchase[] = []; | ||
const dateArray: string[] = []; | ||
|
||
if (date !== null) { | ||
const today = new Date(date); | ||
const sevenDaysAgo = new Date(today); | ||
sevenDaysAgo.setDate(today.getDate() - 6); | ||
|
||
for (let i = 0; i < 7; i++) { | ||
const currentDate = new Date(sevenDaysAgo); | ||
currentDate.setDate(sevenDaysAgo.getDate() + i); | ||
|
||
const formattedDate = currentDate.toISOString().split("T")[0]; | ||
console.log(formattedDate); | ||
dateArray.push(formattedDate); | ||
} | ||
|
||
const snapshot = await getDocs(purchaseRef); | ||
|
||
snapshot.docs.forEach((doc) => { | ||
const purchaseData = doc.data(); | ||
const currentPurchase: Purchase = { | ||
id: doc.id, | ||
date: purchaseData.date, | ||
totalAmount: purchaseData.totalAmount, | ||
userId: purchaseData.userId, | ||
vendorId: purchaseData.vendorId, | ||
vendorName: purchaseData.vendorName, | ||
items: purchaseData.items, | ||
}; | ||
purchaseList.push(currentPurchase); | ||
}); | ||
} else { | ||
return NextResponse.json("Invaid date entered", { status: 400 }); | ||
} | ||
|
||
if (purchaseList.length > 0) { | ||
const lastSeven = []; | ||
for (let i = 0; i < dateArray.length; i++) { | ||
const totalAmount = purchaseList | ||
.filter((purchase) => purchase.date === dateArray[i]) | ||
.reduce((acc, currPurchase) => acc + currPurchase.totalAmount, 0); | ||
|
||
const purchase = { | ||
date: dateArray[i], | ||
amount: totalAmount, | ||
}; | ||
lastSeven.push(purchase); | ||
} | ||
return NextResponse.json(lastSeven, { status: 200 }); | ||
} else { | ||
return NextResponse.json("No purhcase found", { status: 404 }); | ||
} | ||
} else { | ||
return NextResponse.json( | ||
{ error: "You are not authenticated." }, | ||
{ status: 401 } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
Chart as ChartJS, | ||
CategoryScale, | ||
LinearScale, | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
} from "chart.js"; | ||
import { Bar } from "react-chartjs-2"; | ||
|
||
ChartJS.register( | ||
CategoryScale, | ||
LinearScale, | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
Legend | ||
); | ||
import { LastSevenDaysPurchase } from "@/types/types"; | ||
|
||
interface BarChartProps { | ||
purchaseData: LastSevenDaysPurchase[]; | ||
} | ||
|
||
export default function BarChart({ purchaseData }: BarChartProps) { | ||
const dateArray = []; | ||
const amountArray = []; | ||
|
||
for (const obj of purchaseData) { | ||
dateArray.push(obj.date); | ||
amountArray.push(obj.amount); | ||
} | ||
|
||
const options = { | ||
responsive: true, | ||
}; | ||
|
||
const labels = dateArray; | ||
|
||
const data = { | ||
labels, | ||
datasets: [ | ||
{ | ||
label: "Last 7 days", | ||
data: amountArray, | ||
backgroundColor: "rgba(255, 99, 132, 0.5)", | ||
}, | ||
], | ||
}; | ||
|
||
return <Bar options={options} data={data} />; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters