@@ -23,6 +23,7 @@ import {
2323 rejectBookingInstance ,
2424 removeBooking ,
2525 saveBooking ,
26+ updateBooking ,
2627} from '@placeos/bookings' ;
2728import {
2829 AsyncHandler ,
@@ -48,8 +49,9 @@ import {
4849 set ,
4950 startOfDay ,
5051 startOfWeek ,
52+ subDays ,
5153} from 'date-fns' ;
52- import { BehaviorSubject , combineLatest , of } from 'rxjs' ;
54+ import { BehaviorSubject , combineLatest , lastValueFrom , of } from 'rxjs' ;
5355import {
5456 debounceTime ,
5557 filter ,
@@ -182,7 +184,9 @@ export class ParkingStateService extends AsyncHandler {
182184 : startOfDay ( options . date ) ;
183185 const period_end =
184186 options . period === 'week'
185- ? endOfWeek ( options . date , { weekStartsOn : this . _week_start } )
187+ ? endOfWeek ( options . date , {
188+ weekStartsOn : this . _week_start ,
189+ } )
186190 : endOfDay ( options . date ) ;
187191 this . _loading . next ( [ ...this . _loading . getValue ( ) , '[BOOKINGS]' ] ) ;
188192 return queryBookings ( {
@@ -577,16 +581,33 @@ export class ParkingStateService extends AsyncHandler {
577581 if ( result ) this . _change . next ( Date . now ( ) ) ;
578582 }
579583
580- private async _clearAssignedBooking ( space : ParkingSpace ) {
581- const booking_list = await queryBookings ( {
582- period_start : getUnixTime ( startOfDay ( Date . now ( ) ) ) ,
583- period_end : getUnixTime ( endOfDay ( Date . now ( ) ) ) ,
584- type : 'parking' ,
585- email : space . assigned_to ,
586- include_checked_out : true ,
587- } ) . toPromise ( ) ;
588- const filtered = booking_list . filter ( ( _ ) => _ . asset_id === space . id ) ;
589- await Promise . all ( filtered . map ( ( _ ) => removeBooking ( _ . id ) . toPromise ( ) ) ) ;
584+ private async _clearAssignedBooking ( resource : ParkingSpace ) {
585+ const today = Date . now ( ) ;
586+ const booking_list = await lastValueFrom (
587+ queryBookings ( {
588+ period_start : getUnixTime ( startOfDay ( today ) ) ,
589+ period_end : getUnixTime ( endOfDay ( today ) ) ,
590+ type : 'parking' ,
591+ email : resource . assigned_to ,
592+ include_checked_out : true ,
593+ } ) ,
594+ ) ;
595+ const filtered = booking_list . filter ( ( _ ) => _ . asset_id === resource . id ) ;
596+ for ( const booking of filtered ) {
597+ const is_recurring =
598+ booking . recurrence_type && booking . recurrence_type !== 'none' ;
599+ if ( is_recurring && booking . instance ) {
600+ // Set recurrence_end to end of yesterday to preserve past instances
601+ const yesterday_end = getUnixTime ( endOfDay ( subDays ( today , 1 ) ) ) ;
602+ await lastValueFrom (
603+ updateBooking ( booking . id , {
604+ recurrence_end : yesterday_end ,
605+ } ) ,
606+ ) ;
607+ } else {
608+ await lastValueFrom ( removeBooking ( booking . id ) ) ;
609+ }
610+ }
590611 }
591612
592613 private get _week_start ( ) : 0 | 1 | 2 | 3 | 4 | 5 | 6 {
0 commit comments