events-service.service.ts151 lines, 3.8 KB
| 1 | import { CreateEventDto, UpdateEventDto } from '@app/common'; |
| 2 | import { DatabaseService, events } from '@app/database'; |
| 3 | import { KAFKA_SERVICE, KAFKA_TOPICS } from '@app/kafka'; |
| 4 | import { |
| 5 | ForbiddenException, |
| 6 | Inject, |
| 7 | Injectable, |
| 8 | NotFoundException, |
| 9 | OnModuleInit, |
| 10 | } from '@nestjs/common'; |
| 11 | import { ClientKafka } from '@nestjs/microservices'; |
| 12 | import { eq } from 'drizzle-orm'; |
| 13 | |
| 14 | @Injectable() |
| 15 | export class EventsServiceService implements OnModuleInit { |
| 16 | constructor( |
| 17 | @Inject(KAFKA_SERVICE) private readonly kafkClient: ClientKafka, |
| 18 | private readonly dbService: DatabaseService, |
| 19 | ) {} |
| 20 | |
| 21 | async onModuleInit() { |
| 22 | // connect to kafka when module initializes |
| 23 | await this.kafkClient.connect(); |
| 24 | } |
| 25 | |
| 26 | async create(createEventDto: CreateEventDto, organizerId: string) { |
| 27 | const [event] = await this.dbService.db |
| 28 | .insert(events) |
| 29 | .values({ |
| 30 | ...createEventDto, |
| 31 | date: new Date(createEventDto.date), |
| 32 | price: createEventDto.price || 0, |
| 33 | organizerId, |
| 34 | }) |
| 35 | .returning(); |
| 36 | |
| 37 | this.kafkClient.emit(KAFKA_TOPICS.EVENT_CREATED, { |
| 38 | eventId: event.id, |
| 39 | organizerId: event.organizerId, |
| 40 | title: event.title, |
| 41 | timestamp: new Date().toISOString(), |
| 42 | }); |
| 43 | |
| 44 | return event; |
| 45 | } |
| 46 | |
| 47 | async findAll() { |
| 48 | return this.dbService.db |
| 49 | .select() |
| 50 | .from(events) |
| 51 | .where(eq(events.status, 'PUBLISHED')); |
| 52 | } |
| 53 | |
| 54 | async findOne(id: string) { |
| 55 | const [event] = await this.dbService.db |
| 56 | .select() |
| 57 | .from(events) |
| 58 | .where(eq(events.id, id)) |
| 59 | .limit(1); |
| 60 | |
| 61 | if (!event) { |
| 62 | throw new NotFoundException('Event not found'); |
| 63 | } |
| 64 | |
| 65 | return event; |
| 66 | } |
| 67 | |
| 68 | async update( |
| 69 | id: string, |
| 70 | updateEventDto: UpdateEventDto, |
| 71 | userId: string, |
| 72 | userRole: string, |
| 73 | ) { |
| 74 | const event = await this.findOne(id); |
| 75 | |
| 76 | if (event.organizerId !== userId && userRole !== 'ADMIN') { |
| 77 | throw new ForbiddenException( |
| 78 | 'You are not authorized to update this event', |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | const updatedData: Record<string, unknown> = { ...updateEventDto }; |
| 83 | if (updateEventDto.date) { |
| 84 | updatedData.date = new Date(updateEventDto.date); |
| 85 | } |
| 86 | updatedData.updatedAt = new Date(); |
| 87 | |
| 88 | const [updated] = await this.dbService.db |
| 89 | .update(events) |
| 90 | .set(updatedData) |
| 91 | .where(eq(events.id, id)) |
| 92 | .returning(); |
| 93 | |
| 94 | this.kafkClient.emit(KAFKA_TOPICS.EVENT_UPDATED, { |
| 95 | eventId: updated.id, |
| 96 | changes: Object.keys(updateEventDto), |
| 97 | timestamp: new Date().toISOString(), |
| 98 | }); |
| 99 | |
| 100 | return updated; |
| 101 | } |
| 102 | |
| 103 | async publish(id: string, userId: string, userRole: string) { |
| 104 | const event = await this.findOne(id); |
| 105 | |
| 106 | if (event.organizerId !== userId && userRole !== 'ADMIN') { |
| 107 | throw new ForbiddenException( |
| 108 | 'You are not authorized to publish this event', |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | const [published] = await this.dbService.db |
| 113 | .update(events) |
| 114 | .set({ status: 'PUBLISHED', updatedAt: new Date() }) |
| 115 | .where(eq(events.id, id)) |
| 116 | .returning(); |
| 117 | |
| 118 | return published; |
| 119 | } |
| 120 | |
| 121 | async cancel(id: string, userId: string, userRole: string) { |
| 122 | const event = await this.findOne(id); |
| 123 | |
| 124 | if (event.organizerId !== userId && userRole !== 'ADMIN') { |
| 125 | throw new ForbiddenException( |
| 126 | 'You are not authorized to cancel this event', |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | const [cancelled] = await this.dbService.db |
| 131 | .update(events) |
| 132 | .set({ status: 'CANCELLED', updatedAt: new Date() }) |
| 133 | .where(eq(events.id, id)) |
| 134 | .returning(); |
| 135 | |
| 136 | this.kafkClient.emit(KAFKA_TOPICS.EVENT_CANCELLED, { |
| 137 | eventId: cancelled.id, |
| 138 | organizerId: cancelled.organizerId, |
| 139 | timestamp: new Date().toISOString(), |
| 140 | }); |
| 141 | |
| 142 | return cancelled; |
| 143 | } |
| 144 | |
| 145 | async findMyEvent(organizerId: string) { |
| 146 | return this.dbService.db |
| 147 | .select() |
| 148 | .from(events) |
| 149 | .where(eq(events.organizerId, organizerId)); |
| 150 | } |
| 151 | } |