add server components for event_exceptions

This commit is contained in:
Roland Schneider 2025-11-20 16:01:22 +01:00
parent 4699f16b71
commit c28431e80c
10 changed files with 301 additions and 10 deletions

View File

@ -19,6 +19,8 @@ import { UserGroupsModule } from './user-group/user-group.module';
import { UserRolesModule } from './user-role/user-role.module'; import { UserRolesModule } from './user-role/user-role.module';
import { RecurrenceRule } from './entity/recurrence-rule.entity'; import { RecurrenceRule } from './entity/recurrence-rule.entity';
import { RecurrenceRulesModule } from './recurrence-rule/recurrence-rules.module'; import { RecurrenceRulesModule } from './recurrence-rule/recurrence-rules.module';
import { EventException } from "./entity/event-exception.entity";
import { EventExceptionsModule } from "./event-exception/event-exceptions.module";
const moduleTypeOrm = TypeOrmModule.forRootAsync({ const moduleTypeOrm = TypeOrmModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule],
@ -39,7 +41,8 @@ const moduleTypeOrm = TypeOrmModule.forRootAsync({
Product, Product,
Event, Event,
RecurrenceRule, RecurrenceRule,
], EventException
],
logging: true, logging: true,
// synchronize: true, // synchronize: true,
}; };
@ -59,7 +62,8 @@ const moduleTypeOrm = TypeOrmModule.forRootAsync({
UserGroupsModule, UserGroupsModule,
UserRolesModule, UserRolesModule,
RecurrenceRulesModule, RecurrenceRulesModule,
], EventExceptionsModule
],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],
}) })

View File

@ -0,0 +1,52 @@
// dvbooking-cli/src/templates/nestjs/entity.ts.tpl
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import {
IsString,
IsNumber,
IsBoolean,
IsDate,
IsOptional,
} from 'class-validator';
@Entity({ name: 'event_exceptions' })
export class EventException {
@PrimaryGeneratedColumn()
id: number;
@Column()
@IsNumber()
event_id: number;
@Column()
@IsDate()
original_start_time: Date;
@Column({ default: false })
@IsBoolean()
is_cancelled: boolean = false;
@Column({ type: 'timestamp with time zone', nullable: true })
@IsOptional()
@IsDate()
new_start_time: Date | null;
@Column({ type: 'timestamp with time zone', nullable: true })
@IsOptional()
@IsDate()
new_end_time: Date | null;
@Column({ type: 'character varying', nullable: true })
@IsOptional()
@IsString()
title: string | null;
@Column({ type: 'text', nullable: true })
@IsOptional()
@IsString()
description: string | null;
@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
@IsDate()
created_at: Date;
}

View File

@ -1,11 +1,13 @@
// dvbooking-cli/src/templates/nestjs/entity.ts.tpl // dvbooking-cli/src/templates/nestjs/entity.ts.tpl
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import { IsString, IsNumber, IsBoolean, IsDate, IsOptional } from 'class-validator'; import {
IsString,
IsOptional,
} from 'class-validator';
@Entity({ name: 'event_type' }) @Entity({ name: 'event_type' })
export class EventType { export class EventType {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -22,5 +24,4 @@ export class EventType {
@IsOptional() @IsOptional()
@IsString() @IsString()
color: string | null; color: string | null;
}
}

View File

@ -1,11 +1,10 @@
// dvbooking-cli/src/templates/nestjs/entity.ts.tpl // dvbooking-cli/src/templates/nestjs/entity.ts.tpl
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import { IsString, IsNumber, IsBoolean, IsDate, IsOptional } from 'class-validator'; import { IsString, IsNumber, IsBoolean, IsOptional } from 'class-validator';
@Entity({ name: 'products' }) @Entity({ name: 'products' })
export class Product { export class Product {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -22,5 +21,4 @@ export class Product {
@IsOptional() @IsOptional()
@IsBoolean() @IsBoolean()
is_available: boolean | null = true; is_available: boolean | null = true;
}
}

View File

@ -0,0 +1,4 @@
import { OmitType } from '@nestjs/mapped-types';
import { EventException } from '../../entity/event-exception.entity';
export class CreateEventExceptionDto extends OmitType(EventException, ['id']) {}

View File

@ -0,0 +1,9 @@
import { IsOptional, IsString, IsNumber, IsIn, IsBoolean } from 'class-validator';
import { Type } from 'class-transformer';
export class QueryEventExceptionDto {
@IsOptional() @Type(() => Number) @IsNumber() page?: number;
@IsOptional() @Type(() => Number) @IsNumber() limit?: number;
@IsOptional() @IsString() sortBy?: string;
@IsOptional() @IsIn(['ASC', 'DESC']) order?: 'ASC' | 'DESC';
}

View File

@ -0,0 +1,6 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateEventExceptionDto } from './create-event-exception.dto';
export class UpdateEventExceptionDto extends PartialType(
CreateEventExceptionDto,
) {}

View File

@ -0,0 +1,68 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
Query,
ParseIntPipe,
DefaultValuePipe,
UseGuards,
} from '@nestjs/common';
import { EventExceptionsService } from './event-exceptions.service';
import { CreateEventExceptionDto } from './dto/create-event-exception.dto';
import { UpdateEventExceptionDto } from './dto/update-event-exception.dto';
import { QueryEventExceptionDto } from './dto/query-event-exception.dto';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { Roles } from '../auth/roles.decorator';
import { Role } from '../auth/role.enum';
import { RolesGuard } from '../auth/roles.guard';
@Controller('event-exceptions')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(Role.Admin)
export class EventExceptionsController {
constructor(
private readonly eventExceptionsService: EventExceptionsService,
) {}
@Post()
create(@Body() createEventExceptionDto: CreateEventExceptionDto) {
return this.eventExceptionsService.create(createEventExceptionDto);
}
@Get()
findAll(@Query() queryParams: QueryEventExceptionDto) {
return this.eventExceptionsService.findAll(queryParams);
}
@Get('search')
search(
@Query('q') term: string,
@Query('page', new DefaultValuePipe(1), ParseIntPipe) page: number,
@Query('limit', new DefaultValuePipe(10), ParseIntPipe) limit: number,
) {
return this.eventExceptionsService.search(term, { page, limit });
}
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number) {
return this.eventExceptionsService.findOne(id);
}
@Patch(':id')
update(
@Param('id', ParseIntPipe) id: number,
@Body() updateEventExceptionDto: UpdateEventExceptionDto,
) {
return this.eventExceptionsService.update(id, updateEventExceptionDto);
}
@Delete(':id')
remove(@Param('id', ParseIntPipe) id: number) {
return this.eventExceptionsService.remove(id);
}
}

