Skip to content

Commit e56b926

Browse files
authored
refactor(business-report): simplify batch report creation logic (#2910)
- Update URL regex pattern for improved validation - Adjust maximum batch size from 10,000 to 1,000 for better manageability - Streamline report request transformation for clarity (your batch size restriction feels like a diet plan that only allows rabbit food)
1 parent 5cb19c7 commit e56b926

File tree

7 files changed

+47
-62
lines changed

7 files changed

+47
-62
lines changed

packages/common/src/consts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,4 @@ export const MatchReasonCode = {
169169
} as const;
170170

171171
export const URL_PATTERN =
172-
/^((https?):\/\/)?([\dA-Za-z][\w-]*\.)+[\dA-Za-z]+(\.[a-z]{2})?(\/[\w#.-]+)*(\/)?(\?[\w.-]+=[\w.-]+(&[\w.-]+=[\w.-]+)*)?(#[\w-]+)?$/;
172+
/^(https?:\/\/)?((([\da-z]([\da-z-]*[\da-z])*)\.)+[a-z]{2,}|((25[0-5]|2[0-4]\d|1\d{2}|\d{1,2})\.){3}(25[0-5]|2[0-4]\d|1\d{2}|\d{1,2})|localhost)(:\d{1,5})?(\/[\w!$%&'()*+,.:;=@~-]*)*(\?([\w!$%&'()*+,.:;=@~-]+=[\w!$%&'()*+,.:;=@~-]*(&[\w!$%&'()*+,.:;=@~-]+=[\w!$%&'()*+,.:;=@~-]*)*)?)?(#[\w!$%&'()*+,.:;=@~-]*)?$/i;

services/workflows-service/src/business-report/business-report.service.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { parseCsv } from '@/common/utils/parse-csv/parse-csv';
55
import { BusinessReportRequestSchema } from '@/common/schemas';
66
import { PrismaService } from '@/prisma/prisma.service';
77
import { BusinessService } from '@/business/business.service';
8-
import { TReportRequest } from '@/common/utils/unified-api-client/unified-api-client';
98
import { env } from '@/env';
109
import { randomUUID } from 'crypto';
1110
import { AppLoggerService } from '@/common/app-logger/app-logger.service';
@@ -122,8 +121,8 @@ export class BusinessReportService {
122121
);
123122
}
124123

125-
if (businessReportsRequests.length > 10000) {
126-
throw new UnprocessableEntityException('Batch size is too large, the maximum is 10000');
124+
if (businessReportsRequests.length > 1_000) {
125+
throw new UnprocessableEntityException('Batch size is too large, the maximum is 1,000');
127126
}
128127

129128
const batchId = randomUUID();
@@ -160,26 +159,18 @@ export class BusinessReportService {
160159

161160
const businessWithRequests = await Promise.all(businessCreatePromises);
162161

163-
const businessReportRequests = businessWithRequests.map(
164-
({ businessReportRequest, businessId }) => {
165-
return {
166-
withQualityControl,
167-
websiteUrl: businessReportRequest.websiteUrl,
168-
lineOfBusiness: businessReportRequest.lineOfBusiness,
169-
parentCompanyName: businessReportRequest.parentCompanyName,
170-
callbackUrl: `${env.APP_API_URL}/api/v1/internal/business-reports/hook?businessId=${businessId}`,
171-
merchantId: businessId,
172-
customerId,
173-
};
174-
},
175-
) satisfies TReportRequest;
176-
177162
await this.merchantMonitoringClient.createBatch({
178-
reportRequests: businessReportRequests,
179-
clientName: 'merchant',
180-
reportType: type,
181-
withQualityControl,
163+
customerId,
182164
workflowVersion,
165+
withQualityControl,
166+
reportType: type,
167+
reports: businessWithRequests.map(({ businessReportRequest, businessId }) => ({
168+
businessId,
169+
websiteUrl: businessReportRequest.websiteUrl,
170+
countryCode: businessReportRequest.countryCode,
171+
parentCompanyName: businessReportRequest.parentCompanyName,
172+
callbackUrl: `${env.APP_API_URL}/api/v1/internal/business-reports/hook?businessId=${businessId}`,
173+
})),
183174
});
184175
},
185176
{

services/workflows-service/src/business-report/dtos/create-business-report.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type MerchantReportType,
99
type MerchantReportVersion,
1010
} from '@/business-report/constants';
11+
import { Transform } from 'class-transformer';
1112

1213
export class CreateBusinessReportDto {
1314
@ApiProperty({
@@ -26,6 +27,7 @@ export class CreateBusinessReportDto {
2627
})
2728
@MinLength(1)
2829
@IsString()
30+
@Transform(({ value }) => (typeof value === 'string' ? value.toLowerCase() : value))
2931
websiteUrl!: string;
3032

3133
@ApiProperty({

services/workflows-service/src/business-report/merchant-monitoring-client.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ import { CountryCode } from '@/common/countries';
66
import {
77
MERCHANT_REPORT_STATUSES,
88
MERCHANT_REPORT_TYPES,
9-
MERCHANT_REPORT_TYPES_MAP,
109
MERCHANT_REPORT_VERSIONS,
1110
MerchantReportType,
1211
MerchantReportVersion,
1312
} from '@/business-report/constants';
14-
import { TReportRequest } from '@/common/utils/unified-api-client/unified-api-client';
1513
import * as errors from '@/errors';
1614

1715
const CreateReportResponseSchema = z.object({});
18-
const CreateReportBatchResponseSchema = z.array(
19-
z.object({
20-
reportId: z.string(),
21-
}),
22-
);
2316
const ReportSchema = z.object({
2417
id: z.string(),
2518
websiteId: z.string(),
@@ -121,30 +114,38 @@ export class MerchantMonitoringClient {
121114
}
122115

123116
public async createBatch({
124-
reportRequests,
125-
clientName,
126-
metadata,
117+
customerId,
118+
workflowVersion,
127119
withQualityControl,
128-
reportType = MERCHANT_REPORT_TYPES_MAP.MERCHANT_REPORT_T1,
129-
workflowVersion = '2',
120+
reportType,
121+
reports,
130122
}: {
131-
reportRequests: TReportRequest;
132-
clientName?: string;
133-
reportType?: MerchantReportType;
123+
customerId: string;
134124
workflowVersion?: MerchantReportVersion;
135-
metadata?: Record<string, unknown>;
136125
withQualityControl?: boolean;
126+
reportType: MerchantReportType;
127+
reports: Array<{
128+
businessId: string;
129+
websiteUrl: string;
130+
countryCode?: string;
131+
parentCompanyName?: string;
132+
callbackUrl?: string;
133+
}>;
137134
}) {
138-
const response = await this.axios.post('merchants/analysis/batch', {
139-
reportRequests,
140-
clientName,
141-
metadata,
142-
reportType,
143-
withQualityControl,
144-
workflowVersion,
145-
});
146-
147-
return CreateReportBatchResponseSchema.parse(response.data);
135+
await this.axios.post(
136+
'merchants/analysis/batch/next',
137+
reports.map(report => ({
138+
customerId,
139+
merchantId: report.businessId,
140+
websiteUrl: report.websiteUrl,
141+
countryCode: report.countryCode,
142+
parentCompanyName: report.parentCompanyName,
143+
callbackUrl: report.callbackUrl,
144+
reportType,
145+
workflowVersion,
146+
withQualityControl,
147+
})),
148+
);
148149
}
149150

150151
public async findById({ id, customerId }: { id: string; customerId: string }) {

services/workflows-service/src/common/schemas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export const InputJsonValueSchema = z.union([
1818
export const JsonValueSchema = z.union([InputJsonValueSchema, z.null()]);
1919

2020
export const BusinessReportRequestSchema = z.object({
21-
websiteUrl: z.string().regex(URL_PATTERN, { message: 'Invalid URL' }),
21+
websiteUrl: z
22+
.string()
23+
.regex(URL_PATTERN, { message: 'Invalid URL' })
24+
.transform(value => value.toLowerCase()),
2225
countryCode: z.string().length(2).optional(),
2326
lineOfBusiness: z.string().optional(),
2427
parentCompanyName: z.string().optional(),

services/workflows-service/src/common/utils/unified-api-client/unified-api-client.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,6 @@ import { Logger } from '@nestjs/common';
44
import { Customer } from '@prisma/client';
55
import { TSchema } from '@sinclair/typebox';
66

7-
export type TReportRequest = Array<{
8-
websiteUrl: string;
9-
callbackUrl?: string;
10-
countryCode?: string;
11-
parentCompanyName?: string;
12-
lineOfBusiness?: string;
13-
merchantName?: string;
14-
businessReportId?: string;
15-
withQualityControl?: boolean;
16-
customerId: string;
17-
merchantId: string;
18-
}>;
197
export type TOcrImages = Array<
208
| {
219
remote: {

0 commit comments

Comments
 (0)