Skip to content

Commit

Permalink
Merge pull request #63 from VentionCapstone/feat/131_booking-status-u…
Browse files Browse the repository at this point in the history
…pdater-cron

feat: booking end checker cron
  • Loading branch information
mirsadikov authored Jan 26, 2024
2 parents 63a041c + 7c238a1 commit 2ed0c2c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@nestjs/mapped-types": "*",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.2.10",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.16",
"@nestjs/terminus": "^10.2.0",
"@prisma/client": "^5.6.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ScheduleModule } from '@nestjs/schedule';
import { AcceptLanguageResolver, I18nModule, QueryResolver } from 'nestjs-i18n';
import * as path from 'path';
import { AccommodationModule } from './accommodation/accommodation.module';
Expand Down Expand Up @@ -35,6 +36,7 @@ import { WishlistModule } from './wishlist/wishlist.module';
AcceptLanguageResolver,
],
}),
ScheduleModule.forRoot(),
AuthModule,
UserModule,
AmenitiesModule,
Expand Down
63 changes: 63 additions & 0 deletions src/booking/booking.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { BadRequestException, HttpException, Injectable, NotFoundException } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { Status } from '@prisma/client';
import * as dayjs from 'dayjs';
import { Dayjs } from 'dayjs';
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore';
import * as utc from 'dayjs/plugin/utc';
import { PaginationDto } from 'src/accommodation/dto/pagination.dto';
import { STANDARD_CHECKOUT_HOUR } from 'src/common/constants/booking';
import { DEFAULT_DATE_FORMAT } from 'src/common/constants/date';
import { AuthUser } from 'src/common/types/AuthUser.type';
import ErrorsTypes from 'src/errors/errors.enum';
Expand Down Expand Up @@ -266,4 +268,65 @@ export class BookingService {
private getTimeInZone(date: Date, offset: number) {
return dayjs(date).utcOffset(-offset).add(offset, 'minutes');
}

@Cron(CronExpression.EVERY_HOUR)
async checkBookingEnd() {
const serverUtcOffset = dayjs().utcOffset();
const tomorrow = dayjs()
.add(1, 'day')
.startOf('day')
.add(serverUtcOffset, 'minutes')
.toISOString();

const bookings = await this.prismaService.booking.findMany({
select: {
id: true,
endDate: true,
accommodation: {
select: {
timezoneOffset: true,
},
},
},
where: {
status: Status.ACTIVE,
endDate: {
lte: tomorrow,
},
},
});

const completedBookingsIds = [];
const now = dayjs();

for (const booking of bookings) {
const {
id,
endDate,
accommodation: { timezoneOffset },
} = booking;

const bookingEnd = this.getTimeInZone(endDate, timezoneOffset).add(
STANDARD_CHECKOUT_HOUR,
'hours'
);

if (bookingEnd.isBefore(now)) {
completedBookingsIds.push(id);
}
}

if (completedBookingsIds.length > 0) {
await this.prismaService.booking.updateMany({
where: {
id: {
in: completedBookingsIds,
},
},
data: {
status: Status.COMPLETED,
},
});
}
}
}
1 change: 1 addition & 0 deletions src/common/constants/booking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const STANDARD_CHECKOUT_HOUR = 11;

0 comments on commit 2ed0c2c

Please sign in to comment.