From f965e47bfd571c093a1c5a8491c85c93e5fa4ae0 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sun, 15 Feb 2026 14:05:32 +0330 Subject: [PATCH] fix : update shop --- src/common/enums/message.enum.ts | 3 +- src/modules/auth/services/auth.service.ts | 8 ++--- .../coupons/providers/coupon.service.ts | 6 ++-- .../notification-preference.service.ts | 4 +-- .../services/payment-method.service.ts | 4 +-- .../products/providers/category.service.ts | 4 +-- .../review/providers/review.service.ts | 4 +-- src/modules/roles/providers/roles.service.ts | 4 +-- .../shops/providers/schedule.service.ts | 6 ++-- src/modules/shops/providers/shops.service.ts | 34 +++++++++++-------- src/modules/users/providers/user.service.ts | 6 ++-- 11 files changed, 45 insertions(+), 38 deletions(-) diff --git a/src/common/enums/message.enum.ts b/src/common/enums/message.enum.ts index c9080f5..8f38bcf 100755 --- a/src/common/enums/message.enum.ts +++ b/src/common/enums/message.enum.ts @@ -424,7 +424,7 @@ export const enum AdminMessage { ADMIN_ROLE_NOT_FOUND = 'نقش ادمین یافت نشد', } -export const enum RestMessage { +export const enum ShopMessage { NOT_FOUND = 'کسب و کار یافت نشد', DUPLICATE_SUBSCRIPTION_ID = 'شناسه اشتراک تکراری است', SYSTEM_ROLE_NOT_FOUND = 'نقش سیستمی یافت نشد', @@ -466,6 +466,7 @@ export const enum RestMessage { PAYMENT_GATEWAY_ERROR = 'خطا در اتصال به درگاه پرداخت', QUOTA_UPDATED = 'حجم ایمیل با موفقیت به روز شد', USER_DOES_NOT_HAVE_ACCESS_TO_SERVICE = 'کاربر دسترسی به این سرویس را ندارد', + SHOP_EXIST_WITH_THIS_SLUG='فروشگاه با این اسلاگ وجود دارد' } export const enum DomainMessage { diff --git a/src/modules/auth/services/auth.service.ts b/src/modules/auth/services/auth.service.ts index 2d4815d..6d08593 100644 --- a/src/modules/auth/services/auth.service.ts +++ b/src/modules/auth/services/auth.service.ts @@ -6,7 +6,7 @@ import { UserService } from 'src/modules/users/providers/user.service'; import { randomInt } from 'crypto'; import { TokensService } from './tokens.service'; import { ShopRepository } from 'src/modules/shops/repositories/rest.repository'; -import { AuthMessage, RestMessage } from 'src/common/enums/message.enum'; +import { AuthMessage, ShopMessage } from 'src/common/enums/message.enum'; import { AdminRepository } from 'src/modules/admin/repositories/admin.repository'; import { AdminLoginTransformer } from '../transformers/admin-login.transformer'; import { UserLoginTransformer } from '../transformers/user-login.transformer'; @@ -71,7 +71,7 @@ export class AuthService { const shop = await this.shopRepository.findOne({ slug }); if (!shop) { - throw new BadRequestException(RestMessage.NOT_FOUND); + throw new BadRequestException(ShopMessage.NOT_FOUND); } const tokens = await this.tokensService.generateTokens(user.id, shop.id, false, slug); @@ -96,7 +96,7 @@ export class AuthService { const shop = await this.shopRepository.findOne({ slug }); if (!shop) { - throw new BadRequestException(RestMessage.NOT_FOUND); + throw new BadRequestException(ShopMessage.NOT_FOUND); } const tokens = await this.tokensService.generateTokens(admin.id, shop.id, true, slug); @@ -120,7 +120,7 @@ export class AuthService { const shop = await this.shopRepository.findOne({ slug }); if (!shop) { - throw new BadRequestException(RestMessage.NOT_FOUND); + throw new BadRequestException(ShopMessage.NOT_FOUND); } const tokens = await this.tokensService.generateTokens(admin.id, shop.id, true, slug); diff --git a/src/modules/coupons/providers/coupon.service.ts b/src/modules/coupons/providers/coupon.service.ts index 20aef1e..0573700 100644 --- a/src/modules/coupons/providers/coupon.service.ts +++ b/src/modules/coupons/providers/coupon.service.ts @@ -8,7 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData } from '@mikro-orm/core'; import { Coupon } from '../entities/coupon.entity'; import { CouponType } from '../interface/coupon'; -import { CouponMessage, RestMessage } from 'src/common/enums/message.enum'; +import { CouponMessage, ShopMessage } from 'src/common/enums/message.enum'; import { randomBytes } from 'node:crypto'; @Injectable() @@ -22,7 +22,7 @@ export class CouponService { async create(shopId: string, createCouponDto: CreateCouponDto): Promise { const shop = await this.shopRepository.findOne({ id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } // Check if code already exists @@ -260,7 +260,7 @@ export class CouponService { async generateCouponCode(shopId: string): Promise<{ code: string }> { const shop = await this.shopRepository.findOne({ id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const code = this.generateShortCode(shop.slug); const existing = await this.couponRepository.findOne({ code, shop: { id: shopId } }); diff --git a/src/modules/notifications/services/notification-preference.service.ts b/src/modules/notifications/services/notification-preference.service.ts index 41fcba3..20e4b2b 100644 --- a/src/modules/notifications/services/notification-preference.service.ts +++ b/src/modules/notifications/services/notification-preference.service.ts @@ -5,7 +5,7 @@ import { Shop } from '../../shops/entities/shop.entity'; import { CreatePreferenceDto } from '../dto/create-preference.dto'; import { UpdatePreferenceDto } from '../dto/update-preference.dto'; import { NotifTitleEnum } from '../interfaces/notification.interface'; -import { NotificationMessage, RestMessage } from 'src/common/enums/message.enum'; +import { NotificationMessage, ShopMessage } from 'src/common/enums/message.enum'; @Injectable() export class NotificationPreferenceService { @@ -14,7 +14,7 @@ export class NotificationPreferenceService { async create(shopId: string, dto: CreatePreferenceDto): Promise { const shop = await this.em.findOne(Shop, { id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const preference = this.em.create(NotificationPreference, { diff --git a/src/modules/payments/services/payment-method.service.ts b/src/modules/payments/services/payment-method.service.ts index c0428c8..913f1a2 100644 --- a/src/modules/payments/services/payment-method.service.ts +++ b/src/modules/payments/services/payment-method.service.ts @@ -6,7 +6,7 @@ import { CreatePaymentMethodDto } from '../dto/create-payment-method.dto'; import { UpdatePaymentMethodDto } from '../dto/update-payment-method.dto'; import { RequiredEntityData } from '@mikro-orm/core'; import { Shop } from '../../shops/entities/shop.entity'; -import { RestMessage, PaymentMessage } from 'src/common/enums/message.enum'; +import { ShopMessage, PaymentMessage } from 'src/common/enums/message.enum'; @Injectable() export class PaymentMethodService { @@ -19,7 +19,7 @@ export class PaymentMethodService { // Check if shop exists const shop = await this.em.findOne(Shop, { id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const paymentMethod = this.paymentMethodRepository.create({ diff --git a/src/modules/products/providers/category.service.ts b/src/modules/products/providers/category.service.ts index c24c056..637342f 100644 --- a/src/modules/products/providers/category.service.ts +++ b/src/modules/products/providers/category.service.ts @@ -5,7 +5,7 @@ import { CategoryRepository } from '../repositories/category.repository'; import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData } from '@mikro-orm/core'; import { Category } from '../entities/category.entity'; -import { CategoryMessage, RestMessage } from 'src/common/enums/message.enum'; +import { CategoryMessage, ShopMessage } from 'src/common/enums/message.enum'; import { Shop } from '../../shops/entities/shop.entity'; @Injectable() @@ -18,7 +18,7 @@ export class CategoryService { async create(shopId: string, dto: CreateCategoryDto): Promise { const shop = await this.em.findOne(Shop, { id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } let parent: Category | undefined; diff --git a/src/modules/review/providers/review.service.ts b/src/modules/review/providers/review.service.ts index 336a086..fd84ada 100644 --- a/src/modules/review/providers/review.service.ts +++ b/src/modules/review/providers/review.service.ts @@ -8,7 +8,7 @@ import { UserRepository } from '../../users/repositories/user.repository'; import { EntityManager } from '@mikro-orm/postgresql'; import { RequiredEntityData } from '@mikro-orm/core'; import { Review } from '../entities/review.entity'; -import { OrderMessage, ProductMessage, RestMessage, UserMessage, ReviewMessage } from 'src/common/enums/message.enum'; +import { OrderMessage, ProductMessage, ShopMessage, UserMessage, ReviewMessage } from 'src/common/enums/message.enum'; import { Order } from '../../orders/entities/order.entity'; import { OrderItem } from '../../orders/entities/order-item.entity'; import { Shop } from '../../shops/entities/shop.entity'; @@ -115,7 +115,7 @@ export class ReviewService { const { shopSlug: restuarantSlug, ...restDto } = dto; const shop = await this.em.findOne(Shop, { slug: restuarantSlug }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } return this.reviewRepository.findAllPaginated({ ...restDto, shopId: shop.id }); } diff --git a/src/modules/roles/providers/roles.service.ts b/src/modules/roles/providers/roles.service.ts index 7f34716..0c37a7d 100644 --- a/src/modules/roles/providers/roles.service.ts +++ b/src/modules/roles/providers/roles.service.ts @@ -8,7 +8,7 @@ import { EntityManager } from '@mikro-orm/postgresql'; import { CreateRoleDto } from '../dto/create-role.dto'; import { UpdateRoleDto } from '../dto/update-role.dto'; import { RolePermission } from '../entities/rolePermission.entity'; -import { RestMessage, RoleMessage } from 'src/common/enums/message.enum'; +import { ShopMessage, RoleMessage } from 'src/common/enums/message.enum'; @Injectable() export class RolesService { @@ -33,7 +33,7 @@ export class RolesService { if (shopId) { shop = await this.em.findOne(Shop, { id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } } diff --git a/src/modules/shops/providers/schedule.service.ts b/src/modules/shops/providers/schedule.service.ts index 36005ba..cbb6fc9 100644 --- a/src/modules/shops/providers/schedule.service.ts +++ b/src/modules/shops/providers/schedule.service.ts @@ -8,7 +8,7 @@ import { UpdateScheduleDto } from '../dto/update-schedule.dto'; import { FindSchedulesDto } from '../dto/find-schedules.dto'; import { ShopRepository } from '../repositories/rest.repository'; import { Shop } from '../entities/shop.entity'; -import { RestMessage } from 'src/common/enums/message.enum'; +import { ShopMessage } from 'src/common/enums/message.enum'; // import { RestMessage } from 'src/common/enums/message.enum'; @Injectable() @@ -51,7 +51,7 @@ export class ScheduleService { async findTodaySchedules(slug: string): Promise { const shop = await this.em.findOne(Shop, { slug }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const today = new Date(); const weekDay = today.getDay(); // 0 = Sunday, 6 = Saturday @@ -68,7 +68,7 @@ export class ScheduleService { async findScheduleBySlug(slug: string): Promise { const shop = await this.em.findOne(Shop, { slug }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const where: FilterQuery = { diff --git a/src/modules/shops/providers/shops.service.ts b/src/modules/shops/providers/shops.service.ts index 5971de6..c470f00 100644 --- a/src/modules/shops/providers/shops.service.ts +++ b/src/modules/shops/providers/shops.service.ts @@ -5,7 +5,7 @@ import { UpdateSubscriptionDto } from '../dto/update-subscription.dto'; import { Shop } from '../entities/shop.entity'; import { EntityManager } from '@mikro-orm/postgresql'; import { ShopRepository } from '../repositories/rest.repository'; -import { RestMessage } from 'src/common/enums/message.enum'; +import { ShopMessage } from 'src/common/enums/message.enum'; import { FindRestaurantsDto } from '../dto/find-shops.dto'; import { PaginatedResult } from 'src/common/interfaces/pagination.interface'; import { Admin } from '../../admin/entities/admin.entity'; @@ -46,7 +46,7 @@ export class ShopService { const validateSubscriptionId = await em.findOne(Shop, { subscriptionId: dto.subscriptionId }) if (validateSubscriptionId) { - throw new BadRequestException(RestMessage.DUPLICATE_SUBSCRIPTION_ID); + throw new BadRequestException(ShopMessage.DUPLICATE_SUBSCRIPTION_ID); } // Create shop @@ -64,7 +64,7 @@ export class ShopService { const role = await em.findOne(Role, { isSystem: true }); if (!role) { - throw new BadRequestException(RestMessage.SYSTEM_ROLE_NOT_FOUND); + throw new BadRequestException(ShopMessage.SYSTEM_ROLE_NOT_FOUND); } const normalizedPhone = normalizePhone(dto.phone); @@ -116,7 +116,7 @@ export class ShopService { const shop = await this.em.findOne(Shop, { slug }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } return shop; @@ -126,7 +126,7 @@ export class ShopService { const shop = await this.shopRepository.findOne({ id }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } return shop; @@ -137,7 +137,7 @@ export class ShopService { const shop = await this.shopRepository.findOne({ subscriptionId }); console.log('shop', shop) if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } return shop; } @@ -146,19 +146,25 @@ export class ShopService { const shop = await this.findBySlug(slug); return shop; } - // TODO : it must be done inside transaction - async update(id: string, dto: UpdateRestaurantDto): Promise { + async update(id: string, dto: UpdateRestaurantDto): Promise { const shop = await this.shopRepository.findOne({ id: id }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } + if (dto.slug) { + const shopWithThisSlug = await this.shopRepository.findOne({ slug: dto.slug, $not: { id } }); + + if (shopWithThisSlug) { + throw new NotFoundException(ShopMessage.SHOP_EXIST_WITH_THIS_SLUG); + } + } this.shopRepository.assign(shop, dto); - await this.em.persistAndFlush(shop); + await this.em.flush(); return shop; } @@ -168,7 +174,7 @@ export class ShopService { const shop = await this.shopRepository.findOne({ subscriptionId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const isActive = new Date(dto.subscriptionEndDate) > new Date() @@ -187,7 +193,7 @@ export class ShopService { const shop = await em.findOne(Shop, { id }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } // Delete orders first (will cascade to order items and payments due to cascade settings) @@ -235,7 +241,7 @@ export class ShopService { async findOrFail(shopId: string) { const shop = await this.shopRepository.findOne({ id: shopId }, { populate: ['deliveries'] }) if (!shop) { - throw new BadRequestException(RestMessage.NOT_FOUND); + throw new BadRequestException(ShopMessage.NOT_FOUND); } return shop } @@ -243,7 +249,7 @@ export class ShopService { async findOrFailBySlug(shopSlug: string) { const shop = await this.shopRepository.findOne({ slug: shopSlug }, { populate: ['deliveries'] }) if (!shop) { - throw new BadRequestException(RestMessage.NOT_FOUND); + throw new BadRequestException(ShopMessage.NOT_FOUND); } return shop } diff --git a/src/modules/users/providers/user.service.ts b/src/modules/users/providers/user.service.ts index 4f19526..40d738f 100644 --- a/src/modules/users/providers/user.service.ts +++ b/src/modules/users/providers/user.service.ts @@ -18,7 +18,7 @@ import { WalletTransactionRepository } from '../repositories/wallet-transaction. import { WalletService } from './wallet.service'; import { WalletTransactionReason, WalletTransactionType } from '../interface/wallet'; import { WalletTransaction } from '../entities/wallet-transaction.entity'; -import { RestMessage, UserMessage } from 'src/common/enums/message.enum'; +import { ShopMessage, UserMessage } from 'src/common/enums/message.enum'; @Injectable() export class UserService { @@ -87,7 +87,7 @@ export class UserService { // get registerscore from shop const shop = await this.restaurantRepository.findOne({ id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } const points = Number(shop.score?.registerScore || 0); const createData = _raw as unknown as RequiredEntityData; @@ -315,7 +315,7 @@ export class UserService { } const shop = await em.findOne(Shop, { id: shopId }); if (!shop) { - throw new NotFoundException(RestMessage.NOT_FOUND); + throw new NotFoundException(ShopMessage.NOT_FOUND); } let walletTransaction = await em.findOne(WalletTransaction, { user: { id: userId }, shop: { id: shopId } }, { orderBy: { createdAt: 'DESC' } });