diff --git a/src/modules/dpage/dpage.controller.ts b/src/modules/dpage/dpage.controller.ts index dd01969..3170314 100644 --- a/src/modules/dpage/dpage.controller.ts +++ b/src/modules/dpage/dpage.controller.ts @@ -30,7 +30,7 @@ export class DpageController { @AdminRoute() @PermissionsDec(PermissionEnum.DMENU) findAll(@Query() dto: FindBusinessesDto) { - return this.dpageService.findAll(dto); + return this.dpageService.findAllBusinesses(dto); } // @Get(':id') diff --git a/src/modules/dpage/dpage.service.ts b/src/modules/dpage/dpage.service.ts index 8033fda..fbd5781 100644 --- a/src/modules/dpage/dpage.service.ts +++ b/src/modules/dpage/dpage.service.ts @@ -8,6 +8,7 @@ import { IDPageConfig } from "../../configs/dpage.config"; import { LoginDto } from './dto/login.dto'; import { DPAGE_CONFIG } from "./constants"; import { FindBusinessesDto } from "./dto/find-businesses.dto"; +import { UpdateDpageDto } from "./dto/update-dpage.dto"; @Injectable() @@ -17,7 +18,7 @@ export class DPageService { @Inject(DPAGE_CONFIG) private readonly config: IDPageConfig, private readonly httpService: HttpService, private readonly subscriptionService: SubscriptionsService, - ) { } + ) { } private getHeaders(): Record { const credentials = Buffer.from(`${this.config.username}:${this.config.password}`).toString("base64"); @@ -27,7 +28,7 @@ export class DPageService { }; } - async findAll(dto: FindBusinessesDto) { + async findAllBusinesses(dto: FindBusinessesDto) { try { // 2. Login with userSubscriptionId @@ -61,7 +62,7 @@ export class DPageService { } - async setupDpageAccount(dto: SetupAccountDto) { + async createDpageAccount(dto: createAccountDto) { try { const { data } = await firstValueFrom( this.httpService @@ -91,8 +92,64 @@ export class DPageService { } } - remove(id: number) { - return `This action removes a #${id} dpage`; + async updateDpageAccount(id: string, dto: UpdateDpageDto) { + try { + const { data } = await firstValueFrom( + this.httpService + .patch(`${this.config.baseUrl}/super-admin/business/${id}`, dto, + { + headers: this.getHeaders(), + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to perform update account: ${err.message}`, err.stack); + return throwError(() => err); + }), + ), + ); + return data; + } catch (error: unknown) { + if (error instanceof AxiosError && error.response) { + this.logger.error(`External API error response:`, error.response.data); + const errorResponse = { + ...error.response.data, + message: error.response.data.error?.message || error.message, + }; + throw new HttpException(errorResponse, error.response.status); + } + this.logger.error(`Error performing setup account: ${error instanceof Error ? error.message : "Unknown error"}`); + throw error; + } + } + + async removeAccount(id: string) { + try { + const { data } = await firstValueFrom( + this.httpService + .post(`${this.config.baseUrl}/super-admin/business/${id}`, + { + headers: this.getHeaders(), + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to hard delete business: ${err.message}`, err.stack); + return throwError(() => err); + }), + ), + ); + return data; + } catch (error: unknown) { + if (error instanceof AxiosError && error.response) { + this.logger.error(`External API error response:`, error.response.data); + const errorResponse = { + ...error.response.data, + message: error.response.data.error?.message || error.message, + }; + throw new HttpException(errorResponse, error.response.status); + } + this.logger.error(`Error performing hard delete: ${error instanceof Error ? error.message : "Unknown error"}`); + throw error; + } } async loginToDpage(dto: LoginDto, userId: string) { @@ -138,7 +195,7 @@ export class DPageService { } -export class SetupAccountDto { +export class createAccountDto { danakSubscriptionId: string; name: string; slug: string; diff --git a/src/modules/dpage/dto/update-dpage.dto.ts b/src/modules/dpage/dto/update-dpage.dto.ts index 65bdb45..ab857ef 100644 --- a/src/modules/dpage/dto/update-dpage.dto.ts +++ b/src/modules/dpage/dto/update-dpage.dto.ts @@ -1,4 +1,19 @@ -import { PartialType } from '@nestjs/swagger'; -import { LoginDto } from './login.dto'; +import { IsInt, IsOptional, IsString } from 'class-validator'; -export class UpdateDpageDto extends PartialType(LoginDto) {} +export class UpdateDpageDto { + @IsString() + @IsOptional() + danakSubscriptionId: string; + + @IsString() + @IsOptional() + name: string; + + @IsString() + @IsOptional() + slug: string; + + @IsInt() + @IsOptional() + maxCataloguesCount: number +} diff --git a/src/modules/invoices/providers/invoices.service.ts b/src/modules/invoices/providers/invoices.service.ts index f36ccf6..509ec0b 100755 --- a/src/modules/invoices/providers/invoices.service.ts +++ b/src/modules/invoices/providers/invoices.service.ts @@ -904,7 +904,7 @@ export class InvoicesService { try { this.logger.log(`Calling external API for dpage setup: subscription ${userSubscription.id}`); console.log('userSubscription.plan.count',userSubscription.plan.count) - await this.dpageService.setupDpageAccount({ + await this.dpageService.createDpageAccount({ danakSubscriptionId: userSubscription.id, name: userSubscription.businessName, slug: randomInt(100_000, 1_000_000).toString(),