upgrade sub

This commit is contained in:
2026-01-04 08:49:22 +03:30
parent 25d121f272
commit 22cf6104a5
3 changed files with 46 additions and 1 deletions
@@ -2,6 +2,7 @@ import { Controller, Get, Post, Body, Patch, Param, UseGuards, Delete, Query } f
import { RestaurantsService } from '../providers/restaurants.service';
import { CreateRestaurantDto } from '../dto/create-restaurant.dto';
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
import { UpgradeSubscriptionDto } from '../dto/upgrade-subscription.dto';
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import {
@@ -99,4 +100,17 @@ export class RestaurantsController {
return this.restaurantsService.update(id, updateRestaurantDto);
}
@UseGuards(SuperAdminAuthGuard)
@ApiBearerAuth()
@ApiOperation({ summary: 'Upgrade subscription for a restaurant' })
@ApiParam({ name: 'subscriptionId', required: true, description: 'Subscription ID' })
@ApiBody({ type: UpgradeSubscriptionDto })
@Patch('super-admin/restaurants/subscription/:subscriptionId/upgrade')
upgradeSubscription(
@Param('subscriptionId') subscriptionId: string,
@Body() upgradeSubscriptionDto: UpgradeSubscriptionDto
) {
return this.restaurantsService.upgradeSubscription(subscriptionId, upgradeSubscriptionDto);
}
}
@@ -0,0 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsString, IsDate, IsEnum } from 'class-validator';
import { Type } from 'class-transformer';
import { PlanEnum } from '../interface/plan.interface';
export class UpgradeSubscriptionDto {
@ApiProperty({ example: 'premium', enum: PlanEnum, description: 'New plan for the subscription' })
@IsEnum(PlanEnum)
newPlan!: PlanEnum;
@ApiProperty({ example: '2025-12-31', description: 'New subscription end date' })
@Type(() => Date)
@IsDate()
subscriptionEndDate!: Date;
}
@@ -1,6 +1,7 @@
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
import { CreateRestaurantDto } from '../dto/create-restaurant.dto';
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
import { UpgradeSubscriptionDto } from '../dto/upgrade-subscription.dto';
import { Restaurant } from '../entities/restaurant.entity';
import { EntityManager } from '@mikro-orm/postgresql';
import { RestRepository } from '../repositories/rest.repository';
@@ -53,7 +54,7 @@ export class RestaurantsService {
throw new BadRequestException(`Role not found for plan: ${dto.plan}`);
}
const normalizedPhone = normalizePhone(dto.phone);
const normalizedPhone = normalizePhone(dto.phone);
let admin = await em.findOne(Admin, { phone: normalizedPhone });
if (!admin) {
admin = em.create(Admin, {
@@ -150,4 +151,19 @@ const normalizedPhone = normalizePhone(dto.phone);
return restaurant;
}
async upgradeSubscription(subscriptionId: string, dto: UpgradeSubscriptionDto): Promise<Restaurant> {
const restaurant = await this.restRepository.findOne({ subscriptionId });
if (!restaurant) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
restaurant.plan = dto.newPlan;
restaurant.subscriptionEndDate = dto.subscriptionEndDate;
await this.em.persistAndFlush(restaurant);
return restaurant;
}
}