diff --git a/src/controller/contribution.controller.ts b/src/controller/contribution.controller.ts index d7eec92..c4c387b 100644 --- a/src/controller/contribution.controller.ts +++ b/src/controller/contribution.controller.ts @@ -3,6 +3,7 @@ import { ContributionListQuery, CreateContributionBody, DeleteContributionBody, + GetAllocationDetailsQuery, PrepareClaimBody, UpdateContributionStateBody, } from '@core/type/doc/contribution'; @@ -64,4 +65,10 @@ export class ContributionController { await this.contributionService.deleteContribution(contributionId, body); return CoreApiResponse.success(); } + + @Get('allocationDetails') + async getAllocationDetails(@Query() query: GetAllocationDetailsQuery) { + const data = await this.contributionService.getAllocationDetails(query); + return CoreApiResponse.success(data); + } } diff --git a/src/core/service/contribution.service.ts b/src/core/service/contribution.service.ts index 657aac4..43a353c 100644 --- a/src/core/service/contribution.service.ts +++ b/src/core/service/contribution.service.ts @@ -3,6 +3,7 @@ import { ContributionListQuery, CreateContributionBody, DeleteContributionBody, + GetAllocationDetailsQuery, PrepareClaimBody, UpdateContributionStateBody, } from '@core/type/doc/contribution'; @@ -292,4 +293,42 @@ export class ContributionService { }, }); } + + async getAllocationDetails(query: GetAllocationDetailsQuery) { + const { endDate, startDate, projectId } = query; + const project = await this.prisma.project.findFirst({ + where: { + id: projectId, + deleted: false, + }, + }); + if (!project) { + throw new HttpException( + Code.NOT_FOUND_ERROR.message, + Code.NOT_FOUND_ERROR.code, + ); + } + const contributions = await this.prisma.contribution.findMany({ + where: { + projectId, + deleted: false, + status: Status.CLAIM, + startDate: { + gte: new Date(startDate), + }, + endDate: { + lte: new Date(endDate), + }, + }, + }); + const data: Record = {}; + contributions.forEach((item) => { + const contributorId = item.toIds[0]; + if (!data[contributorId]) { + data[contributorId] = 0; + } + data[contributorId] += item.credit; + }); + return data; + } } diff --git a/src/core/type/doc/contribution.ts b/src/core/type/doc/contribution.ts index e7554b9..44fa71e 100644 --- a/src/core/type/doc/contribution.ts +++ b/src/core/type/doc/contribution.ts @@ -102,3 +102,22 @@ export class PrepareClaimBody { } export class DeleteContributionBody extends AuthBody {} + +export class GetAllocationDetailsQuery { + @IsNotEmpty() + @IsString() + @ApiProperty({ type: 'string' }) + projectId: string; + + @IsNotEmpty() + @IsNumber() + @Type(() => Number) + @ApiProperty({ type: 'number' }) + startDate: number; + + @IsNotEmpty() + @IsNumber() + @Type(() => Number) + @ApiProperty({ type: 'number' }) + endDate: number; +}