-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
181 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { ArtService } from '../art/art.service'; | ||
import { Art } from '../art/entities/art.entity'; | ||
import { ArtImage } from '../artImage/entities/artImage.entity'; | ||
import { User } from '../user/entities/user.entity'; | ||
import { UserService } from '../user/user.service'; | ||
import { Payment } from './entities/payment.entity'; | ||
import { PaymentResolver } from './payment.resolver'; | ||
import { PaymentServie } from './payment.service'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature()], | ||
imports: [ | ||
TypeOrmModule.forFeature([ | ||
Art, // | ||
ArtImage, | ||
User, | ||
Payment, | ||
]), | ||
], | ||
providers: [ | ||
PaymentResolver, // | ||
PaymentServie, | ||
ArtService, | ||
UserService, | ||
], | ||
}) | ||
export class PaymentModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,75 @@ | ||
import { UseGuards } from '@nestjs/common'; | ||
import { Args, Mutation, Resolver } from '@nestjs/graphql'; | ||
import { CACHE_MANAGER, Inject, UseGuards } from '@nestjs/common'; | ||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; | ||
import { Cache } from 'cache-manager'; | ||
import { GqlAuthAccessGuard } from 'src/common/auth/gql-auth.guard'; | ||
import { CurrentUser, ICurrentUser } from 'src/common/auth/gql-user.param'; | ||
import { UserService } from '../user/user.service'; | ||
import { Payment } from './entities/payment.entity'; | ||
import { PaymentServie } from './payment.service'; | ||
|
||
@Resolver() | ||
export class PaymentResolver { | ||
constructor(private readonly paymentService: PaymentServie) {} | ||
constructor( | ||
private readonly paymentService: PaymentServie, | ||
private readonly userService: UserService, | ||
|
||
@Inject(CACHE_MANAGER) | ||
private readonly cacheManager: Cache, | ||
) {} | ||
|
||
@UseGuards(GqlAuthAccessGuard) | ||
@Query(() => Payment) | ||
async fetchPurchaseList(@CurrentUser() currentUser: ICurrentUser) { | ||
return await this.paymentService.find(currentUser.id); | ||
} | ||
|
||
@Mutation(() => String) | ||
async checkTimedoutAndProcess() { | ||
try { | ||
const arts = await this.paymentService.checkTimeout(); | ||
console.log(arts, '작품들'); | ||
arts.map(async (e) => { | ||
const bidInfo = await this.cacheManager.get(e.id); | ||
const price = bidInfo[0]; | ||
const bidder = await this.userService.findOne(bidInfo[1]); | ||
if (price) { | ||
await this.paymentService.successfulBid(e.id, price, bidder, e.user); | ||
} else { | ||
await this.paymentService.failedBid(e.id); | ||
} | ||
|
||
return e.id; | ||
}); | ||
|
||
return ''; | ||
} catch (error) { | ||
throw error + 'checkout'; | ||
} | ||
} | ||
|
||
@UseGuards(GqlAuthAccessGuard) | ||
@Mutation(() => String) | ||
async instantBid( | ||
@Args('artId') artId: string, | ||
@Args('price') price: number, | ||
@Args('artistEmail') artistEmail: string, | ||
@CurrentUser() currentUser: ICurrentUser, | ||
) { | ||
await this.cacheManager.del(artId); | ||
const artist = await this.userService.findOne(artistEmail); | ||
const bidder = await this.userService.findOne(currentUser.email); | ||
await this.paymentService.successfulBid(artId, price, bidder, artist); | ||
|
||
return 'artId'; | ||
} | ||
|
||
@UseGuards(GqlAuthAccessGuard) | ||
@Mutation(() => Payment) | ||
async createPayment( | ||
@Mutation(() => [String]) | ||
async Bid( | ||
@Args('artId') artId: string, | ||
@Args('amount') amount: number, | ||
@Args('bid_price') bid_price: number, | ||
@CurrentUser() currentUser: ICurrentUser, | ||
) { | ||
return await this.paymentService.create( | ||
{ | ||
amount, | ||
artId, | ||
}, | ||
currentUser, | ||
); | ||
return await this.paymentService.call(artId, bid_price, currentUser.email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,115 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Connection } from 'typeorm'; | ||
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Cache } from 'cache-manager'; | ||
import { Connection, LessThan, Repository } from 'typeorm'; | ||
import { Art } from '../art/entities/art.entity'; | ||
import { History } from '../history/entities/history.entity'; | ||
import { User } from '../user/entities/user.entity'; | ||
import { Payment } from './entities/payment.entity'; | ||
|
||
@Injectable() | ||
export class PaymentServie { | ||
constructor(private readonly connection: Connection) {} | ||
async create({ amount, artId }, currentUser) { | ||
constructor( | ||
@InjectRepository(Art) | ||
private readonly artRepository: Repository<Art>, | ||
|
||
@InjectRepository(Payment) | ||
private readonly paymentRepository: Repository<Payment>, | ||
|
||
@Inject(CACHE_MANAGER) | ||
private readonly cacheManager: Cache, | ||
|
||
private readonly connection: Connection, | ||
) {} | ||
|
||
// 마감된 작품 체크 | ||
async checkTimeout() { | ||
const time = new Date(); | ||
const yyyy = time.getFullYear(); | ||
const mm = time.getMonth() + 1; | ||
const dd = time.getDate(); | ||
const currentTime = `${yyyy}-${mm}-${dd}`; | ||
|
||
console.log(currentTime, ' 현 재 시 간 '); | ||
return await this.artRepository.find({ | ||
where: { | ||
deadline: LessThan(currentTime), | ||
}, | ||
}); | ||
} | ||
|
||
// 낙찰 | ||
async successfulBid(artId, price, bidder, artist) { | ||
const queryRunner = this.connection.createQueryRunner(); | ||
await queryRunner.connect(); | ||
await queryRunner.startTransaction('SERIALIZABLE'); | ||
await queryRunner.startTransaction(); | ||
try { | ||
// 현재 결제 요청 유저 정보 조회 | ||
const user = await queryRunner.manager.findOne( | ||
User, | ||
{ id: currentUser.id }, | ||
{ lock: { mode: 'pessimistic_write' } }, | ||
); | ||
|
||
const art = await queryRunner.manager.findOne(Art, { id: artId }); | ||
|
||
// 작품 정보 수정 및 softDelete | ||
const soldoutArt = await queryRunner.manager.save(Art, { | ||
...art, | ||
price: price, | ||
is_soldout: true, | ||
}); | ||
await queryRunner.manager.softDelete(Art, { id: artId }); | ||
|
||
// 낙찰 테이블 저장 | ||
const payment = await queryRunner.manager.save(Payment, { | ||
amount: amount, | ||
user: user, | ||
art: art, | ||
amount: price, | ||
user: bidder, | ||
art: soldoutArt, | ||
}); | ||
|
||
// 히스토리 테이블 저장 | ||
// 히스토리 테이블(낙찰자) 저장 | ||
await queryRunner.manager.save(History, { | ||
point: amount, | ||
user: user, | ||
point: price, | ||
user: bidder, | ||
payment: payment, | ||
}); | ||
|
||
// 유저 누적 포인트 업데이트 | ||
await queryRunner.manager.save(User, { | ||
...user, | ||
point: user.point - amount, | ||
...bidder, | ||
point: -price, | ||
}); | ||
|
||
// 작가 누적 포인트 업데이트 | ||
await queryRunner.manager.save(User, { | ||
...artist, | ||
point: +price, | ||
}); | ||
|
||
await queryRunner.commitTransaction(); | ||
return payment; | ||
} catch (error) { | ||
await queryRunner.rollbackTransaction(); | ||
throw error + 'create'; | ||
throw error + 'successfulBid'; | ||
} finally { | ||
await queryRunner.release(); | ||
} | ||
} | ||
|
||
// 유찰 | ||
async failedBid(artId) { | ||
await this.artRepository.softDelete({ id: artId }); | ||
} | ||
|
||
// 입찰 | ||
async call(artId, bid_price, email) { | ||
const art = await this.artRepository.findOne(artId); | ||
const time = Number(art.deadline) - Number(new Date()); | ||
console.log(time); | ||
if (time > 0) { | ||
await this.cacheManager.set(artId, [bid_price, email], { | ||
ttl: time + 60000, | ||
}); | ||
} | ||
return [bid_price, email]; | ||
} | ||
|
||
// 구매한 작품 목록 조회 | ||
async find(userId) { | ||
return await this.paymentRepository.find({ user: userId }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.