@@ -13,6 +13,7 @@ import site.billilge.api.backend.domain.member.exception.MemberErrorCode
1313import site.billilge.api.backend.domain.member.repository.MemberRepository
1414import site.billilge.api.backend.domain.notification.enums.NotificationStatus
1515import site.billilge.api.backend.domain.notification.service.NotificationService
16+ import site.billilge.api.backend.domain.rental.dto.request.AdminRentalHistoryRequest
1617import site.billilge.api.backend.domain.rental.dto.request.RentalHistoryRequest
1718import site.billilge.api.backend.domain.rental.dto.request.RentalStatusUpdateRequest
1819import site.billilge.api.backend.domain.rental.dto.response.*
@@ -129,16 +130,63 @@ class RentalService(
129130 true
130131 )
131132
132- notificationService.sendNotificationToAdmin(
133- NotificationStatus .ADMIN_RENTAL_APPLY ,
134- listOf (
135- rentUser.name,
136- rentUser.studentId,
137- " ${String .format(" %02d" , rentalHour)} :${String .format(" %02d" , rentalMinute)} " ,
138- item.name
139- ),
140- true
133+ if (! isDevMode) {
134+ notificationService.sendNotificationToAdmin(
135+ NotificationStatus .ADMIN_RENTAL_APPLY ,
136+ listOf (
137+ rentUser.name,
138+ rentUser.studentId,
139+ " ${String .format(" %02d" , rentalHour)} :${String .format(" %02d" , rentalMinute)} " ,
140+ item.name
141+ ),
142+ true
143+ )
144+ }
145+ }
146+
147+ @Transactional
148+ fun createRentalByAdmin (request : AdminRentalHistoryRequest ) {
149+
150+ val item = itemRepository.findById(request.itemId)
151+ .orElseThrow { ApiException (RentalErrorCode .ITEM_NOT_FOUND ) }
152+ val rentedCount = request.count
153+
154+ if (rentedCount > item.count)
155+ throw ApiException (RentalErrorCode .ITEM_OUT_OF_STOCK )
156+
157+ val rentUser = memberRepository.findById(request.memberId)
158+ .orElseThrow { ApiException (RentalErrorCode .MEMBER_NOT_FOUND ) }
159+
160+ if (! rentUser.isFeePaid)
161+ throw ApiException (RentalErrorCode .MEMBER_IS_NOT_PAYER )
162+
163+ val koreanZone = ZoneId .of(" Asia/Seoul" )
164+ val today = LocalDate .now(koreanZone)
165+ val requestedRentalDateTime = LocalDateTime .of(
166+ today,
167+ LocalTime .of(request.rentalTime.hour, request.rentalTime.minute)
141168 )
169+
170+ val rentAt = requestedRentalDateTime.atZone(koreanZone).toLocalDateTime()
171+
172+ val newRental = RentalHistory (
173+ member = rentUser,
174+ item = item,
175+ rentalStatus = if (item.type == ItemType .RENTAL ) RentalStatus .RENTAL else RentalStatus .RETURNED ,
176+ rentedCount = rentedCount,
177+ rentAt = rentAt
178+ ).apply {
179+ if (rentalStatus == RentalStatus .RETURNED ) {
180+ returnedAt = LocalDateTime .now()
181+ }
182+ }
183+
184+ rentalRepository.save(newRental)
185+ }
186+
187+ @Transactional
188+ fun deleteRentalHistory (rentalHistoryId : Long ) {
189+ rentalRepository.deleteById(rentalHistoryId)
142190 }
143191
144192 fun getMemberRentalHistory (memberId : Long? , rentalStatus : RentalStatus ? ): RentalHistoryFindAllResponse {
0 commit comments