Skip to content

Commit

Permalink
fix: fix an issue where upcoming invoice doesn't have the nextPeriodC…
Browse files Browse the repository at this point in the history
…harges field (#9580)
  • Loading branch information
tianrunhe committed Apr 8, 2024
1 parent 1a7e298 commit cb52596
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import RowInfo from '../../../../components/Row/RowInfo'
import RowInfoHeading from '../../../../components/Row/RowInfoHeading'
import {PALETTE} from '../../../../styles/paletteV3'
import makeDateString from '../../../../utils/makeDateString'
import makeMonthDateString from '../../../../utils/makeMonthDateString'
import invoiceLineFormat from '../../../invoice/helpers/invoiceLineFormat'

const InvoiceAmount = styled('span')({
Expand Down Expand Up @@ -105,7 +104,9 @@ const InvoiceRow = (props: Props) => {
<InvoiceInfo>
<InfoRow>
<InvoiceTitle>
{makeMonthDateString(endAt)} to {makeMonthDateString(nextPeriodEnd)}
{status === 'UPCOMING'
? `Due on ${makeDateString(endAt)}`
: `${makeDateString(endAt)} to ${makeDateString(nextPeriodEnd)}`}
</InvoiceTitle>
<InfoRowRight>
<InvoiceAmount>
Expand Down
10 changes: 0 additions & 10 deletions packages/client/utils/makeMonthDateString.ts

This file was deleted.

28 changes: 25 additions & 3 deletions packages/server/graphql/queries/helpers/makeUpcomingInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import {fromEpochSeconds} from '../../../utils/epochTime'
import getUpcomingInvoiceId from '../../../utils/getUpcomingInvoiceId'
import {getStripeManager} from '../../../utils/stripe'
import StripeManager from '../../../utils/stripe/StripeManager'
import Invoice from '../../../database/types/Invoice'
import Organization from '../../../database/types/Organization'

export default async function makeUpcomingInvoice(
orgId: string,
org: Organization,
quantity: number,
stripeId?: string | null
) {
): Promise<Invoice | undefined> {
if (!stripeId) return undefined
const manager = getStripeManager()
let stripeInvoice: Stripe.Invoice
Expand Down Expand Up @@ -41,6 +43,10 @@ export default async function makeUpcomingInvoice(
stripeInvoice = await manager.retrieveUpcomingInvoice(stripeId)
}

const {id: orgId, tier, name: orgName, picture} = org
const unitPrice = subscription?.plan?.amount ?? 0
const amount = unitPrice * quantity

return {
id: getUpcomingInvoiceId(orgId),
amountDue: stripeInvoice.amount_due,
Expand All @@ -51,6 +57,22 @@ export default async function makeUpcomingInvoice(
orgId,
startAt: fromEpochSeconds(stripeInvoice.period_start),
startingBalance: stripeInvoice.starting_balance,
status: 'UPCOMING'
status: 'UPCOMING',
createdAt: fromEpochSeconds(stripeInvoice.period_start),
billingLeaderEmails: [],
lines: [],
orgName,
tier,
paidAt: null,
picture: picture ?? null,
nextPeriodCharges: {
amount,
quantity,
nextPeriodEnd: fromEpochSeconds(
stripeInvoice.period_end - stripeInvoice.period_start + stripeInvoice.period_end
),
unitPrice,
interval: subscription?.plan?.interval ?? 'month'
}
}
}
3 changes: 2 additions & 1 deletion packages/server/graphql/queries/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ export default {
.count()
.run()
])
const org = await dataLoader.get('organizations').load(orgId)
const upcomingInvoice = after
? undefined
: await makeUpcomingInvoice(orgId, orgUserCount, stripeId)
: await makeUpcomingInvoice(org, orgUserCount, stripeId)
const extraInvoices: Invoice[] = tooManyInvoices || []
const paginatedInvoices = after ? extraInvoices.slice(1) : extraInvoices
const allInvoices = upcomingInvoice
Expand Down

0 comments on commit cb52596

Please sign in to comment.