fix: bug in update sub plan
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
import { DataSource, In } from "typeorm";
|
import Decimal from "decimal.js";
|
||||||
|
import { DataSource, In, Not } from "typeorm";
|
||||||
|
|
||||||
import { ServiceMessage, SubscriptionMessage } from "../../../common/enums/message.enum";
|
import { ServiceMessage, SubscriptionMessage } from "../../../common/enums/message.enum";
|
||||||
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
|
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
|
||||||
@@ -99,25 +100,33 @@ export class SubscriptionsService {
|
|||||||
}
|
}
|
||||||
//************************************ */
|
//************************************ */
|
||||||
async updateSubscriptionPlan(id: string, updateDto: UpdateSubscriptionPlanDto) {
|
async updateSubscriptionPlan(id: string, updateDto: UpdateSubscriptionPlanDto) {
|
||||||
|
// Find the existing subscription plan
|
||||||
|
const subscription = await this.subscriptionsPlanRepository.findOne({ where: { id }, relations: { service: true } });
|
||||||
|
if (!subscription) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
||||||
|
|
||||||
|
// Check if service exists when trying to update it
|
||||||
if (updateDto.serviceId) {
|
if (updateDto.serviceId) {
|
||||||
const danakService = await this.danakServices.findServiceById(updateDto.serviceId);
|
const danakService = await this.danakServices.findServiceById(updateDto.serviceId);
|
||||||
if (!danakService) throw new BadRequestException(ServiceMessage.SERVICE_NOT_FOUND_BY_ID);
|
if (!danakService) throw new BadRequestException(ServiceMessage.SERVICE_NOT_FOUND_BY_ID);
|
||||||
|
subscription.service = danakService;
|
||||||
}
|
}
|
||||||
|
|
||||||
const subscription = await this.subscriptionsPlanRepository.findOneBy({ id });
|
// Check for name uniqueness if updating name
|
||||||
if (!subscription) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
|
||||||
|
|
||||||
if (updateDto.name) {
|
if (updateDto.name) {
|
||||||
const existSubscription = await this.subscriptionsPlanRepository.findOneByName(updateDto.name, id);
|
const existSubscription = await this.subscriptionsPlanRepository.findOneBy({
|
||||||
|
name: updateDto.name,
|
||||||
|
id: Not(id),
|
||||||
|
service: { id: subscription.service.id },
|
||||||
|
});
|
||||||
if (existSubscription) throw new BadRequestException(SubscriptionMessage.NAME_EXIST);
|
if (existSubscription) throw new BadRequestException(SubscriptionMessage.NAME_EXIST);
|
||||||
|
subscription.name = updateDto.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.subscriptionsPlanRepository.update(id, updateDto);
|
if (updateDto.duration) subscription.duration = updateDto.duration;
|
||||||
await this.subscriptionsPlanRepository.save({
|
if (updateDto.price) subscription.price = new Decimal(updateDto.price);
|
||||||
...subscription,
|
|
||||||
...updateDto,
|
await this.subscriptionsPlanRepository.save(subscription);
|
||||||
service: updateDto.serviceId ? { id: updateDto.serviceId } : subscription.service,
|
|
||||||
});
|
|
||||||
return {
|
return {
|
||||||
message: SubscriptionMessage.UPDATED,
|
message: SubscriptionMessage.UPDATED,
|
||||||
subscription,
|
subscription,
|
||||||
|
|||||||
Reference in New Issue
Block a user