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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 4x 11x 11x 9x 7x 11x 5x 5x 5x 5x 5x 11x 2x 2x 2x 2x 2x 2x 11x 11x 4x 11x 11x 11x 11x 2x 11x 11x 11x 11x 1x 11x 1x 11x 11x 11x 11x 1x 11x 1x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import { Controller, Get, Post, Body, Patch, Param, Delete, Query, UseGuards, Req, ParseIntPipe, HttpCode } from '@nestjs/common';
import { ListingsService } from '@app/modules/listings/listings.service';
import { CreateListingDto } from '@app/modules/listings/dto/create-listing.dto';
import { UpdateListingDto } from '@app/modules/listings/dto/update-listing.dto';
import { ListingsFilterDto } from '@app/modules/listings/dto/listings-filter.dto';
import { JwtAuthGuard } from '@app/common/guards/jwt-auth.guard';
import { RolesGuard } from '@app/common/guards/roles.guard';
import { Roles } from '@app/common/decorators/roles.decorator';
import { User } from '@prisma/client';
import { ReportListingDto } from '@app/modules/listings/dto/report-listing.dto';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ContactOwnerDto } from '@app/modules/listings/dto/contact-owner.dto';
import { ListingOwnerGuard } from '@app/common/guards/listing-owner.guard';
@ApiTags('Listings')
@ApiBearerAuth()
@Controller('listings')
export class ListingsController {
constructor(private readonly listingsService: ListingsService) {}
@Post()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Create a new listing', description: 'Creates a new listing. Requires one of the roles: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
create(@Body() createListingDto: CreateListingDto, @Req() req: { user: User }) {
return this.listingsService.create(createListingDto, req.user.id);
}
@Get()
findAll(@Query() filter: ListingsFilterDto) {
return this.listingsService.findAll(filter);
}
// ────── FAVORITES ENDPOINTS ──────────────────────────────────────────
@Post(':id/favorite')
@UseGuards(JwtAuthGuard)
@HttpCode(200)
async toggleFavorite(@Param('id', ParseIntPipe) listingId: number, @Req() req: { user: User }) {
const result = await this.listingsService.toggleFavorite(listingId, req.user.id);
return result;
}
@Get('favorites')
@UseGuards(JwtAuthGuard)
async listFavorites(
@Req() req: { user: User },
@Query('limit') limit?: string,
@Query('offset') offset?: string,
): Promise<{ data: any[]; total: number }> {
const size = Math.max(1, Math.min(100, (limit && limit.trim() !== '' && !isNaN(parseInt(limit))) ? parseInt(limit) : 20));
const off = Math.max(0, (offset && offset.trim() !== '' && !isNaN(parseInt(offset))) ? parseInt(offset) : 0);
const page = Math.floor(off / Math.max(1, size)) + 1;
const resp = await this.listingsService.getMyFavorites(req.user.id, page, size);
return { data: resp.data, total: resp.meta.total } as any;
}
// Alias endpoint for test compatibility
@Get('users/me/favorites')
@UseGuards(JwtAuthGuard)
async listFavoritesMe(
@Req() req: { user: User },
@Query('limit') limit?: string,
@Query('offset') offset?: string,
) {
const size = Math.max(1, Math.min(100, limit && !isNaN(parseInt(limit)) ? parseInt(limit) : 20));
const off = Math.max(0, offset && !isNaN(parseInt(offset)) ? parseInt(offset) : 0);
const page = Math.floor(off / Math.max(1, size)) + 1;
const resp = await this.listingsService.getMyFavorites(req.user.id, page, size);
// Legacy alias: return listing array for compatibility with older tests
const listingsOnly = (resp.data as any[]).map((f: any) => f.listing).filter(Boolean);
return { data: listingsOnly, total: resp.meta.total } as any;
}
// Alias endpoint for test compatibility
@Get('favorites/my')
@UseGuards(JwtAuthGuard)
async listFavoritesMy(
@Req() req: { user: User },
@Query('limit') limit?: string,
@Query('offset') offset?: string,
) {
const size = Math.max(1, Math.min(100, limit && !isNaN(parseInt(limit)) ? parseInt(limit) : 20));
const off = Math.max(0, offset && !isNaN(parseInt(offset)) ? parseInt(offset) : 0);
const page = Math.floor(off / Math.max(1, size)) + 1;
const resp = await this.listingsService.getMyFavorites(req.user.id, page, size);
// Legacy alias: return listing array
const listingsOnly = (resp.data as any[]).map((f: any) => f.listing).filter(Boolean);
return { data: listingsOnly, total: resp.meta.total } as any;
}
@Delete(':id/favorite')
@UseGuards(JwtAuthGuard)
@HttpCode(200)
removeFavoriteByListing(@Param('id', ParseIntPipe) listingId: number, @Req() req: { user: User }) {
return this.listingsService.removeFavoriteByListingId(listingId, req.user.id);
}
// Delete favorite by favorite ID (for test compatibility)
@Delete('favorites/:favoriteId')
@UseGuards(JwtAuthGuard)
@HttpCode(200)
removeFavoriteById(@Param('favoriteId', ParseIntPipe) favoriteId: number, @Req() req: { user: User }) {
return this.listingsService.removeFavorite(favoriteId, req.user.id);
}
@Patch(':id')
@UseGuards(JwtAuthGuard, RolesGuard, ListingOwnerGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Update a listing', description: 'Requires one of the roles: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
update(
@Param('id', ParseIntPipe) id: number,
@Body() updateListingDto: UpdateListingDto,
) {
return this.listingsService.update(id, updateListingDto);
}
@Delete(':id')
@UseGuards(JwtAuthGuard, RolesGuard, ListingOwnerGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Delete a listing', description: 'Requires one of the roles: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
remove(@Param('id', ParseIntPipe) id: number) {
return this.listingsService.remove(id);
}
@Patch(':id/publish')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Publish a listing', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
publish(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.publish(id, req.user);
}
@Post(':id/pay-to-publish')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@HttpCode(201)
@ApiOperation({ summary: 'Create pay-to-publish payment intent for a listing' })
createPayToPublish(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.createPayToPublishIntent(id, req.user.id);
}
// New lifecycle endpoints
@Post('draft')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Create listing draft', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
createDraft(@Body() dto: CreateListingDto, @Req() req: { user: User }) {
return this.listingsService.createDraft(dto, req.user.id);
}
@Patch(':id/draft')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Edit listing draft', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
editDraft(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateListingDto, @Req() req: { user: User }) {
return this.listingsService.editDraft(id, dto, req.user.id);
}
@Patch(':id/unpublish')
@UseGuards(JwtAuthGuard, RolesGuard, ListingOwnerGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Unpublish a listing', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
unpublish(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.unpublish(id, req.user.id);
}
@Patch(':id/archive')
@UseGuards(JwtAuthGuard, RolesGuard, ListingOwnerGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Archive a listing', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
archive(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.archive(id, req.user.id);
}
@Post(':id/duplicate')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER', 'REALTOR_PRO', 'USER')
@ApiOperation({ summary: 'Duplicate a listing', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER, REALTOR_PRO, USER.' })
duplicate(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.duplicate(id, req.user.id);
}
// Deprecated favorites endpoints removed in favor of standardized ones below
// Bulk import
@Post('bulk/import')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT')
@HttpCode(202)
bulkImport(@Req() req: { user: User }, @Body() body: { csvKey: string }) {
return this.listingsService.enqueueBulkImport(req.user.id, body.csvKey);
}
@Get('bulk/import/:jobId')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER')
bulkImportStatus(@Param('jobId') jobId: string) {
return this.listingsService.getBulkImportStatus(jobId);
}
@Post(':id/report')
@UseGuards(JwtAuthGuard)
report(
@Param('id', ParseIntPipe) id: number,
@Req() req: { user: User },
@Body() reportListingDto: ReportListingDto,
) {
return this.listingsService.report(id, req.user.id, reportListingDto);
}
@Post(':id/share')
@UseGuards(JwtAuthGuard)
share(
@Param('id', ParseIntPipe) id: number,
@Req() req: { user: User },
) {
return this.listingsService.share(id, req.user.id);
}
@Post(':id/contact-owner')
@UseGuards(JwtAuthGuard)
contactOwner(
@Param('id', ParseIntPipe) id: number,
@Req() req: { user: User },
@Body() contactOwnerDto: ContactOwnerDto,
) {
return this.listingsService.contactOwner(id, req.user.id, contactOwnerDto);
}
@Get(':id/analytics')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT', 'AGENCY_OWNER')
@ApiOperation({ summary: 'Get listing analytics', description: 'Requires one of: ADMIN, AGENT, AGENCY_OWNER.' })
getAnalytics(
@Param('id', ParseIntPipe) id: number,
@Req() req: { user: User },
) {
return this.listingsService.getAnalytics(id, req.user);
}
@Get(':id/price-history')
getPriceHistory(@Param('id', ParseIntPipe) id: number) {
return this.listingsService.getPriceHistory(id);
}
// Monetization hooks
@Get('fees')
fees() { return this.listingsService.getListingFees(); }
// New inquiry & availability
@Post(':id/inquiry')
@UseGuards(JwtAuthGuard)
@HttpCode(201)
createInquiry(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }, @Body() body: { message: string }) {
return this.listingsService.createInquiry(id, req.user.id, body.message);
}
@Get(':id/inquiries')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT')
listInquiries(@Param('id', ParseIntPipe) id: number, @Req() req: { user: User }) {
return this.listingsService.listInquiries(id, req.user.id);
}
@Post(':id/availability')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN', 'AGENT')
addAvailability(
@Param('id', ParseIntPipe) id: number,
@Req() req: { user: User },
@Body() body: { startAt: string; endAt: string; capacity?: number },
) { return this.listingsService.addAvailability(id, req.user.id, new Date(body.startAt), new Date(body.endAt), body.capacity); }
@Get(':id')
findOne(@Param('id', ParseIntPipe) id: number, @Req() req: any) {
// Optional auth: if Authorization header is present, try to decode to enrich response
let userId = req?.user?.id as number | undefined;
if (!userId) {
const authHeader = (req?.headers?.authorization || '') as string;
const token = authHeader.startsWith('Bearer ') ? authHeader.slice(7) : undefined;
if (token) {
// Best-effort extraction of subject; safe for read-only personalization
try {
const [, payloadB64] = token.split('.');
if (payloadB64) {
const json = Buffer.from(payloadB64, 'base64url').toString('utf8');
const payload = JSON.parse(json);
if (payload && typeof payload.sub === 'number') {
userId = payload.sub;
}
}
} catch (_e) {}
}
}
if (userId) {
return this.listingsService.findOneWithFavorited(id, userId);
}
return this.listingsService.findOne(id);
}
} |