Skip to content

Commit

Permalink
[ST-663] 코인 관련 엔티티 정의 및 API 엔드포인트 추가 (#72)
Browse files Browse the repository at this point in the history
* ⭐ 유저 내부 코인 엔티티 정의

* 🔨 유저 coin 엔티티 반영

* ⭐ 무료 코인 수령 API

* 🔨 코인 수령 메소드 수정
  • Loading branch information
w8385 authored Oct 4, 2023
1 parent 81b41bb commit 2697243
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/user/entities/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ export interface UserKey {

export interface User extends UserKey {
bio?: string;
coin: {
amount: number;
lastReceivedFreeCoinAt: Date;
};
createdAt?: string;
followers: string[];
following: string[];
name: string;
participatingChattingRooms: string[];
profileImage?: string;
role: string;
school?: {
Expand All @@ -17,5 +22,4 @@ export interface User extends UserKey {
department?: string;
grade: number;
};
participatingChattingRooms: string[];
}
11 changes: 11 additions & 0 deletions src/user/entities/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ export const UserSchema = new Schema({
bio: {
type: String,
},
coin: {
type: Object,
schema: {
amount: {
type: Number,
},
lastReceivedFreeCoinAt: {
type: Date,
},
},
},
profileImage: {
type: String,
},
Expand Down
7 changes: 7 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,11 @@ export class UserController {
setFCMTokenUserDto.fcmToken,
);
}

@ApiTags('User')
@ApiBearerAuth('Authorization')
@Get('user/receiveFreeCoin')
receiveFreeCoin(@Headers() headers: Headers) {
return this.userService.receiveFreeCoin(AccessToken.userId(headers));
}
}
25 changes: 24 additions & 1 deletion src/user/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export class UserRepository {
async create(userId: string, createUserDto: CreateUserDto, role: string) {
const user: User = {
bio: createUserDto.bio,
createdAt: new Date().toISOString(),
coin: {
amount: 0,
lastReceivedFreeCoinAt: new Date(0),
},
followers: [],
following: [],
id: userId,
Expand Down Expand Up @@ -276,4 +279,24 @@ export class UserRepository {
});
});
}

async receiveFreeCoin(userId: string) {
const user: User = await this.get(userId);
if (user === undefined) {
throw new Error('사용자를 찾을 수 없습니다.');
}

const now = new Date();
if (now.valueOf() - user.coin.lastReceivedFreeCoinAt.valueOf() < 86400000) {
throw new Error('이미 무료 코인을 받았습니다.');
}

try {
user.coin.amount += 2;
user.coin.lastReceivedFreeCoinAt = now;
await this.userModel.update({ id: userId }, { coin: user.coin });
} catch (error) {
throw new Error('무료 코인을 받을 수 없습니다.');
}
}
}
9 changes: 9 additions & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,13 @@ export class UserService {
return new Fail(error.message);
}
}

async receiveFreeCoin(userId: string) {
try {
await this.userRepository.receiveFreeCoin(userId);
return new Success('성공적으로 무료 코인을 지급받았습니다.');
} catch (error) {
return new Fail(error.message);
}
}
}

0 comments on commit 2697243

Please sign in to comment.