|
1 | 1 | import { Session } from "@/lib/auth"; |
2 | | -import { qstash } from "@/lib/cron"; |
3 | 2 | import { ACTIVE_ENROLLMENT_STATUSES } from "@/lib/zod/schemas/partners"; |
4 | 3 | import { prisma } from "@dub/prisma"; |
5 | | -import { APP_DOMAIN_WITH_NGROK } from "@dub/utils"; |
6 | | -import { waitUntil } from "@vercel/functions"; |
7 | | -import { recordAuditLog } from "../audit-logs/record-audit-log"; |
| 4 | +import { processPartnerDeactivation } from "./process-partner-deactivation"; |
8 | 5 |
|
9 | 6 | interface BulkDeactivatePartnersParams { |
10 | 7 | workspaceId: string; |
@@ -52,94 +49,13 @@ export async function bulkDeactivatePartners({ |
52 | 49 | return; |
53 | 50 | } |
54 | 51 |
|
55 | | - const partnerIdsToDeactivate = programEnrollments.map( |
56 | | - ({ partner }) => partner.id, |
57 | | - ); |
| 52 | + const partners = programEnrollments.map(({ partner }) => partner); |
58 | 53 |
|
59 | | - await prisma.$transaction([ |
60 | | - prisma.link.updateMany({ |
61 | | - where: { |
62 | | - programId, |
63 | | - partnerId: { |
64 | | - in: partnerIdsToDeactivate, |
65 | | - }, |
66 | | - }, |
67 | | - data: { |
68 | | - expiresAt: new Date(), |
69 | | - }, |
70 | | - }), |
71 | | - |
72 | | - prisma.programEnrollment.updateMany({ |
73 | | - where: { |
74 | | - partnerId: { |
75 | | - in: partnerIdsToDeactivate, |
76 | | - }, |
77 | | - programId, |
78 | | - status: { |
79 | | - in: ACTIVE_ENROLLMENT_STATUSES, |
80 | | - }, |
81 | | - }, |
82 | | - data: { |
83 | | - status: "deactivated", |
84 | | - clickRewardId: null, |
85 | | - leadRewardId: null, |
86 | | - saleRewardId: null, |
87 | | - discountId: null, |
88 | | - }, |
89 | | - }), |
90 | | - ]); |
91 | | - |
92 | | - console.log("[bulkDeactivatePartners] Deactivated partners in program.", { |
| 54 | + await processPartnerDeactivation({ |
| 55 | + workspaceId, |
93 | 56 | programId, |
94 | | - partnerIds: partnerIdsToDeactivate, |
| 57 | + partners, |
| 58 | + user, |
| 59 | + programDeactivated, |
95 | 60 | }); |
96 | | - |
97 | | - if (user) { |
98 | | - waitUntil( |
99 | | - recordAuditLog( |
100 | | - programEnrollments.map(({ partner }) => ({ |
101 | | - workspaceId, |
102 | | - programId, |
103 | | - action: "partner.deactivated", |
104 | | - description: `Partner ${partner.id} deactivated`, |
105 | | - actor: user, |
106 | | - targets: [ |
107 | | - { |
108 | | - type: "partner", |
109 | | - id: partner.id, |
110 | | - metadata: { |
111 | | - name: partner.name, |
112 | | - email: partner.email ?? null, |
113 | | - }, |
114 | | - }, |
115 | | - ], |
116 | | - })), |
117 | | - ), |
118 | | - ); |
119 | | - } |
120 | | - |
121 | | - const qstashResponse = await qstash.publishJSON({ |
122 | | - url: `${APP_DOMAIN_WITH_NGROK}/api/cron/partners/deactivate`, |
123 | | - body: { |
124 | | - programId, |
125 | | - partnerIds: partnerIdsToDeactivate, |
126 | | - programDeactivated, |
127 | | - }, |
128 | | - }); |
129 | | - |
130 | | - if (qstashResponse.messageId) { |
131 | | - console.log( |
132 | | - "[bulkDeactivatePartners] Deactivation job enqueued successfully.", |
133 | | - { |
134 | | - response: qstashResponse, |
135 | | - }, |
136 | | - ); |
137 | | - } else { |
138 | | - console.error( |
139 | | - "[bulkDeactivatePartners] Failed to enqueue deactivation job", |
140 | | - { |
141 | | - response: qstashResponse, |
142 | | - }, |
143 | | - ); |
144 | | - } |
145 | 61 | } |
0 commit comments