Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 17x 17x 17x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { Injectable, NotFoundException } from '@nestjs/common'
import { CreateEventDto, UpdateEventDto } from '@fundraising/types'
import { PrismaService } from '../../database/prisma.service'
import { WhiteLabelingService } from '../white-labeling/white-labeling.service'
@Injectable()
export class EventsService {
constructor(
private prisma: PrismaService,
private whiteLabelingService: WhiteLabelingService,
) {}
private readonly defaultSelect = {
id: true,
slug: true,
name: true,
goalAmount: true,
date: true,
description: true,
status: true,
createdAt: true,
updatedAt: true,
}
async create(createEventDto: CreateEventDto) {
return this.prisma.event.create({
data: {
slug: createEventDto.slug,
name: createEventDto.name,
goalAmount: createEventDto.goalAmount,
date: createEventDto.date ? new Date(createEventDto.date) : new Date(),
description: createEventDto.description,
status: createEventDto.status || 'active',
},
select: this.defaultSelect,
})
}
async findAll() {
const events = await this.prisma.event.findMany({
select: this.defaultSelect,
})
// Aggregate donations (SUCCEEDED only)
const aggregations = await this.prisma.donation.groupBy({
by: ['eventId'],
_sum: { amount: true },
_count: { id: true },
where: { status: 'COMPLETED' },
})
return events.map((event) => {
const stats = aggregations.find((a) => a.eventId === event.id)
return {
...event,
raised: (stats?._sum.amount?.toNumber() || 0) / 100,
donorCount: stats?._count.id || 0,
}
})
}
async findPublic() {
const events = await this.prisma.event.findMany({
where: {
status: { in: ['active', 'ACTIVE'] }, // Handle case sensitivity if needed, though status is usually lowercase in Schema? Schema says String.
},
select: this.defaultSelect,
})
// Aggregate donations for public events
const aggregations = await this.prisma.donation.groupBy({
by: ['eventId'],
_sum: { amount: true },
_count: { id: true },
where: {
status: 'COMPLETED',
eventId: { in: events.map((e) => e.id) },
},
})
return events.map((event) => {
const stats = aggregations.find((a) => a.eventId === event.id)
return {
...event,
raised: (stats?._sum.amount?.toNumber() || 0) / 100,
donorCount: stats?._count.id || 0,
}
})
}
async findOne(slugOrId: string) {
const event = await this.prisma.event.findFirst({
where: {
OR: [{ id: slugOrId }, { slug: slugOrId }],
},
select: this.defaultSelect,
})
if (!event) throw new NotFoundException('Event not found')
// Aggregate donations (COMPLETED)
const stats = await this.prisma.donation.aggregate({
_sum: { amount: true },
_count: { id: true },
// Support both usages if legacy data exists, but primarily COMPLETED
where: { eventId: event.id, status: 'COMPLETED' },
})
// Fetch recent donations
const recentDonations = await this.prisma.donation.findMany({
where: { eventId: event.id, status: 'COMPLETED' },
orderBy: { createdAt: 'desc' },
take: 10,
select: {
id: true,
amount: true,
currency: true,
donorName: true,
message: true,
isAnonymous: true,
createdAt: true,
},
})
return {
...event,
raised: (stats._sum.amount?.toNumber() || 0) / 100,
donorCount: stats._count.id || 0,
donations: recentDonations.map((d) => ({
...d,
amount: d.amount.toNumber(),
})),
}
}
async update(id: string, updateEventDto: UpdateEventDto) {
// Check for formConfig in the payload (extra field from frontend)
const formConfig = updateEventDto.formConfig
Eif (formConfig) {
await this.whiteLabelingService.updateEventSettings(id, {
donation: { form: formConfig } as any,
})
}
return this.prisma.event.update({
where: { id },
data: {
...(updateEventDto.name && { name: updateEventDto.name }),
...(updateEventDto.slug && { slug: updateEventDto.slug }),
...(updateEventDto.goalAmount && {
goalAmount: updateEventDto.goalAmount,
}),
...(updateEventDto.date && { date: new Date(updateEventDto.date) }),
...(updateEventDto.description && {
description: updateEventDto.description,
}),
...(updateEventDto.status && { status: updateEventDto.status }),
},
select: this.defaultSelect,
})
}
async remove(id: string) {
return this.prisma.event.delete({
where: { id },
select: this.defaultSelect,
})
}
async findStaff(eventId: string) {
const event = await this.prisma.event.findUnique({
where: { id: eventId },
include: { staffMembers: { orderBy: { name: 'asc' } } },
})
return event?.staffMembers || []
}
async assignStaff(eventId: string, staffId: string) {
return this.prisma.event.update({
where: { id: eventId },
data: {
staffMembers: { connect: { id: staffId } },
},
})
}
async unassignStaff(eventId: string, memberId: string) {
return this.prisma.event.update({
where: { id: eventId },
data: {
staffMembers: { disconnect: { id: memberId } },
},
})
}
}
|