Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/contents/contents.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ export class CategoryService {
}
currentParentId = parentCategory?.parentId;
}
} else {
/**
* TODO: 유료 플랜 사용자이면 카테고리 개수 제한 없도록 추가 구성해야함.
*/
// if parentId is null, it means that category is root category
// root categories can't be more than 10 in one user
const isOverCategoryLimit =
await this.categoryRepository.isOverCategoryLimit(user);
if (isOverCategoryLimit) {
throw new ConflictException(
"Root categories can't be more than 10 in one user",
);
}
}

// check if category exists in user's categories(check if category name is duplicated in same level too)
Expand Down
15 changes: 15 additions & 0 deletions src/contents/repository/category.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,19 @@ export class CategoryRepository extends Repository<Category> {

return category;
}

/**
* 대 카테고리는 유저 당 10개까지만 생성 가능
* 해당 유저의 대 카테고리 개수를 확인하고, 10개 이상이면 true 반환
* @param user.id
* @returns boolean
*/
async isOverCategoryLimit(user: User): Promise<boolean> {
const categoryCount = await this.createQueryBuilder('category')
.where('category.userId = :id', { id: user.id })
.andWhere('category.parentId IS NULL')
.getCount();

return categoryCount >= 10;
}
}
37 changes: 37 additions & 0 deletions src/users/entities/paid-plan.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Column, Entity, OneToMany } from 'typeorm';
import { IsNumber, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { CoreEntity } from '../../common/entities/core.entity';
import { User } from './user.entity';

@Entity()
export class PaidPlan extends CoreEntity {
@ApiProperty({ example: 'ultimate', description: 'plan name' })
@Column({ unique: true })
@IsString()
name!: string;

@ApiProperty({ example: 3000, description: 'plan price' })
@Column()
@IsNumber()
price!: number;

@ApiProperty({
example: 30,
description: 'The period (in days) of the paid plan.',
})
@Column()
@IsString()
duration_days!: number;

@ApiProperty({ example: 'ultimate plan', description: 'plan description' })
@Column()
@IsString()
description!: string;

@ApiProperty({ description: 'Users in use', type: [User] })
@OneToMany((type) => User, (user) => user.paidPlan, {
nullable: true,
})
users?: User[];
}
20 changes: 19 additions & 1 deletion src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { InternalServerErrorException } from '@nestjs/common';
import { BeforeInsert, BeforeUpdate, Column, Entity, OneToMany } from 'typeorm';
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
ManyToOne,
OneToMany,
} from 'typeorm';
import * as bcrypt from 'bcrypt';
import { IsBoolean, IsEmail, IsEnum, IsString, Matches } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { Content } from '../../contents/entities/content.entity';
import { Category } from '../../contents/entities/category.entity';
import { Collection } from '../../collections/entities/collection.entity';
import { CoreEntity } from '../../common/entities/core.entity';
import { PaidPlan } from './paid-plan.entity';

export enum UserRole {
Client = 'Client',
Expand Down Expand Up @@ -77,6 +85,16 @@ export class User extends CoreEntity {
})
collections?: Collection[];

@ApiProperty({
description: 'User Plan',
type: PaidPlan,
required: false,
})
@ManyToOne((type) => PaidPlan, (paidPlan) => paidPlan.users, {
nullable: true,
})
paidPlan?: PaidPlan;

@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
Expand Down