add batch icon uploader
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
|
||||
import { Controller, Get, Post, Body, Patch, Param, Delete, UploadedFiles, UseInterceptors } from '@nestjs/common';
|
||||
import { IconService } from '../providers/icon.service';
|
||||
import { GroupService } from '../providers/group.service';
|
||||
import { CreateIconDto } from '../dto/create-icon.dto';
|
||||
import { UpdateIconDto } from '../dto/update-icon.dto';
|
||||
import { CreateGroupDto } from '../dto/create-group.dto';
|
||||
import { UpdateGroupDto } from '../dto/update-group.dto';
|
||||
import { ApiTags, ApiOperation, ApiParam, ApiBody, ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { ApiTags, ApiOperation, ApiParam, ApiBody, ApiBearerAuth, ApiConsumes } from '@nestjs/swagger';
|
||||
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
import { FilesInterceptor, type File } from '@nest-lab/fastify-multer';
|
||||
import { CreateBatchIconsDto } from '../dto/create-batch-icons.dto';
|
||||
|
||||
@ApiTags('icons')
|
||||
@Controller()
|
||||
@@ -37,6 +39,21 @@ export class IconsController {
|
||||
return this.iconService.create(dto);
|
||||
}
|
||||
|
||||
|
||||
@Post('super-admin/icons/batch')
|
||||
@UseGuards(SuperAdminAuthGuard)
|
||||
@ApiOperation({ summary: 'Upload and create icons in batch' })
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@UseInterceptors(FilesInterceptor('files'))
|
||||
@ApiBody({ type: CreateBatchIconsDto })
|
||||
createBatchIcons(
|
||||
@Body('groupId') groupId: string,
|
||||
@UploadedFiles() files: File[],
|
||||
) {
|
||||
return this.iconService.createBatch(groupId, files);
|
||||
}
|
||||
|
||||
|
||||
@Get('super-admin/icons')
|
||||
@UseGuards(SuperAdminAuthGuard)
|
||||
@ApiOperation({ summary: 'Get all icons' })
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class CreateBatchIconsDto {
|
||||
@IsString()
|
||||
@ApiProperty({ example: 'group-id-here' })
|
||||
groupId!: string;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'array',
|
||||
items: {
|
||||
type: 'string',
|
||||
format: 'binary',
|
||||
},
|
||||
})
|
||||
files!: any[];
|
||||
}
|
||||
@@ -8,9 +8,10 @@ import { IconRepository } from './repositories/icon.repository';
|
||||
import { GroupRepository } from './repositories/group.repository';
|
||||
import { IconsController } from './controllers/icons.controller';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { UploaderModule } from '../uploader/uploader.module';
|
||||
|
||||
@Module({
|
||||
imports: [MikroOrmModule.forFeature([Icon, Group]), JwtModule],
|
||||
imports: [MikroOrmModule.forFeature([Icon, Group]), JwtModule, UploaderModule],
|
||||
controllers: [IconsController],
|
||||
providers: [IconService, GroupService, IconRepository, GroupRepository],
|
||||
exports: [IconRepository, GroupRepository, IconService, GroupService],
|
||||
|
||||
@@ -7,19 +7,19 @@ import { RequiredEntityData } from '@mikro-orm/core';
|
||||
import { Icon } from '../entities/icon.entity';
|
||||
import { IconMessage, GroupMessage } from 'src/common/enums/message.enum';
|
||||
import { Group } from '../entities/group.entity';
|
||||
import { UploaderService } from 'src/modules/uploader/providers/uploader.service';
|
||||
import type { File } from '@nest-lab/fastify-multer';
|
||||
|
||||
@Injectable()
|
||||
export class IconService {
|
||||
constructor(
|
||||
private readonly iconRepository: IconRepository,
|
||||
private readonly em: EntityManager,
|
||||
private readonly uploaderService: UploaderService,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateIconDto): Promise<Icon> {
|
||||
const group = await this.em.findOne(Group, { id: dto.groupId });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
const group = await this.findGroupOrFail(dto.groupId);
|
||||
|
||||
const data: RequiredEntityData<Icon> = {
|
||||
url: dto.url,
|
||||
@@ -31,6 +31,21 @@ export class IconService {
|
||||
return icon;
|
||||
}
|
||||
|
||||
async createBatch(groupId: string, files: File[]): Promise<Icon[]> {
|
||||
const group = await this.findGroupOrFail(groupId);
|
||||
const uploadedFiles = await this.uploaderService.uploadMultiple(files);
|
||||
|
||||
const icons = uploadedFiles.map(({ url }) =>
|
||||
this.iconRepository.create({
|
||||
url,
|
||||
group,
|
||||
}),
|
||||
);
|
||||
|
||||
await this.em.persistAndFlush(icons);
|
||||
return icons;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Icon[]> {
|
||||
return this.iconRepository.find({}, { populate: ['group'] });
|
||||
}
|
||||
@@ -65,6 +80,15 @@ export class IconService {
|
||||
return icon;
|
||||
}
|
||||
|
||||
private async findGroupOrFail(groupId: string): Promise<Group> {
|
||||
const group = await this.em.findOne(Group, { id: groupId });
|
||||
if (!group) {
|
||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
async remove(id: string): Promise<void> {
|
||||
const icon = await this.iconRepository.findOne({ id });
|
||||
if (!icon) {
|
||||
@@ -73,4 +97,3 @@ export class IconService {
|
||||
await this.em.removeAndFlush(icon);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,12 @@ import { PaymentListeners } from './listeners/payment.listeners';
|
||||
import { AdminModule } from '../admin/admin.module';
|
||||
import { NotificationsModule } from '../notifications/notifications.module';
|
||||
import { OrdersModule } from '../orders/orders.module';
|
||||
import { RestaurantsModule } from '../restaurants/restaurants.module';
|
||||
|
||||
@Module({
|
||||
imports: [MikroOrmModule.forFeature([PaymentMethod, Payment, Restaurant]),
|
||||
AuthModule, JwtModule, InventoryModule, AdminModule, NotificationsModule,
|
||||
RestaurantsModule,
|
||||
forwardRef(() => OrdersModule)],
|
||||
controllers: [PaymentsController],
|
||||
providers: [PaymentsService, PaymentMethodService, PaymentMethodRepository,
|
||||
@@ -30,7 +32,6 @@ import { OrdersModule } from '../orders/orders.module';
|
||||
PaymentRepository,
|
||||
PaymentMethodService,
|
||||
PaymentsService,
|
||||
// PaymentGatewayService,
|
||||
],
|
||||
})
|
||||
export class PaymentsModule { }
|
||||
|
||||
Reference in New Issue
Block a user