Enhancement/cal 708 delete account (#1403)
* --WIP * --WIP * --WIP * added prisma migration and delete cascade for user * stripe customer removal and other --wip * --wip * added stripe user delete * removed log remnants * fixed signout import * cleanup * Changes requested * fixed common-json apostrophe * Simplifies account deletion logic and add e2e tests * Cleanup Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
12 files changed
+235
-34
No files matched your search
@@ -168,4 +168,45 @@ async function handleRefundError(opts: { event: CalendarEvent; reason: string; p
|
||||
});
|
||||
}
|
||||
|
||||
const userType = Prisma.validator<Prisma.UserArgs>()({
|
||||
select: {
|
||||
email: true,
|
||||
metadata: true,
|
||||
},
|
||||
});
|
||||
|
||||
type UserType = Prisma.UserGetPayload<typeof userType>;
|
||||
export async function getStripeCustomerId(user: UserType): Promise<string | null> {
|
||||
let customerId: string | null = null;
|
||||
|
||||
if (user?.metadata && typeof user.metadata === "object" && "stripeCustomerId" in user.metadata) {
|
||||
customerId = (user?.metadata as Prisma.JsonObject).stripeCustomerId as string;
|
||||
} else {
|
||||
/* We fallback to finding the customer by email (which is not optimal) */
|
||||
const customersReponse = await stripe.customers.list({
|
||||
email: user.email,
|
||||
limit: 1,
|
||||
});
|
||||
if (customersReponse.data[0]?.id) {
|
||||
customerId = customersReponse.data[0].id;
|
||||
}
|
||||
}
|
||||
|
||||
return customerId;
|
||||
}
|
||||
|
||||
export async function deleteStripeCustomer(user: UserType): Promise<string | null> {
|
||||
const customerId = await getStripeCustomerId(user);
|
||||
|
||||
if (!customerId) {
|
||||
console.warn("No stripe customer found for user:" + user.email);
|
||||
return null;
|
||||
}
|
||||
|
||||
//delete stripe customer
|
||||
const deletedCustomer = await stripe.customers.del(customerId);
|
||||
|
||||
return deletedCustomer.id;
|
||||
}
|
||||
|
||||
export default stripe;
|
||||
Reference in new issue
Block a user