icons module

This commit is contained in:
2026-05-21 09:24:03 +03:30
parent 5087a3e977
commit c7b5c4dca5
17 changed files with 1228 additions and 277 deletions
+2 -1
View File
@@ -11,6 +11,7 @@ import { EventEmitterModule } from '@nestjs/event-emitter';
import { BusinessModule } from './modules/business/business.module';
import { CatalogueModule } from './modules/catalogue/catalogue.module';
import { HttpModule } from '@nestjs/axios';
import { IconsModule } from './modules/icons/icons.module';
@Module({
imports: [
@@ -30,7 +31,7 @@ import { HttpModule } from '@nestjs/axios';
BusinessModule,
CatalogueModule,
HttpModule.register({ global: true, timeout: 10000, headers: { "Content-Type": "application/json" } }),
IconsModule,
],
controllers: [],
providers: [],
@@ -0,0 +1,131 @@
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, 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()
export class IconsController {
constructor(
private readonly iconService: IconService,
private readonly groupService: GroupService,
) {}
@Get('admin/groups/icons')
@UseGuards(AdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Get all icons' })
findAllPublicIcons() {
return this.groupService.findAllGroups();
}
/**
* Super Admin Endpoints
*/
@Post('super-admin/icons')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Create icon' })
@ApiBody({ type: CreateIconDto })
createIcon(@Body() dto: CreateIconDto) {
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' })
findAllIcons() {
return this.iconService.findAll();
}
@Get('super-admin/icons/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Get icon by id' })
@ApiParam({ name: 'id', required: true, type: String })
findOneIcon(@Param('id') id: string) {
return this.iconService.findOne(id);
}
@Patch('super-admin/icons/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Update icon' })
@ApiParam({ name: 'id' })
@ApiBody({ type: UpdateIconDto })
updateIcon(@Param('id') id: string, @Body() dto: UpdateIconDto) {
return this.iconService.update(id, dto);
}
@Delete('super-admin/icons/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Delete icon' })
@ApiParam({ name: 'id' })
removeIcon(@Param('id') id: string) {
return this.iconService.remove(id);
}
// Group endpoints (must come before :id routes to avoid route conflicts)
@Post('super-admin/icons/groups')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Create icon group' })
@ApiBody({ type: CreateGroupDto })
createGroup(@Body() dto: CreateGroupDto) {
return this.groupService.create(dto);
}
@Get('super-admin/icons/groups')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Get all icon groups' })
async findAllGroups() {
console.log('find icona');
const a = await this.groupService.findAllGroups();
console.log(a);
return a;
}
@Get('super-admin/icons/groups/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Get icon group by id' })
@ApiParam({ name: 'id', required: true, type: String })
findOneGroup(@Param('id') id: string) {
return this.groupService.findOne(id);
}
@Patch('super-admin/icons/groups/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Update icon group' })
@ApiParam({ name: 'id' })
updateGroup(@Param('id') id: string, @Body() dto: UpdateGroupDto) {
return this.groupService.update(id, dto);
}
@Delete('super-admin/icons/groups/:id')
@UseGuards(SuperAdminAuthGuard)
@ApiOperation({ summary: 'Delete icon group' })
@ApiParam({ name: 'id' })
removeGroup(@Param('id') id: string) {
return this.groupService.remove(id);
}
}
@@ -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[];
}
@@ -0,0 +1,9 @@
import { IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class CreateGroupDto {
@IsString()
@ApiProperty({ example: 'Social Media' })
name!: string;
}
+12
View File
@@ -0,0 +1,12 @@
import { IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class CreateIconDto {
@IsString()
@ApiProperty({ example: 'https://example.com/icons/facebook.svg' })
url!: string;
@IsString()
@ApiProperty({ example: 'group-id-here' })
groupId!: string;
}
@@ -0,0 +1,5 @@
import { PartialType } from '@nestjs/swagger';
import { CreateGroupDto } from './create-group.dto';
export class UpdateGroupDto extends PartialType(CreateGroupDto) {}
+4
View File
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/swagger';
import { CreateIconDto } from './create-icon.dto';
export class UpdateIconDto extends PartialType(CreateIconDto) {}
@@ -0,0 +1,15 @@
import { Entity, Index, Property, Collection, OneToMany, Cascade } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Icon } from './icon.entity';
@Entity({ tableName: 'icon_groups' })
export class Group extends BaseEntity {
@Property()
name!: string;
@OneToMany(() => Icon, icon => icon.group,{
cascade: [Cascade.ALL],
orphanRemoval: true,
})
icons = new Collection<Icon>(this);
}
+13
View File
@@ -0,0 +1,13 @@
import { Entity, Index, Property, ManyToOne } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Group } from './group.entity';
@Entity({ tableName: 'icons' })
@Index({ properties: ['url'] })
export class Icon extends BaseEntity {
@Property()
url!: string;
@ManyToOne(() => Group)
group!: Group;
}
+19
View File
@@ -0,0 +1,19 @@
import { Module } from '@nestjs/common';
import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Icon } from './entities/icon.entity';
import { Group } from './entities/group.entity';
import { IconService } from './providers/icon.service';
import { GroupService } from './providers/group.service';
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, UploaderModule],
controllers: [IconsController],
providers: [IconService, GroupService, IconRepository, GroupRepository],
exports: [IconRepository, GroupRepository, IconService, GroupService],
})
export class IconsModule {}
@@ -0,0 +1,56 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateGroupDto } from '../dto/create-group.dto';
import { UpdateGroupDto } from '../dto/update-group.dto';
import { GroupRepository } from '../repositories/group.repository';
import { EntityManager } from '@mikro-orm/postgresql';
import { RequiredEntityData } from '@mikro-orm/core';
import { Group } from '../entities/group.entity';
import { GroupMessage } from 'src/common/enums/message.enum';
@Injectable()
export class GroupService {
constructor(
private readonly groupRepository: GroupRepository,
private readonly em: EntityManager,
) {}
async create(dto: CreateGroupDto): Promise<Group> {
const data: RequiredEntityData<Group> = {
name: dto.name,
};
const group = this.groupRepository.create(data);
await this.em.persistAndFlush(group);
return group;
}
async findOne(id: string): Promise<Group> {
const group = await this.groupRepository.findOne({ id }, { populate: ['icons'] });
if (!group) {
throw new NotFoundException(GroupMessage.NOT_FOUND);
}
return group;
}
async update(id: string, dto: UpdateGroupDto): Promise<Group> {
const group = await this.groupRepository.findOne({ id });
if (!group) {
throw new NotFoundException(GroupMessage.NOT_FOUND);
}
this.em.assign(group, dto);
await this.em.persistAndFlush(group);
return group;
}
async remove(id: string): Promise<void> {
const group = await this.groupRepository.findOne({ id }, { populate: ['icons'] });
if (!group) {
throw new NotFoundException(GroupMessage.NOT_FOUND);
}
await this.em.removeAndFlush(group);
}
findAllGroups(): Promise<Group[]> {
return this.groupRepository.find({}, { populate: ['icons'] });
}
}
@@ -0,0 +1,99 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateIconDto } from '../dto/create-icon.dto';
import { UpdateIconDto } from '../dto/update-icon.dto';
import { IconRepository } from '../repositories/icon.repository';
import { EntityManager } from '@mikro-orm/postgresql';
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.findGroupOrFail(dto.groupId);
const data: RequiredEntityData<Icon> = {
url: dto.url,
group: group,
};
const icon = this.iconRepository.create(data);
await this.em.persistAndFlush(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[]> {
return this.iconRepository.find({}, { populate: ['group'] });
}
async findOne(id: string): Promise<Icon> {
const icon = await this.iconRepository.findOne({ id }, { populate: ['group'] });
if (!icon) {
throw new NotFoundException(IconMessage.NOT_FOUND);
}
return icon;
}
async update(id: string, dto: UpdateIconDto): Promise<Icon> {
const icon = await this.iconRepository.findOne({ id });
if (!icon) {
throw new NotFoundException(IconMessage.NOT_FOUND);
}
if (dto.groupId) {
const group = await this.em.findOne(Group, { id: dto.groupId });
if (!group) {
throw new NotFoundException(GroupMessage.NOT_FOUND);
}
icon.group = group;
}
this.em.assign(icon, {
url: dto.url,
});
await this.em.persistAndFlush(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> {
const icon = await this.iconRepository.findOne({ id });
if (!icon) {
throw new NotFoundException(IconMessage.NOT_FOUND);
}
await this.em.removeAndFlush(icon);
}
}
@@ -0,0 +1,10 @@
import { Injectable } from '@nestjs/common';
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { Group } from '../entities/group.entity';
@Injectable()
export class GroupRepository extends EntityRepository<Group> {
constructor(readonly em: EntityManager) {
super(em, Group);
}
}
@@ -0,0 +1,10 @@
import { Injectable } from '@nestjs/common';
import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { Icon } from '../entities/icon.entity';
@Injectable()
export class IconRepository extends EntityRepository<Icon> {
constructor(readonly em: EntityManager) {
super(em, Icon);
}
}