* create seed for org upgrade * migrate about/new pages * pages refactor + use persistant zustand store * invited members view + confirm button * add check to org slug * check reserved subdomain * remove quotes from subdomain env var * intro to creation + billing * create price * fix types * createWithPaymentIntent + permission check on teams * open stripe link in popup * intro to tests * organization price and seat override tests * move permissions to a new permission service * update env and permission check * intro to paid invoice trigger * dont use subId * wip * Get e2e working with migration of teams and members * fix ts errors * Get flow working again * Fix various issues and refactor * Some more fixes * Fix wrong page route link * Platform onboarding fix and moving members of team to org rix * Platform onboarding fix and moving members of team to org rix * Fix tests, found a bug * Get custom price flow working * Get admin impersonation flow working for a non-existent user * Fix unit test * Admin onboarding handover * fix admin onboarding tests * fix ts error * Get updateQuantity working * More fixes * fix test * fix schema name * Add tests * Add missing file * More tests * Add more tests * fix mt-2 moving down input into overflow * fix handover layout removing HOC * Update PR_TODO.md * fix ts error due to merge conflict --------- Co-authored-by: Hariom Balhara <hariombalhara@gmgmail.com> Co-authored-by: Hariom <hariombalhara@gmail.com> Co-authored-by: Omar López <zomars@me.com>
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { createOrganizationFromOnboarding } from "@calcom/features/ee/organizations/lib/server/createOrganizationFromOnboarding";
|
|
import logger from "@calcom/lib/logger";
|
|
import { safeStringify } from "@calcom/lib/safeStringify";
|
|
import { OrganizationOnboardingRepository } from "@calcom/lib/server/repository/organizationOnboarding";
|
|
|
|
import type { SWHMap } from "./__handler";
|
|
|
|
const invoicePaidSchema = z.object({
|
|
object: z.object({
|
|
customer: z.string(),
|
|
subscription: z.string(),
|
|
lines: z.object({
|
|
data: z.array(
|
|
z.object({
|
|
subscription_item: z.string(),
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
});
|
|
|
|
async function handlePaymentReceivedForOnboarding({
|
|
organizationOnboarding,
|
|
paymentSubscriptionId,
|
|
paymentSubscriptionItemId,
|
|
}: {
|
|
organizationOnboarding: { id: string };
|
|
paymentSubscriptionId: string;
|
|
paymentSubscriptionItemId: string;
|
|
}) {
|
|
await OrganizationOnboardingRepository.update(organizationOnboarding.id, {
|
|
stripeSubscriptionId: paymentSubscriptionId,
|
|
stripeSubscriptionItemId: paymentSubscriptionItemId,
|
|
});
|
|
}
|
|
|
|
const handler = async (data: SWHMap["invoice.paid"]["data"]) => {
|
|
const { object: invoice } = invoicePaidSchema.parse(data);
|
|
const subscriptionItemId = invoice.lines.data[0]?.subscription_item;
|
|
const subscriptionId = invoice.subscription;
|
|
logger.debug(
|
|
`Processing invoice paid webhook for customer ${invoice.customer} and subscription ${invoice.subscription}`
|
|
);
|
|
|
|
const organizationOnboarding = await OrganizationOnboardingRepository.findByStripeCustomerId(
|
|
invoice.customer
|
|
);
|
|
|
|
if (!organizationOnboarding) {
|
|
logger.error(
|
|
`NonRecoverableError: No onboarding record found for stripe customer id: ${invoice.customer}.`
|
|
);
|
|
|
|
// Don't throw as we don't want to retry.
|
|
return {
|
|
success: false,
|
|
error: `No onboarding record found for stripe customer id: ${invoice.customer}.`,
|
|
};
|
|
}
|
|
|
|
const paymentSubscriptionId = subscriptionId;
|
|
const paymentSubscriptionItemId = subscriptionItemId;
|
|
|
|
await handlePaymentReceivedForOnboarding({
|
|
organizationOnboarding,
|
|
paymentSubscriptionId,
|
|
paymentSubscriptionItemId,
|
|
});
|
|
|
|
try {
|
|
const { organization } = await createOrganizationFromOnboarding({
|
|
organizationOnboarding,
|
|
paymentSubscriptionId,
|
|
paymentSubscriptionItemId,
|
|
});
|
|
|
|
logger.debug(`Marking onboarding as complete for organization ${organization.id}`);
|
|
await OrganizationOnboardingRepository.markAsComplete(organizationOnboarding.id);
|
|
return { success: true };
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
await OrganizationOnboardingRepository.update(organizationOnboarding.id, {
|
|
error: error.message,
|
|
});
|
|
}
|
|
logger.error(
|
|
`Error creating organization from onboarding:${organizationOnboarding.id}`,
|
|
safeStringify({ error: error instanceof Error ? error.message : error })
|
|
);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export default handler;
|