Skip to content

Commit

Permalink
feat: filter record
Browse files Browse the repository at this point in the history
  • Loading branch information
KieSun committed Feb 20, 2024
1 parent 3e6ab2d commit 364669c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/controller/contribution.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ContributionListQuery,
CreateContributionBody,
DeleteContributionBody,
GetAllocationDetailsQuery,
PrepareClaimBody,
UpdateContributionStateBody,
} from '@core/type/doc/contribution';
Expand Down Expand Up @@ -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);
}
}
39 changes: 39 additions & 0 deletions src/core/service/contribution.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ContributionListQuery,
CreateContributionBody,
DeleteContributionBody,
GetAllocationDetailsQuery,
PrepareClaimBody,
UpdateContributionStateBody,
} from '@core/type/doc/contribution';
Expand Down Expand Up @@ -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<string, number> = {};
contributions.forEach((item) => {
const contributorId = item.toIds[0];
if (!data[contributorId]) {
data[contributorId] = 0;
}
data[contributorId] += item.credit;
});
return data;
}
}
19 changes: 19 additions & 0 deletions src/core/type/doc/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 364669c

Please sign in to comment.