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 { Notification } from '../../notifications/entities/notification.entity';
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class RestaurantsService {
|
||||
constructor(
|
||||
@@ -58,7 +59,7 @@ export class RestaurantsService {
|
||||
phone: dto.phone,
|
||||
isActive: true,
|
||||
plan: dto.plan,
|
||||
domain: `https://dmenu-plus-front.dev.danakcorp.com/${slug}`,
|
||||
domain: `https://dmenu.danakcorp.com/${slug}`,
|
||||
subscriptionId: dto.subscriptionId,
|
||||
subscriptionEndDate: dto.subscriptionEndDate,
|
||||
subscriptionStartDate: dto.subscriptionStartDate,
|
||||
@@ -138,7 +139,9 @@ export class RestaurantsService {
|
||||
}
|
||||
|
||||
async findOneBySubscriptionId(subscriptionId: string): Promise<Restaurant> {
|
||||
console.log('subscriptionId', subscriptionId)
|
||||
const restaurant = await this.restRepository.findOne({ subscriptionId });
|
||||
console.log('restaurant', restaurant)
|
||||
if (!restaurant) {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
@@ -149,7 +152,7 @@ export class RestaurantsService {
|
||||
const restaurant = await this.findBySlug(slug);
|
||||
return restaurant;
|
||||
}
|
||||
|
||||
// TODO : it must be done inside transaction
|
||||
async update(id: string, dto: UpdateRestaurantDto): Promise<Restaurant> {
|
||||
|
||||
const restaurant = await this.restRepository.findOne({ id: id });
|
||||
@@ -158,6 +161,31 @@ export class RestaurantsService {
|
||||
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);
|
||||
|
||||
await this.em.persistAndFlush(restaurant);
|
||||
@@ -186,10 +214,10 @@ export class RestaurantsService {
|
||||
throw new NotFoundException(RestMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
restaurant.plan = dto.newPlan;
|
||||
restaurant.subscriptionEndDate = dto.subscriptionEndDate;
|
||||
|
||||
await this.em.persistAndFlush(restaurant);
|
||||
await this.update(restaurant.id, {
|
||||
plan: dto.newPlan,
|
||||
subscriptionEndDate:dto.subscriptionEndDate
|
||||
})
|
||||
|
||||
return restaurant;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user