Skip to content

Commit

Permalink
fix: business report swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Nov 7, 2024
1 parent a46a15c commit 24188fd
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 194 deletions.
10 changes: 5 additions & 5 deletions apps/backoffice-v2/src/domains/business-reports/fetchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const fetchLatestBusinessReport = async ({
reportType: MerchantReportType;
}) => {
const [data, error] = await apiClient({
endpoint: `business-reports/latest?businessId=${businessId}&type=${reportType}`,
endpoint: `../external/business-reports/latest?businessId=${businessId}&type=${reportType}`,
method: Method.GET,
schema: BusinessReportSchema,
});
Expand Down Expand Up @@ -98,7 +98,7 @@ export const fetchBusinessReports = async ({
);

const [data, error] = await apiClient({
endpoint: `business-reports/?${queryParams}`,
endpoint: `../external/business-reports/?${queryParams}`,
method: Method.GET,
schema: BusinessReportsSchema,
});
Expand All @@ -108,7 +108,7 @@ export const fetchBusinessReports = async ({

export const fetchBusinessReportById = async ({ id }: { id: string }) => {
const [businessReport, error] = await apiClient({
endpoint: `business-reports/${id}`,
endpoint: `../external/business-reports/${id}`,
method: Method.GET,
schema: BusinessReportSchema,
});
Expand Down Expand Up @@ -148,7 +148,7 @@ export const createBusinessReport = async ({
}

const [businessReport, error] = await apiClient({
endpoint: `business-reports`,
endpoint: `../external/business-reports`,
method: Method.POST,
schema: z.undefined(),
body: {
Expand Down Expand Up @@ -187,7 +187,7 @@ export const createBusinessReportBatch = async ({
formData.append('workflowVersion', workflowVersion);

const [batchId, error] = await apiClient({
endpoint: `business-reports/upload-batch`,
endpoint: `../external/business-reports/upload-batch`,
method: Method.POST,
schema: z.object({ batchId: z.string() }),
body: formData,
Expand Down
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
Original file line number Diff line number Diff line change
@@ -1,22 +1,214 @@
import * as common from '@nestjs/common';
import * as swagger from '@nestjs/swagger';
import { ApiBearerAuth } from '@nestjs/swagger';
import { ApiBearerAuth, ApiConsumes } from '@nestjs/swagger';
import * as errors from '@/errors';
import { PrismaService } from '@/prisma/prisma.service';
import { UseGuards } from '@nestjs/common';
import {
BadRequestException,
Body,
Param,
Query,
Res,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { AdminAuthGuard } from '@/common/guards/admin-auth.guard';
import { BusinessReportService } from '@/business-report/business-report.service';
import { AppLoggerService } from '@/common/app-logger/app-logger.service';
import { CustomerService } from '@/customer/customer.service';
import { BusinessService } from '@/business/business.service';
import { CurrentProject } from '@/common/decorators/current-project.decorator';
import type { TProjectId } from '@/types';
import { GetLatestBusinessReportDto } from '@/business-report/get-latest-business-report.dto';
import {
BusinessReportListRequestParamDto,
BusinessReportListResponseDto,
ListBusinessReportsSchema,
} from '@/business-report/business-report-list.dto';
import { ZodValidationPipe } from '@/common/pipes/zod.pipe';
import { CreateBusinessReportDto } from '@/business-report/dto/create-business-report.dto';
import { Business } from '@prisma/client';
import { BusinessReportDto } from '@/business-report/business-report.dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { getDiskStorage } from '@/storage/get-file-storage-manager';
import { fileFilter } from '@/storage/file-filter';
import { RemoveTempFileInterceptor } from '@/common/interceptors/remove-temp-file.interceptor';
import { CreateBusinessReportBatchBodyDto } from '@/business-report/dto/create-business-report-batch-body.dto';
import type { Response } from 'express';
import { PrismaService } from '@/prisma/prisma.service';

@ApiBearerAuth()
@swagger.ApiTags('Business Reports')
@common.Controller('external/business-reports')
@swagger.ApiExcludeController()
export class BusinessReportControllerExternal {
constructor(private readonly prisma: PrismaService) {}
constructor(
protected readonly businessReportService: BusinessReportService,
protected readonly logger: AppLoggerService,
protected readonly customerService: CustomerService,
protected readonly businessService: BusinessService,
private readonly prisma: PrismaService,
) {}

@common.Get('/latest')
@swagger.ApiOkResponse({ type: [String] })
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
@swagger.ApiExcludeEndpoint()
async getLatestBusinessReport(
@CurrentProject() currentProjectId: TProjectId,
@Query() { businessId, type }: GetLatestBusinessReportDto,
) {
const { id: customerId } = await this.customerService.getByProjectId(currentProjectId);

const latestReport = await this.businessReportService.findLatest({
businessId,
customerId,
reportType: type,
});

return latestReport ?? {};
}

@common.Get()
@swagger.ApiOkResponse({ type: BusinessReportListResponseDto })
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
@common.UsePipes(new ZodValidationPipe(ListBusinessReportsSchema, 'query'))
async listBusinessReports(
@CurrentProject() currentProjectId: TProjectId,
@Query() { businessId, page, search }: BusinessReportListRequestParamDto,
) {
const { id: customerId } = await this.customerService.getByProjectId(currentProjectId);

return await this.businessReportService.findMany({
withoutUnpublishedOngoingReports: true,
limit: page.size,
page: page.number,
customerId: customerId,
...(businessId ? { businessId } : {}),
...(search ? { searchQuery: search } : {}),
});
}

@common.Post()
@swagger.ApiOkResponse({})
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
async createBusinessReport(
@Body()
{
websiteUrl,
countryCode,
merchantName,
businessCorrelationId,
reportType,
workflowVersion,
}: CreateBusinessReportDto,
@CurrentProject() currentProjectId: TProjectId,
) {
const { id: customerId, config } = await this.customerService.getByProjectId(currentProjectId);

const { maxBusinessReports, withQualityControl } = config || {};
await this.businessReportService.checkBusinessReportsLimit(maxBusinessReports, customerId);

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

if (!businessCorrelationId) {
business = await this.businessService.create({
data: {
companyName: merchantNameWithDefault,
country: countryCode,
website: websiteUrl,
projectId: currentProjectId,
},
select: {
id: true,
correlationId: true,
},
});
}

if (businessCorrelationId) {
business =
(await this.businessService.getByCorrelationId(businessCorrelationId, [currentProjectId], {
select: {
id: true,
correlationId: true,
},
})) ?? undefined;
}

if (!business) {
throw new BadRequestException(
`Business with an id of ${businessCorrelationId} was not found`,
);
}

await this.businessReportService.createBusinessReportAndTriggerReportCreation({
reportType,
business,
websiteUrl,
countryCode,
merchantName: merchantNameWithDefault,
workflowVersion,
withQualityControl,
customerId,
});
}

@common.Get(':id')
@swagger.ApiOkResponse({ type: BusinessReportDto })
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
@common.UsePipes(new ZodValidationPipe(ListBusinessReportsSchema, 'query'))
async getBusinessReportById(
@CurrentProject() currentProjectId: TProjectId,
@Param('id') id: string,
) {
const { id: customerId } = await this.customerService.getByProjectId(currentProjectId);

return await this.businessReportService.findById({ id, customerId });
}

@swagger.ApiExcludeEndpoint()
@common.Post('/upload-batch')
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
@ApiConsumes('multipart/form-data')
@UseInterceptors(
FileInterceptor('file', {
storage: getDiskStorage(),
fileFilter,
}),
RemoveTempFileInterceptor,
)
async createBusinessReportBatch(
@UploadedFile() file: Express.Multer.File,
@Body() { type, workflowVersion }: CreateBusinessReportBatchBodyDto,
@Res() res: Response,
@CurrentProject() currentProjectId: TProjectId,
) {
const { id: customerId, config } = await this.customerService.getByProjectId(currentProjectId);

const { maxBusinessReports, withQualityControl } = config || {};
await this.businessReportService.checkBusinessReportsLimit(maxBusinessReports, customerId);

const result = await this.businessReportService.processBatchFile({
type,
workflowVersion,
customerId,
maxBusinessReports,
merchantSheet: file,
projectId: currentProjectId,
withQualityControl: typeof withQualityControl === 'boolean' ? withQualityControl : false,
});

res.status(201);
res.setHeader('content-type', 'application/json');
res.send(result);
}

@common.Get()
@UseGuards(AdminAuthGuard)
@swagger.ApiOkResponse({ type: [String] })
@swagger.ApiForbiddenResponse({ type: errors.ForbiddenException })
@swagger.ApiExcludeEndpoint()
async list() {
return await this.prisma.businessReport.findMany({
include: {
Expand Down
Loading

0 comments on commit 24188fd

Please sign in to comment.