Skip to content

Commit

Permalink
filtering transactions by year
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Dec 7, 2024
1 parent de42317 commit 62e9a05
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
32 changes: 27 additions & 5 deletions app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,33 @@ import Th from '../components/Th'
export default function Admin({}) {
return <Provider>
<Balance />
<Transactions />
<TransactionYears />
</Provider>
}

const GET_TRANSACTION_YEARS = gql`
query GetTransactionYears {
transactionYears
}`

function TransactionYears() {
const {loading, error, data} = useQuery(GET_TRANSACTION_YEARS)
const [year, setYear] = useState(new Date().getFullYear())
if (loading) return <Loading />
if (error) return <Error error={error}/>
return <>
<select onChange={evt => setYear(parseInt(evt.target.value))}>
{data.transactionYears.map((y: number) =>
<option key={y} value={y} selected={y===year}>{y}</option>
)}
</select>
<Transactions year={year}/>
</>
}

const GET_TRANSACTIONS = gql`
query GetTransactions {
transactions {
query GetTransactions($year: Int) {
transactions(year: $year) {
_id
timestamp
email
Expand All @@ -43,9 +63,11 @@ type Transaction = {
description: string
}

function Transactions() {
function Transactions({year}:{year: number}) {
const [edit,setEdit] = useState(false)
const {loading, error, data} = useQuery(GET_TRANSACTIONS)
const {loading, error, data} = useQuery(GET_TRANSACTIONS, {
variables: {year}
})
if (loading) return <Loading />
if (error) return <Error error={error}/>
return <>
Expand Down
41 changes: 35 additions & 6 deletions app/graphql/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ const typeDefs = gql`
"""
all transactions
"""
transactions: [Transaction]
transactions(year: Int): [Transaction]
"""
years with transaction
"""
transactionYears: [Int]
"""
users and their credit
Expand Down Expand Up @@ -130,8 +135,8 @@ const typeDefs = gql`
remove card pairing
"""
card_remove_pairing: Boolean
}
`
}`

const resolvers = {
Query: {
cost: async(_: any, __: {}, context: Context) => {
Expand Down Expand Up @@ -200,16 +205,40 @@ const resolvers = {
return result
},

transactions: async(_: any, __: {}, context: Context) => {
transactionYears: async(_: any, __: {}, context: Context) => {
if (!context.user) throw new Error("not logged in")
const db = (await databasePromise).db
const account = db.collection("account")
const result = await account.aggregate([
{ $group: {
_id: { $year: "$timestamp" },
}},
{ $project: { _id: 0, year: "$_id" } }
]).toArray()
return result.map(x => x.year)
},

transactions: async(_: any, {year} : {year?: number}, context: Context) => {
if (!context.user) throw new Error("not logged in")
if (!config.ADMINS.split(',').includes(context.user.email)) throw new Error("not admin")

const db = (await databasePromise).db
const account = db.collection("account")

// Build the query object
const query: Record<string, any> = {};
if (year) {
query.timestamp = {
$gte: new Date(`${year}-01-01T00:00:00Z`),
$lt: new Date(`${year + 1}-01-01T00:00:00Z`),
};
}

const result = await account
.find({})
.find(query)
.sort({ timestamp: -1 })
.toArray()
.toArray();

return result
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-coffee",
"version": "1.1.2",
"version": "1.1.3",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down

0 comments on commit 62e9a05

Please sign in to comment.