Skip to content

Commit

Permalink
feat: add a report note when monitoring status is toggled (BAL-3398) (#…
Browse files Browse the repository at this point in the history
…2979)

* feat: add a report note when monitoring status is toggled

* chore: remove storing reason in metadata
  • Loading branch information
r4zendev authored Jan 21, 2025
1 parent 5ce73dc commit d020900
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { TNoteableType } from '@/domains/notes/types';

export const useCreateNoteMutation = ({
onSuccess,
disableToast = false,
}: {
onSuccess?: <TData>(data: TData) => void;
disableToast: boolean;
}) => {
const queryClient = useQueryClient();

Expand Down Expand Up @@ -41,7 +43,9 @@ export const useCreateNoteMutation = ({
onSuccess: data => {
void queryClient.invalidateQueries();

toast.success(t(`toast:note_created.success`));
if (!disableToast) {
toast.success(t(`toast:note_created.success`));
}

onSuccess?.(data);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ export const MerchantMonitoringBusinessReport: FunctionComponent = () => {
</FormControl>
<FormMessage />
<SelectContent>
{deboardingReasonOptions?.map(({ label, value }, index) => {
{deboardingReasonOptions?.map((option, index) => {
return (
<SelectItem key={index} value={value}>
{label}
<SelectItem key={index} value={option}>
{option}
</SelectItem>
);
})}
Expand Down Expand Up @@ -223,15 +223,12 @@ export const MerchantMonitoringBusinessReport: FunctionComponent = () => {
throw new Error('Merchant ID is missing');
}

turnOngoingMonitoringOn(
{ merchantId: businessReport.merchantId },
{
onSuccess: () => {
setIsDeboardModalOpen(false);
setIsDropdownOpen(false);
},
turnOngoingMonitoringOn(businessReport.merchantId, {
onSuccess: () => {
setIsDeboardModalOpen(false);
setIsDropdownOpen(false);
},
);
});
}}
variant={'ghost'}
className="justify-start"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { handleZodError } from '@/common/utils/handle-zod-error/handle-zod-error
export type TurnOngoingMonitoringBody = z.infer<typeof TurnOngoingMonitoringBodySchema>;
export const TurnOngoingMonitoringBodySchema = z.object({
state: z.string(),
reason: z.string().optional(),
userReason: z.string().optional(),
});

export type TurnOngoingMonitoringResponse = z.infer<typeof TurnOngoingMonitoringResponseSchema>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParsedBooleanSchema, useReportTabs } from '@ballerine/ui';
import { t } from 'i18next';
import { capitalize } from 'lodash-es';
import { useCallback, useMemo } from 'react';
import { SubmitHandler, useForm } from 'react-hook-form';
import { useNavigate, useParams } from 'react-router-dom';
Expand All @@ -15,6 +16,7 @@ import {
MERCHANT_REPORT_TYPES_MAP,
} from '@/domains/business-reports/constants';
import { useBusinessReportByIdQuery } from '@/domains/business-reports/hooks/queries/useBusinessReportByIdQuery/useBusinessReportByIdQuery';
import { useCreateNoteMutation } from '@/domains/notes/hooks/mutations/useCreateNoteMutation/useCreateNoteMutation';
import { useNotesByNoteable } from '@/domains/notes/hooks/queries/useNotesByNoteable/useNotesByNoteable';
import { useToggleMonitoringMutation } from '@/pages/MerchantMonitoringBusinessReport/hooks/useToggleMonitoringMutation/useToggleMonitoringMutation';
import { isObject } from '@ballerine/common';
Expand Down Expand Up @@ -56,11 +58,11 @@ const statusToBadgeData = {
} as const;

const deboardingReasonOptions = [
{ value: 'fraud', label: 'Fraudulent Activity Detected' },
{ value: 'regulations', label: 'Non-Compliance with Regulations' },
{ value: 'disputes', label: 'Excessive Chargebacks or Disputes' },
{ value: 'expired', label: 'Business Relationship Ended' },
{ value: 'other', label: 'Other' },
'Fraudulent Activity Detected',
'Non-Compliance with Regulations',
'Excessive Chargebacks or Disputes',
'Business Relationship Ended',
'Other',
] as const;

export const useMerchantMonitoringBusinessReportLogic = () => {
Expand Down Expand Up @@ -92,12 +94,21 @@ export const useMerchantMonitoringBusinessReportLogic = () => {
throw new Error('Merchant ID is missing');
}

return turnOffMonitoringMutation.mutate({ merchantId: businessReport.merchantId, body: data });
return turnOffMonitoringMutation.mutate(businessReport.merchantId);
};

const { mutateAsync: mutateCreateNote } = useCreateNoteMutation({ disableToast: true });
const turnOnMonitoringMutation = useToggleMonitoringMutation({
state: 'on',
onSuccess: () => {
void mutateCreateNote({
content: 'Monitoring turned on',
entityId: businessReport?.merchantId ?? '',
entityType: 'Business',
noteableId: businessReport?.id ?? '',
noteableType: 'Report',
parentNoteId: null,
});
toast.success(t(`toast:business_monitoring_on.success`));
},
onError: error => {
Expand All @@ -112,6 +123,22 @@ export const useMerchantMonitoringBusinessReportLogic = () => {
const turnOffMonitoringMutation = useToggleMonitoringMutation({
state: 'off',
onSuccess: () => {
const { reason, userReason } = form.getValues();
const content = [
'Monitoring turned off',
reason ? `with reason: ${capitalize(reason)}` : null,
userReason ? `(${userReason})` : '',
]
.filter(Boolean)
.join(' ');
void mutateCreateNote({
content,
entityId: businessReport?.merchantId ?? '',
entityType: 'Business',
noteableId: businessReport?.id ?? '',
noteableType: 'Report',
parentNoteId: null,
});
setIsDeboardModalOpen(false);
setIsDropdownOpen(false);
form.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';

import { HttpError } from '@/common/errors/http-error';
import {
turnOngoingMonitoring,
TurnOngoingMonitoringBody,
} from '@/pages/MerchantMonitoringBusinessReport/fetchers';
import { turnOngoingMonitoring } from '@/pages/MerchantMonitoringBusinessReport/fetchers';

export const useToggleMonitoringMutation = ({
state,
Expand All @@ -19,10 +16,8 @@ export const useToggleMonitoringMutation = ({
const queryClient = useQueryClient();

return useMutation({
mutationFn: async (data: {
merchantId: string;
body?: Omit<TurnOngoingMonitoringBody, 'state'>;
}) => turnOngoingMonitoring({ merchantId: data.merchantId, body: { ...data.body, state } }),
mutationFn: async (merchantId: string) =>
turnOngoingMonitoring({ merchantId, body: { state } }),
onSuccess: data => {
void queryClient.invalidateQueries();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ export class BusinessControllerExternal {
featureConfig: {
[FEATURE_LIST.ONGOING_MERCHANT_REPORT]: {
enabled: isEnabled,
reason: data.reason ?? null,
userReason: data.userReason ?? null,
disabledAt: isEnabled ? null : new Date().getTime(),
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsIn, IsOptional, IsString } from 'class-validator';
import { IsIn, IsString } from 'class-validator';

export class BusinessMonitoringPatchDto {
@ApiProperty({ type: String, required: true })
@IsString()
@IsIn(['on', 'off'])
state!: 'on' | 'off';

@ApiProperty({ type: String, required: false })
@IsOptional()
@IsString()
reason?: string;

@ApiProperty({ type: String, required: false })
@IsOptional()
@IsString()
userReason?: string;
}

0 comments on commit d020900

Please sign in to comment.