From a0437b02af537c615d15ec42407a68b091c27d43 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Mon, 5 Jan 2026 09:08:51 +0330 Subject: [PATCH] add : direct login --- src/modules/dmenu/DTO/direct-login.dto.ts | 12 +++++++ src/modules/dmenu/dmenu.controller.ts | 7 ++++ .../dmenu/providers/restaurant.service.ts | 35 ++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/modules/dmenu/DTO/direct-login.dto.ts diff --git a/src/modules/dmenu/DTO/direct-login.dto.ts b/src/modules/dmenu/DTO/direct-login.dto.ts new file mode 100644 index 0000000..f583d1f --- /dev/null +++ b/src/modules/dmenu/DTO/direct-login.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsString } from 'class-validator'; + +export class DirectLoginDto { + @ApiProperty({ example: '09123456789', description: 'شماره تلفن کاربر' }) + @IsString() + phone!: string; + + @ApiProperty({ example: 'zhivan', description: 'شناسه (slug) رستوران' }) + @IsString() + slug!: string; +} diff --git a/src/modules/dmenu/dmenu.controller.ts b/src/modules/dmenu/dmenu.controller.ts index c88b913..1c091b0 100644 --- a/src/modules/dmenu/dmenu.controller.ts +++ b/src/modules/dmenu/dmenu.controller.ts @@ -12,6 +12,7 @@ import { FindRestaurantsDto } from "./DTO/find-restaurants.dto"; import { SetupRestaurantDto } from "./DTO/create-restaurant.dto"; import { UpdateRestaurantDto } from "./DTO/update-restaurant.dto"; import { CreateMyRestaurantAdminDto } from "./DTO/create-restaurant-admin.dto"; +import { DirectLoginDto } from "./DTO/direct-login.dto"; import { AuthGuards } from "../../common/decorators/auth-guard.decorator"; import { PermissionsDec } from "../../common/decorators/permission.decorator"; import { PermissionEnum } from "../users/enums/permission.enum"; @@ -194,4 +195,10 @@ export class DmenuController { getRestaurantSubscription(@Param("subscriptionId") subscriptionId: string) { return this.restaurantService.getRestaurantSubscription(subscriptionId); } + + @Post('dmenu/auth/direct-login') + @ApiOperation({ summary: "Direct login with phone number and restaurant slug" }) + directLogin(@Body() dto: DirectLoginDto) { + return this.restaurantService.directLogin(dto); + } } diff --git a/src/modules/dmenu/providers/restaurant.service.ts b/src/modules/dmenu/providers/restaurant.service.ts index 25f1e43..6ca5543 100644 --- a/src/modules/dmenu/providers/restaurant.service.ts +++ b/src/modules/dmenu/providers/restaurant.service.ts @@ -13,6 +13,7 @@ import { CreateMyRestaurantAdminDto } from "../DTO/create-restaurant-admin.dto"; import { PlanEnum, ISetupRestaurant, IExternalApiResponse } from "../interface/plan.interface"; import { SubscriptionsService } from "../../subscriptions/providers/subscriptions.service"; import { SubscriptionMessage } from "../../../common/enums/message.enum"; +import { DirectLoginDto } from "../DTO/direct-login.dto"; @Injectable() export class RestaurantService { @@ -308,7 +309,7 @@ export class RestaurantService { } async assignRestaurantAdmin(restaurantId: string, dto: CreateMyRestaurantAdminDto) { - try { + try { const { data } = await firstValueFrom( this.httpService .post(`${this.config.baseUrl}/super-admin/restaurants/${restaurantId}/admins`, dto, { @@ -367,4 +368,36 @@ export class RestaurantService { throw error; } } + + async directLogin(dto: DirectLoginDto) { + try { + const { data } = await firstValueFrom( + this.httpService + .post(`${this.config.baseUrl}/super-admin/auth/direct-login`, { + phone: dto.phone, + slug: dto.slug, + }, { + headers: this.getHeaders(), + }) + .pipe( + catchError((err: AxiosError) => { + this.logger.error(`Failed to perform direct login: ${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 direct login: ${error instanceof Error ? error.message : "Unknown error"}`); + throw error; + } + } }