Skip to content

Commit

Permalink
refactor(*): it is no longer required to pass a company name to creat…
Browse files Browse the repository at this point in the history
…e a merchant report (#2492)
  • Loading branch information
Omri-Levy committed Jul 1, 2024
1 parent ddc465d commit 21c7be8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,34 @@ import { countryCodes } from '@ballerine/common';
const URL_REGEX =
/((https?):\/\/)?([a-zA-Z0-9-_]+\.)+[a-zA-Z0-9]+(\.[a-z]{2})?(\/[a-zA-Z0-9_#-]+)*(\/)?(\?[a-zA-Z0-9_-]+=[a-zA-Z0-9_-]+(&[a-zA-Z0-9_-]+=[a-zA-Z0-9_-]+)*)?(#[a-zA-Z0-9_-]+)?/;

export const CreateBusinessReportSchema = z
.object({
websiteUrl: z.string().regex(URL_REGEX, {
message: 'Invalid website URL',
}),
companyName: z.union([
z
.string({
required_error: 'Company name is required',
invalid_type_error: 'Company name must be a string',
})
.max(255),
z.undefined(),
]),
operatingCountry: z.union([
z.enum(
// @ts-expect-error - countryCodes is an array of strings but its always the same strings
countryCodes,
{
errorMap: () => {
return {
message: 'Invalid operating country',
};
},
export const CreateBusinessReportSchema = z.object({
websiteUrl: z.string().regex(URL_REGEX, {
message: 'Invalid website URL',
}),
companyName: z
.string({
invalid_type_error: 'Company name must be a string',
})
.max(255)
.optional(),
operatingCountry: z.union([
z.enum(
// @ts-expect-error - countryCodes is an array of strings but its always the same strings
countryCodes,
{
errorMap: () => {
return {
message: 'Invalid operating country',
};
},
),
z.undefined(),
]),
businessCorrelationId: z.union([
z
.string({
required_error: 'Business ID is required',
invalid_type_error: 'Business ID must be a string',
})
.max(255),
z.undefined(),
]),
})
.superRefine((v, ctx) => {
if (v.companyName || v.businessCorrelationId) {
return;
}

ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Company name or business ID must be provided',
path: ['companyName'],
});
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Company name or business ID must be provided',
path: ['businessCorrelationId'],
});
});
},
),
z.undefined(),
]),
businessCorrelationId: z
.string({
invalid_type_error: 'Business ID must be a string',
})
.max(255)
.optional(),
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ export class BusinessReportControllerInternal {
}: CreateBusinessReportDto,
@CurrentProject() currentProjectId: TProjectId,
) {
if (!merchantName && !businessCorrelationId) {
throw new BadRequestException('Merchant name or business id is required');
}

let business: Pick<Business, 'id' | 'correlationId'> | undefined;
const merchantNameWithDefault = merchantName || 'Not provided';

if (!businessCorrelationId && merchantName) {
if (!businessCorrelationId) {
business = await this.businessService.create({
data: {
companyName: merchantName,
companyName: merchantNameWithDefault,
country: countryCode,
website: websiteUrl,
projectId: currentProjectId,
Expand Down

0 comments on commit 21c7be8

Please sign in to comment.