Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[백한결] 판매금액 정산 배치 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions template/package/package.typeorm.json → package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.2",
"@nestjs/swagger": "^7.1.13",
"@nestjs/typeorm": "^10.0.0",
"class-transformer": "^0.5.1",
Expand Down
Empty file modified setup.sh
100644 → 100755
Empty file.
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions src/orders/order.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

export enum OrderStatus {
PaymentComplete = "결제완료",
Shipping = "배송중",
Delivered = "배송완료",
PurchaseConfirmed = "구매확정",
Cancelled = "취소"
}

@Entity()
export class Order {
@PrimaryGeneratedColumn()
id: number;

@Column()
sellerId: number;

@Column('decimal')
amount: number;

@Column({
type: 'enum',
enum: OrderStatus,
})
status: OrderStatus;

@Column({ nullable: true, type: 'timestamp' })
confirmedAt?: Date;
}
36 changes: 36 additions & 0 deletions src/settlements/settlements.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Order, OrderStatus } from "src/orders/order.entity";
import { Raw, Repository } from "typeorm";

@Injectable()
export class SettlementsService {
private readonly commissionRate = 0.05;

constructor(@InjectRepository(Order) private orderRepository: Repository<Order>) {}

async calculateSettlements(): Promise<{ [sellerId: number]: number }> {
const orders = await this.orderRepository.find({
where: [
{ status: OrderStatus.PurchaseConfirmed },
{
status: OrderStatus.Delivered,
confirmedAt: Raw(alias => `DATE_ADD(${alias}, INTERVAL 5 DAY) <= NOW()`)
}
]
});

const settlements = {};

orders.forEach(order => {
const netAmount = order.amount * (1 - this.commissionRate);
if (settlements[order.sellerId]) {
settlements[order.sellerId] += netAmount;
} else {
settlements[order.sellerId] = netAmount;
}
});

return settlements;
}
}
16 changes: 16 additions & 0 deletions src/settlements/settlements.task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from "@nestjs/common";
import { Cron, CronExpression } from '@nestjs/schedule';
import { SettlementsService } from "./settlements.service";

@Injectable()
export class SettlementsTask {
constructor(private settlementsService: SettlementsService) { }

@Cron('0 0 0 * * *', {
timeZone: 'Asia/Seoul'
})
async handleCron() {
const settlements = await this.settlementsService.calculateSettlements();
console.log(settlements);
}
}
6 changes: 0 additions & 6 deletions template/environment/prisma/.env.example

This file was deleted.

39 changes: 0 additions & 39 deletions template/environment/prisma/docker-compose.yml

This file was deleted.

11 changes: 0 additions & 11 deletions template/environment/typeorm/.env.example

This file was deleted.

87 changes: 0 additions & 87 deletions template/package/package.prisma.json

This file was deleted.

12 changes: 0 additions & 12 deletions template/source/prisma-src/app.module.ts

This file was deleted.

12 changes: 0 additions & 12 deletions template/source/prisma-src/main.ts

This file was deleted.

15 changes: 0 additions & 15 deletions template/source/prisma-src/prisma/prisma.service.ts

This file was deleted.

23 changes: 0 additions & 23 deletions template/source/prisma-src/prisma/schema.prisma

This file was deleted.

Loading