Skip to content

Commit

Permalink
display current quarter donations
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinodds committed May 16, 2024
1 parent 5f93242 commit 2c7db8d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
9 changes: 9 additions & 0 deletions components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ export const notEmpty = <T,>(list: T[]) => {
if (list.length == 0) throw new TypeError('List must not be empty')
return list as [T, ...T[]]
}

export const getQuarterDates = (): { startDate: Date; endDate: Date } => {
const currentDate = new Date()
const quarter = Math.floor(currentDate.getMonth() / 3)
const year = currentDate.getFullYear()
const startDate = new Date(year, quarter * 3, 1)
const endDate = new Date(year, quarter * 3 + 3, 0)
return { startDate, endDate }
}
10 changes: 5 additions & 5 deletions pages/project/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ function ProjectPage({ projectId }: { projectId: string }) {
)}
<div className="text-sm text-secondary whitespace-nowrap">
<IconMoneybag className="inline" />
{project.fundingGoal !== null
? `Quarterly goal: $${num(project.fundingGoal)}`
: ''}{' '}
{/* TOTAL FUNDING ALL TIME */}
{/* ${num(project.donationTotal, 0)} */}
{project.fundingGoal !== null && project.fundingGoal.gt(0)
? `$${project.quarterDonationTotal} / $${num(
project.fundingGoal,
)}`
: `$${num(project.donationTotal, 0)}`}{' '}
</div>
<div className="text-sm text-secondary whitespace-nowrap">
<IconWoman className="inline" />{' '}
Expand Down
22 changes: 20 additions & 2 deletions server/routers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Prisma, User } from '@prisma/client'
import { EventStatus, EventType } from '@prisma/client'
import { TRPCError } from '@trpc/server'

import { getQuarterDates } from '@/components/utils'
import { PROJECT_SORT_KEYS, ProjectSortKey } from '@/lib/constants'
import { markdownToHtml, markdownToPlainHtml } from '@/lib/editor'

Expand Down Expand Up @@ -260,8 +261,25 @@ export const projectRouter = router({
"projectId" = ${id}
AND "state" = 'CONFIRMED'
`

return { ...project, ...result[0] }
const { startDate, endDate } = getQuarterDates()
const quarterResult: {
quarterDonationTotal: Prisma.Decimal
}[] = await ctx.prisma.$queryRaw`
SELECT
COALESCE(SUM(amount), 0) AS "quarterDonationTotal"
FROM
"Donation"
WHERE
"projectId" = ${id}
AND "state" = 'CONFIRMED'
AND "time" >= ${startDate.toISOString()}::timestamp
AND "time" <= ${endDate.toISOString()}::timestamp
`
return {
...project,
...result[0],
quarterDonationTotal: quarterResult[0].quarterDonationTotal,
}
}),
search: protectedProcedure
.input(
Expand Down

0 comments on commit 2c7db8d

Please sign in to comment.