diff --git a/src/books/books.service.ts b/src/books/books.service.ts index 111ae41..64b0223 100644 --- a/src/books/books.service.ts +++ b/src/books/books.service.ts @@ -51,6 +51,12 @@ export class BooksService { const queryBuilder = await this.booksRepository .createQueryBuilder('book') .leftJoinAndSelect('book.info', 'info') + .leftJoinAndSelect( + 'book.lendings', + 'lendings', + 'NOT EXISTS(SELECT * FROM returning where returning.lendingId = lendings.id)', + ) + .leftJoinAndSelect('lendings.returning', 'returning') .where('book.callSign like :query', { query: `%${query}%` }) .orWhere('info.title like :query', { query: `%${query}%` }) .orWhere('info.author like :query', { query: `%${query}%` }) diff --git a/src/books/entities/book.entity.ts b/src/books/entities/book.entity.ts index 23262a5..f7bfbe4 100644 --- a/src/books/entities/book.entity.ts +++ b/src/books/entities/book.entity.ts @@ -92,4 +92,13 @@ export class Book { } else if (this.status == 1) return 'λΆ„μ‹€'; else if (this.status == 2) return 'νŒŒμ†'; } + + @Expose({ groups: ['books.searchBook'] }) + get isLenderable() { + if (!this.lendings || this.lendings.length == 0) return true; + const lastLending = this.lendings.sort( + (a, b) => b.createdAt.getTime() - a.createdAt.getTime(), + )[0]; + return !!lastLending.returning; + } } diff --git a/src/books/entities/bookInfo.entity.ts b/src/books/entities/bookInfo.entity.ts index b41ef3c..80cc35c 100644 --- a/src/books/entities/bookInfo.entity.ts +++ b/src/books/entities/bookInfo.entity.ts @@ -35,6 +35,7 @@ export class BookInfo { 'lendings.search', 'lendings.findOne', 'reservations.search', + 'users.search', ], }) title: string; diff --git a/src/lendings/entities/lending.entity.ts b/src/lendings/entities/lending.entity.ts index bb71416..d294661 100644 --- a/src/lendings/entities/lending.entity.ts +++ b/src/lendings/entities/lending.entity.ts @@ -42,17 +42,22 @@ export class Lending { @Expose({ groups: [] }) librarian: User; - @Expose({ groups: ['lendings.search', 'lendings.findOne'] }) + @Expose({ groups: ['lendings.search', 'lendings.findOne', 'users.search'] }) @ManyToOne(() => Book, (book) => book.lendings, { nullable: false }) book: Book; @OneToOne(() => Returning, (returning) => returning.lending) - @Expose({ groups: [] }) + @Expose({ groups: ['users.search'] }) returning: Returning; @Expose({ name: 'dueDate', - groups: ['lendings.search', 'lendings.findOne', 'reservations.search'], + groups: [ + 'lendings.search', + 'lendings.findOne', + 'reservations.search', + 'users.search', + ], }) getdueDate() { if (this.returning) throw new Error('lendings.service.find(All) catch'); diff --git a/src/reservations/entities/reservation.entity.ts b/src/reservations/entities/reservation.entity.ts index e9b772a..7bdde7a 100644 --- a/src/reservations/entities/reservation.entity.ts +++ b/src/reservations/entities/reservation.entity.ts @@ -21,7 +21,7 @@ export class Reservation { @Column({ nullable: true, }) - @Expose({ groups: ['reservations.search'] }) + @Expose({ groups: ['reservations.search', 'users.search'] }) endAt: Date; @CreateDateColumn() @@ -43,6 +43,27 @@ export class Reservation { user: User; @ManyToOne(() => Book, (book) => book.reservations) - @Expose({ groups: ['reservations.search'] }) + @Expose({ groups: ['reservations.search', 'users.search'] }) book: Book; + + @Expose({ name: 'lenderableDate', groups: ['users.search'] }) + getLenderableDate() { + return this.book.getDueDate(); + } + + @Expose({ name: 'endAt', groups: ['users.search'] }) + getEndAt() { + if (!this.endAt) { + return '-'; + } + return this.endAt.toJSON().substring(2, 10).split('-').join('.') + '.'; + } + + @Expose({ name: 'rank', groups: ['users.search'] }) + getRank() { + if (!this.endAt) { + return null; + } + return 1; + } } diff --git a/src/users/entities/user.entity.ts b/src/users/entities/user.entity.ts index a847368..d03eb0f 100644 --- a/src/users/entities/user.entity.ts +++ b/src/users/entities/user.entity.ts @@ -23,7 +23,12 @@ export class User { @Column() @Expose({ - groups: ['lendings.search', 'lendings.findOne', 'reservations.search'], + groups: [ + 'lendings.search', + 'lendings.findOne', + 'reservations.search', + 'users.search', + ], }) login: string; @@ -64,11 +69,11 @@ export class User { librarianLendings: Lending[]; @OneToMany(() => Lending, (lending) => lending.user) - @Expose({ groups: [] }) + @Expose({ groups: ['users.search'] }) lendings: Lending[]; @OneToMany(() => Reservation, (reservation) => reservation.user) - @Expose({ groups: [] }) + @Expose({ groups: ['users.search'] }) reservations: Reservation[]; @Expose({ groups: ['users.search'] }) @@ -85,4 +90,14 @@ export class User { if (penalty < today) return 0; return Math.ceil(Math.abs(+penalty - +today) / (1000 * 3600 * 24)); } + + @Expose({ + groups: ['users.search'], + }) + get lendingCnt() { + if (!this.lendings) { + return 0; + } + return this.lendings.length; + } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index a455e95..6b0e244 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -46,6 +46,25 @@ export class UsersService { async searchByLogin(login: string, options: IPaginationOptions) { const queryBuilder = await this.userRepository .createQueryBuilder('user') + .leftJoinAndSelect( + 'user.reservations', + 'reservations', + 'reservations.canceledAt IS NULL AND (reservations.endAt IS NULL OR reservations.endAt >= :current)', + { current: new Date() }, + ) + .leftJoinAndSelect('reservations.book', 'reservationsBook') + .leftJoinAndSelect('reservationsBook.info', 'reservationsBookInfo') + .leftJoinAndSelect( + 'reservationsBook.lendings', + 'reservationsBookLendings', + ) + .leftJoinAndSelect( + 'user.lendings', + 'lendings', + 'NOT EXISTS(SELECT * FROM returning where returning.lendingId = lendings.id)', + ) + .leftJoinAndSelect('lendings.book', 'lendingsBook') + .leftJoinAndSelect('lendingsBook.info', 'lendingsBookInfo') .where('user.login like :login', { login: `%${login}%` }); return paginate(queryBuilder, options); }