shellphone.app/app/settings/queries/get-payments.ts

22 lines
679 B
TypeScript
Raw Normal View History

2021-09-30 22:59:35 +00:00
import { resolver } from "blitz";
2021-10-01 21:05:07 +00:00
import db from "db";
2021-09-30 22:59:35 +00:00
import { getPayments } from "integrations/paddle";
export default resolver.pipe(resolver.authorize(), async (_ = null, { session }) => {
if (!session.orgId) {
return [];
}
2021-10-01 21:05:07 +00:00
const subscriptions = await db.subscription.findMany({ where: { organizationId: session.orgId } });
if (subscriptions.length === 0) {
2021-09-30 22:59:35 +00:00
return [];
}
2021-10-01 21:05:07 +00:00
const paymentsBySubscription = await Promise.all(
subscriptions.map((subscription) => getPayments({ subscriptionId: subscription.paddleSubscriptionId })),
);
const payments = paymentsBySubscription.flat();
2021-09-30 22:59:35 +00:00
return payments.sort((a, b) => b.payout_date.localeCompare(a.payout_date));
});