diff --git a/src/modules/bookings/bookings.controller.ts b/src/modules/bookings/bookings.controller.ts index 27b84da..4543d90 100644 --- a/src/modules/bookings/bookings.controller.ts +++ b/src/modules/bookings/bookings.controller.ts @@ -40,13 +40,15 @@ export class BookingsController { @ApiQuery({ name: 'customerId', required: false }) @ApiQuery({ name: 'businessId', required: false }) @ApiQuery({ name: 'status', required: false, enum: BookingStatus }) + @ApiQuery({ name: 'scope', required: false, enum: ['mine'] }) findAll( @Query('customerId') customerId?: string, @Query('businessId') businessId?: string, @Query('status') status?: BookingStatus, + @Query('scope') scope?: 'mine', @CurrentUser() user?: AuthUser, ): Promise { - return this.service.findAll({ customerId, businessId, status }, user); + return this.service.findAll({ customerId, businessId, status, scope }, user); } @Get(':id') @@ -61,7 +63,7 @@ export class BookingsController { @Post() @UseGuards(JwtAuthGuard, RolesGuard) - @Roles(UserRole.ADMIN, UserRole.CUSTOMER) + @Roles(UserRole.ADMIN, UserRole.BUSINESS_OWNER, UserRole.CUSTOMER) create( @Body() payload: CreateBookingDto, @CurrentUser() user: AuthUser, diff --git a/src/modules/bookings/bookings.service.ts b/src/modules/bookings/bookings.service.ts index 1d36b2f..cca4d31 100644 --- a/src/modules/bookings/bookings.service.ts +++ b/src/modules/bookings/bookings.service.ts @@ -28,6 +28,7 @@ type FindAllFilters = { customerId?: string; businessId?: string; status?: BookingStatus; + scope?: 'mine'; }; @Injectable() @@ -69,9 +70,20 @@ export class BookingsService { actorCustomerId: actor.id, }); } else if (actor?.role === UserRole.BUSINESS_OWNER) { - qb.andWhere('business.ownerId = :actorOwnerId', { - actorOwnerId: actor.id, - }); + qb.andWhere( + '(business.ownerId = :actorOwnerId OR b.customerId = :actorCustomerId)', + { actorOwnerId: actor.id, actorCustomerId: actor.id }, + ); + } else if (actor?.role === UserRole.ADMIN) { + if (filters.scope === 'mine') { + qb.andWhere('b.customerId = :actorCustomerId', { + actorCustomerId: actor.id, + }); + } else if (filters.customerId) { + qb.andWhere('b.customerId = :customerId', { + customerId: filters.customerId, + }); + } } else if (filters.customerId) { qb.andWhere('b.customerId = :customerId', { customerId: filters.customerId, @@ -106,11 +118,8 @@ export class BookingsService { } async create(dto: CreateBookingDto, actor?: AuthUser): Promise { - if ( - actor?.role === UserRole.CUSTOMER && - dto.customerId !== actor.id - ) { - throw new ForbiddenException('Customers can only book for themselves'); + if (actor && dto.customerId !== actor.id) { + throw new ForbiddenException('You can only book for yourself'); } const customer = await this.userRepo.findOne({ @@ -452,7 +461,7 @@ export class BookingsService { if ( actor.role === UserRole.BUSINESS_OWNER && - row.business?.ownerId === actor.id + (row.business?.ownerId === actor.id || row.customerId === actor.id) ) { return; } @@ -471,7 +480,7 @@ export class BookingsService { if ( actor.role === UserRole.BUSINESS_OWNER && - row.business?.ownerId === actor.id + (row.business?.ownerId === actor.id || row.customerId === actor.id) ) { return; } @@ -486,7 +495,7 @@ export class BookingsService { if ( actor.role === UserRole.BUSINESS_OWNER && - row.business?.ownerId === actor.id + (row.business?.ownerId === actor.id || row.customerId === actor.id) ) { return; }