import { Body, Controller, Delete, Get, Headers, Param, Patch, Post, Query, UseGuards, } from '@nestjs/common'; import { AuthUser } from '../auth/auth.types'; import { CurrentUser } from '../auth/decorators/current-user.decorator'; import { RequireBusinessPermission } from '../auth/decorators/require-business-permission.decorator'; import { BusinessPermissionGuard } from '../auth/guards/business-permission.guard'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { WebsiteAnalyticsService } from '../website-analytics/website-analytics.service'; import { CreateWebsiteStaticImageSlotDto, ListWebsiteStaticImageSlotsDto, PublicWebsiteStaticImagesQueryDto, UpdateWebsiteStaticImageSlotDto, } from './dto/website-static-images.dto'; import { WebsiteStaticImagesService } from './website-static-images.service'; @Controller('tenants/:host/website/static-images') export class PublicWebsiteStaticImagesController { constructor( private readonly service: WebsiteStaticImagesService, private readonly analytics: WebsiteAnalyticsService, ) {} @Get() async list( @Param('host') host: string, @Query() query: PublicWebsiteStaticImagesQueryDto, @Headers('user-agent') userAgent?: string, ) { const result = await this.service.listPublic(host, query.pageKey); const pageKey = query.pageKey?.trim(); // Only count when the storefront loads a specific page's slots (home / about / contact / …). if (pageKey) { if (pageKey === 'home') { this.analytics.trackPublicPage(host, 'home', null, undefined, userAgent); } else { this.analytics.trackPublicPage( host, 'static_page', null, `/${pageKey}`, userAgent, ); } } return result; } @Get(':key') getOne(@Param('host') host: string, @Param('key') key: string) { return this.service.getPublicByKey(host, key); } } @Controller('businesses/:businessId/website/static-images') @UseGuards(JwtAuthGuard, BusinessPermissionGuard) export class WebsiteStaticImagesController { constructor(private readonly service: WebsiteStaticImagesService) {} @Get() @RequireBusinessPermission('website.read') list( @Param('businessId') businessId: string, @Query() query: ListWebsiteStaticImageSlotsDto, @CurrentUser() user: AuthUser, ) { return this.service.list(businessId, query, user); } @Post('sync') @RequireBusinessPermission('website.update') syncFromWebsite( @Param('businessId') businessId: string, @CurrentUser() user: AuthUser, ) { return this.service.syncFromWebsite(businessId, user); } @Get(':slotId') @RequireBusinessPermission('website.read') getOne( @Param('businessId') businessId: string, @Param('slotId') slotId: string, @CurrentUser() user: AuthUser, ) { return this.service.getOne(businessId, slotId, user); } @Post() @RequireBusinessPermission('website.update') create( @Param('businessId') businessId: string, @Body() dto: CreateWebsiteStaticImageSlotDto, @CurrentUser() user: AuthUser, ) { return this.service.create(businessId, dto, user); } @Patch(':slotId') @RequireBusinessPermission('website.update') update( @Param('businessId') businessId: string, @Param('slotId') slotId: string, @Body() dto: UpdateWebsiteStaticImageSlotDto, @CurrentUser() user: AuthUser, ) { return this.service.update(businessId, slotId, dto, user); } @Delete(':slotId') @RequireBusinessPermission('website.update') remove( @Param('businessId') businessId: string, @Param('slotId') slotId: string, @CurrentUser() user: AuthUser, ) { return this.service.remove(businessId, slotId, user); } }