up rest
This commit is contained in:
@@ -28,6 +28,7 @@ import { WalletTransaction } from '../../users/entities/wallet-transaction.entit
|
|||||||
import { SmsLog } from '../../notifications/entities/smsLogs.entity';
|
import { SmsLog } from '../../notifications/entities/smsLogs.entity';
|
||||||
import { Notification } from '../../notifications/entities/notification.entity';
|
import { Notification } from '../../notifications/entities/notification.entity';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RestaurantsService {
|
export class RestaurantsService {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -58,7 +59,7 @@ export class RestaurantsService {
|
|||||||
phone: dto.phone,
|
phone: dto.phone,
|
||||||
isActive: true,
|
isActive: true,
|
||||||
plan: dto.plan,
|
plan: dto.plan,
|
||||||
domain: `https://dmenu-plus-front.dev.danakcorp.com/${slug}`,
|
domain: `https://dmenu.danakcorp.com/${slug}`,
|
||||||
subscriptionId: dto.subscriptionId,
|
subscriptionId: dto.subscriptionId,
|
||||||
subscriptionEndDate: dto.subscriptionEndDate,
|
subscriptionEndDate: dto.subscriptionEndDate,
|
||||||
subscriptionStartDate: dto.subscriptionStartDate,
|
subscriptionStartDate: dto.subscriptionStartDate,
|
||||||
@@ -138,7 +139,9 @@ export class RestaurantsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findOneBySubscriptionId(subscriptionId: string): Promise<Restaurant> {
|
async findOneBySubscriptionId(subscriptionId: string): Promise<Restaurant> {
|
||||||
|
console.log('subscriptionId', subscriptionId)
|
||||||
const restaurant = await this.restRepository.findOne({ subscriptionId });
|
const restaurant = await this.restRepository.findOne({ subscriptionId });
|
||||||
|
console.log('restaurant', restaurant)
|
||||||
if (!restaurant) {
|
if (!restaurant) {
|
||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -149,7 +152,7 @@ export class RestaurantsService {
|
|||||||
const restaurant = await this.findBySlug(slug);
|
const restaurant = await this.findBySlug(slug);
|
||||||
return restaurant;
|
return restaurant;
|
||||||
}
|
}
|
||||||
|
// TODO : it must be done inside transaction
|
||||||
async update(id: string, dto: UpdateRestaurantDto): Promise<Restaurant> {
|
async update(id: string, dto: UpdateRestaurantDto): Promise<Restaurant> {
|
||||||
|
|
||||||
const restaurant = await this.restRepository.findOne({ id: id });
|
const restaurant = await this.restRepository.findOne({ id: id });
|
||||||
@@ -158,6 +161,31 @@ export class RestaurantsService {
|
|||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dto.plan && dto.plan !== restaurant.plan) {
|
||||||
|
const isPremium = dto.plan === PlanEnum.Premium
|
||||||
|
// find admins and make them premium or base
|
||||||
|
const adminRoles = await this.em.find(AdminRole, {
|
||||||
|
restaurant: {
|
||||||
|
id
|
||||||
|
},
|
||||||
|
role: {
|
||||||
|
name: isPremium ? 'مدیر (پلن پایه)' : 'مدیر ( پلن ویژه)'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const newRole = await this.em.findOne(Role, {
|
||||||
|
name: isPremium ? 'مدیر ( پلن ویژه)' : 'مدیر (پلن پایه)'
|
||||||
|
})
|
||||||
|
if (!newRole) {
|
||||||
|
throw new BadRequestException("Role Not Found")
|
||||||
|
}
|
||||||
|
adminRoles.forEach(a => {
|
||||||
|
a.role = newRole
|
||||||
|
})
|
||||||
|
|
||||||
|
this.em.persistAndFlush(adminRoles)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
this.restRepository.assign(restaurant, dto);
|
this.restRepository.assign(restaurant, dto);
|
||||||
|
|
||||||
await this.em.persistAndFlush(restaurant);
|
await this.em.persistAndFlush(restaurant);
|
||||||
@@ -186,10 +214,10 @@ export class RestaurantsService {
|
|||||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
restaurant.plan = dto.newPlan;
|
await this.update(restaurant.id, {
|
||||||
restaurant.subscriptionEndDate = dto.subscriptionEndDate;
|
plan: dto.newPlan,
|
||||||
|
subscriptionEndDate:dto.subscriptionEndDate
|
||||||
await this.em.persistAndFlush(restaurant);
|
})
|
||||||
|
|
||||||
return restaurant;
|
return restaurant;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user