mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-08-25 15:59:38 +00:00
SP-1757, SP-1758, SP-1809, SP-1810: Feat/implement booking (#469)
* fix: commission device API * task: add create booking API * add get All api for dashboard & mobile * add Find APIs for bookings * implement sending email updates on update bookable space * move email interfaces to separate files
This commit is contained in:
@ -1,15 +1,17 @@
|
||||
import { AuthService } from '@app/common/auth/services/auth.service';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
||||
import {
|
||||
UserOtpRepository,
|
||||
UserRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { UserSessionRepository } from '@app/common/modules/session/repositories/session.repository';
|
||||
import { UserOtpRepository } from '@app/common/modules/user/repositories';
|
||||
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { RoleService } from 'src/role/services';
|
||||
import { UserAuthController } from './controllers';
|
||||
import { UserAuthService } from './services';
|
||||
import { AuthService } from '@app/common/auth/services/auth.service';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
|
@ -16,7 +16,7 @@ import { UserSessionRepository } from '../../../libs/common/src/modules/session/
|
||||
import { UserEntity } from '../../../libs/common/src/modules/user/entities/user.entity';
|
||||
import { UserRepository } from '../../../libs/common/src/modules/user/repositories';
|
||||
import { UserOtpRepository } from '../../../libs/common/src/modules/user/repositories/user.repository';
|
||||
import { EmailService } from '../../../libs/common/src/util/email.service';
|
||||
import { EmailService } from '../../../libs/common/src/util/email/email.service';
|
||||
import { ForgetPasswordDto, UserOtpDto, VerifyOtpDto } from '../dtos';
|
||||
import { UserSignUpDto } from '../dtos/user-auth.dto';
|
||||
import { UserLoginDto } from '../dtos/user-login.dto';
|
||||
|
@ -1,17 +1,29 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { BookableSpaceController } from './controllers';
|
||||
import { BookableSpaceService } from './services';
|
||||
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories';
|
||||
import { BookingRepositoryModule } from '@app/common/modules/booking/booking.repository.module';
|
||||
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories/bookable-space.repository';
|
||||
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||
import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { BookableSpaceController } from './controllers/bookable-space.controller';
|
||||
import { BookingController } from './controllers/booking.controller';
|
||||
import { BookableSpaceService } from './services/bookable-space.service';
|
||||
import { BookingService } from './services/booking.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
controllers: [BookableSpaceController],
|
||||
imports: [BookingRepositoryModule],
|
||||
controllers: [BookableSpaceController, BookingController],
|
||||
providers: [
|
||||
BookableSpaceService,
|
||||
BookingService,
|
||||
EmailService,
|
||||
BookableSpaceEntityRepository,
|
||||
BookingEntityRepository,
|
||||
|
||||
SpaceRepository,
|
||||
UserRepository,
|
||||
],
|
||||
exports: [BookableSpaceService],
|
||||
exports: [BookableSpaceService, BookingService],
|
||||
})
|
||||
export class BookingModule {}
|
||||
|
@ -19,11 +19,11 @@ import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { PageResponse } from '@app/common/dto/pagination.response.dto';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { CreateBookableSpaceDto } from '../dtos';
|
||||
import { BookableSpaceRequestDto } from '../dtos/bookable-space-request.dto';
|
||||
import { BookableSpaceResponseDto } from '../dtos/bookable-space-response.dto';
|
||||
import { CreateBookableSpaceDto } from '../dtos/create-bookable-space.dto';
|
||||
import { UpdateBookableSpaceDto } from '../dtos/update-bookable-space.dto';
|
||||
import { BookableSpaceService } from '../services';
|
||||
import { BookableSpaceService } from '../services/bookable-space.service';
|
||||
|
||||
@ApiTags('Booking Module')
|
||||
@Controller({
|
||||
|
107
src/booking/controllers/booking.controller.ts
Normal file
107
src/booking/controllers/booking.controller.ts
Normal file
@ -0,0 +1,107 @@
|
||||
import { ControllerRoute } from '@app/common/constants/controller-route';
|
||||
import { EnableDisableStatusEnum } from '@app/common/constants/days.enum';
|
||||
import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||
import { BookingRequestDto } from '../dtos/booking-request.dto';
|
||||
import { BookingResponseDto } from '../dtos/booking-response.dto';
|
||||
import { CreateBookingDto } from '../dtos/create-booking.dto';
|
||||
import { MyBookingRequestDto } from '../dtos/my-booking-request.dto';
|
||||
import { BookingService } from '../services/booking.service';
|
||||
|
||||
@ApiTags('Booking Module')
|
||||
@Controller({
|
||||
version: EnableDisableStatusEnum.ENABLED,
|
||||
path: ControllerRoute.BOOKING.ROUTE,
|
||||
})
|
||||
export class BookingController {
|
||||
constructor(private readonly bookingService: BookingService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.BOOKING.ACTIONS.ADD_BOOKING_SUMMARY,
|
||||
description: ControllerRoute.BOOKING.ACTIONS.ADD_BOOKING_DESCRIPTION,
|
||||
})
|
||||
async create(
|
||||
@Body() dto: CreateBookingDto,
|
||||
@Req() req: Request,
|
||||
): Promise<BaseResponseDto> {
|
||||
const userUuid = req['user']?.uuid;
|
||||
if (!userUuid) {
|
||||
throw new Error('User UUID is required in the request');
|
||||
}
|
||||
const result = await this.bookingService.create(userUuid, dto);
|
||||
return new SuccessResponseDto({
|
||||
data: result,
|
||||
message: 'Successfully created booking',
|
||||
});
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(AdminRoleGuard)
|
||||
@Get()
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.BOOKING.ACTIONS.GET_ALL_BOOKINGS_SUMMARY,
|
||||
description: ControllerRoute.BOOKING.ACTIONS.GET_ALL_BOOKINGS_DESCRIPTION,
|
||||
})
|
||||
async findAll(
|
||||
@Query() query: BookingRequestDto,
|
||||
@Req() req: Request,
|
||||
): Promise<BaseResponseDto> {
|
||||
const project = req['user']?.project?.uuid;
|
||||
if (!project) {
|
||||
throw new Error('Project UUID is required in the request');
|
||||
}
|
||||
const result = await this.bookingService.findAll(query, project);
|
||||
return new SuccessResponseDto({
|
||||
data: plainToInstance(BookingResponseDto, result, {
|
||||
excludeExtraneousValues: true,
|
||||
}),
|
||||
message: 'Successfully fetched all bookings',
|
||||
});
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('my-bookings')
|
||||
@ApiOperation({
|
||||
summary: ControllerRoute.BOOKING.ACTIONS.GET_MY_BOOKINGS_SUMMARY,
|
||||
description: ControllerRoute.BOOKING.ACTIONS.GET_MY_BOOKINGS_DESCRIPTION,
|
||||
})
|
||||
async findMyBookings(
|
||||
@Query() query: MyBookingRequestDto,
|
||||
@Req() req: Request,
|
||||
): Promise<BaseResponseDto> {
|
||||
const userUuid = req['user']?.uuid;
|
||||
const project = req['user']?.project?.uuid;
|
||||
if (!project) {
|
||||
throw new Error('Project UUID is required in the request');
|
||||
}
|
||||
const result = await this.bookingService.findMyBookings(
|
||||
query,
|
||||
userUuid,
|
||||
project,
|
||||
);
|
||||
return new SuccessResponseDto({
|
||||
data: plainToInstance(BookingResponseDto, result, {
|
||||
excludeExtraneousValues: true,
|
||||
}),
|
||||
message: 'Successfully fetched all bookings',
|
||||
});
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './bookable-space.controller';
|
14
src/booking/dtos/booking-request.dto.ts
Normal file
14
src/booking/dtos/booking-request.dto.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, Matches } from 'class-validator';
|
||||
|
||||
export class BookingRequestDto {
|
||||
@ApiProperty({
|
||||
description: 'Month in MM/YYYY format',
|
||||
example: '07/2025',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@Matches(/^(0[1-9]|1[0-2])\/\d{4}$/, {
|
||||
message: 'Date must be in MM/YYYY format',
|
||||
})
|
||||
month: string;
|
||||
}
|
88
src/booking/dtos/booking-response.dto.ts
Normal file
88
src/booking/dtos/booking-response.dto.ts
Normal file
@ -0,0 +1,88 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Expose, Transform, Type } from 'class-transformer';
|
||||
|
||||
export class BookingUserResponseDto {
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
uuid: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
firstName: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
lastName: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: String,
|
||||
nullable: true,
|
||||
})
|
||||
@Expose()
|
||||
email: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: String,
|
||||
nullable: true,
|
||||
})
|
||||
@Expose()
|
||||
@Transform(({ obj }) => obj.inviteUser?.companyName || null)
|
||||
companyName: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: String,
|
||||
nullable: true,
|
||||
})
|
||||
@Expose()
|
||||
phoneNumber: string;
|
||||
}
|
||||
|
||||
export class BookingSpaceResponseDto {
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
uuid: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
spaceName: string;
|
||||
}
|
||||
|
||||
export class BookingResponseDto {
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
uuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: Date,
|
||||
})
|
||||
@Expose()
|
||||
date: Date;
|
||||
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
startTime: string;
|
||||
|
||||
@ApiProperty()
|
||||
@Expose()
|
||||
endTime: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: Number,
|
||||
})
|
||||
@Expose()
|
||||
cost: number;
|
||||
|
||||
@ApiProperty({
|
||||
type: BookingUserResponseDto,
|
||||
})
|
||||
@Type(() => BookingUserResponseDto)
|
||||
@Expose()
|
||||
user: BookingUserResponseDto;
|
||||
|
||||
@ApiProperty({
|
||||
type: BookingSpaceResponseDto,
|
||||
})
|
||||
@Type(() => BookingSpaceResponseDto)
|
||||
@Expose()
|
||||
space: BookingSpaceResponseDto;
|
||||
}
|
35
src/booking/dtos/create-booking.dto.ts
Normal file
35
src/booking/dtos/create-booking.dto.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsDate, IsNotEmpty, IsString, IsUUID, Matches } from 'class-validator';
|
||||
|
||||
export class CreateBookingDto {
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
example: '4fa85f64-5717-4562-b3fc-2c963f66afa7',
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsUUID('4', { message: 'Invalid space UUID provided' })
|
||||
spaceUuid: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: Date,
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@IsDate()
|
||||
date: Date;
|
||||
|
||||
@ApiProperty({ example: '09:00' })
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'Start time cannot be empty' })
|
||||
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||
message: 'Start time must be in HH:mm format (24-hour)',
|
||||
})
|
||||
startTime: string;
|
||||
|
||||
@ApiProperty({ example: '17:00' })
|
||||
@IsString()
|
||||
@IsNotEmpty({ message: 'End time cannot be empty' })
|
||||
@Matches(/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/, {
|
||||
message: 'End time must be in HH:mm format (24-hour)',
|
||||
})
|
||||
endTime: string;
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './create-bookable-space.dto';
|
14
src/booking/dtos/my-booking-request.dto.ts
Normal file
14
src/booking/dtos/my-booking-request.dto.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsIn, IsOptional } from 'class-validator';
|
||||
|
||||
export class MyBookingRequestDto {
|
||||
@ApiProperty({
|
||||
description: 'Filter bookings by time period',
|
||||
example: 'past',
|
||||
enum: ['past', 'future'],
|
||||
required: false,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsIn(['past', 'future'])
|
||||
when?: 'past' | 'future';
|
||||
}
|
@ -2,24 +2,30 @@ import { BaseResponseDto } from '@app/common/dto/base.response.dto';
|
||||
import { PageResponseDto } from '@app/common/dto/pagination.response.dto';
|
||||
import { timeToMinutes } from '@app/common/helper/timeToMinutes';
|
||||
import { TypeORMCustomModel } from '@app/common/models/typeOrmCustom.model';
|
||||
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories';
|
||||
import { BookableSpaceEntityRepository } from '@app/common/modules/booking/repositories/bookable-space.repository';
|
||||
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories/space.repository';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { to12HourFormat } from '@app/common/util/time-to-12-hours-convetion';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { In } from 'typeorm';
|
||||
import { CreateBookableSpaceDto } from '../dtos';
|
||||
import { format } from 'date-fns';
|
||||
import { Brackets, In } from 'typeorm';
|
||||
import { BookableSpaceRequestDto } from '../dtos/bookable-space-request.dto';
|
||||
import { CreateBookableSpaceDto } from '../dtos/create-bookable-space.dto';
|
||||
import { UpdateBookableSpaceDto } from '../dtos/update-bookable-space.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BookableSpaceService {
|
||||
constructor(
|
||||
private readonly emailService: EmailService,
|
||||
private readonly bookableSpaceEntityRepository: BookableSpaceEntityRepository,
|
||||
private readonly bookingEntityRepository: BookingEntityRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
) {}
|
||||
|
||||
@ -84,8 +90,7 @@ export class BookableSpaceService {
|
||||
}
|
||||
|
||||
/**
|
||||
* todo: if updating availability, send to the ones who have access to this space
|
||||
* todo: if updating other fields, just send emails to all users who's bookings might be affected
|
||||
* Update bookable space configuration
|
||||
*/
|
||||
async update(spaceUuid: string, dto: UpdateBookableSpaceDto) {
|
||||
// fetch spaces exist
|
||||
@ -102,11 +107,179 @@ export class BookableSpaceService {
|
||||
dto.startTime || space.bookableConfig.startTime,
|
||||
dto.endTime || space.bookableConfig.endTime,
|
||||
);
|
||||
if (
|
||||
dto.startTime != space.bookableConfig.startTime ||
|
||||
dto.endTime != space.bookableConfig.endTime ||
|
||||
dto.daysAvailable != space.bookableConfig.daysAvailable
|
||||
) {
|
||||
this.handleTimingUpdate(
|
||||
{
|
||||
daysAvailable:
|
||||
dto.daysAvailable || space.bookableConfig.daysAvailable,
|
||||
startTime: dto.startTime || space.bookableConfig.startTime,
|
||||
endTime: dto.endTime || space.bookableConfig.endTime,
|
||||
},
|
||||
space,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
dto.active !== undefined &&
|
||||
dto.active !== space.bookableConfig.active
|
||||
) {
|
||||
this.handleAvailabilityUpdate(dto.active, space);
|
||||
}
|
||||
|
||||
Object.assign(space.bookableConfig, dto);
|
||||
return this.bookableSpaceEntityRepository.save(space.bookableConfig);
|
||||
}
|
||||
|
||||
private async handleTimingUpdate(
|
||||
dto: UpdateBookableSpaceDto,
|
||||
space: SpaceEntity,
|
||||
): Promise<void> {
|
||||
const affectedUsers = await this.getAffectedBookings(space.uuid);
|
||||
if (!affectedUsers.length) return;
|
||||
|
||||
const groupedParams = this.groupBookingsByUser(affectedUsers);
|
||||
|
||||
return this.emailService.sendUpdateBookingTimingEmailWithTemplate(
|
||||
groupedParams,
|
||||
{
|
||||
space_name: space.spaceName,
|
||||
start_time: to12HourFormat(dto.startTime),
|
||||
end_time: to12HourFormat(dto.endTime),
|
||||
days: dto.daysAvailable.join(', '),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private async getAffectedBookings(spaceUuid: string) {
|
||||
const today = new Date();
|
||||
const nowTime = format(today, 'HH:mm');
|
||||
|
||||
const bookingWithDayCte = this.bookingEntityRepository
|
||||
.createQueryBuilder('b')
|
||||
.select('b.*')
|
||||
.addSelect(
|
||||
`
|
||||
CASE EXTRACT(DOW FROM b.date)
|
||||
WHEN 0 THEN 'Sun'
|
||||
WHEN 1 THEN 'Mon'
|
||||
WHEN 2 THEN 'Tue'
|
||||
WHEN 3 THEN 'Wed'
|
||||
WHEN 4 THEN 'Thu'
|
||||
WHEN 5 THEN 'Fri'
|
||||
WHEN 6 THEN 'Sat'
|
||||
END::"bookable-space_days_available_enum"
|
||||
`,
|
||||
'booking_day',
|
||||
)
|
||||
.where(
|
||||
`(DATE(b.date) > :today OR (DATE(b.date) = :today AND b.startTime >= :nowTime))`,
|
||||
{ today, nowTime },
|
||||
)
|
||||
.andWhere('b.space_uuid = :spaceUuid', { spaceUuid });
|
||||
|
||||
const query = this.bookableSpaceEntityRepository
|
||||
.createQueryBuilder('bs')
|
||||
.distinct(true)
|
||||
.addCommonTableExpression(bookingWithDayCte, 'booking_with_day')
|
||||
.select('u.first_name', 'name')
|
||||
.addSelect('u.email', 'email')
|
||||
.addSelect('DATE(bwd.date)', 'date')
|
||||
.addSelect('bwd.start_time', 'start_time')
|
||||
.addSelect('bwd.end_time', 'end_time')
|
||||
.from('booking_with_day', 'bwd')
|
||||
.innerJoin('user', 'u', 'u.uuid = bwd.user_uuid')
|
||||
.where('bs.space_uuid = :spaceUuid', { spaceUuid })
|
||||
.andWhere(
|
||||
new Brackets((qb) => {
|
||||
qb.where('NOT (bwd.booking_day = ANY(bs.days_available))')
|
||||
.orWhere('bwd.start_time < bs.start_time')
|
||||
.orWhere('bwd.end_time > bs.end_time');
|
||||
}),
|
||||
);
|
||||
|
||||
return query.getRawMany<{
|
||||
name: string;
|
||||
email: string;
|
||||
date: string;
|
||||
start_time: string;
|
||||
end_time: string;
|
||||
}>();
|
||||
}
|
||||
|
||||
private groupBookingsByUser(
|
||||
bookings: {
|
||||
name: string;
|
||||
email: string;
|
||||
date: string;
|
||||
start_time: string;
|
||||
end_time: string;
|
||||
}[],
|
||||
): {
|
||||
name: string;
|
||||
email: string;
|
||||
bookings: { date: string; start_time: string; end_time: string }[];
|
||||
}[] {
|
||||
const grouped: Record<
|
||||
string,
|
||||
{
|
||||
name: string;
|
||||
email: string;
|
||||
bookings: { date: string; start_time: string; end_time: string }[];
|
||||
}
|
||||
> = {};
|
||||
|
||||
for (const { name, email, date, start_time, end_time } of bookings) {
|
||||
const formattedDate = format(new Date(date), 'yyyy-MM-dd');
|
||||
const formattedStartTime = to12HourFormat(start_time);
|
||||
const formattedEndTime = to12HourFormat(end_time);
|
||||
|
||||
if (!grouped[email]) {
|
||||
grouped[email] = {
|
||||
name,
|
||||
email,
|
||||
bookings: [],
|
||||
};
|
||||
}
|
||||
|
||||
grouped[email].bookings.push({
|
||||
date: formattedDate,
|
||||
start_time: formattedStartTime,
|
||||
end_time: formattedEndTime,
|
||||
});
|
||||
}
|
||||
|
||||
return Object.values(grouped);
|
||||
}
|
||||
|
||||
private async handleAvailabilityUpdate(
|
||||
active: boolean,
|
||||
space: SpaceEntity,
|
||||
): Promise<void> {
|
||||
space = await this.spaceRepository.findOne({
|
||||
where: { uuid: space.uuid },
|
||||
relations: ['userSpaces', 'userSpaces.user'],
|
||||
});
|
||||
const emails = space.userSpaces.map((userSpace) => ({
|
||||
email: userSpace.user.email,
|
||||
name: userSpace.user.firstName,
|
||||
}));
|
||||
if (!emails.length) return Promise.resolve();
|
||||
|
||||
return this.emailService.sendUpdateBookingAvailabilityEmailWithTemplate(
|
||||
emails,
|
||||
{
|
||||
availability: active ? 'Available' : 'Unavailable',
|
||||
space_name: space.spaceName,
|
||||
isAvailable: active,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch spaces by UUIDs and throw an error if any are missing
|
||||
*/
|
||||
|
215
src/booking/services/booking.service.ts
Normal file
215
src/booking/services/booking.service.ts
Normal file
@ -0,0 +1,215 @@
|
||||
import { DaysEnum } from '@app/common/constants/days.enum';
|
||||
import { timeToMinutes } from '@app/common/helper/timeToMinutes';
|
||||
import { BookingEntityRepository } from '@app/common/modules/booking/repositories/booking.repository';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories/space.repository';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories/user.repository';
|
||||
import {
|
||||
BadRequestException,
|
||||
ConflictException,
|
||||
ForbiddenException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { format } from 'date-fns';
|
||||
import { Between } from 'typeorm/find-options/operator/Between';
|
||||
import { BookingRequestDto } from '../dtos/booking-request.dto';
|
||||
import { CreateBookingDto } from '../dtos/create-booking.dto';
|
||||
import { MyBookingRequestDto } from '../dtos/my-booking-request.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BookingService {
|
||||
constructor(
|
||||
private readonly bookingEntityRepository: BookingEntityRepository,
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
async create(userUuid: string, dto: CreateBookingDto) {
|
||||
console.log(userUuid);
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid },
|
||||
relations: ['userSpaces', 'userSpaces.space'],
|
||||
});
|
||||
console.log(user.userSpaces);
|
||||
if (!user.userSpaces.some(({ space }) => space.uuid === dto.spaceUuid)) {
|
||||
throw new ForbiddenException(
|
||||
`User does not have permission to book this space: ${dto.spaceUuid}`,
|
||||
);
|
||||
}
|
||||
// Validate time slots first
|
||||
this.validateTimeSlot(dto.startTime, dto.endTime);
|
||||
|
||||
// fetch spaces exist
|
||||
const space = await this.getSpaceConfigurationAndBookings(dto.spaceUuid);
|
||||
|
||||
// Validate booking availability
|
||||
this.validateBookingAvailability(space, dto);
|
||||
|
||||
// Create and save booking
|
||||
return this.createBookings(space, userUuid, dto);
|
||||
}
|
||||
|
||||
async findAll({ month }: BookingRequestDto, project: string) {
|
||||
const [monthNumber, year] = month.split('/').map(Number);
|
||||
const fromDate = new Date(year, monthNumber - 1, 1);
|
||||
const toDate = new Date(year, monthNumber, 0, 23, 59, 59);
|
||||
return this.bookingEntityRepository.find({
|
||||
where: {
|
||||
space: { community: { project: { uuid: project } } },
|
||||
date: Between(fromDate, toDate),
|
||||
},
|
||||
relations: ['space', 'user', 'user.inviteUser'],
|
||||
order: { date: 'DESC' },
|
||||
});
|
||||
}
|
||||
|
||||
async findMyBookings(
|
||||
{ when }: MyBookingRequestDto,
|
||||
userUuid: string,
|
||||
project: string,
|
||||
) {
|
||||
const now = new Date();
|
||||
const nowTime = format(now, 'HH:mm');
|
||||
|
||||
const query = this.bookingEntityRepository
|
||||
.createQueryBuilder('booking')
|
||||
.leftJoinAndSelect('booking.space', 'space')
|
||||
.innerJoin(
|
||||
'space.community',
|
||||
'community',
|
||||
'community.project = :project',
|
||||
{ project },
|
||||
)
|
||||
.leftJoinAndSelect('booking.user', 'user')
|
||||
.where('user.uuid = :userUuid', { userUuid });
|
||||
|
||||
if (when === 'past') {
|
||||
query.andWhere(
|
||||
`(DATE(booking.date) < :today OR (DATE(booking.date) = :today AND booking.startTime < :nowTime))`,
|
||||
{ today: now, nowTime },
|
||||
);
|
||||
} else if (when === 'future') {
|
||||
query.andWhere(
|
||||
`(DATE(booking.date) > :today OR (DATE(booking.date) = :today AND booking.startTime >= :nowTime))`,
|
||||
{ today: now, nowTime },
|
||||
);
|
||||
}
|
||||
|
||||
query.orderBy({
|
||||
'DATE(booking.date)': 'DESC',
|
||||
'booking.startTime': 'DESC',
|
||||
});
|
||||
|
||||
return query.getMany();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch space by UUID and throw an error if not found or if not configured for booking
|
||||
*/
|
||||
private async getSpaceConfigurationAndBookings(
|
||||
spaceUuid: string,
|
||||
): Promise<SpaceEntity> {
|
||||
const space = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['bookableConfig', 'bookings'],
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
throw new NotFoundException(`Space not found: ${spaceUuid}`);
|
||||
}
|
||||
if (!space.bookableConfig) {
|
||||
throw new NotFoundException(
|
||||
`This space is not configured for booking: ${spaceUuid}`,
|
||||
);
|
||||
}
|
||||
|
||||
return space;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the slot start time is before the end time
|
||||
*/
|
||||
private validateTimeSlot(startTime: string, endTime: string): void {
|
||||
const start = timeToMinutes(startTime);
|
||||
const end = timeToMinutes(endTime);
|
||||
|
||||
if (start >= end) {
|
||||
throw new BadRequestException(
|
||||
`End time must be after start time for slot: ${startTime}-${endTime}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the space is available for booking on the requested day
|
||||
* and if the requested time slot is within the available hours
|
||||
*/
|
||||
private validateBookingAvailability(
|
||||
space: SpaceEntity,
|
||||
dto: CreateBookingDto,
|
||||
): void {
|
||||
// Check if the space is available for booking on the requested day
|
||||
const availableDays = space.bookableConfig?.daysAvailable || [];
|
||||
const requestedDay = new Date(dto.date).toLocaleDateString('en-US', {
|
||||
weekday: 'short',
|
||||
}) as DaysEnum;
|
||||
|
||||
if (!availableDays.includes(requestedDay)) {
|
||||
const dayFullName = new Date(dto.date).toLocaleDateString('en-US', {
|
||||
weekday: 'long',
|
||||
});
|
||||
throw new BadRequestException(
|
||||
`Space is not available for booking on ${dayFullName}s`,
|
||||
);
|
||||
}
|
||||
|
||||
const dtoStartTimeInMinutes = timeToMinutes(dto.startTime);
|
||||
const dtoEndTimeInMinutes = timeToMinutes(dto.endTime);
|
||||
|
||||
if (
|
||||
dtoStartTimeInMinutes < timeToMinutes(space.bookableConfig.startTime) ||
|
||||
dtoEndTimeInMinutes > timeToMinutes(space.bookableConfig.endTime)
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
`Booking time must be within the available hours for space: ${space.spaceName}`,
|
||||
);
|
||||
}
|
||||
|
||||
const previousBookings = space.bookings.filter(
|
||||
(booking) =>
|
||||
timeToMinutes(booking.startTime) < dtoEndTimeInMinutes &&
|
||||
timeToMinutes(booking.endTime) > dtoStartTimeInMinutes &&
|
||||
format(new Date(booking.date), 'yyyy-MM-dd') ===
|
||||
format(new Date(dto.date), 'yyyy-MM-dd'),
|
||||
);
|
||||
|
||||
if (previousBookings.length > 0) {
|
||||
// tell the user what time is unavailable
|
||||
const unavailableTimes = previousBookings.map((booking) => {
|
||||
return `${booking.startTime}-${booking.endTime}`;
|
||||
});
|
||||
throw new ConflictException(
|
||||
`Space is already booked during this times: ${unavailableTimes.join(', ')}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Create bookable space entries after all validations pass
|
||||
*/
|
||||
private async createBookings(
|
||||
space: SpaceEntity,
|
||||
user: string,
|
||||
{ spaceUuid, date, ...dto }: CreateBookingDto,
|
||||
) {
|
||||
const entry = this.bookingEntityRepository.create({
|
||||
space: { uuid: spaceUuid },
|
||||
user: { uuid: user },
|
||||
...dto,
|
||||
date: new Date(date),
|
||||
cost: space.bookableConfig?.points || null,
|
||||
});
|
||||
|
||||
return this.bookingEntityRepository.save(entry);
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
export * from './bookable-space.service';
|
@ -9,6 +9,7 @@ import { OccupancyService } from '@app/common/helper/services/occupancy.service'
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
@ -27,6 +28,7 @@ import {
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { RegionRepository } from '@app/common/modules/region/repositories';
|
||||
@ -57,7 +59,7 @@ import {
|
||||
UserRepository,
|
||||
UserSpaceRepository,
|
||||
} from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { CommunityModule } from 'src/community/community.module';
|
||||
import { CommunityService } from 'src/community/services';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
@ -81,8 +83,6 @@ import { SubspaceProductAllocationService } from 'src/space/services/subspace/su
|
||||
import { TagService as NewTagService } from 'src/tags/services';
|
||||
import { UserDevicePermissionService } from 'src/user-device-permission/services';
|
||||
import { UserService, UserSpaceService } from 'src/users/services';
|
||||
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, InviteUserRepositoryModule, CommunityModule],
|
||||
|
@ -15,7 +15,7 @@ import { SpaceRepository } from '@app/common/modules/space';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities/space.entity';
|
||||
import { UserEntity } from '@app/common/modules/user/entities';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import {
|
||||
BadRequestException,
|
||||
HttpException,
|
||||
|
@ -28,7 +28,7 @@ import { PasswordType } from '@app/common/constants/password-type.enum';
|
||||
import { VisitorPasswordEnum } from '@app/common/constants/visitor-password.enum';
|
||||
import { SuccessResponseDto } from '@app/common/dto/success.response.dto';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { DoorLockService } from 'src/door-lock/services';
|
||||
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
||||
|
@ -1,39 +1,39 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { VisitorPasswordService } from './services/visitor-password.service';
|
||||
import { VisitorPasswordController } from './controllers/visitor-password.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import { EmailService } from '@app/common/util/email.service';
|
||||
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
||||
import { DoorLockModule } from 'src/door-lock/door.lock.module';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { DeviceStatusFirebaseService } from '@app/common/firebase/devices-status/services/devices-status.service';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/repositories';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { TuyaService } from '@app/common/integrations/tuya/services/tuya.service';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { DeviceRepositoryModule } from '@app/common/modules/device';
|
||||
import { DeviceStatusLogRepository } from '@app/common/modules/device-status-log/repositories';
|
||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||
import {
|
||||
PowerClampDailyRepository,
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||
import { ProductRepository } from '@app/common/modules/product/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import {
|
||||
SceneIconRepository,
|
||||
SceneRepository,
|
||||
} from '@app/common/modules/scene/repositories';
|
||||
import { SceneDeviceRepository } from '@app/common/modules/scene-device/repositories';
|
||||
import { AutomationRepository } from '@app/common/modules/automation/repositories';
|
||||
import { ProjectRepository } from '@app/common/modules/project/repositiories';
|
||||
import { PowerClampService } from '@app/common/helper/services/power.clamp.service';
|
||||
import {
|
||||
PowerClampHourlyRepository,
|
||||
PowerClampDailyRepository,
|
||||
PowerClampMonthlyRepository,
|
||||
} from '@app/common/modules/power-clamp/repositories';
|
||||
import { SqlLoaderService } from '@app/common/helper/services/sql-loader.service';
|
||||
import { OccupancyService } from '@app/common/helper/services/occupancy.service';
|
||||
import { CommunityRepository } from '@app/common/modules/community/repositories';
|
||||
import { AqiDataService } from '@app/common/helper/services/aqi.data.service';
|
||||
import { PresenceSensorDailySpaceRepository } from '@app/common/modules/presence-sensor/repositories';
|
||||
import { AqiSpaceDailyPollutantStatsRepository } from '@app/common/modules/aqi/repositories';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { VisitorPasswordRepository } from '@app/common/modules/visitor-password/repositories';
|
||||
import { EmailService } from '@app/common/util/email/email.service';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { DeviceService } from 'src/device/services';
|
||||
import { DoorLockModule } from 'src/door-lock/door.lock.module';
|
||||
import { PasswordEncryptionService } from 'src/door-lock/services/encryption.services';
|
||||
import { SceneService } from 'src/scene/services';
|
||||
import { VisitorPasswordController } from './controllers/visitor-password.controller';
|
||||
import { VisitorPasswordService } from './services/visitor-password.service';
|
||||
@Module({
|
||||
imports: [ConfigModule, DeviceRepositoryModule, DoorLockModule],
|
||||
controllers: [VisitorPasswordController],
|
||||
|
Reference in New Issue
Block a user