Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | 4x 2x 2x 1x 3x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 2x 1x 2x 1x 1x 1x 1x 1x 2x 1x 2x 1x 1x 1x 2x 1x 3x 3x 3x 3x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 1x 1x | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
type AdvanceReferralBody,
adminRrmApi,
type RrmPayoutState,
type RrmProspectFilters,
type RrmReversalReason,
type RrmStage,
type RrmTaskState,
type RrmTaskType,
} from "../../lib/api/admin/rrm";
import { queryKeys } from "../../lib/query-keys";
import { STALE_1MIN, STALE_2MIN, STALE_30SEC } from "../../lib/stale-times";
/**
* RRM query hooks (F-21).
*
* A separate file from `useAdminQueries.ts` on purpose: RRM is a time-boxed
* campaign with its own polling needs, and mixing it into the shared admin
* hooks would drag those refetch intervals along with it.
*
* Two screens poll because their numbers decay if nobody looks: the inbox
* (backlog over 24h auto-pauses the campaign) and the ramp (its gate is
* evaluated server-side and can flip while the page is open).
*/
export function useRrmFunnel() {
return useQuery({
queryKey: queryKeys.admin.rrm.funnel(),
queryFn: () => adminRrmApi.getFunnel(),
staleTime: STALE_1MIN,
refetchInterval: 60_000,
});
}
export function useRrmProspects(filters: RrmProspectFilters = {}) {
return useQuery({
queryKey: queryKeys.admin.rrm.prospects(
filters as Record<string, string | number>,
),
queryFn: () => adminRrmApi.listProspects(filters),
staleTime: STALE_2MIN,
});
}
export function useRrmProspect(id: string | undefined) {
return useQuery({
queryKey: queryKeys.admin.rrm.prospect(id ?? ""),
queryFn: () => adminRrmApi.getProspect(id as string),
enabled: Boolean(id),
staleTime: STALE_30SEC,
});
}
/**
* The organic-only screen (3.2). Same freshness as `useRrmProspects` — this
* is the number the whole campaign exists to produce, but nothing about it
* decays fast enough to warrant polling.
*/
export function useRrmSignups(filters: { page?: number; limit?: number } = {}) {
return useQuery({
queryKey: queryKeys.admin.rrm.signups(
filters as Record<string, string | number>,
),
queryFn: () => adminRrmApi.listSignups(filters),
staleTime: STALE_2MIN,
});
}
/** One-click "Mark replied" from the Sign-ups row (3.4). */
export function useRrmMarkReplied() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.markReplied(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/** Bulk "sent the link by hand" stage move from the Prospects list (1.4). */
export function useRrmBulkStage() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (body: { ids: string[]; stage: RrmStage; reason: string }) =>
adminRrmApi.bulkStage(body),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* Bulk CSV import from the Prospects header. Invalidates everything RRM: an
* import moves the totals on the dashboard and the funnel, not only the list
* the operator is looking at.
*/
export function useRrmImportProspects() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (file: File) => adminRrmApi.importProspects(file),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/** Hard delete for test data (3.6) — super_admin only, distinct from erase. */
export function useRrmDeleteProspect() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.deleteProspect(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* Polls every 30s. The inbox is not a convenience screen — requirement
* P1-0.10 makes backlog older than 24h an auto-pause condition, so a stale
* count here is a stalled campaign nobody noticed.
*/
export function useRrmInbox() {
return useQuery({
queryKey: queryKeys.admin.rrm.inbox(),
queryFn: () => adminRrmApi.listInbox(),
staleTime: STALE_30SEC,
refetchInterval: 30_000,
});
}
/**
* Polls every 30s. The gate is evaluated server-side, so a page left open
* must not offer a Release button against conditions that have since failed.
*/
export function useRrmRamp() {
return useQuery({
queryKey: queryKeys.admin.rrm.ramp(),
queryFn: () => adminRrmApi.getRamp(),
staleTime: STALE_30SEC,
refetchInterval: 30_000,
});
}
/**
* Template approval — the ladder's hard precondition. The scheduler enrols
* nobody while any of these is unapproved, so this is the first thing to read
* when enrolment sits at zero.
*
* Deliberately NOT polled, unlike the ramp beside it: this endpoint reads our
* own stored copy, and only a sync changes it. The key is derived from
* `rrm.all` so every existing RRM invalidation already covers it.
*/
export function useRrmTemplates() {
return useQuery({
queryKey: [...queryKeys.admin.rrm.all, "templates"] as const,
queryFn: () => adminRrmApi.getTemplates(),
staleTime: STALE_2MIN,
});
}
/** Asks Meta for the current verdict and stores it. Approval itself still
* happens in WhatsApp Manager — this only reflects it. */
export function useSyncRrmTemplates() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: () => adminRrmApi.syncTemplates(),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useRrmTasks(
filters: { owner?: string; state?: RrmTaskState; type?: RrmTaskType } = {},
) {
return useQuery({
queryKey: queryKeys.admin.rrm.tasks(filters as Record<string, string>),
queryFn: () => adminRrmApi.listTasks(filters),
staleTime: STALE_1MIN,
});
}
export function useRrmCampaignConfig() {
return useQuery({
queryKey: queryKeys.admin.rrm.config(),
queryFn: () => adminRrmApi.getCampaignConfig(),
staleTime: STALE_2MIN,
});
}
// ── Partner programme (SRS-PP-001) ──────────────────────────────────────
/**
* The verification queue, and the referrals table.
*
* 30 seconds, not the 2 minutes the prospects list uses. This is a WORK QUEUE:
* two operators looking at the same screen must not both pick up the same
* referral, and a stale list is how that happens.
*/
export function useAdminReferrals(
filters: {
status?: string;
partnerId?: string;
search?: string;
limit?: number;
offset?: number;
} = {},
) {
return useQuery({
queryKey: queryKeys.admin.rrm.referrals(
filters as Record<string, string | number>,
),
queryFn: () => adminRrmApi.listReferrals(filters),
staleTime: STALE_30SEC,
});
}
export function useAdminReferral(id: string | undefined) {
return useQuery({
queryKey: queryKeys.admin.rrm.referral(id ?? ""),
queryFn: () => adminRrmApi.getReferral(id as string),
enabled: Boolean(id),
staleTime: STALE_30SEC,
});
}
/**
* Verify — the money moment.
*
* Invalidates the referral, the queue AND the partner: the credit changes what
* that partner is owed, so a stale partner screen would show a balance that
* disagrees with the ledger. FR-P-2.1 calls a discrepancy there a P0.
*/
export function useVerifyReferral() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, note }: { id: string; note?: string }) =>
adminRrmApi.verifyReferral(id, note),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* The conversion payment. Same invalidation as verify — crediting ₹1,000 moves
* the referral, the partner's ledger AND the funnel.
*/
export function useRecordOutcome() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
id,
kind,
notes,
}: {
id: string;
kind: "validated" | "converted";
notes?: string;
}) => adminRrmApi.recordOutcome(id, kind, notes),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useRejectReferral() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, reason }: { id: string; reason: string }) =>
adminRrmApi.rejectReferral(id, reason),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useMarkReferralDuplicate() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.markReferralDuplicate(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* Handoff, quote, lost.
*
* Invalidates the whole RRM tree like its siblings, and it has to: the handoff
* writes an inquiry id onto the referral and moves it out of `verified`, so a
* cached detail would keep offering "Hand to a designer" for a referral a
* designer already has.
*/
export function useAdvanceReferral() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, ...body }: { id: string } & AdvanceReferralBody) =>
adminRrmApi.advanceReferral(id, body),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useAdminPartners(
filters: {
search?: string;
suspended?: "true" | "false";
limit?: number;
offset?: number;
} = {},
) {
return useQuery({
queryKey: queryKeys.admin.rrm.partners(
filters as Record<string, string | number>,
),
queryFn: () => adminRrmApi.listPartners(filters),
staleTime: STALE_2MIN,
});
}
export function useAdminPartner(id: string | undefined) {
return useQuery({
queryKey: queryKeys.admin.rrm.partner(id ?? ""),
queryFn: () => adminRrmApi.getPartner(id as string),
enabled: Boolean(id),
staleTime: STALE_30SEC,
});
}
export function useSuspendPartner() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, reason }: { id: string; reason: string }) =>
adminRrmApi.suspendPartner(id, reason),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useUnsuspendPartner() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.unsuspendPartner(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* A STOP an operator read on their own phone. One column, one direction —
* the partner replies START to turn updates back on, and nothing here does.
*/
export function useMutePartner() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.mutePartner(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* DPDP erasure (super-admin only). Invalidates the whole RRM tree like its
* siblings — the erased partner's name changes on the table, their prospect
* moves to `do_not_contact`, and any referral that named them re-renders.
*/
export function useErasePartner() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => adminRrmApi.erasePartner(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useSetPartnerCaps() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
id,
caps,
}: {
id: string;
caps: { capPerDay?: number | null; capPerMonth?: number | null };
}) => adminRrmApi.setPartnerCaps(id, caps),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/** FR-M-7, the WhatsApp fallback: the operator records a PAN on the row. */
export function useSetPartnerPan() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, pan }: { id: string; pan: string }) =>
adminRrmApi.setPartnerPan(id, pan),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
// ─── Money (FR-M) ────────────────────────────────────────────────────────────
/**
* The settle queue. `pending` by default — this screen is a work list, and an
* operator opening it wants what is owed, not an archive of everything ever
* paid.
*/
export function useRrmPayouts(state?: RrmPayoutState) {
const filters: Record<string, string> = state ? { state } : {};
return useQuery({
queryKey: queryKeys.admin.rrm.payouts(filters),
queryFn: () => adminRrmApi.listPayouts(filters),
// Money an operator is about to move. A two-minute cache means two
// operators can both be looking at a payout one of them already settled.
staleTime: 0,
});
}
export function useRrmSettlePayout() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, reference }: { id: string; reference: string }) =>
adminRrmApi.settlePayout(id, reference),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
export function useRrmFailPayout() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
id,
failureReason,
}: {
id: string;
failureReason: string;
}) => adminRrmApi.failPayout(id, failureReason),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/**
* Withdraw a credit that has not been paid yet (the 5-day hold's whole point).
*
* The API answers 409 when the row is `paid` or locked to a pending payout,
* and that message is written for an operator to read — show it as written.
*/
export function useReverseEarning() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
id,
reason,
notes,
}: {
id: string;
reason: RrmReversalReason;
notes?: string;
}) => adminRrmApi.reverseEarning(id, reason, notes),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/** FR-M-6: the dispute that arrives after the money was already sent. */
export function useClawbackEarning() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({
id,
reason,
notes,
}: {
id: string;
reason: RrmReversalReason;
notes?: string;
}) => adminRrmApi.clawbackEarning(id, reason, notes),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.admin.rrm.all });
},
});
}
/** FR-I-3's funnel diagnostic. Scoped to one partner when `partnerId` is set. */
export function useRrmReferralFunnel(
filters: { from?: string; to?: string; partnerId?: string } = {},
) {
return useQuery({
queryKey: queryKeys.admin.rrm.referralFunnel(
filters as Record<string, string>,
),
queryFn: () => adminRrmApi.referralFunnel(filters),
staleTime: STALE_2MIN,
});
}
|