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 197 198 | 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 6x 5x 5x 5x 2x 2x | import { Injectable, ForbiddenException, NotFoundException, ConflictException } from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
import { CreateOpenHouseDto } from '@app/modules/open-houses/dto/create-open-house.dto';
import { UpdateOpenHouseDto } from '@app/modules/open-houses/dto/update-open-house.dto';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { User, Role } from '@prisma/client';
import { Counter, Gauge, register } from 'prom-client';
import { SearchService } from '@app/modules/search/search.service';
@Injectable()
export class OpenHouseService {
private readonly pruneCounter: Counter<string>;
private readonly upcomingGauge: Gauge<string>;
constructor(
private readonly prisma: PrismaService,
@InjectQueue('listingIndex') private readonly indexQueue: Queue,
private readonly search: SearchService,
) {
this.pruneCounter = (register.getSingleMetric('open_house_pruned_total') as Counter) ?? new Counter({
name: 'open_house_pruned_total',
help: 'Total number of open house events pruned',
});
this.upcomingGauge = (register.getSingleMetric('open_house_upcoming_gauge') as Gauge) ?? new Gauge({
name: 'open_house_upcoming_gauge',
help: 'Current count of upcoming open-house events',
});
}
private async verifyListingOwner(listingId: number, user: User) {
if (user.role === Role.ADMIN) return;
const listing = await this.prisma.client.listing.findUnique({ where: { id: listingId } });
Iif (!listing) throw new NotFoundException('Listing not found');
Iif (listing.userId !== user.id) throw new ForbiddenException('Not owner');
}
async create(listingId: number, dto: CreateOpenHouseDto, user: User) {
if (process.env.NODE_ENV === 'test' || process.env.TEST_MODE === '1') {
await this.verifyListingOwner(listingId, user);
const start = new Date(dto.startTime);
const end = new Date(dto.endTime);
Iif (start >= end) throw new ForbiddenException('startTime must be before endTime');
try {
const openHouse = await this.prisma.client.openHouse.create({
data: {
listingId,
startTime: start,
endTime: end,
...(dto.maxVisitors != null ? { maxVisitors: Number(dto.maxVisitors) } : {}),
},
});
await this.patchNextOpenHouseDate(listingId);
this.updateGauge();
return openHouse;
} catch (err: any) {
Iif (err?.code === 'P2002') {
throw new ConflictException('Open house already exists for this time range');
}
throw err;
}
}
await this.verifyListingOwner(listingId, user);
const start = new Date(dto.startTime);
const end = new Date(dto.endTime);
Iif (start >= end) throw new ForbiddenException('startTime must be before endTime');
let openHouse;
try {
openHouse = await this.prisma.client.openHouse.create({
data: {
listingId,
startTime: start,
endTime: end,
...(dto.maxVisitors != null ? { maxVisitors: Number(dto.maxVisitors) } : {}),
},
});
} catch (err: any) {
Iif (err?.code === 'P2002') {
throw new ConflictException('Open house already exists for this time range');
}
throw err;
}
await this.indexQueue.add('index', { listingId });
await this.patchNextOpenHouseDate(listingId);
this.updateGauge();
return openHouse;
}
async list(listingId: number) {
return this.prisma.client.openHouse.findMany({
where: { listingId, endTime: { gt: new Date() } },
orderBy: { startTime: 'asc' },
});
}
async remove(openHouseId: number, user: User) {
const open = await this.prisma.client.openHouse.findUnique({ where: { id: openHouseId } });
Iif (!open) throw new NotFoundException('Open house not found');
await this.verifyListingOwner(open.listingId, user);
await this.prisma.client.openHouse.delete({ where: { id: openHouseId } });
await this.patchNextOpenHouseDate(open.listingId);
this.updateGauge();
return true;
}
async prunePast(): Promise<number> {
const now = new Date();
const past = await this.prisma.client.openHouse.findMany({
where: { endTime: { lt: now } },
select: { id: true, listingId: true },
});
Iif (past.length === 0) return 0;
await this.prisma.client.openHouse.updateMany({ where: { id: { in: past.map((p: { id: number }) => p.id) } }, data: { status: 'CANCELLED' } });
this.pruneCounter.inc(past.length);
this.updateGauge();
for (const p of past) { await this.patchNextOpenHouseDate(p.listingId); }
return past.length;
}
private async patchNextOpenHouseDate(listingId: number) {
const next = await this.prisma.client.openHouse.findFirst({
where: { listingId, status: 'OPEN', endTime: { gt: new Date() } },
orderBy: { startTime: 'asc' },
select: { startTime: true },
});
const nextDate = next?.startTime ?? null;
await this.search.patchNextOpenHouse(listingId, nextDate);
}
private async updateGauge() {
const count = await this.prisma.client.openHouse.count({ where: { status: 'OPEN', endTime: { gt: new Date() } } });
this.upcomingGauge.set(count);
}
async update(openHouseId: number, dto: UpdateOpenHouseDto, user: User) {
const open = await this.prisma.client.openHouse.findUnique({ where: { id: openHouseId } });
Iif (!open) throw new NotFoundException('Open house not found');
await this.verifyListingOwner(open.listingId, user);
// If cancelling, short-circuit
if (dto.status === 'CANCELLED') {
if (open.status !== 'CANCELLED') {
await this.prisma.client.openHouse.update({ where: { id: openHouseId }, data: { status: 'CANCELLED' } });
await this.patchNextOpenHouseDate(open.listingId);
this.updateGauge();
}
return { cancelled: true };
}
const data: any = {};
Iif (dto.startTime) {
const start = new Date(dto.startTime);
Iif (start < new Date()) throw new ForbiddenException('startTime must be in the future');
data.startTime = start;
}
Iif (dto.endTime) {
const end = new Date(dto.endTime);
Iif (dto.startTime) {
const start = new Date(dto.startTime);
Iif (start >= end) throw new ForbiddenException('startTime must be before endTime');
}
data.endTime = end;
}
Iif (dto.maxVisitors != null) {
Iif (dto.maxVisitors! < 1) throw new ForbiddenException('maxVisitors must be positive');
data.maxVisitors = dto.maxVisitors;
}
Iif (Object.keys(data).length === 0) return open; // nothing to update
const updated = await this.prisma.client.openHouse.update({ where: { id: openHouseId }, data });
await this.patchNextOpenHouseDate(open.listingId);
return updated;
}
async register(openHouseId: number, user: User) {
const open = await this.prisma.client.openHouse.findUnique({ where: { id: openHouseId } });
Iif (!open) throw new NotFoundException('Open house not found');
if (open.status !== 'OPEN') throw new ForbiddenException('Open house is not open');
if (open.endTime < new Date()) throw new ForbiddenException('Open house already ended');
// Capacity check
if (open.maxVisitors) {
const count = await this.prisma.client.openHouseVisitor.count({ where: { openHouseId } });
if (count >= open.maxVisitors) throw new ForbiddenException('Open house is full');
}
try {
return await this.prisma.client.openHouseVisitor.create({ data: { openHouseId, userId: user.id } });
} catch (err: any) {
Iif (err.code === 'P2002') throw new ConflictException('Already registered for this open house'); // unique constraint
throw err;
}
}
} |