implement getbookings for calendar
This commit is contained in:
@@ -4,7 +4,14 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, Between, IsNull, DataSource, In } from 'typeorm';
|
||||
import {
|
||||
Repository,
|
||||
Between,
|
||||
IsNull,
|
||||
DataSource,
|
||||
In,
|
||||
FindManyOptions,
|
||||
} from 'typeorm';
|
||||
import { RRule } from 'rrule'; // Corrected import
|
||||
|
||||
import { Event } from '../entity/event.entity';
|
||||
@@ -16,6 +23,7 @@ import { Booking } from '../entity/booking.entity';
|
||||
import { CancelBookingDto } from './dto/cancel-booking.dto';
|
||||
import { CalendarCreateBookingDto } from './dto/create-booking.dto';
|
||||
import { EventType } from '../entity/event-type.entity';
|
||||
import { CalendarGetBookingDto } from './dto/calendar.get.booking.dto';
|
||||
|
||||
// --- Type-Safe Maps ---
|
||||
const frequencyMap: Record<string, RRule.Frequency> = {
|
||||
@@ -593,10 +601,35 @@ export class CalendarService {
|
||||
async getBookings(
|
||||
userId: number,
|
||||
eventId: number,
|
||||
startTime: Date,
|
||||
queryParams: CalendarGetBookingDto,
|
||||
): Promise<Booking[]> {
|
||||
console.info('getBookings', userId, eventId, startTime);
|
||||
console.info('getBookings', userId, eventId);
|
||||
await Promise.resolve();
|
||||
const { page = 1, limit = 0, sortBy, order } = queryParams;
|
||||
const findOptions: FindManyOptions<Booking> = {};
|
||||
const paginated = limit > 0;
|
||||
if (paginated) {
|
||||
findOptions.skip = (page - 1) * limit;
|
||||
findOptions.take = limit;
|
||||
}
|
||||
if (sortBy && order) {
|
||||
findOptions.order = { [sortBy]: order };
|
||||
}
|
||||
const [data, totalItems] =
|
||||
await this.bookingRepository.findAndCount(findOptions);
|
||||
if (!paginated) {
|
||||
return { data, total: data.length };
|
||||
}
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
totalItems,
|
||||
itemCount: data.length,
|
||||
itemsPerPage: limit,
|
||||
totalPages: Math.ceil(totalItems / limit),
|
||||
currentPage: page,
|
||||
},
|
||||
};
|
||||
// const booking = await this.bookingRepository.findOneBy({ id: bookingId });
|
||||
// if (!booking) {
|
||||
// throw new NotFoundException(`Booking with ID ${bookingId} not found.`);
|
||||
|
||||
16
server/src/calendar/dto/calendar.get.booking.dto.ts
Normal file
16
server/src/calendar/dto/calendar.get.booking.dto.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import {
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsNumber,
|
||||
IsIn,
|
||||
IsBoolean,
|
||||
} from 'class-validator';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CalendarGetBookingDto {
|
||||
@IsOptional() @Type(() => Number) @IsNumber() page?: number;
|
||||
@IsOptional() @Type(() => Number) @IsNumber() limit?: number;
|
||||
@IsOptional() @IsString() sortBy?: string;
|
||||
@IsOptional() @IsIn(['ASC', 'DESC']) order?: 'ASC' | 'DESC';
|
||||
@IsString() startTime: string;
|
||||
}
|
||||
@@ -18,3 +18,15 @@ export interface UserPayload {
|
||||
username: string;
|
||||
roles: string[];
|
||||
}
|
||||
export interface PaginatedResponse<T> {
|
||||
data: T[];
|
||||
meta: PaginatedResponseMeta;
|
||||
}
|
||||
|
||||
export interface PaginatedResponseMeta {
|
||||
totalItems: number;
|
||||
itemCount: number;
|
||||
itemsPerPage: number;
|
||||
totalPages: number;
|
||||
currentPage: number;
|
||||
}
|
||||
|
||||
39
server/src/util.ts
Normal file
39
server/src/util.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { PaginatedResponse } from './types';
|
||||
import { FindManyOptions, FindOptions, FindOptionsOrder } from 'typeorm';
|
||||
|
||||
export const createPaginatedResponse = <T>(
|
||||
data: T[],
|
||||
totalItems: number,
|
||||
limit: number,
|
||||
page: number,
|
||||
) => {
|
||||
return {
|
||||
data,
|
||||
meta: {
|
||||
totalItems,
|
||||
itemCount: data.length,
|
||||
itemsPerPage: limit,
|
||||
totalPages: Math.ceil(totalItems / limit),
|
||||
currentPage: page,
|
||||
},
|
||||
} as PaginatedResponse<T>;
|
||||
};
|
||||
|
||||
export const createFindOptions = <T>(
|
||||
page: number,
|
||||
limit: number,
|
||||
sortBy?: string,
|
||||
order?: 'ASC' | 'DESC',
|
||||
): FindManyOptions<T> => {
|
||||
const findOptions: FindManyOptions<T> = {};
|
||||
|
||||
const paginated = limit > 0;
|
||||
if (paginated) {
|
||||
findOptions.skip = (page - 1) * limit;
|
||||
findOptions.take = limit;
|
||||
}
|
||||
if (sortBy && order) {
|
||||
findOptions.order = { [sortBy]: order } as FindOptionsOrder<T>;
|
||||
}
|
||||
return findOptions;
|
||||
};
|
||||
Reference in New Issue
Block a user