user refactor
This commit is contained in:
@@ -54,7 +54,7 @@ export class AuthService {
|
|||||||
const user = await this.userService.getOrCreate({
|
const user = await this.userService.getOrCreate({
|
||||||
phone: normalizedPhone,
|
phone: normalizedPhone,
|
||||||
gender: true,
|
gender: true,
|
||||||
maxCredit: 0
|
// maxCredit: 0
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -476,27 +476,27 @@ export class OrderService {
|
|||||||
orderItem.designer = designer
|
orderItem.designer = designer
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attributes) {
|
if (attributes != undefined) {
|
||||||
orderItem.attributes = attributes
|
orderItem.attributes = attributes
|
||||||
}
|
}
|
||||||
if (printAttachments) {
|
if (printAttachments != undefined) {
|
||||||
orderItem.printAttachments = printAttachments
|
orderItem.printAttachments = printAttachments
|
||||||
}
|
}
|
||||||
if (description) {
|
if (description != undefined) {
|
||||||
orderItem.description = description
|
orderItem.description = description
|
||||||
}
|
}
|
||||||
if (quantity) {
|
if (quantity != undefined) {
|
||||||
orderItem.quantity = quantity
|
orderItem.quantity = quantity
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attachments) {
|
if (attachments != undefined) {
|
||||||
orderItem.attachments = attachments
|
orderItem.attachments = attachments
|
||||||
}
|
}
|
||||||
if (discount) {
|
if (discount != undefined) {
|
||||||
orderItem.discount = discount
|
orderItem.discount = discount
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unitPrice) {
|
if (unitPrice != undefined) {
|
||||||
orderItem.unitPrice = unitPrice
|
orderItem.unitPrice = unitPrice
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,10 +504,6 @@ export class OrderService {
|
|||||||
orderItem.printAttributes = printAttributes
|
orderItem.printAttributes = printAttributes
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attributes != undefined) {
|
|
||||||
orderItem.attributes = attributes
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.em.flush()
|
await this.em.flush()
|
||||||
|
|
||||||
return orderItem
|
return orderItem
|
||||||
@@ -526,10 +522,13 @@ export class OrderService {
|
|||||||
if (order.payments.length > 0) {
|
if (order.payments.length > 0) {
|
||||||
throw new BadRequestException("order with payments can not be deleted!")
|
throw new BadRequestException("order with payments can not be deleted!")
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.em.transactional(async (em) => {
|
await this.em.transactional(async (em) => {
|
||||||
|
await this.ticketRepository.nativeDelete({ orderItem: { order: { id: orderId } } })
|
||||||
await this.orderRepository.nativeDelete(order)
|
await this.orderRepository.nativeDelete(order)
|
||||||
await this.orderItemRepository.nativeDelete({ order: { id: orderId } })
|
// order item will be deleted because cascade is true
|
||||||
// await this.ticketRepository.nativeDelete({ orderItemorder: { id: orderId } })
|
|
||||||
|
// TODO : images also must be deleted
|
||||||
await em.flush()
|
await em.flush()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import { PayOrderDto } from '../dto/pay-order.dto';
|
|||||||
import { PaymentRepository } from '../repositories/payment.repository';
|
import { PaymentRepository } from '../repositories/payment.repository';
|
||||||
import { CreditRepository } from 'src/modules/user/repositories/credit.repository';
|
import { CreditRepository } from 'src/modules/user/repositories/credit.repository';
|
||||||
import { CreditService } from 'src/modules/user/providers/credit.service';
|
import { CreditService } from 'src/modules/user/providers/credit.service';
|
||||||
import { CreditTransactionType } from 'src/modules/user/interface/credit';
|
import { CreditTransactionType } from 'src/modules/user/interface/user';
|
||||||
import { Payment } from '../entities/payment.entity';
|
import { Payment } from '../entities/payment.entity';
|
||||||
import { FindPaymentsDto } from '../dto/find-payments.dto';
|
import { FindPaymentsDto } from '../dto/find-payments.dto';
|
||||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
|||||||
@@ -1,22 +1,17 @@
|
|||||||
import { Controller, Get, UseGuards, Patch, Body } from '@nestjs/common';
|
import { Controller, Get, UseGuards, Patch, Body, Query, Post, Param, Delete } from '@nestjs/common';
|
||||||
import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody, ApiOkResponse } from '@nestjs/swagger';
|
import { ApiTags, ApiBearerAuth, ApiOperation, ApiBody } from '@nestjs/swagger';
|
||||||
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
import { AuthGuard } from 'src/modules/auth/guards/auth.guard';
|
||||||
import { UserService } from '../providers/user.service';
|
import { UserService } from '../providers/user.service';
|
||||||
import { CreateUserDto } from '../dto/create-user.dto';
|
import { CreateUserAsAdminDto } from '../dto/create-user.dto';
|
||||||
import { UpdateUserDto } from '../dto/update-user.dto';
|
import { UpdateUserAsAdminDto, UpdateUserDto } from '../dto/update-user.dto';
|
||||||
import { CreateUserAddressDto } from '../dto/create-user-address.dto';
|
import { UserId } from 'src/common/decorators';
|
||||||
import { UpdateUserAddressDto } from '../dto/update-user-address.dto';
|
|
||||||
import { UserId } from 'src/common/decorators/user-id.decorator';
|
|
||||||
import { } from 'src/common/decorators';
|
import { } from 'src/common/decorators';
|
||||||
import { FindUsersDto } from '../dto/find-user.dto';
|
import { FindUsersDto } from '../dto/find-user.dto';
|
||||||
import { FindWalletTransactionsDto } from '../dto/find-wallet-transactions.dto';
|
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
import { UserSuccessMessage } from 'src/common/enums/message.enum';
|
|
||||||
import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|
||||||
import { Permission } from 'src/common/enums/permission.enum';
|
|
||||||
import { CreditService } from '../providers/credit.service';
|
import { CreditService } from '../providers/credit.service';
|
||||||
|
|
||||||
@ApiTags('User')
|
@ApiTags('User')
|
||||||
|
@ApiBearerAuth()
|
||||||
@Controller()
|
@Controller()
|
||||||
export class UsersController {
|
export class UsersController {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -24,152 +19,63 @@ export class UsersController {
|
|||||||
private readonly walletService: CreditService,
|
private readonly walletService: CreditService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
|
@Get('public/users/me')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiBearerAuth()
|
|
||||||
@ApiOperation({ summary: 'Get the current authenticated user profile' })
|
@ApiOperation({ summary: 'Get the current authenticated user profile' })
|
||||||
|
|
||||||
@Get('public/user/me')
|
|
||||||
async getUser(@UserId() userId: string) {
|
async getUser(@UserId() userId: string) {
|
||||||
const user = await this.userService.findById(userId);
|
const user = await this.userService.findById(userId);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@ApiBearerAuth()
|
|
||||||
@ApiOperation({ summary: 'Update the authenticated user profile' })
|
|
||||||
|
|
||||||
|
|
||||||
|
@Patch('public/users/update')
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@ApiOperation({ summary: 'Update the authenticated user profile' })
|
||||||
@ApiBody({ type: UpdateUserDto })
|
@ApiBody({ type: UpdateUserDto })
|
||||||
@Patch('public/user/update')
|
|
||||||
async updateUser(@UserId() userId: string, @Body() dto: UpdateUserDto) {
|
async updateUser(@UserId() userId: string, @Body() dto: UpdateUserDto) {
|
||||||
const user = await this.userService.updateUser(userId, dto);
|
const user = await this.userService.updateUser(userId, dto);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// =================== Admin =====================
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
@Post('admin/users')
|
||||||
// @ApiBearerAuth()
|
@UseGuards(AuthGuard)
|
||||||
// @ApiOperation({ summary: 'Get User Wallet' })
|
@ApiOperation({ summary: 'Create user ' })
|
||||||
|
createUser(@Body() dto: CreateUserAsAdminDto) {
|
||||||
|
return this.userService.createUserAsAdmin(dto);
|
||||||
|
}
|
||||||
|
|
||||||
// @Get('public/user/wallet/balance')
|
@Post('admin/users/:id')
|
||||||
// async getUserWalletBalance(@UserId() userId: string,) {
|
@UseGuards(AuthGuard)
|
||||||
// const wallet = await this.walletService.getUserCurrentWalletBalance(userId,);
|
@ApiOperation({ summary: 'find user ' })
|
||||||
// return wallet;
|
findUser(@Param('id') id: string) {
|
||||||
// }
|
return this.userService.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
@Patch('admin/users/:id')
|
||||||
// @ApiBearerAuth()
|
@UseGuards(AuthGuard)
|
||||||
// @ApiOperation({ summary: 'Get User Wallet' })
|
@ApiOperation({ summary: 'Update user ' })
|
||||||
// @Get('public/user/points/balance')
|
upateUser(@Param('id') id: string, @Body() dto: UpdateUserAsAdminDto) {
|
||||||
// async getUserWallet(@UserId() userId: string,) {
|
return this.userService.updateUser(id, dto);
|
||||||
// const wallet = await this.walletService.getUserCurrentPoinrBalance(userId,);
|
}
|
||||||
// return wallet;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
@Delete('admin/users/:id')
|
||||||
// @ApiBearerAuth()
|
@UseGuards(AuthGuard)
|
||||||
// @ApiOperation({ summary: 'Get User Wallet Transactions' })
|
@ApiOperation({ summary: 'find user ' })
|
||||||
|
deleteUser(@Param('id') id: string) {
|
||||||
|
return this.userService.softDeleteUser(id);
|
||||||
|
}
|
||||||
|
|
||||||
// @Get('public/user/wallet/transactions')
|
|
||||||
// async getUserWalletTransactions(
|
|
||||||
// @UserId() userId: string,
|
|
||||||
// ,
|
|
||||||
// @Query(new ValidationPipe({ transform: true, whitelist: true }))
|
|
||||||
// query: FindWalletTransactionsDto,
|
|
||||||
// ) {
|
|
||||||
// const transactions = await this.userService.getUserWalletTransactions(userId, , query);
|
|
||||||
// return transactions;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
@Get('admin/users')
|
||||||
// @ApiBearerAuth()
|
@UseGuards(AdminAuthGuard)
|
||||||
// @ApiOperation({ summary: 'Create a new address for the authenticated user' })
|
@ApiBearerAuth()
|
||||||
|
|
||||||
// @ApiBody({ type: CreateUserAddressDto })
|
|
||||||
// @ApiOkResponse({ description: 'Created user address' })
|
|
||||||
// @Post('public/user/addresses')
|
|
||||||
// async createUserAddress(
|
|
||||||
// @UserId() userId: string,
|
|
||||||
// @Body(new ValidationPipe({ transform: true, whitelist: true })) dto: CreateUserAddressDto,
|
|
||||||
// ) {
|
|
||||||
// const address = await this.userService.createUserAddress(userId, dto);
|
|
||||||
// return address;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @ApiOperation({ summary: 'Get a single address by ID for the authenticated user' })
|
|
||||||
|
|
||||||
// @ApiOkResponse({ description: 'User address details' })
|
|
||||||
// @Get('public/user/addresses/:addressId')
|
|
||||||
// async getUserAddress(@UserId() userId: string, @Param('addressId') addressId: string) {
|
|
||||||
// const address = await this.userService.getUserAddress(userId, addressId);
|
|
||||||
// return address;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @ApiOperation({ summary: 'Update an address for the authenticated user' })
|
|
||||||
|
|
||||||
// @ApiBody({ type: UpdateUserAddressDto })
|
|
||||||
// @ApiOkResponse({ description: 'Updated user address' })
|
|
||||||
// @Patch('public/user/addresses/:addressId')
|
|
||||||
// async updateUserAddress(
|
|
||||||
// @UserId() userId: string,
|
|
||||||
// @Param('addressId') addressId: string,
|
|
||||||
// @Body(new ValidationPipe({ transform: true, whitelist: true })) dto: UpdateUserAddressDto,
|
|
||||||
// ) {
|
|
||||||
// const address = await this.userService.updateUserAddress(userId, addressId, dto);
|
|
||||||
// return address;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @ApiOperation({ summary: 'Delete an address for the authenticated user' })
|
|
||||||
|
|
||||||
// @ApiOkResponse({ description: 'Address deleted successfully' })
|
|
||||||
// @Delete('public/user/addresses/:addressId')
|
|
||||||
// async deleteUserAddress(@UserId() userId: string, @Param('addressId') addressId: string) {
|
|
||||||
// await this.userService.deleteUserAddress(userId, addressId);
|
|
||||||
// return { message: UserSuccessMessage.ADDRESS_DELETED_SUCCESS };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @ApiOperation({ summary: 'Set an address as default for the authenticated user' })
|
|
||||||
|
|
||||||
// @ApiOkResponse({ description: 'Address set as default' })
|
|
||||||
// @Patch('public/user/addresses/:addressId/default')
|
|
||||||
// async setDefaultAddress(@UserId() userId: string, @Param('addressId') addressId: string) {
|
|
||||||
// const address = await this.userService.setDefaultAddress(userId, addressId);
|
|
||||||
// return address;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// @UseGuards(AuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @ApiOperation({ summary: 'Convert user score (points) to wallet balance' })
|
|
||||||
|
|
||||||
// @Post('public/user/convert-score-to-wallet')
|
|
||||||
// async convertScoreToWallet(@UserId() userId: string,) {
|
|
||||||
// await this.userService.convertScoreToWallet(userId,);
|
|
||||||
// return {
|
|
||||||
// message: UserSuccessMessage.SCORE_CONVERTED_SUCCESS,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// /******************** Admin Routes **********************/
|
|
||||||
|
|
||||||
// @UseGuards(AdminAuthGuard)
|
|
||||||
// @ApiBearerAuth()
|
|
||||||
// @Permissions(Permission.MANAGE_USERS)
|
// @Permissions(Permission.MANAGE_USERS)
|
||||||
// @ApiOperation({ summary: 'Get paginated list of restuarant users with filters' })
|
@ApiOperation({ summary: 'Get paginated list of restuarant users with filters' })
|
||||||
// @Get('admin/users')
|
async findUsers(
|
||||||
// async findAllRestaurantUsers(
|
@Query() query: FindUsersDto) {
|
||||||
// @Query(new ValidationPipe({ transform: true, whitelist: true }))
|
return this.userService.findAll(query);
|
||||||
// query: FindUsersDto,
|
}
|
||||||
// ,
|
|
||||||
// ) {
|
|
||||||
// return this.userService.findAll(, query);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,11 @@ export class CreateUserDto {
|
|||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
gender: boolean;
|
gender: boolean;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class CreateUserAsAdminDto extends CreateUserDto {
|
||||||
@ApiProperty({ description: "User's max credit in Toman", example: 100000 })
|
@ApiProperty({ description: "User's max credit in Toman", example: 100000 })
|
||||||
@IsInt()
|
@IsInt()
|
||||||
maxCredit: number;
|
maxCredit: number;
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
import { IsString, IsOptional, IsNumber, IsBoolean, Min, Max } from 'class-validator';
|
|
||||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* DTO for updating a user address.
|
|
||||||
*/
|
|
||||||
export class UpdateUserAddressDto {
|
|
||||||
@ApiPropertyOptional({ description: 'Address title/label (e.g., "Home", "Work")', example: 'Home' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
title?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Street address', example: '123 Main Street' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
address?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'City name', example: 'Tehran' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
city?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Province', example: 'Tehran Province' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
province?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Postal/ZIP code', example: '12345-6789' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
postalCode?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Latitude coordinate', example: 35.6892, minimum: -90, maximum: 90 })
|
|
||||||
@IsOptional()
|
|
||||||
@IsNumber()
|
|
||||||
@Min(-90)
|
|
||||||
@Max(90)
|
|
||||||
latitude?: number;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Longitude coordinate', example: 51.389, minimum: -180, maximum: 180 })
|
|
||||||
@IsOptional()
|
|
||||||
@IsNumber()
|
|
||||||
@Min(-180)
|
|
||||||
@Max(180)
|
|
||||||
longitude?: number;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Contact phone for this address', example: '+989123456789' })
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
phone?: string;
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ description: 'Set as default address', example: false })
|
|
||||||
@IsOptional()
|
|
||||||
@IsBoolean()
|
|
||||||
isDefault?: boolean;
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { PartialType } from '@nestjs/swagger';
|
import { PartialType } from '@nestjs/swagger';
|
||||||
import { CreateUserDto } from './create-user.dto';
|
import { CreateUserAsAdminDto, CreateUserDto } from './create-user.dto';
|
||||||
|
|
||||||
export class UpdateUserDto extends PartialType(CreateUserDto) {}
|
export class UpdateUserDto extends PartialType(CreateUserDto) {}
|
||||||
|
export class UpdateUserAsAdminDto extends PartialType(CreateUserAsAdminDto) {}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
import { Entity, Property, ManyToOne, Index, Enum, PrimaryKey } from '@mikro-orm/core';
|
import { Entity, Property, ManyToOne, Index, Enum, PrimaryKey } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { User } from './user.entity';
|
import { User } from './user.entity';
|
||||||
import { CreditTransactionType } from '../interface/credit';
|
import { CreditTransactionType } from '../interface/user';
|
||||||
|
|
||||||
@Entity({ tableName: 'credit_transactions' })
|
@Entity({ tableName: 'credit_transactions' })
|
||||||
@Index({ properties: ['user'] })
|
@Index({ properties: ['user'] })
|
||||||
export class CreditTransaction extends BaseEntity {
|
export class CreditTransaction extends BaseEntity {
|
||||||
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
@PrimaryKey({ type: 'bigint', autoincrement: true })
|
||||||
id: bigint
|
id: bigint
|
||||||
|
|
||||||
@ManyToOne(() => User)
|
@ManyToOne(() => User)
|
||||||
user!: User;
|
user!: User;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
export enum CreditTransactionType {
|
|
||||||
WITHDRAW = 'withdraw',
|
|
||||||
DEPOSIT = 'deposit',
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
export enum CreditTransactionType {
|
||||||
|
WITHDRAW = 'withdraw',
|
||||||
|
DEPOSIT = 'deposit',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export interface CreateUser {
|
||||||
|
phone: string;
|
||||||
|
firstName?: string;
|
||||||
|
lastName?: string;
|
||||||
|
address?: string;
|
||||||
|
gender: boolean;
|
||||||
|
maxCredit: number;
|
||||||
|
}
|
||||||
@@ -2,12 +2,13 @@ import { Injectable, NotFoundException, BadRequestException } from '@nestjs/comm
|
|||||||
import { RequiredEntityData } from '@mikro-orm/core';
|
import { RequiredEntityData } from '@mikro-orm/core';
|
||||||
import { User } from '../entities/user.entity';
|
import { User } from '../entities/user.entity';
|
||||||
import { EntityManager } from '@mikro-orm/postgresql';
|
import { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { CreateUserDto } from '../dto/create-user.dto';
|
import { CreateUserAsAdminDto, CreateUserDto } from '../dto/create-user.dto';
|
||||||
import { FindUsersDto } from '../dto/find-user.dto';
|
import { FindUsersDto } from '../dto/find-user.dto';
|
||||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||||
import { UserRepository } from '../repositories/user.repository';
|
import { UserRepository } from '../repositories/user.repository';
|
||||||
import { normalizePhone } from '../../util/phone.util';
|
import { normalizePhone } from '../../util/phone.util';
|
||||||
import { UpdateUserDto } from '../dto/update-user.dto';
|
import { UpdateUserDto } from '../dto/update-user.dto';
|
||||||
|
import { CreateUser } from '../interface/user';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserService {
|
export class UserService {
|
||||||
@@ -16,6 +17,19 @@ export class UserService {
|
|||||||
private readonly em: EntityManager,
|
private readonly em: EntityManager,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
|
async createUserAsAdmin(dto: CreateUserAsAdminDto): Promise<User> {
|
||||||
|
|
||||||
|
const exist = await this.findByPhone(dto.phone)
|
||||||
|
|
||||||
|
if (exist) {
|
||||||
|
throw new BadRequestException("User with this phone already exist")
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await this.createUser({ ...dto, phone: normalizePhone(dto.phone) })
|
||||||
|
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
async getOrCreate(dto: CreateUserDto): Promise<User> {
|
async getOrCreate(dto: CreateUserDto): Promise<User> {
|
||||||
const normalizedPhone = normalizePhone(dto.phone)
|
const normalizedPhone = normalizePhone(dto.phone)
|
||||||
|
|
||||||
@@ -25,17 +39,7 @@ export class UserService {
|
|||||||
return currentUser
|
return currentUser
|
||||||
}
|
}
|
||||||
|
|
||||||
const createData: RequiredEntityData<User> = {
|
const user = await this.createUser({ ...dto, phone: normalizedPhone,maxCredit:0 })
|
||||||
phone: normalizedPhone,
|
|
||||||
firstName: dto.firstName,
|
|
||||||
lastName: dto.lastName,
|
|
||||||
gender: dto.gender,
|
|
||||||
maxCredit: dto.maxCredit
|
|
||||||
};
|
|
||||||
|
|
||||||
const user = this.userRepository.create(createData);
|
|
||||||
|
|
||||||
await this.em.persistAndFlush([user]);
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
@@ -76,4 +80,34 @@ export class UserService {
|
|||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================= Core Logic ===================
|
||||||
|
|
||||||
|
async createUser(param: CreateUser) {
|
||||||
|
|
||||||
|
const createData: RequiredEntityData<User> = {
|
||||||
|
phone: param.phone,
|
||||||
|
firstName: param.firstName,
|
||||||
|
lastName: param.lastName,
|
||||||
|
gender: param.gender,
|
||||||
|
maxCredit: param.maxCredit
|
||||||
|
};
|
||||||
|
|
||||||
|
const user = this.userRepository.create(createData);
|
||||||
|
|
||||||
|
await this.em.persistAndFlush([user]);
|
||||||
|
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
async softDeleteUser(userId: string) {
|
||||||
|
|
||||||
|
const user = await this.findOrFail(userId)
|
||||||
|
|
||||||
|
user.deletedAt = new Date()
|
||||||
|
|
||||||
|
await this.em.flush();
|
||||||
|
|
||||||
|
return { message: 'success' }
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user