Skip to content

Commit

Permalink
Merge pull request #11 from lxdao-official/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
KieSun authored Feb 27, 2024
2 parents 53da302 + 3f37096 commit f924edf
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 13 deletions.
3 changes: 3 additions & 0 deletions prisma/migrations/20240220083932_2_20/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Contribution" ADD COLUMN "endDate" TIMESTAMP(3),
ADD COLUMN "startDate" TIMESTAMP(3);
15 changes: 15 additions & 0 deletions prisma/migrations/20240220093751_payment/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE "Payment" (
"id" TEXT NOT NULL,
"purpose" TEXT NOT NULL,
"counterparties" TEXT[],
"category" TEXT NOT NULL,
"chainId" TEXT NOT NULL,
"allocate" TEXT NOT NULL,
"amount" TEXT NOT NULL,
"createAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"deleted" BOOLEAN NOT NULL DEFAULT false,

CONSTRAINT "Payment_pkey" PRIMARY KEY ("id")
);
11 changes: 11 additions & 0 deletions prisma/migrations/20240220093844_payment1/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
Warnings:
- Added the required column `projectId` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" ADD COLUMN "projectId" TEXT NOT NULL;

-- AddForeignKey
ALTER TABLE "Payment" ADD CONSTRAINT "Payment_projectId_fkey" FOREIGN KEY ("projectId") REFERENCES "Project"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
8 changes: 8 additions & 0 deletions prisma/migrations/20240220095207_payment2/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `symbol` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" ADD COLUMN "symbol" TEXT NOT NULL;
8 changes: 8 additions & 0 deletions prisma/migrations/20240220100436_payment3/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `crateor` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" ADD COLUMN "crateor" TEXT NOT NULL;
10 changes: 10 additions & 0 deletions prisma/migrations/20240220100530_payment4/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `crateor` on the `Payment` table. All the data in the column will be lost.
- Added the required column `createor` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" DROP COLUMN "crateor",
ADD COLUMN "createor" TEXT NOT NULL;
10 changes: 10 additions & 0 deletions prisma/migrations/20240220100709_payment5/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `createor` on the `Payment` table. All the data in the column will be lost.
- Added the required column `creator` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" DROP COLUMN "createor",
ADD COLUMN "creator" TEXT NOT NULL;
8 changes: 8 additions & 0 deletions prisma/migrations/20240220101214_payment6/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `txHash` to the `Payment` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Payment" ADD COLUMN "txHash" TEXT NOT NULL;
21 changes: 21 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ model Project {
contributors Contributor[]
MintReocrd MintReocrd[]
ContributionType ContributionType[]
Payment Payment[]
}

