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.

update createRewardSchema

Kiran K697b7e2b0a91
diff --git a/apps/web/lib/actions/partners/create-reward.ts b/apps/web/lib/actions/partners/create-reward.ts
index cef28ef473..e4f1674a72 100644
--- a/apps/web/lib/actions/partners/create-reward.ts
+++ b/apps/web/lib/actions/partners/create-reward.ts
@@ -2,6 +2,7 @@
import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { createId } from "@/lib/api/create-id";
+import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import {
@@ -17,110 +18,55 @@ export const createRewardAction = authActionClient
.schema(createRewardSchema)
.action(async ({ parsedInput, ctx }) => {
const { workspace, user } = ctx;
- let {
- event,
- amount,
- type,
- maxDuration,
- isDefault,
- includedPartnerIds,
- excludedPartnerIds,
- modifiers,
- } = parsedInput;
-
- includedPartnerIds = includedPartnerIds || [];
- excludedPartnerIds = excludedPartnerIds || [];
+ const { event, amount, type, maxDuration, modifiers, groupId } =
+ parsedInput;
const programId = getDefaultProgramIdOrThrow(workspace);
+ const { canUseAdvancedRewardLogic } = getPlanCapabilities(workspace.plan);
- // Only one default reward is allowed for each event
- if (isDefault) {
- const defaultReward = await prisma.reward.findFirst({
- where: {
- programId,
- event,
- default: true,
- },
- });
-
- if (defaultReward) {
- throw new Error(
- `There is an existing default ${event} reward already. A program can only have one ${event} default reward.`,
- );
- }
- }
-
- if (
- modifiers &&
- !getPlanCapabilities(workspace.plan).canUseAdvancedRewardLogic
- )
+ if (modifiers && !canUseAdvancedRewardLogic) {
throw new Error(
"Advanced reward structures are only available on the Advanced plan and above.",
);
+ }
- const finalPartnerIds = [...includedPartnerIds, ...excludedPartnerIds];
+ await getGroupOrThrow({
+ groupId,
+ programId,
+ });
- if (finalPartnerIds && finalPartnerIds.length > 0) {
- const programEnrollments = await prisma.programEnrollment.findMany({
- where: {
+ await prisma.$transaction(async (tx) => {
+ const reward = await tx.reward.create({
+ data: {
+ id: createId({ prefix: "rw_" }),
programId,
- partnerId: {
- in: finalPartnerIds,
- },
- },
- select: {
- partnerId: true,
+ event,
+ type,
+ amount,
+ maxDuration,
+ modifiers: modifiers || Prisma.JsonNull,
},
});
- const invalidPartnerIds = finalPartnerIds.filter(
- (id) =>
- !programEnrollments.some((enrollment) => enrollment.partnerId === id),
- );
-
- if (invalidPartnerIds.length > 0) {
- throw new Error(
- `Invalid partner IDs provided (partners must be enrolled in the program): ${invalidPartnerIds.join(", ")}`,
- );
- }
- }
-
- const reward = await prisma.reward.create({
- data: {
- id: createId({ prefix: "rw_" }),
- programId,
- event,
- type,
- amount,
- maxDuration,
- default: isDefault,
- modifiers: modifiers || Prisma.JsonNull,
- },
- });
+ const rewardIdColumn = REWARD_EVENT_COLUMN_MAPPING[reward.event];
- const rewardIdColumn = REWARD_EVENT_COLUMN_MAPPING[reward.event];
+ await tx.partnerGroup.update({
+ where: {
+ id: groupId,
+ },
+ data: {
+ [rewardIdColumn]: reward.id,
+ },
+ });
- await prisma.programEnrollment.updateMany({
- where: {
- programId,
- ...(reward.default
- ? {
- [rewardIdColumn]: null,
- ...(excludedPartnerIds.length > 0 && {
- partnerId: {
- notIn: excludedPartnerIds,
- },
- }),
- }
- : {
- partnerId: {
- in: includedPartnerIds,
- },
- }),
- },
- data: {
- [rewardIdColumn]: reward.id,
- },
+ await tx.programEnrollment.updateMany({
+ where: {
+ groupId,
+ },
+ data: {
+ [rewardIdColumn]: reward.id,
+ },
+ });
});
waitUntil(
diff --git a/apps/web/lib/zod/schemas/rewards.ts b/apps/web/lib/zod/schemas/rewards.ts
index 3ba8f17bfe..b85632572f 100644
--- a/apps/web/lib/zod/schemas/rewards.ts
+++ b/apps/web/lib/zod/schemas/rewards.ts
@@ -82,16 +82,8 @@ export const createOrUpdateRewardSchema = z.object({
type: z.nativeEnum(RewardStructure).default(RewardStructure.flat),
amount: z.number().min(0),
maxDuration: maxDurationSchema,
- isDefault: z.boolean(),
- includedPartnerIds: z
- .array(z.string())
- .nullish()
- .describe("Only applicable for non-default rewards"),
- excludedPartnerIds: z
- .array(z.string())
- .nullish()
- .describe("Only applicable for default rewards"),
modifiers: rewardConditionsArraySchema.nullish(),
+ groupId: z.string(),
});
export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
@@ -106,7 +98,7 @@ export const createRewardSchema = createOrUpdateRewardSchema.superRefine(
export const updateRewardSchema = createOrUpdateRewardSchema
.omit({
event: true,
- isDefault: true,
+ groupId: true,
})
.merge(
z.object({