From 835f2a3853aa91c79893deb5af01eb6e04cd42d6 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 13 Dec 2025 21:39:35 +0330 Subject: [PATCH] up --- .../restaurants/dto/restaurant-specification.dto.ts | 5 +++++ .../restaurants/providers/restaurants.service.ts | 3 +-- src/modules/users/entities/user-wallet.entity.ts | 3 ++- src/modules/users/providers/user.service.ts | 12 ++++++++---- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/modules/restaurants/dto/restaurant-specification.dto.ts b/src/modules/restaurants/dto/restaurant-specification.dto.ts index 14c341a..bfd4ac0 100644 --- a/src/modules/restaurants/dto/restaurant-specification.dto.ts +++ b/src/modules/restaurants/dto/restaurant-specification.dto.ts @@ -84,4 +84,9 @@ export class RestaurantSpecificationDto { @ApiPropertyOptional({ example: 0.09, description: 'VAT rate (e.g., 0.09 for 9%)' }) vat?: number; + + @ApiPropertyOptional({ example: { registerScore: 100 }, description: 'Score' }) + score?: { + registerScore?: number; + }; } diff --git a/src/modules/restaurants/providers/restaurants.service.ts b/src/modules/restaurants/providers/restaurants.service.ts index 7bc7e8e..2982a03 100644 --- a/src/modules/restaurants/providers/restaurants.service.ts +++ b/src/modules/restaurants/providers/restaurants.service.ts @@ -1,8 +1,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { CreateRestaurantDto } from '../dto/create-restaurant.dto'; import { UpdateRestaurantDto } from '../dto/update-restaurant.dto'; -import { RestaurantSpecificationDto } from '../dto/restaurant-specification.dto'; -import { Restaurant } from '../entities/restaurant.entity'; + import { Restaurant } from '../entities/restaurant.entity'; import { EntityManager } from '@mikro-orm/postgresql'; import { RestRepository } from '../repositories/rest.repository'; import { RestMessage } from 'src/common/enums/message.enum'; diff --git a/src/modules/users/entities/user-wallet.entity.ts b/src/modules/users/entities/user-wallet.entity.ts index 977e45b..fd328ef 100644 --- a/src/modules/users/entities/user-wallet.entity.ts +++ b/src/modules/users/entities/user-wallet.entity.ts @@ -1,9 +1,10 @@ -import { Entity, Property, ManyToOne, Index } from '@mikro-orm/core'; +import { Entity, Property, ManyToOne, Index, Unique } from '@mikro-orm/core'; import { BaseEntity } from '../../../common/entities/base.entity'; import { Restaurant } from 'src/modules/restaurants/entities/restaurant.entity'; import { User } from './user.entity'; @Entity({ tableName: 'user_wallets' }) +@Unique({ properties: ['user', 'restaurant'], name: 'unique_user_restaurant' }) @Index({ properties: ['user', 'restaurant'] }) // Composite index for most common query: find wallet by user and restaurant @Index({ properties: ['user'] }) // Index for queries finding all wallets for a user @Index({ properties: ['restaurant'] }) // Index for queries finding all wallets for a restaurant diff --git a/src/modules/users/providers/user.service.ts b/src/modules/users/providers/user.service.ts index 95766fa..9d57449 100644 --- a/src/modules/users/providers/user.service.ts +++ b/src/modules/users/providers/user.service.ts @@ -69,20 +69,24 @@ export class UserService { return this.userRepository.findOne({ id }); } - async create(phone: string): Promise { + async create(phone: string, restId: string): Promise { const normalizedPhone = normalizePhone(phone); const _raw = { phone: normalizedPhone, firstName: normalizedPhone, birthDate: undefined, marriageDate: undefined, - wallet: 0, - points: 0, }; - + // get registerscore from restaurant + const restaurant = await this.restaurantRepository.findOne({ id: restId }); + if (!restaurant) { + throw new NotFoundException(`Restaurant not found.`); + } + const points = Number(restaurant.score?.registerScore || 0); const createData = _raw as unknown as RequiredEntityData; const user = this.userRepository.create(createData); + this.userWalletRepository.create({ user: user, restaurant: restaurant, wallet: 0, points: points }); await this.em.persistAndFlush(user); return user; }