remove unused

This commit is contained in:
2026-01-05 20:40:51 +03:30
parent f0eae80342
commit 57612d734e
2 changed files with 2 additions and 125 deletions
@@ -3,8 +3,6 @@ import { BadRequestException, HttpException, Injectable, Logger } from "@nestjs/
import { Inject } from "@nestjs/common";
import { AxiosError } from "axios";
import { catchError, firstValueFrom, throwError } from "rxjs";
import { DataSource } from "typeorm";
import slugify from "slugify";
import { IDmenuConfig } from "../../../configs/icons.config";
import { DMENU_CONFIG } from "../constants";
@@ -15,12 +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 { UsersService } from "../../users/providers/users.service";
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
import { User } from "../../users/entities/user.entity";
import { Role } from "../../users/entities/role.entity";
import { RoleEnum } from "../../users/enums/role.enum";
import { SubscribeServiceDto } from "../../subscriptions/DTO/subscribe-service.dto";
@Injectable()
export class RestaurantService {
@@ -30,9 +23,6 @@ export class RestaurantService {
@Inject(DMENU_CONFIG) private readonly config: IDmenuConfig,
private readonly httpService: HttpService,
private readonly subscriptionService: SubscriptionsService,
private readonly usersService: UsersService,
private readonly danakServicesService: DanakServicesService,
private readonly dataSource: DataSource,
) { }
private getHeaders(): Record<string, string> {
@@ -347,116 +337,9 @@ export class RestaurantService {
}
}
private async updateRestaurantSubscription(restaurantId: string, subscriptionId: string, subscriptionExpiryDate: Date) {
try {
const { data } = await firstValueFrom(
this.httpService
.patch(`${this.config.baseUrl}/super-admin/restaurants/${restaurantId}/subscription`, {
subscriptionId,
subscriptionExpiryDate,
}, {
headers: this.getHeaders(),
})
.pipe(
catchError((err: AxiosError) => {
this.logger.error(`Failed to update restaurant subscription: ${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 updating restaurant subscription: ${error instanceof Error ? error.message : "Unknown error"}`);
throw error;
}
}
private async findOrCreateUserByPhone(phone: string, dto: CreateMyRestaurantAdminDto) {
let user = await this.usersService.findOneWithPhone(phone);
if (!user) {
// Create a basic user for restaurant admin assignment
const queryRunner = this.dataSource.createQueryRunner();
try {
await queryRunner.connect();
await queryRunner.startTransaction();
const role = await queryRunner.manager.findOneBy(Role, { name: RoleEnum.USER });
if (!role) throw new BadRequestException("User role not found");
const userName = slugify(`u-${phone}`, { lower: true, trim: true });
const defaultFirstName = "کاربر";
const defaultLastName = userName;
user = queryRunner.manager.create(User, {
phone,
userName,
password: "temp_password", // This should be changed later
firstName: dto.firstName || defaultFirstName,
lastName: dto.lastName || defaultLastName,
roles: [role],
});
await queryRunner.manager.save(user);
// Note: Skipping user settings and wallet creation for now as they might be optional for this use case
await queryRunner.commitTransaction();
} catch (error) {
await queryRunner.rollbackTransaction();
throw error;
} finally {
await queryRunner.release();
}
}
return user;
}
async assignRestaurantAdmin(restaurantId: string, dto: CreateMyRestaurantAdminDto) {
try {
// 1. Create user with phone if not exists
const user = await this.findOrCreateUserByPhone(dto.phone, dto);
// 2. Get restaurant by id
const restaurant = await this.getRestaurantById(restaurantId);
// 3. If restaurant doesn't have subscription id, create user subscription with free plan of dmenu service
let subscriptionId = restaurant.subscriptionId;
let subscriptionExpiryDate = restaurant.subscriptionExpiryDate;
if (!subscriptionId) {
const dmenuService = await this.danakServicesService.getServiceByName("dmenu");
const freePlan = await this.subscriptionService['subscriptionsPlanRepository'].findOne({
where: { service: { id: dmenuService.id }, isFree: true },
});
if (!freePlan) {
throw new BadRequestException("Free plan not found for dmenu service");
}
const subscriptionDto: SubscribeServiceDto = {
planId: freePlan.id,
businessName: restaurant.name || `Restaurant ${restaurantId}`,
businessPhone: dto.phone,
description: restaurant.description || `Restaurant admin for ${restaurant.name || restaurantId}`,
slug: slugify(restaurant.name || `restaurant-${restaurantId}`, { lower: true, strict: true }),
};
const subscriptionResult = await this.subscriptionService.subscribeToPlan(dmenuService.id, subscriptionDto, user.id);
subscriptionId = subscriptionResult.userSubscription.id;
subscriptionExpiryDate = subscriptionResult.userSubscription.endDate;
}
// 4. Update restaurant with subscription id and expiry date
await this.updateRestaurantSubscription(restaurantId, subscriptionId, subscriptionExpiryDate);
// 5. Assign restaurant admin
const { data } = await firstValueFrom(
this.httpService
.post(`${this.config.baseUrl}/super-admin/restaurants/${restaurantId}/admins`, dto, {
@@ -472,9 +355,6 @@ export class RestaurantService {
return {
...data,
user,
subscriptionId,
subscriptionExpiryDate,
};
} catch (error: unknown) {
if (error instanceof AxiosError && error.response) {