Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add changeHistoryLog (#3010)

Steven Teydbb584a6ca26
diff --git a/apps/web/lib/actions/partners/onboard-partner.ts b/apps/web/lib/actions/partners/onboard-partner.ts
index c9bc375f86..1d1d6a0011 100644
--- a/apps/web/lib/actions/partners/onboard-partner.ts
+++ b/apps/web/lib/actions/partners/onboard-partner.ts
@@ -40,14 +40,9 @@ export const onboardPartnerAction = authUserActionClient
const payload: Prisma.PartnerCreateInput = {
name: name || user.email,
email: user.email,
- // you can only update these fields if the partner doesn't already have a stripeConnectId
- ...(existingPartner?.stripeConnectId
- ? {}
- : {
- country,
- profileType,
- companyName,
- }),
+ // can only update these fields if it's not already set (else you need to update under profile settings)
+ ...(existingPartner?.country ? {} : { country }),
+ ...(existingPartner?.profileType ? {} : { profileType }),
...(description && { description }),
image: imageUrl,
users: {
@@ -69,7 +64,7 @@ export const onboardPartnerAction = authUserActionClient
},
};
- const [partner] = await Promise.all([
+ await Promise.all([
existingPartner
? prisma.partner.update({
where: {
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index ba694fcf3b..641814087e 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -5,6 +5,8 @@ import { throwIfNoPermission } from "@/lib/auth/partner-user-permissions";
import { qstash } from "@/lib/cron";
import { getPartnerDiscoveryRequirements } from "@/lib/partners/discoverability";
import { storage } from "@/lib/storage";
+import { stripe } from "@/lib/stripe";
+import { partnerProfileChangeHistoryLogSchema } from "@/lib/zod/schemas/partner-profile";
import {
MAX_PARTNER_DESCRIPTION_LENGTH,
PartnerProfileSchema,
@@ -263,12 +265,49 @@ const updatedComplianceFieldsChecks = async ({
input.profileType !== undefined &&
partner.profileType.toLowerCase() !== input.profileType.toLowerCase();
- if (partner.payoutsEnabledAt && (countryChanged || profileTypeChanged)) {
+ if (!countryChanged && !profileTypeChanged) {
+ return;
+ }
+
+ if (partner.payoutsEnabledAt) {
throw new Error(
"Since you've already connected your bank account for payouts, you cannot change your country or profile type. Please contact support to update those fields.",
);
}
- // TODO: log country and profile type changes
- return;
+ const partnerChangeHistoryLog = partner.changeHistoryLog
+ ? partnerProfileChangeHistoryLogSchema.parse(partner.changeHistoryLog)
+ : [];
+
+ if (countryChanged) {
+ partnerChangeHistoryLog.push({
+ field: "country",
+ from: partner.country as string,
+ to: input.country as string,
+ changedAt: new Date(),
+ });
+ }
+
+ if (profileTypeChanged) {
+ partnerChangeHistoryLog.push({
+ field: "profileType",
+ from: partner.profileType,
+ to: input.profileType as PartnerProfileType,
+ changedAt: new Date(),
+ });
+ }
+
+ if (partner.stripeConnectId) {
+ await stripe.accounts.del(partner.stripeConnectId);
+ }
+
+ await prisma.partner.update({
+ where: {
+ id: partner.id,
+ },
+ data: {
+ stripeConnectId: null,
+ changeHistoryLog: partnerChangeHistoryLog,
+ },
+ });
};
diff --git a/apps/web/lib/zod/schemas/partner-profile.ts b/apps/web/lib/zod/schemas/partner-profile.ts
index aaac62353c..cd7e6d30eb 100644
--- a/apps/web/lib/zod/schemas/partner-profile.ts
+++ b/apps/web/lib/zod/schemas/partner-profile.ts
@@ -4,6 +4,7 @@ import {
} from "@/lib/analytics/constants";
import {
CommissionType,
+ PartnerProfileType,
PartnerRole,
ProgramEnrollmentStatus,
} from "@prisma/client";
@@ -172,3 +173,20 @@ export const partnerUserSchema = z.object({
image: z.string().nullish(),
createdAt: z.date(),
});
+
+export const partnerProfileChangeHistoryLogSchema = z.array(
+ z.union([
+ z.object({
+ field: z.literal("country"),
+ from: z.string(),
+ to: z.string(),
+ changedAt: z.coerce.date(),
+ }),
+ z.object({
+ field: z.literal("profileType"),
+ from: z.nativeEnum(PartnerProfileType),
+ to: z.nativeEnum(PartnerProfileType),
+ changedAt: z.coerce.date(),
+ }),
+ ]),
+);
diff --git a/packages/prisma/schema/partner.prisma b/packages/prisma/schema/partner.prisma
index 9ee89192e3..81b6698d9a 100644
--- a/packages/prisma/schema/partner.prisma
+++ b/packages/prisma/schema/partner.prisma
@@ -58,6 +58,7 @@ model Partner {
tiktok String?
tiktokVerifiedAt DateTime?
invoiceSettings Json?
+ changeHistoryLog Json?
programs ProgramEnrollment[]
users PartnerUser[]