model User {
Expand Down Expand Up @@ -73,6 +74,8 @@ model Contribution {
projectId String
type String[]
contributionDate String?
startDate DateTime?
endDate DateTime?
createAt DateTime @default(now())
updatedAt DateTime @updatedAt
deleted Boolean @default(false)
Expand Down Expand Up @@ -118,3 +121,21 @@ model MintReocrd {
updatedAt DateTime @updatedAt
deleted Boolean @default(false)
}

model Payment {
id String @id @default(uuid())
purpose String
counterparties String[]
category String
chainId String
allocate String
symbol String
amount String
project Project @relation(fields: [projectId], references: [id])
projectId String
creator String
txHash String
createAt DateTime @default(now())
updatedAt DateTime @updatedAt
deleted Boolean @default(false)
}
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import LogsMiddleware from '@/src/middleware/logs';
import { ContributionModule } from '@/src/module/contribution.module';
import { ContributorModule } from '@/src/module/contributor.module';
import { EasModule } from '@/src/module/eas.module';
import { PaymentModule } from '@/src/module/payment.module';
import { UserModule } from '@/src/module/user.module';
import { Logger, MiddlewareConsumer, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
Expand Down Expand Up @@ -31,6 +32,7 @@ import { ProjectModule } from './module/project.module';
UserModule,
ContributionModule,
EasModule,
PaymentModule,
],
providers: [
{
Expand Down
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);
}
}
24 changes: 24 additions & 0 deletions src/controller/payment.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { CoreApiResponse } from '@core/api/coreApiResponse';
import { CreatePaymentBody, PaymentListQuery } from '@core/type/doc/payment';
import { Body, Controller, Get, Inject, Post, Query } from '@nestjs/common';
import { PaymentService } from '@service/payment.service';

@Controller('payment')
export class PaymentController {
constructor(
@Inject(PaymentService)
private readonly paymentService: PaymentService,
) {}

@Get('list')
async getPaymentList(@Query() data: PaymentListQuery) {
const user = await this.paymentService.getPaymentList(data);
return CoreApiResponse.success(user);
}

@Post('create')
async createPayment(@Body() body: CreatePaymentBody) {
const data = await this.paymentService.createPayment(body);
return CoreApiResponse.success(data);
}
}
43 changes: 41 additions & 2 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 @@ -162,7 +163,8 @@ export class ContributionService {
credit,
operatorId,
type,
contributionDate,
startDate,
endDate,
} = body;
const project = await this.prisma.project.findFirst({
where: {
Expand Down Expand Up @@ -194,7 +196,8 @@ export class ContributionService {
credit,
projectId,
type,
contributionDate,
startDate: new Date(startDate),
endDate: new Date(endDate),
ownerId: operatorId,
id,
},
Expand Down Expand Up @@ -290,4 +293,40 @@ export class ContributionService {
},
});
}

async getAllocationDetails(query: GetAllocationDetailsQuery) {
const { endDateFrom, endDateTo, 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,
endDate: {
gte: new Date(endDateFrom),
lte: new Date(endDateTo),
},
},
});
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;
}
}
59 changes: 59 additions & 0 deletions src/core/service/payment.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Code } from '@core/code';
import { CreatePaymentBody, PaymentListQuery } from '@core/type/doc/payment';
import { paginate } from '@core/utils/paginator';
import { HttpException, Injectable } from '@nestjs/common';
import { PrismaService } from 'nestjs-prisma';

@Injectable()
export class PaymentService {
constructor(private prisma: PrismaService) {}

async getPaymentList(data: PaymentListQuery) {
const { pageSize, currentPage, projectId } = data;
return paginate(
this.prisma.payment,
{
where: {
deleted: false,
projectId,
},
},
{
pageSize,
currentPage,
},
);
}

async createPayment(body: CreatePaymentBody) {
const { projectId, wallet, ...data } = body;
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 user = await this.prisma.contributor.findFirst({
where: {
projectId,
wallet,
},
});
if (!user) {
throw new HttpException(Code.NO_AUTH.message, Code.NO_AUTH.code);
}
return this.prisma.payment.create({
data: {
...data,
creator: wallet,
projectId,
},
});
}
}
4 changes: 1 addition & 3 deletions src/core/service/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ export class ProjectService {
voteApprove,
voteSystem,
voteThreshold,
rule,
operatorId,
} = body;
await this.getProject(projectId, true);
Expand All @@ -178,7 +177,7 @@ export class ProjectService {
id: operatorId,
},
});
if (user?.permission === Permission.Contributor) {
if (user?.permission !== Permission.Admin) {
throw new HttpException(Code.NO_AUTH.message, Code.NO_AUTH.code);
}
this.checkVoteThreshold(voteThreshold);
Expand All @@ -194,7 +193,6 @@ export class ProjectService {
voteApprove,
voteSystem,
voteThreshold,
rule,
},
});
}
Expand Down
32 changes: 29 additions & 3 deletions src/core/type/doc/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@ export class CreateContributionBody extends AuthBody {
type: string[];

@IsNotEmpty()
@IsString()
@ApiProperty({ type: 'string' })
contributionDate: string;
@IsNumber()
@Type(() => Number)
@ApiProperty({ type: 'number' })
startDate: number;

@IsNotEmpty()
@IsNumber()
@Type(() => Number)
@ApiProperty({ type: 'number' })
endDate: number;
}

export class PrepareClaimBody {
Expand All @@ -95,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' })
endDateFrom: number;

@IsNotEmpty()
@IsNumber()
@Type(() => Number)
@ApiProperty({ type: 'number' })
endDateTo: number;
}
Loading

0 comments on commit f924edf

Please sign in to comment.