Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { recordAuditLog } from "@/lib/api/audit-logs/record-audit-log";
import { DubApiError } from "@/lib/api/errors";
import { getGroupOrThrow } from "@/lib/api/groups/get-group-or-throw";
import { movePartnersToGroup } from "@/lib/api/groups/move-partners-to-group";
import { scrubFromPartnerGroupReferences } from "@/lib/api/groups/scrub-from-partner-group-references";
import { upsertGroupMoveRules } from "@/lib/api/groups/upsert-group-move-rules";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
import { parseRequestBody } from "@/lib/api/utils";
Expand Down Expand Up @@ -365,7 +366,17 @@ export const DELETE = withWorkspace(
// for `remap-discount-codes` that runs in movePartnersToGroup
// but we will delete the Discount in `remap-discount-codes` once there are no remaining discount codes.

// 2. Delete the group move workflow
// TODO:
// Move this to a background job using defineJob

// 2. Remove this group from other groups' fromPartnerGroup move-rule conditions
await scrubFromPartnerGroupReferences({
programId,
deletedGroupId: group.id,
tx,
});

// 3. Delete the group move workflow
if (group.workflowId) {
await tx.workflow.delete({
where: {
Expand All @@ -374,7 +385,7 @@ export const DELETE = withWorkspace(
});
}

// 3. Delete the group
// 4. Delete the group
await tx.partnerGroup.delete({
where: {
id: group.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";

import { findGroupsWithMatchingRules } from "@/lib/api/groups/find-groups-with-matching-rules";
import { validateGroupMoveRules } from "@/lib/api/groups/validate-group-move-rules";
import { PAYOUT_HOLDING_PERIOD_DAYS } from "@/lib/constants/payouts";
import { mutatePrefix } from "@/lib/swr/mutate";
import { useApiMutation } from "@/lib/swr/use-api-mutation";
Expand Down Expand Up @@ -117,34 +116,22 @@ function GroupAdditionalSettingsForm({

const onSubmit = async (data: FormData) => {
if (!group) return;
if (data.moveRules && data.moveRules.length > 0) {
try {
validateGroupMoveRules({
rules: data.moveRules,
destinationGroupId: group.id,
});
} catch (error) {
toast.error(error.message);
if (data.moveRules && data.moveRules.length > 0 && groups) {
const groupsWithMatchingRules = findGroupsWithMatchingRules({
groups,
currentRules: data.moveRules,
currentGroupId: group.id,
});

if (groupsWithMatchingRules.length > 0) {
const groupNames = groupsWithMatchingRules
.map((g) => g.name)
.join(", ");
toast.error(
`This rule is already in use by the ${groupNames} ${pluralize("group", groupsWithMatchingRules.length)}. Select a different activity or amount.`,
);
return;
}

if (groups) {
const groupsWithMatchingRules = findGroupsWithMatchingRules({
groups,
currentRules: data.moveRules,
currentGroupId: group.id,
});

if (groupsWithMatchingRules.length > 0) {
const groupNames = groupsWithMatchingRules
.map((g) => g.name)
.join(", ");
toast.error(
`This rule is already in use by the ${groupNames} ${pluralize("group", groupsWithMatchingRules.length)}. Select a different activity or amount.`,
);
return;
}
}
}

await updateGroup(`/api/groups/${group.id}`, {
Expand Down
Loading
Loading