Skip to content

Commit

Permalink
fix: fix refunds
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanvanherwijnen committed Aug 9, 2024
1 parent 7575f41 commit eb1684a
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
"@fastify/cors": "^9.0.1",
"@fastify/middie": "8.3.1",
"@fastify/static": "7.0.4",
"@modular-api/api": "^0.4.17",
"@modular-api/api": "^0.4.19",
"@modular-api/fastify-cart": "^0.2.4",
"@modular-api/fastify-checkout": "^0.3.7",
"@modular-api/fastify-checkout": "^0.3.9",
"@modular-api/fastify-oidc": "^0.5.4",
"@mollie/api-client": "^3.7.0",
"@slimfact/app": "^0.1.0",
Expand Down
37 changes: 37 additions & 0 deletions packages/api/src/pgboss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PgBoss, { Job } from 'pg-boss'
import { db, postgresConnectionString } from '../src/kysely/index.js'
import { InvoiceStatus, RawNewInvoice } from '@modular-api/fastify-checkout'
import { FastifyInstance } from 'fastify'
import { RefundStatus } from '@modular-api/fastify-checkout/types'

let boss: PgBoss
const createSubscriptionWorker = ({ fastify }: { fastify: FastifyInstance }) =>
Expand All @@ -16,6 +17,27 @@ const createSubscriptionWorker = ({ fastify }: { fastify: FastifyInstance }) =>
return true
}

const createRefundWorker = ({ fastify }: { fastify: FastifyInstance }) =>
async function refundWorker() {
const refunds = await db
.selectFrom('checkout.refunds')
.where((web) =>
web.or([
web('status', '=', RefundStatus.PENDING),
web('status', '=', RefundStatus.PROCESSING),
web('status', '=', RefundStatus.QUEUED)
])
)
.select('id')
.execute()

for (const refund of refunds) {
await fastify.checkout?.paymentHandlers?.mollie?.getRefund({
id: refund.id
})
}
}

export const initialize = async ({ fastify }: { fastify: FastifyInstance }) => {
boss = new PgBoss(postgresConnectionString)

Expand All @@ -34,6 +56,21 @@ export const initialize = async ({ fastify }: { fastify: FastifyInstance }) => {
subscriptionWorker
)
})

const refundWorker = createRefundWorker({ fastify })
await boss.work(
'checkRefunds',
{ batchSize: 1, includeMetadata: true },
refundWorker
)
if (!schedules.some((schedule) => schedule.name === 'checkRefunds')) {
const queueName = `checkRefunds`

if (!(await boss.getQueue(queueName))) {
await boss.createQueue(queueName)
}
await boss.schedule(queueName, '0 0 * * *', {}, {})
}
// await boss.work(
// 'subscription:*',
// { batchSize: 1, includeMetadata: true },
Expand Down
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"devDependencies": {
"@modular-api/fastify-cart": "^0.2.4",
"@modular-api/fastify-checkout": "^0.3.3",
"@modular-api/quasar-components": "^0.2.10",
"@modular-api/quasar-components": "^0.2.11",
"@quasar/extras": "1.16.11",
"@quasar/quasar-ui-qcalendar": "4.0.0-beta.16",
"@simsustech/quasar-components": "^0.10.3",
Expand Down
17 changes: 16 additions & 1 deletion packages/app/src/components/invoice/InvoiceExpansionItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@
name="payments"
:label="lang.payment.payments"
/>
<q-tab
v-if="modelValue.refunds?.length"
name="refunds"
:label="lang.refund.refunds"
/>
</q-tabs>

<q-separator />
Expand Down Expand Up @@ -396,6 +401,15 @@
/>
</q-list>
</q-tab-panel>
<q-tab-panel v-if="modelValue.refunds?.length" name="refunds">
<q-list>
<refund-item
v-for="refund in modelValue.refunds"
:key="refund.id"
:model-value="refund"
/>
</q-list>
</q-tab-panel>
</q-tab-panels>

<!-- <q-scroll-area :style="scrollAreaSize">
Expand All @@ -417,7 +431,8 @@ import Price from '../Price.vue'
import {
InvoiceLineRow,
InvoiceDiscountSurchargeRow,
PaymentItem
PaymentItem,
RefundItem
} from '@modular-api/quasar-components/checkout'
import { computed, ref, toRefs } from 'vue'
import { useQuasar } from 'quasar'
Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/lang/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ const lang: Language = {
}
},
refund: {
refund: 'Refund'
refund: 'Refund',
refunds: 'Refunds',
messages: {
confirmRefund: (amount) =>
`Are you sure you want to refund the amount of ${amount}?`
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/app/src/lang/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export interface Language {
}
refund: {
refund: string
refunds: string
messages: {
confirmRefund: (amount: number | string) => string
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/app/src/lang/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ const lang: Language = {
}
},
refund: {
refund: 'Terugbetaling'
refund: 'Terugbetaling',
refunds: 'Terugbetalingen',
messages: {
confirmRefund: (amount) =>
`Weet u zeker dat u een terugbetaling t.w.v. ${amount} wil doen?`
}
}
}

Expand Down
25 changes: 17 additions & 8 deletions packages/app/src/pages/InvoicePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,24 @@ const payWithSmartpin = async () => {
}
const refund = async () => {
if (invoice.value) {
const result = useMutation('admin.refundInvoice', {
args: {
id: invoice.value.id
},
immediate: true
if (invoice.value?.amountDue && invoice.value.amountDue < 0) {
$q.dialog({
message: lang.value.refund.messages.confirmRefund(
format(-invoice.value.amountDue)
),
cancel: true
}).onOk(async () => {
if (invoice.value?.id) {
const result = useMutation('admin.refundInvoice', {
args: {
id: invoice.value.id
},
immediate: true
})
await result.immediatePromise
}
})
await result.immediatePromise
}
}
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb1684a

Please sign in to comment.