WIP stripe customer metedada sync (#3153)

This commit is contained in:
alannnc
2022-07-07 15:24:42 -06:00
committed by GitHub
parent a7212660a4
commit 2c30976e99
3 changed files with 41 additions and 1 deletions
+9
View File
@@ -0,0 +1,9 @@
import isPrismaObj from "./isPrismaObj";
const hasKeyInMetadata = <T extends string>(
x: { metadata: unknown } | null,
key: T
): x is { metadata: { [key in T]: string | boolean | number } } =>
isPrismaObj(x?.metadata) && !!x?.metadata && key in x.metadata;
export default hasKeyInMetadata;
+11
View File
@@ -0,0 +1,11 @@
import type { Prisma } from "@prisma/client";
function isPrismaObj(obj: unknown): obj is Prisma.JsonObject {
return typeof obj === "object" && !Array.isArray(obj);
}
export function isPrismaObjOrUndefined(obj: unknown) {
return isPrismaObj(obj) ? obj : undefined;
}
export default isPrismaObj;
+21 -1
View File
@@ -10,9 +10,11 @@ import { sendFeedbackEmail } from "@calcom/emails";
import { sendCancelledEmails } from "@calcom/emails";
import { parseRecurringEvent, isPrismaObjOrUndefined } from "@calcom/lib";
import { baseEventTypeSelect, bookingMinimalSelect } from "@calcom/prisma";
import stripe from "@calcom/stripe/server";
import { closePayments } from "@ee/lib/stripe/server";
import { checkUsername } from "@lib/core/server/checkUsername";
import hasKeyInMetadata from "@lib/hasKeyInMetadata";
import jackson from "@lib/jackson";
import prisma from "@lib/prisma";
import { isTeamOwner } from "@lib/queries/teams";
@@ -704,12 +706,30 @@ const loggedInViewerRouter = createProtectedRouter()
data.avatar = await resizeBase64Image(input.avatar);
}
await prisma.user.update({
const updatedUser = await prisma.user.update({
where: {
id: user.id,
},
data,
select: {
id: true,
username: true,
email: true,
metadata: true,
},
});
// Notify stripe about the change
if (updatedUser && updatedUser.metadata && hasKeyInMetadata(updatedUser, "stripeCustomerId")) {
const stripeCustomerId = `${updatedUser.metadata.stripeCustomerId}`;
await stripe.customers.update(stripeCustomerId, {
metadata: {
username: updatedUser.username,
email: updatedUser.email,
userId: updatedUser.id,
},
});
}
},
})
.mutation("eventTypeOrder", {