feat: whyrating - initial project from turbostarter boilerplate
This commit is contained in:
37
packages/billing/src/providers/polar/subscription.ts
Normal file
37
packages/billing/src/providers/polar/subscription.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { HttpStatusCode } from "@turbostarter/shared/constants";
|
||||
import { logger } from "@turbostarter/shared/logger";
|
||||
import { HttpException } from "@turbostarter/shared/utils";
|
||||
|
||||
import { config } from "../../config";
|
||||
import { getCustomerByCustomerId, updateCustomer } from "../../lib/customer";
|
||||
|
||||
import { polar } from "./client";
|
||||
import { toBillingStatus } from "./mappers/to-billing-status";
|
||||
|
||||
export const subscriptionStatusChangeHandler = async ({
|
||||
id,
|
||||
}: {
|
||||
id: string;
|
||||
}) => {
|
||||
const subscription = await polar().subscriptions.get({ id });
|
||||
|
||||
const customer = await getCustomerByCustomerId(subscription.customerId);
|
||||
|
||||
if (!customer) {
|
||||
throw new HttpException(HttpStatusCode.NOT_FOUND, {
|
||||
code: "billing:error.customerNotFound",
|
||||
});
|
||||
}
|
||||
|
||||
const priceId = subscription.productId;
|
||||
const plan = config.plans.find((p) => p.prices.find((x) => x.id === priceId));
|
||||
|
||||
await updateCustomer(customer.userId, {
|
||||
status: toBillingStatus(subscription.status),
|
||||
...(plan && { plan: plan.id }),
|
||||
});
|
||||
|
||||
logger.info(
|
||||
`✅ Subscription status changed for user ${customer.userId} to ${subscription.status}`,
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user