119 lines
3.3 KiB
TypeScript
119 lines
3.3 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
Delete,
|
|
Get,
|
|
Param,
|
|
Patch,
|
|
Post,
|
|
Query,
|
|
UploadedFiles,
|
|
UseGuards,
|
|
UseInterceptors,
|
|
} from '@nestjs/common';
|
|
import { FilesInterceptor } from '@nestjs/platform-express';
|
|
import { memoryStorage } from 'multer';
|
|
import { BusinessImageEntity } from '../../entities/business-image.entity';
|
|
import { BusinessEntity } from '../../entities/business.entity';
|
|
import { UserRole } from '../../entities/user.entity';
|
|
import { AuthUser } from '../auth/auth.types';
|
|
import { CurrentUser } from '../auth/decorators/current-user.decorator';
|
|
import { Roles } from '../auth/decorators/roles.decorator';
|
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
|
|
import { RolesGuard } from '../auth/guards/roles.guard';
|
|
import { BusinessImagesService } from './business-images.service';
|
|
import { BusinessesService } from './businesses.service';
|
|
import { AssignBusinessOwnerDto } from './dto/assign-business-owner.dto';
|
|
import { CreateBusinessDto } from './dto/create-business.dto';
|
|
import { UpdateBusinessDto } from './dto/update-business.dto';
|
|
|
|
@Controller('businesses')
|
|
export class BusinessesController {
|
|
constructor(
|
|
private readonly service: BusinessesService,
|
|
private readonly images: BusinessImagesService,
|
|
) {}
|
|
|
|
@Get()
|
|
findAll(
|
|
@Query('categoryId') categoryId?: string,
|
|
@Query('isActive') isActive?: string,
|
|
): Promise<BusinessEntity[]> {
|
|
const parsedIsActive =
|
|
typeof isActive === 'string' ? isActive === 'true' : undefined;
|
|
|
|
return this.service.findAll({
|
|
categoryId,
|
|
isActive: parsedIsActive,
|
|
});
|
|
}
|
|
|
|
@Get(':id')
|
|
findOne(@Param('id') id: string): Promise<BusinessEntity> {
|
|
return this.service.findOne(id);
|
|
}
|
|
|
|
@Post()
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN)
|
|
create(@Body() payload: CreateBusinessDto): Promise<BusinessEntity> {
|
|
return this.service.create(payload);
|
|
}
|
|
|
|
@Post(':id/images')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN)
|
|
@UseInterceptors(
|
|
FilesInterceptor('files', 10, {
|
|
storage: memoryStorage(),
|
|
limits: { fileSize: 5 * 1024 * 1024 },
|
|
}),
|
|
)
|
|
uploadImages(
|
|
@Param('id') id: string,
|
|
@UploadedFiles() files: Express.Multer.File[],
|
|
): Promise<BusinessImageEntity[]> {
|
|
return this.images.upload(id, files);
|
|
}
|
|
|
|
@Delete(':id/images/:imageId')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN)
|
|
async removeImage(
|
|
@Param('id') id: string,
|
|
@Param('imageId') imageId: string,
|
|
): Promise<{ success: true }> {
|
|
await this.images.remove(id, imageId);
|
|
return { success: true };
|
|
}
|
|
|
|
@Post(':id/assign-owner')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN)
|
|
assignOwner(
|
|
@Param('id') id: string,
|
|
@Body() payload: AssignBusinessOwnerDto,
|
|
): Promise<BusinessEntity> {
|
|
return this.service.assignOwner(id, payload);
|
|
}
|
|
|
|
@Patch(':id')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN, UserRole.BUSINESS_OWNER)
|
|
update(
|
|
@Param('id') id: string,
|
|
@Body() payload: UpdateBusinessDto,
|
|
@CurrentUser() user: AuthUser,
|
|
): Promise<BusinessEntity> {
|
|
return this.service.update(id, payload, user);
|
|
}
|
|
|
|
@Delete(':id')
|
|
@UseGuards(JwtAuthGuard, RolesGuard)
|
|
@Roles(UserRole.ADMIN)
|
|
async remove(@Param('id') id: string): Promise<{ success: true }> {
|
|
await this.service.remove(id);
|
|
return { success: true };
|
|
}
|
|
}
|