Skip to content

Commit

Permalink
Fix: 커밋 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Kimsoo0119 committed Jul 7, 2024
1 parent bdeabf3 commit c94307b
Show file tree
Hide file tree
Showing 10 changed files with 427 additions and 131 deletions.
72 changes: 72 additions & 0 deletions src/coupon/coupon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { BadRequestException } from '@nestjs/common';
import { LectureCoupon } from '@prisma/client';

export interface ICoupon {
validateUsageCount(): void;
applyDiscount(price: number): number;
readonly percentage: number;
readonly discountPrice: number;
}

export default class Coupon implements Coupon {
private _id: number;
private _lecturerId: number;
private _title: string;
private _percentage: number;
private _discountPrice: number;
private _maxDiscountPrice: number;
private _maxUsageCount: number;
private _usageCount: number;
private _isDisabled: boolean;
private _isStackable: boolean;
private _isPrivate: boolean;
private _startAt: Date;
private _endAt: Date;
private _createdAt: Date;
private _updatedAt: Date;
private _deletedAt: Date;

constructor(coupon: LectureCoupon) {
Object.assign(
this,
Object.fromEntries(
Object.entries(coupon).map(([key, value]) => [`_${key}`, value]),
),
);
}

validateUsageCount() {
if (this._maxUsageCount && this._usageCount >= this._maxUsageCount) {
throw new BadRequestException(
`쿠폰 사용 제한 횟수를 초과했습니다.`,
'CouponLimit',
);
}
}

applyDiscount(price: number): number {
let discountedPrice = price;

if (this._percentage > 0) {
const percentageDiscount = (price * this._percentage) / 100;
const actualDiscount =
this._maxDiscountPrice !== null
? Math.min(percentageDiscount, this._maxDiscountPrice)
: percentageDiscount;
discountedPrice -= actualDiscount;
}

if (this._discountPrice > 0) {
discountedPrice -= this._discountPrice;
}

return Math.max(500, discountedPrice);
}

get percentage(): number {
return this._percentage;
}
get discountPrice(): number {
return this._discountPrice;
}
}
29 changes: 10 additions & 19 deletions src/lecture/repositories/popular-lecture.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PrismaTransaction } from '@src/common/interface/common-interface';
import { Injectable } from '@nestjs/common';
import { Lecture, Reservation } from '@prisma/client';
import { PrismaService } from '@src/prisma/prisma.service';
Expand All @@ -7,24 +6,19 @@ import { PrismaService } from '@src/prisma/prisma.service';
export class PopularLectureRepository {
constructor(private readonly prismaService: PrismaService) {}

async trxReadLectureReservationCount(
trasaction: PrismaTransaction,
lectureId: number,
): Promise<number> {
return await trasaction.reservation.count({
async readLectureReservationCount(lectureId: number): Promise<number> {
return await this.prismaService.reservation.count({
where: { lectureSchedule: { lectureId } },
});
}

async trxReadLectureLikesCount(
trasaction: PrismaTransaction,
lectureId: number,
): Promise<number> {
return await trasaction.likedLecture.count({ where: { lectureId } });
async readLectureLikesCount(lectureId: number): Promise<number> {
return await this.prismaService.likedLecture.count({
where: { lectureId },
});
}

async trxReadLectureWithUserId(
transaction: PrismaTransaction,
async readLectureWithUserId(
lectureId: number,
userId?: number,
): Promise<Lecture> {
Expand All @@ -44,17 +38,14 @@ export class PopularLectureRepository {

userId ? (include['likedLecture'] = { where: { userId } }) : false;

return await transaction.lecture.findFirst({
return await this.prismaService.lecture.findFirst({
where: { id: lectureId, isActive: true },
include,
});
}

async trxReadLecture(
transaction: PrismaTransaction,
lectureId: number,
): Promise<Lecture> {
return await transaction.lecture.findFirst({
async readLecture(lectureId: number): Promise<Lecture> {
return await this.prismaService.lecture.findFirst({
where: { id: lectureId, isActive: true },
include: {
lecturer: true,
Expand Down
78 changes: 34 additions & 44 deletions src/lecture/services/popular-lecture.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,48 @@ export class PopularLectureService {
) {}

async readPopularLecture(userId?: number): Promise<LectureDto[]> {
return await this.prismaService.$transaction(
async (trasaction: PrismaTransaction) => {
const popularScores = [];
const where = { isActive: true };
userId
? (where['lecturer'] = { blockedLecturer: { none: { userId } } })
: false;
const popularScores = [];
const where = { isActive: true };
userId
? (where['lecturer'] = { blockedLecturer: { none: { userId } } })
: false;

const lectures = await trasaction.lecture.findMany({
where,
select: { id: true },
});
const lectures = await this.prismaService.lecture.findMany({
where,
select: { id: true },
});

for (const lecture of lectures) {
const reservationCount =
await this.popularLectureRepository.trxReadLectureReservationCount(
trasaction,
lecture.id,
);
const likesCount =
await this.popularLectureRepository.trxReadLectureLikesCount(
trasaction,
lecture.id,
);
const popularScore = this.createPopularScore(
lecture.id,
reservationCount,
likesCount,
);
popularScores.push(popularScore);
}
for (const lecture of lectures) {
const reservationCount =
await this.popularLectureRepository.readLectureReservationCount(
lecture.id,
);
const likesCount =
await this.popularLectureRepository.readLectureLikesCount(lecture.id);
const popularScore = this.createPopularScore(
lecture.id,
reservationCount,
likesCount,
);
popularScores.push(popularScore);
}

const sortedPopularScores = this.sortPopularScores(popularScores);
const sortedPopularScores = this.sortPopularScores(popularScores);

const topEightPopularScores = sortedPopularScores.slice(0, 8);
const topEightPopularScores = sortedPopularScores.slice(0, 8);

const popularLectures = [];
const popularLectures = [];

for (const popularLecture of topEightPopularScores) {
const lecture =
await this.popularLectureRepository.trxReadLectureWithUserId(
trasaction,
popularLecture.id,
userId,
);
for (const popularLecture of topEightPopularScores) {
const lecture = await this.popularLectureRepository.readLectureWithUserId(
popularLecture.id,
userId,
);

popularLectures.push(new LectureDto(lecture));
}
popularLectures.push(new LectureDto(lecture));
}

return popularLectures;
},
);
return popularLectures;
}

private createPopularScore(
Expand Down
23 changes: 8 additions & 15 deletions src/lecturer/repositories/popular-lecturer.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,20 @@ import { Lecturer } from '@prisma/client';
export class PopularLecturerRepository {
constructor(private readonly prismaService: PrismaService) {}

async trxReadLecturerReservationCount(
trasaction: PrismaTransaction,
lecturerId: number,
): Promise<number> {
return await trasaction.reservation.count({
async readLecturerReservationCount(lecturerId: number): Promise<number> {
return await this.prismaService.reservation.count({
where: { lectureSchedule: { lecture: { lecturerId } } },
});
}

async trxReadLecturerLikesCount(
trasaction: PrismaTransaction,
lecturerId: number,
): Promise<number> {
return await trasaction.likedLecturer.count({ where: { lecturerId } });
async readLecturerLikesCount(lecturerId: number): Promise<number> {
return await this.prismaService.likedLecturer.count({
where: { lecturerId },
});
}

async trxReadLecturerWithLecturerId(
transaction: PrismaTransaction,
lecturerId: number,
): Promise<Lecturer> {
return await transaction.lecturer.findFirst({
async readLecturerWithLecturerId(lecturerId: number): Promise<Lecturer> {
return await this.prismaService.lecturer.findFirst({
where: { id: lecturerId },
include: { lecturerProfileImageUrl: true },
});
Expand Down
114 changes: 63 additions & 51 deletions src/lecturer/services/popular-lecturer.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PrismaService } from '@src/prisma/prisma.service';
import { PopularLecturerRepository } from './../repositories/popular-lecturer.repository';
import { Injectable } from '@nestjs/common';
import { PrismaTransaction } from '@src/common/interface/common-interface';
import { LecturerDto } from '@src/common/dtos/lecturer.dto';

@Injectable()
Expand All @@ -12,63 +11,76 @@ export class PopularLecturerService {
) {}

async readManyPopularLecturer(userId?: number): Promise<LecturerDto[]> {
return await this.prismaService.$transaction(
async (trasaction: PrismaTransaction) => {
const where = { deletedAt: null };
const where = { deletedAt: null };

userId ? (where['blockedLecturer'] = { none: { userId } }) : false;
userId ? (where['blockedLecturer'] = { none: { userId } }) : false;

const popularScores = [];
const lecturers = await trasaction.lecturer.findMany({
where,
select: { id: true },
});
const popularScores = [];
const lecturers = await this.prismaService.lecturer.findMany({
where,
select: { id: true },
});

for (const lecturer of lecturers) {
const reservationCount =
await this.popularLecturerRepository.trxReadLecturerReservationCount(
trasaction,
lecturer.id,
);
const likesCount =
await this.popularLecturerRepository.trxReadLecturerLikesCount(
trasaction,
lecturer.id,
);
const popularScore = {
id: lecturer.id,
reservationCount,
likesCount,
score:
Math.round((reservationCount * 0.6 + likesCount * 0.4) * 100) /
100,
};
popularScores.push(popularScore);
}
for (const lecturer of lecturers) {
const reservationCount =
await this.popularLecturerRepository.readLecturerReservationCount(
lecturer.id,
);
const likesCount =
await this.popularLecturerRepository.readLecturerLikesCount(
lecturer.id,
);
const popularScore = this.createPopularScore(
lecturer.id,
reservationCount,
likesCount,
);

popularScores.sort((a, b) => {
if (a.score !== b.score) {
return b.score - a.score;
} else {
return b.reservationCount - a.reservationCount;
}
});
popularScores.push(popularScore);
}

const topTenPopularScores = popularScores.slice(0, 10);
const popularLecturers = [];
const sortedPopularScores = this.sortPopularScores(popularScores);

for (const popularLecturer of topTenPopularScores) {
const lecturer =
await this.popularLecturerRepository.trxReadLecturerWithLecturerId(
trasaction,
popularLecturer.id,
);
const topTenPopularScores = sortedPopularScores.slice(0, 10);
const popularLecturers = [];

popularLecturers.push(new LecturerDto(lecturer));
}
for (const popularLecturer of topTenPopularScores) {
const lecturer =
await this.popularLecturerRepository.readLecturerWithLecturerId(
popularLecturer.id,
);

return popularLecturers;
},
);
popularLecturers.push(new LecturerDto(lecturer));
}

return popularLecturers;
}

private createPopularScore(
lecturerId: number,
reservationCount: number,
likesCount: number,
) {
const popularScore = {
id: lecturerId,
reservationCount,
likesCount,
score:
Math.round((reservationCount * 0.6 + likesCount * 0.4) * 100) / 100,
};

return popularScore;
}

private sortPopularScores(popularScores) {
popularScores.sort((a, b) => {
if (a.score !== b.score) {
return b.score - a.score;
} else {
return b.reservationCount - a.reservationCount;
}
});

return popularScores;
}
}
Loading

0 comments on commit c94307b

Please sign in to comment.