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 { IconService } from '../providers/icon.service';
|
||||||
import { GroupService } from '../providers/group.service';
|
import { GroupService } from '../providers/group.service';
|
||||||
import { CreateIconDto } from '../dto/create-icon.dto';
|
import { CreateIconDto } from '../dto/create-icon.dto';
|
||||||
import { UpdateIconDto } from '../dto/update-icon.dto';
|
import { UpdateIconDto } from '../dto/update-icon.dto';
|
||||||
import { CreateGroupDto } from '../dto/create-group.dto';
|
import { CreateGroupDto } from '../dto/create-group.dto';
|
||||||
import { UpdateGroupDto } from '../dto/update-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 { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
|
||||||
import { UseGuards } from '@nestjs/common';
|
import { UseGuards } from '@nestjs/common';
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
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')
|
@ApiTags('icons')
|
||||||
@Controller()
|
@Controller()
|
||||||
@@ -37,6 +39,21 @@ export class IconsController {
|
|||||||
return this.iconService.create(dto);
|
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')
|
@Get('super-admin/icons')
|
||||||
@UseGuards(SuperAdminAuthGuard)
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Get all icons' })
|
@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 { GroupRepository } from './repositories/group.repository';
|
||||||
import { IconsController } from './controllers/icons.controller';
|
import { IconsController } from './controllers/icons.controller';
|
||||||
import { JwtModule } from '@nestjs/jwt';
|
import { JwtModule } from '@nestjs/jwt';
|
||||||
|
import { UploaderModule } from '../uploader/uploader.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [MikroOrmModule.forFeature([Icon, Group]), JwtModule],
|
imports: [MikroOrmModule.forFeature([Icon, Group]), JwtModule, UploaderModule],
|
||||||
controllers: [IconsController],
|
controllers: [IconsController],
|
||||||
providers: [IconService, GroupService, IconRepository, GroupRepository],
|
providers: [IconService, GroupService, IconRepository, GroupRepository],
|
||||||
exports: [IconRepository, GroupRepository, IconService, GroupService],
|
exports: [IconRepository, GroupRepository, IconService, GroupService],
|
||||||
|
|||||||
@@ -7,19 +7,19 @@ import { RequiredEntityData } from '@mikro-orm/core';
|
|||||||
import { Icon } from '../entities/icon.entity';
|
import { Icon } from '../entities/icon.entity';
|
||||||
import { IconMessage, GroupMessage } from 'src/common/enums/message.enum';
|
import { IconMessage, GroupMessage } from 'src/common/enums/message.enum';
|
||||||
import { Group } from '../entities/group.entity';
|
import { Group } from '../entities/group.entity';
|
||||||
|
import { UploaderService } from 'src/modules/uploader/providers/uploader.service';
|
||||||
|
import type { File } from '@nest-lab/fastify-multer';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class IconService {
|
export class IconService {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly iconRepository: IconRepository,
|
private readonly iconRepository: IconRepository,
|
||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
|
private readonly uploaderService: UploaderService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async create(dto: CreateIconDto): Promise<Icon> {
|
async create(dto: CreateIconDto): Promise<Icon> {
|
||||||
const group = await this.em.findOne(Group, { id: dto.groupId });
|
const group = await this.findGroupOrFail(dto.groupId);
|
||||||
if (!group) {
|
|
||||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data: RequiredEntityData<Icon> = {
|
const data: RequiredEntityData<Icon> = {
|
||||||
url: dto.url,
|
url: dto.url,
|
||||||
@@ -31,6 +31,21 @@ export class IconService {
|
|||||||
return icon;
|
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[]> {
|
async findAll(): Promise<Icon[]> {
|
||||||
return this.iconRepository.find({}, { populate: ['group'] });
|
return this.iconRepository.find({}, { populate: ['group'] });
|
||||||
}
|
}
|
||||||
@@ -65,6 +80,15 @@ export class IconService {
|
|||||||
return icon;
|
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> {
|
async remove(id: string): Promise<void> {
|
||||||
const icon = await this.iconRepository.findOne({ id });
|
const icon = await this.iconRepository.findOne({ id });
|
||||||
if (!icon) {
|
if (!icon) {
|
||||||
@@ -73,4 +97,3 @@ export class IconService {
|
|||||||
await this.em.removeAndFlush(icon);
|
await this.em.removeAndFlush(icon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,10 +17,12 @@ import { PaymentListeners } from './listeners/payment.listeners';
|
|||||||
import { AdminModule } from '../admin/admin.module';
|
import { AdminModule } from '../admin/admin.module';
|
||||||
import { NotificationsModule } from '../notifications/notifications.module';
|
import { NotificationsModule } from '../notifications/notifications.module';
|
||||||
import { OrdersModule } from '../orders/orders.module';
|
import { OrdersModule } from '../orders/orders.module';
|
||||||
|
import { RestaurantsModule } from '../restaurants/restaurants.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [MikroOrmModule.forFeature([PaymentMethod, Payment, Restaurant]),
|
imports: [MikroOrmModule.forFeature([PaymentMethod, Payment, Restaurant]),
|
||||||
AuthModule, JwtModule, InventoryModule, AdminModule, NotificationsModule,
|
AuthModule, JwtModule, InventoryModule, AdminModule, NotificationsModule,
|
||||||
|
RestaurantsModule,
|
||||||
forwardRef(() => OrdersModule)],
|
forwardRef(() => OrdersModule)],
|
||||||
controllers: [PaymentsController],
|
controllers: [PaymentsController],
|
||||||
providers: [PaymentsService, PaymentMethodService, PaymentMethodRepository,
|
providers: [PaymentsService, PaymentMethodService, PaymentMethodRepository,
|
||||||
@@ -30,7 +32,6 @@ import { OrdersModule } from '../orders/orders.module';
|
|||||||
PaymentRepository,
|
PaymentRepository,
|
||||||
PaymentMethodService,
|
PaymentMethodService,
|
||||||
PaymentsService,
|
PaymentsService,
|
||||||
// PaymentGatewayService,
|
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class PaymentsModule { }
|
export class PaymentsModule { }
|
||||||
|
|||||||
Reference in New Issue
Block a user