update dmenu module
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export const DMENU_CONFIG = "ICONS_CONFIG";
|
||||
@@ -1,92 +1,102 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from "@nestjs/common";
|
||||
import { ApiBearerAuth, ApiBody, ApiOperation, ApiParam, ApiTags } from "@nestjs/swagger";
|
||||
import { ApiBearerAuth, ApiOperation, ApiParam, ApiTags } from "@nestjs/swagger";
|
||||
|
||||
import { CreateGroupDto } from "./DTO/create-group.dto";
|
||||
import { CreateIconDto } from "./DTO/create-icon.dto";
|
||||
import { UpdateGroupDto } from "./DTO/update-group.dto";
|
||||
import { UpdateIconDto } from "./DTO/update-icon.dto";
|
||||
import { ContactService } from "./providers/contact.service";
|
||||
import { IconsService } from "./providers/icons.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 { AuthGuards } from "../../common/decorators/auth-guard.decorator";
|
||||
import { PermissionsDec } from "../../common/decorators/permission.decorator";
|
||||
import { PermissionEnum } from "../users/enums/permission.enum";
|
||||
|
||||
@ApiTags("icons")
|
||||
@Controller("admin/icons")
|
||||
@ApiTags("d-menu")
|
||||
@Controller("admin/dmenu")
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ICONS)
|
||||
@PermissionsDec(PermissionEnum.DMENU)
|
||||
@ApiBearerAuth()
|
||||
export class IconsController {
|
||||
constructor(private readonly iconsService: IconsService) {}
|
||||
export class DmenuController {
|
||||
constructor(
|
||||
private readonly iconsService: IconsService,
|
||||
private readonly contactService: ContactService,
|
||||
|
||||
) {}
|
||||
|
||||
// Icon endpoints
|
||||
@Post()
|
||||
@Post('icons')
|
||||
@ApiOperation({ summary: "Create icon" })
|
||||
@ApiBody({ type: CreateIconDto })
|
||||
createIcon(@Body() dto: CreateIconDto) {
|
||||
return this.iconsService.createIcon(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@Get('icons')
|
||||
@ApiOperation({ summary: "Get all icons" })
|
||||
findAllIcons() {
|
||||
return this.iconsService.findAllIcons();
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get icon by id" })
|
||||
@ApiParam({ name: "id", required: true, type: String })
|
||||
findOneIcon(@Param("id") id: string) {
|
||||
return this.iconsService.findOneIcon(id);
|
||||
}
|
||||
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "Update icon" })
|
||||
@ApiParam({ name: "id" })
|
||||
@ApiBody({ type: UpdateIconDto })
|
||||
updateIcon(@Param("id") id: string, @Body() dto: UpdateIconDto) {
|
||||
return this.iconsService.updateIcon(id, dto);
|
||||
}
|
||||
|
||||
@Delete(":id")
|
||||
@ApiOperation({ summary: "Delete icon" })
|
||||
@ApiParam({ name: "id" })
|
||||
removeIcon(@Param("id") id: string) {
|
||||
return this.iconsService.removeIcon(id);
|
||||
}
|
||||
|
||||
// Group endpoints (must come before :id routes to avoid route conflicts)
|
||||
@Post("groups")
|
||||
// Group endpoints: place BEFORE `icons/:id` routes to avoid route conflicts
|
||||
@Post("icons/groups")
|
||||
@ApiOperation({ summary: "Create icon group" })
|
||||
@ApiBody({ type: CreateGroupDto })
|
||||
createGroup(@Body() dto: CreateGroupDto) {
|
||||
return this.iconsService.createGroup(dto);
|
||||
}
|
||||
|
||||
@Get("groups")
|
||||
@Get("icons/groups")
|
||||
@ApiOperation({ summary: "Get all icon groups" })
|
||||
findAllGroups() {
|
||||
return this.iconsService.findAllGroups();
|
||||
}
|
||||
|
||||
@Get("groups/:id")
|
||||
@Get("icons/groups/:id")
|
||||
@ApiOperation({ summary: "Get icon group by id" })
|
||||
@ApiParam({ name: "id", required: true, type: String })
|
||||
findOneGroup(@Param("id") id: string) {
|
||||
return this.iconsService.findOneGroup(id);
|
||||
}
|
||||
|
||||
@Patch("groups/:id")
|
||||
@Patch("icons/groups/:id")
|
||||
@ApiOperation({ summary: "Update icon group" })
|
||||
@ApiParam({ name: "id" })
|
||||
@ApiBody({ type: UpdateGroupDto })
|
||||
updateGroup(@Param("id") id: string, @Body() dto: UpdateGroupDto) {
|
||||
return this.iconsService.updateGroup(id, dto);
|
||||
}
|
||||
|
||||
@Delete("groups/:id")
|
||||
@Delete("icons/groups/:id")
|
||||
@ApiOperation({ summary: "Delete icon group" })
|
||||
@ApiParam({ name: "id" })
|
||||
removeGroup(@Param("id") id: string) {
|
||||
return this.iconsService.removeGroup(id);
|
||||
}
|
||||
|
||||
@Get("icons/:id")
|
||||
@ApiOperation({ summary: "Get icon by id" })
|
||||
@ApiParam({ name: "id", required: true, type: String })
|
||||
findOneIcon(@Param("id") id: string) {
|
||||
return this.iconsService.findOneIcon(id);
|
||||
}
|
||||
|
||||
@Patch("icons/:id")
|
||||
@ApiOperation({ summary: "Update icon" })
|
||||
@ApiParam({ name: "id" })
|
||||
updateIcon(@Param("id") id: string, @Body() dto: UpdateIconDto) {
|
||||
return this.iconsService.updateIcon(id, dto);
|
||||
}
|
||||
|
||||
@Delete("icons/:id")
|
||||
@ApiOperation({ summary: "Delete icon" })
|
||||
@ApiParam({ name: "id" })
|
||||
removeIcon(@Param("id") id: string) {
|
||||
return this.iconsService.removeIcon(id);
|
||||
}
|
||||
// contacts
|
||||
@Get('contacts')
|
||||
findAll() {
|
||||
return this.contactService.findAll();
|
||||
}
|
||||
|
||||
@Get("contacts/:id")
|
||||
findOne(@Param("id") id: string) {
|
||||
return this.contactService.findOne(+id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { DMENU_CONFIG } from "./constants";
|
||||
import { DmenuController } from "./dmenu.controller";
|
||||
import { ContactService } from "./providers/contact.service";
|
||||
import { IconsService } from "./providers/icons.service";
|
||||
import { dmenuConfig } from "../../configs/icons.config";
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [DmenuController],
|
||||
providers: [
|
||||
IconsService,
|
||||
ContactService,
|
||||
{
|
||||
provide: DMENU_CONFIG,
|
||||
useFactory: dmenuConfig().useFactory,
|
||||
inject: dmenuConfig().inject,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class DmenuModule {}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { HttpService } from "@nestjs/axios";
|
||||
import { Injectable, Logger } from "@nestjs/common";
|
||||
import { Inject } from "@nestjs/common";
|
||||
import { AxiosError } from "axios";
|
||||
import { catchError, firstValueFrom, throwError } from "rxjs";
|
||||
|
||||
import { IDmenuConfig } from "../../../configs/icons.config";
|
||||
import { DMENU_CONFIG } from "../constants";
|
||||
|
||||
@Injectable()
|
||||
export class ContactService {
|
||||
private readonly logger = new Logger(ContactService.name);
|
||||
|
||||
constructor(
|
||||
@Inject(DMENU_CONFIG) private readonly config: IDmenuConfig,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
|
||||
private getHeaders(): Record<string, string> {
|
||||
const credentials = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64");
|
||||
return {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Basic ${credentials}`,
|
||||
};
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService.get(`${this.config.baseUrl}/super-admin/contacts`, { headers: this.getHeaders() }).pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error(`Failed to fetch contacts: ${err.message}`, err.stack);
|
||||
return throwError(() => err);
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
this.logger.error(`Error fetching contacts: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
try {
|
||||
const { data } = await firstValueFrom(
|
||||
this.httpService.get(`${this.config.baseUrl}/super-admin/contacts/${id}`, { headers: this.getHeaders() }).pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error(`Failed to fetch contact: ${err.message}`, err.stack);
|
||||
return throwError(() => err);
|
||||
}),
|
||||
),
|
||||
);
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
this.logger.error(`Error fetching contact: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-3
@@ -3,8 +3,8 @@ import { Inject, Injectable, Logger } from "@nestjs/common";
|
||||
import { AxiosError } from "axios";
|
||||
import { catchError, firstValueFrom, throwError } from "rxjs";
|
||||
|
||||
import { IIconsConfig } from "../../../configs/icons.config";
|
||||
import { ICONS_CONFIG } from "../constants";
|
||||
import { IDmenuConfig } from "../../../configs/icons.config";
|
||||
import { DMENU_CONFIG } from "../constants";
|
||||
import { CreateGroupDto } from "../DTO/create-group.dto";
|
||||
import { CreateIconDto } from "../DTO/create-icon.dto";
|
||||
import { UpdateGroupDto } from "../DTO/update-group.dto";
|
||||
@@ -15,7 +15,7 @@ export class IconsService {
|
||||
private readonly logger = new Logger(IconsService.name);
|
||||
|
||||
constructor(
|
||||
@Inject(ICONS_CONFIG) private readonly config: IIconsConfig,
|
||||
@Inject(DMENU_CONFIG) private readonly config: IDmenuConfig,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
export const ICONS_CONFIG = "ICONS_CONFIG";
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
|
||||
import { ICONS_CONFIG } from "./constants";
|
||||
import { IconsController } from "./icons.controller";
|
||||
import { IconsService } from "./providers/icons.service";
|
||||
import { iconsConfig } from "../../configs/icons.config";
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [IconsController],
|
||||
providers: [
|
||||
IconsService,
|
||||
{
|
||||
provide: ICONS_CONFIG,
|
||||
useFactory: iconsConfig().useFactory,
|
||||
inject: iconsConfig().inject,
|
||||
},
|
||||
],
|
||||
exports: [IconsService],
|
||||
})
|
||||
export class IconsModule {}
|
||||
@@ -20,5 +20,5 @@ export enum PermissionEnum {
|
||||
PAYMENTS = "payments",
|
||||
MANAGE_SSO_CLIENTS = "manage_sso_clients",
|
||||
SUPPORT_PLAN = "support_plan",
|
||||
ICONS = "icons",
|
||||
}
|
||||
DMENU = "dmenu",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user