From 22cf6104a5896bfa4c66156c79589169e59aad2d Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 4 Jan 2026 08:49:22 +0330 Subject: [PATCH] upgrade sub --- .../controllers/restaurants.controller.ts | 14 ++++++++++++++ .../dto/upgrade-subscription.dto.ts | 15 +++++++++++++++ .../providers/restaurants.service.ts | 18 +++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/modules/restaurants/dto/upgrade-subscription.dto.ts diff --git a/src/modules/restaurants/controllers/restaurants.controller.ts b/src/modules/restaurants/controllers/restaurants.controller.ts index 4876b40..360fb9f 100644 --- a/src/modules/restaurants/controllers/restaurants.controller.ts +++ b/src/modules/restaurants/controllers/restaurants.controller.ts @@ -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); + } + } diff --git a/src/modules/restaurants/dto/upgrade-subscription.dto.ts b/src/modules/restaurants/dto/upgrade-subscription.dto.ts new file mode 100644 index 0000000..4552f69 --- /dev/null +++ b/src/modules/restaurants/dto/upgrade-subscription.dto.ts @@ -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; +} diff --git a/src/modules/restaurants/providers/restaurants.service.ts b/src/modules/restaurants/providers/restaurants.service.ts index 98d0693..bd12d11 100644 --- a/src/modules/restaurants/providers/restaurants.service.ts +++ b/src/modules/restaurants/providers/restaurants.service.ts @@ -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 { + 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; + } }