201 lines
7.0 KiB
TypeScript
201 lines
7.0 KiB
TypeScript
import { HttpService } from "@nestjs/axios";
|
|
import { Inject, Injectable, Logger } from "@nestjs/common";
|
|
import { AxiosError } from "axios";
|
|
import { catchError, firstValueFrom, throwError } from "rxjs";
|
|
|
|
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";
|
|
import { UpdateIconDto } from "../DTO/update-icon.dto";
|
|
|
|
@Injectable()
|
|
export class IconsService {
|
|
private readonly logger = new Logger(IconsService.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}`,
|
|
};
|
|
}
|
|
|
|
// Icon methods
|
|
async createIcon(dto: CreateIconDto) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.post(`${this.config.baseUrl}/super-admin/icons`, dto, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to create icon: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error creating icon: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAllIcons() {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.get(`${this.config.baseUrl}/super-admin/icons`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to fetch icons: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error fetching icons: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findOneIcon(id: string) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.get(`${this.config.baseUrl}/super-admin/icons/${id}`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to fetch icon: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error fetching icon: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async updateIcon(id: string, dto: UpdateIconDto) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.patch(`${this.config.baseUrl}/super-admin/icons/${id}`, dto, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to update icon: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error updating icon: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async removeIcon(id: string) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.delete(`${this.config.baseUrl}/super-admin/icons/${id}`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to delete icon: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error deleting icon: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// Group methods
|
|
async createGroup(dto: CreateGroupDto) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.post(`${this.config.baseUrl}/super-admin/icons/groups`, dto, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to create group: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error creating group: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findAllGroups() {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.get(`${this.config.baseUrl}/super-admin/icons/groups`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to fetch groups: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error fetching groups: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findOneGroup(id: string) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.get(`${this.config.baseUrl}/super-admin/icons/groups/${id}`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to fetch group: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error fetching group: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async updateGroup(id: string, dto: UpdateGroupDto) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.patch(`${this.config.baseUrl}/super-admin/icons/groups/${id}`, dto, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to update group: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error updating group: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async removeGroup(id: string) {
|
|
try {
|
|
const { data } = await firstValueFrom(
|
|
this.httpService.delete(`${this.config.baseUrl}/super-admin/icons/groups/${id}`, { headers: this.getHeaders() }).pipe(
|
|
catchError((err: AxiosError) => {
|
|
this.logger.error(`Failed to delete group: ${err.message}`, err.stack);
|
|
return throwError(() => err);
|
|
}),
|
|
),
|
|
);
|
|
return data;
|
|
} catch (error: unknown) {
|
|
this.logger.error(`Error deleting group: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
throw error;
|
|
}
|
|
}
|
|
} |