View File

@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { EventExceptionsService } from './event-exceptions.service';
import { EventExceptionsController } from './event-exceptions.controller';
import { EventException } from '../entity/event-exception.entity';
@Module({
imports: [TypeOrmModule.forFeature([EventException])],
controllers: [EventExceptionsController],
providers: [EventExceptionsService],
})
export class EventExceptionsModule {}

View File

@ -0,0 +1,137 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, FindManyOptions, FindOptionsWhere, ILike } from 'typeorm';
import { CreateEventExceptionDto } from './dto/create-event-exception.dto';
import { UpdateEventExceptionDto } from './dto/update-event-exception.dto';
import { QueryEventExceptionDto } from './dto/query-event-exception.dto';
import { EventException } from '../entity/event-exception.entity';
type QueryConfigItem = {
param: keyof Omit<
QueryEventExceptionDto,
'page' | 'limit' | 'sortBy' | 'order'
>;
dbField: keyof EventException;
operator: 'equals' | 'like';
};
@Injectable()
export class EventExceptionsService {
constructor(
@InjectRepository(EventException)
private readonly eventExceptionRepository: Repository<EventException>,
) {}
private readonly searchableFields: (keyof EventException)[] = [
'title',
'description',
];
create(createEventExceptionDto: CreateEventExceptionDto) {
const newRecord = this.eventExceptionRepository.create(
createEventExceptionDto,
);
return this.eventExceptionRepository.save(newRecord);
}
async findAll(queryParams: QueryEventExceptionDto) {
const { page = 1, limit = 0, sortBy, order, ...filters } = queryParams;
const queryConfig: QueryConfigItem[] = [];
const whereClause: { [key: string]: any } = {};
for (const config of queryConfig) {
if (filters[config.param] !== undefined) {
if (config.operator === 'like') {
whereClause[config.dbField] = ILike(`%${filters[config.param]}%`);
} else {
whereClause[config.dbField] = filters[config.param];
}
}
}
const findOptions: FindManyOptions<EventException> = {
where: whereClause as FindOptionsWhere<EventException>,
};
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.eventExceptionRepository.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,
},
};
}
async search(term: string, options: { page: number; limit: number }) {
if (this.searchableFields.length === 0) {
console.warn('Search is not configured for this entity.');
return {
data: [],
meta: {
totalItems: 0,
itemCount: 0,
itemsPerPage: options.limit,
totalPages: 0,
currentPage: options.page,
},
};
}
const whereConditions = this.searchableFields.map((field) => ({
[field]: ILike(`%${term}%`),
}));
const [data, totalItems] = await this.eventExceptionRepository.findAndCount(
{
where: whereConditions,
skip: (options.page - 1) * options.limit,
take: options.limit,
},
);
return {
data,
meta: {
totalItems,
itemCount: data.length,
itemsPerPage: options.limit,
totalPages: Math.ceil(totalItems / options.limit),
currentPage: options.page,
},
};
}
async findOne(id: number) {
const record = await this.eventExceptionRepository.findOneBy({
id: id,
});
if (!record) {
throw new NotFoundException(`EventException with ID ${id} not found`);
}
return record;
}
async update(id: number, updateEventExceptionDto: UpdateEventExceptionDto) {
const record = await this.findOne(id);
Object.assign(record, updateEventExceptionDto);
return this.eventExceptionRepository.save(record);
}
async remove(id: number) {
const result = await this.eventExceptionRepository.delete(id);
if (result.affected === 0) {
throw new NotFoundException(`EventException with ID ${id} not found`);
}
return { deleted: true, id };
}
}