update entity id

This commit is contained in:
2026-02-05 10:08:37 +03:30
parent d4ae03fa4f
commit 068286e505
37 changed files with 185 additions and 209 deletions
+5 -5
View File
@@ -1,12 +1,12 @@
import { Index, Property, Filter, OptionalProps } from '@mikro-orm/core';
import { Index, Property, Filter, OptionalProps, PrimaryKey } from '@mikro-orm/core';
import { ulid } from 'ulid';
@Filter({ name: 'notDeleted', cond: { deletedAt: null }, default: true })
@Index({ properties: ['deletedAt'] })
@Index({ properties: ['createdAt'] })
export abstract class BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt';
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid()
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
createdAt: Date = new Date();
+2 -4
View File
@@ -1,4 +1,4 @@
import { Cascade, Entity, ManyToOne, OneToOne, PrimaryKey, Property } from '@mikro-orm/core';
import { Cascade, Entity, ManyToOne, OneToOne, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { normalizePhone } from '../../util/phone.util';
import { Role } from 'src/modules/roles/entities/role.entity';
@@ -7,9 +7,7 @@ import { NotificationPreference } from 'src/modules/notification/entities/notifi
@Entity({ tableName: 'admins' })
export class Admin extends BaseEntity {
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid()
[OptionalProps]?: 'createdAt' | 'deletedAt'
@Property({ nullable: true })
firstName?: string;
@@ -25,7 +25,7 @@ export class FormBuilderController {
@Get('public/entity/:id/field')
@ApiOperation({ summary: 'find all fields' })
findAllSectionFieldsAsUser(@Param('id') entityId: string) {
return this.fieldService.findAll(+entityId);
return this.fieldService.findAll(entityId);
}
// ============= Admin =================
@@ -34,14 +34,14 @@ export class FormBuilderController {
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
@UseGuards(AdminAuthGuard)
createField(@Body() dto: CreateFieldDto, @Param('id') entityId: string) {
return this.fieldService.create(+entityId, dto);
return this.fieldService.create(entityId, dto);
}
@Get('admin/entity/:id/field')
@ApiOperation({ summary: 'find all fields' })
@UseGuards(AdminAuthGuard)
findAllSectionFields(@Param('id') entityId: string) {
return this.fieldService.findAll(+entityId);
return this.fieldService.findAll(entityId);
}
@Get('admin/entity/group/field')
@@ -55,7 +55,7 @@ export class FormBuilderController {
@ApiOperation({ summary: 'Find one field' })
@UseGuards(AdminAuthGuard)
findOneFiled(@Param('id') id: string) {
return this.fieldService.findById(+id);
return this.fieldService.findById(id);
}
@Patch('admin/entity/field/:id')
@@ -63,7 +63,7 @@ export class FormBuilderController {
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
updateField(@Param('id') id: string, @Body() dto: UpdateFieldDto) {
return this.fieldService.update(+id, dto);
return this.fieldService.update(id, dto);
}
@Delete('admin/entity/field/:id')
@@ -71,7 +71,7 @@ export class FormBuilderController {
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
removeField(@Param('id') id: string) {
return this.fieldService.remove(+id);
return this.fieldService.remove(id);
}
/*================================ Field Option ========================== */
@@ -81,21 +81,21 @@ export class FormBuilderController {
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
createFieldOption(@Body() dto: CreateFieldOptionDto, @Param('id') id: string) {
return this.fieldOptionService.create(+id, dto);
return this.fieldOptionService.create(id, dto);
}
@Get('admin/entity/field/:id/option')
@ApiOperation({ summary: 'find all field Option' })
@UseGuards(AdminAuthGuard)
findAllFiledOptions(@Param('id') fieldId: string) {
return this.fieldOptionService.findAll(+fieldId);
return this.fieldOptionService.findAll(fieldId);
}
@Get('admin/entity/field/option/:id')
@ApiOperation({ summary: 'Find one field Option' })
@UseGuards(AdminAuthGuard)
findOneFiledOption(@Param('id') id: string) {
return this.fieldOptionService.findById(+id);
return this.fieldOptionService.findById(id);
}
@Patch('admin/entity/field/option/:id')
@@ -103,7 +103,7 @@ export class FormBuilderController {
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
updateFieldOption(@Param('id') id: string, @Body() dto: UpdateFieldOptionDto) {
return this.fieldOptionService.update(+id, dto);
return this.fieldOptionService.update(id, dto);
}
@Delete('admin/entity/field/option/:id')
@@ -111,6 +111,6 @@ export class FormBuilderController {
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.MANAGE_FORM_BUILDER)
removeFieldOption(@Param('id') id: string) {
return this.fieldOptionService.remove(+id);
return this.fieldOptionService.remove(id);
}
}
@@ -1,12 +1,12 @@
import { IsArray, IsNotEmpty, IsNumber } from 'class-validator';
import { IsArray, IsNotEmpty, IsNumber, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
export class FindFieldsGroupDto {
@IsNotEmpty()
@IsArray()
@IsNumber({}, { each: true })
@IsString({ each: true })
@ApiProperty()
entityIds: number[]
entityIds: string[]
}
@@ -1,11 +1,10 @@
import { Entity, Property, ManyToOne, PrimaryKey, Unique } from '@mikro-orm/core';
import { Entity, Property, ManyToOne, PrimaryKey, Unique, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Field } from './field.entity';
@Entity({ tableName: 'field_option' })
export class FieldOption extends BaseEntity {
@PrimaryKey({ autoincrement: true, type: 'bigint' })
id: number;
[OptionalProps]?: 'createdAt' | 'deletedAt'
@Property()
value: string;
@@ -1,4 +1,4 @@
import { Collection, Entity, Enum, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Collection, Entity, Enum, ManyToOne, OneToMany, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
import { FieldOption } from './field-option.entity';
import { EntityType, FieldType } from '../interface/print';
import { BaseEntity } from 'src/common/entities/base.entity';
@@ -6,8 +6,7 @@ import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'field' })
export class Field extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: number
[OptionalProps]?: 'createdAt' | 'deletedAt'
@OneToMany(() => FieldOption, (attrValue) => attrValue.field)
options = new Collection<FieldOption>(this)
@@ -17,8 +16,8 @@ export class Field extends BaseEntity {
entityType: EntityType;
// Foreign key based on entityType
@Property({ type: 'bigint' })
entityId: number;
@Property({ type: 'string',columnType:'char(26)' })
entityId: string;
@Property()
name: string;
@@ -17,7 +17,7 @@ export class FieldOptionService {
private readonly em: EntityManager,
) { }
async create(fieldId: number, dto: CreateFieldOptionDto) {
async create(fieldId: string, dto: CreateFieldOptionDto) {
const field = await this.fieldRepository.findOne({ id: fieldId })
if (!field) {
@@ -38,7 +38,7 @@ export class FieldOptionService {
}
async update(fieldOptionId: number, dto: UpdateFieldOptionDto) {
async update(fieldOptionId: string, dto: UpdateFieldOptionDto) {
const fieldOption = await this.fieldOptionRepository.findOne({ id: fieldOptionId })
@@ -54,11 +54,11 @@ export class FieldOptionService {
}
findAll(fieldId: number) {
findAll(fieldId: string) {
return this.fieldOptionRepository.find({ field: { id: fieldId } });
}
async findById(fieldOptionId: number): Promise<FieldOption> {
async findById(fieldOptionId: string): Promise<FieldOption> {
const fieldOption = await this.fieldOptionRepository.findOne({ id: fieldOptionId },
{ populate: ['field'] });
@@ -67,7 +67,7 @@ export class FieldOptionService {
return fieldOption;
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
const fieldOption = await this.findById(id);
fieldOption.deletedAt = new Date();
@@ -23,7 +23,7 @@ export class FieldService {
private readonly em: EntityManager,
) { }
async create(entityId: number, dto: CreateFieldDto) {
async create(entityId: string, dto: CreateFieldDto) {
const { entityType, isRequired, multiple, name, type, order } = dto
let entity: null | Product | Print = null
@@ -48,7 +48,15 @@ export class FieldService {
entityType
};
const field = this.fieldRepository.create(data)
const field = this.fieldRepository.create({
name,
order,
type,
isRequired,
multiple,
entityId: entity.id,
entityType
})
await this.em.persistAndFlush(field)
@@ -56,7 +64,7 @@ export class FieldService {
}
async update(fieldId: number, dto: UpdateFieldDto) {
async update(fieldId: string, dto: UpdateFieldDto) {
const field = await this.fieldRepository.findOne({ id: fieldId })
@@ -72,7 +80,7 @@ export class FieldService {
}
findAll(entityId: number) {
findAll(entityId: string) {
return this.fieldRepository.find({
entityId
},
@@ -102,7 +110,7 @@ export class FieldService {
return result
}
async findById(fieldId: number): Promise<Field> {
async findById(fieldId: string): Promise<Field> {
const field = await this.fieldRepository.findOne({ id: fieldId },
{ populate: [] });
@@ -110,7 +118,7 @@ export class FieldService {
return field;
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
const field = await this.findById(id);
field.deletedAt = new Date();
@@ -1,13 +1,12 @@
import { Entity, Property, ManyToOne, Unique, PrimaryKey, OneToOne, Cascade } from '@mikro-orm/core';
import { Entity, Property, ManyToOne, Unique, PrimaryKey, OneToOne, Cascade, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { NotifEvent } from '../interfaces/notification.interface';
import { Admin } from 'src/modules/admin/entities/admin.entity';
@Entity({ tableName: 'notification_preferences' })
export class NotificationPreference extends BaseEntity {
@PrimaryKey({ type: 'int', autoincrement: true })
id: number
[OptionalProps]?: 'createdAt' | 'deletedAt'
@OneToOne(() => Admin, { owner: true, unique: true, cascade: [Cascade.PERSIST, Cascade.REMOVE] })
admin!: Admin;
@@ -1,4 +1,4 @@
import { Entity, Property, ManyToOne, Enum, PrimaryKey } from '@mikro-orm/core';
import { Entity, Property, ManyToOne, Enum, PrimaryKey, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { User } from '../../user/entities/user.entity';
import { NotifEvent } from '../interfaces/notification.interface';
@@ -6,8 +6,7 @@ import { Admin } from 'src/modules/admin/entities/admin.entity';
@Entity({ tableName: 'notifications' })
export class Notification extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: number
[OptionalProps]?: 'createdAt' | 'deletedAt'
@ManyToOne(() => User, { nullable: true })
user?: User;
@@ -66,7 +66,7 @@ export class OrderController {
@UseGuards(AuthGuard)
@ApiOperation({ summary: 'Update order item ' })
updateOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Param('itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) {
return this.orderService.updateOrderItemAsUser(userId, orderId, +itemId, body);
return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body);
}
@@ -78,7 +78,7 @@ export class OrderController {
@Param('orderItemId') orderItemId: string,
@UserId() userId: string
) {
return this.orderService.confirmOrderItem(userId, orderId, +orderItemId);
return this.orderService.confirmOrderItem(userId, orderId, orderItemId);
}
@Delete('public/orders/:orderId/items/:orderItemId')
@@ -89,7 +89,7 @@ export class OrderController {
@Param('orderItemId') orderItemId: string,
@UserId() userId: string
) {
return this.orderService.removeOrderItemAsUser(userId, orderId, +orderItemId);
return this.orderService.removeOrderItemAsUser(userId, orderId, orderItemId);
}
/*========================== Admin Routes =====================*/
@@ -125,6 +125,24 @@ export class OrderController {
return this.orderService.findOrderOrFail(orderId);
}
@Patch('admin/orders/:id')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.UPDATE_ORDER)
@ApiOperation({ summary: 'Update Order ' })
updateOrder(@Param('id') id: string, @Body() dto: UpdateOrderAsAdminDto) {
return this.orderService.updateOrderAsAdmin2(id, dto);
}
@Delete('admin/orders/:orderId')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.REMOVE_ORDER)
@ApiOperation({ summary: 'Hard Delete Order ' })
hardDelete(@Param('orderId') orderId: string) {
return this.orderService.hardDeleteOrder(orderId);
}
// =============== Order Item ================
@Get('admin/orders/item/print')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.VIEW_All_ORDERS)
@@ -137,14 +155,17 @@ export class OrderController {
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'order item detail' })
getOrderItem(@Param('id') itemId: string) {
return this.orderService.findOrderItemOrFailByItemId(+itemId);
return this.orderService.findOrderItemOrFailByItemId(itemId);
}
@Patch('admin/orders/:orderId')
@Post('admin/orders/:orderId/item/:itemId/designer')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Update Order ' })
updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) {
return this.orderService.updateOrderAsAdmin2(orderId, dto);
@Permissions(PermissionEnum.ASSIGN_DESIGNER)
@ApiOperation({ summary: 'Assign Order Designer ' })
assignDesigner(@Param('orderId') orderId: string, @Param('itemId') itemId: string,
@Body() body: AssignDesignerDto) {
return this.orderService.assignDesigner(orderId, itemId, body.designerId);
}
@Patch('admin/orders/:id/item/:itemId/print')
@@ -152,45 +173,31 @@ export class OrderController {
@Permissions(PermissionEnum.CREATE_ORDER_PRINT)
@ApiOperation({ summary: 'Create Print form for order item' })
createPrintForm(@Param('id') id: string, @Param('itemId') itemId: string, @Body() dto: CreatePrintFormDto) {
return this.orderService.createPrintForm(id, +itemId, dto);
return this.orderService.createPrintForm(id, itemId, dto);
}
@Delete('admin/orders/:orderId')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Hard Delete Order ' })
hardDelete(@Param('orderId') orderId: string) {
return this.orderService.hardDeleteOrder(orderId);
}
@Post('admin/orders/:orderId/item/:itemId/designer')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Assign Order Designer ' })
assignDesigner(@Param('orderId') orderId: string, @Param('itemId') itemId: string,
@Body() body: AssignDesignerDto) {
return this.orderService.assignDesigner(orderId, +itemId, body.designerId);
}
@Post('admin/orders/:orderId/invoice')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: 'Create Order invoice ' })
createInvoice(@Param('orderId') orderId: string) {
return this.orderService.createInvoice(orderId);
}
@Delete('admin/orders/:id/items/:orderItemId')
// @Post('admin/orders/:orderId/invoice')
// @UseGuards(AdminAuthGuard)
// @ApiOperation({ summary: 'Create Order invoice ' })
// createInvoice(@Param('orderId') orderId: string) {
// return this.orderService.createInvoice(orderId);
// }
@Delete('admin/orders/:id/items/:itemId')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.UPDATE_ORDER)
@ApiOperation({ summary: 'Remove order Item ' })
removeOrderItemAdAdmin(
@Param('id') orderId: string,
@Param('orderItemId') orderItemId: string,
@Param('itemId') itemId: string,
) {
return this.orderService.removeOrderItemAsAdmin(orderId, +orderItemId);
return this.orderService.removeOrderItemAsAdmin(orderId, itemId);
}
@Post('admin/orders/:id/item')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.UPDATE_ORDER)
@ApiOperation({ summary: 'add item to order ' })
addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) {
return this.orderService.addOrderItemAsAdmin(orderId, body);
@@ -199,10 +206,11 @@ export class OrderController {
@Patch('admin/orders/:id/item/:itemId')
@UseGuards(AdminAuthGuard)
@Permissions(PermissionEnum.UPDATE_ORDER)
@ApiOperation({ summary: 'Update order item' })
updateOrderItemAsAdmin(@Param('id') orderId: string, @Param('itemId') itemId: string,
@Body() body: UpdateOrderItemDtoAsAdmin) {
return this.orderService.updateOrderItemAsAdmin(orderId, +itemId, body);
return this.orderService.updateOrderItemAsAdmin(orderId, itemId, body);
}
}
+3 -3
View File
@@ -14,9 +14,9 @@ import { IAttachment, IField, OrderStatusEnum } from '../interface/order.interfa
export class CreateOrderItemAsUserDto {
@IsInt()
@IsString()
@ApiProperty()
productId: number;
productId: string;
@ApiProperty()
@IsNumber()
@@ -73,7 +73,7 @@ export class CreateOrderAsUSerDto {
@ApiProperty({
isArray: true, type: [CreateOrderItemAsUserDto], example: [
{
productId: 1,
productId: 'sd4',
quantity: 100,
attributes: [
{
+1 -1
View File
@@ -10,5 +10,5 @@ export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdm
export class UpdateOrCreateOrderItem extends CreateOrderItemDtoAsAdmin {
@ApiProperty()
@IsNumber()
id: number
id: string
}
@@ -1,17 +1,16 @@
import { Entity, Formula, Index, ManyToOne, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, Formula, Index, ManyToOne, OptionalProps, Property } from '@mikro-orm/core';
import { Order } from './order.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { IAttachment, IField } from '../interface/order.interface';
import { Admin } from 'src/modules/admin/entities/admin.entity';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'order_items' })
@Index({ properties: ['order'] })
export class OrderItem {
export class OrderItem extends BaseEntity{
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'subTotal' | 'total';
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: number
@ManyToOne(() => Order)
order!: Order;
@@ -62,9 +61,4 @@ export class OrderItem {
@Property({ nullable: true, columnType: 'timestamptz' })
printFormCreatedAt?: Date;
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
createdAt: Date = new Date();
@Property({ nullable: true, columnType: 'timestamptz' })
deletedAt?: Date;
}
+2 -6
View File
@@ -20,11 +20,12 @@ import { Payment } from 'src/modules/payment/entities/payment.entity';
import { ulid } from 'ulid';
import { Admin } from 'src/modules/admin/entities/admin.entity';
import { PaymentStatusEnum } from 'src/modules/payment/interface/payment';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'orders' })
@Index({ properties: ['user', 'status'] })
@Index({ properties: ['status'] })
export class Order {
export class Order extends BaseEntity{
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'balance' | 'paidAmount'
| 'discount' | 'subTotal' | 'total' | 'taxAmount';
@@ -205,11 +206,6 @@ export class Order {
@Property({ type: 'boolean', default: false })
enableTax?: Boolean = false
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
createdAt: Date = new Date();
@Property({ nullable: true, columnType: 'timestamptz' })
deletedAt?: Date;
@BeforeCreate()
async generateOrderNumber(args: EventArgs<Order>) {
@@ -33,7 +33,7 @@ export interface IField { fieldId: number, value: string }
export interface IAttachment { type: string, url: string }
export interface CreateOrderItem {
productId: number,
productId: string,
quantity: number
attributes?: IField[]
description?: string
+10 -10
View File
@@ -98,7 +98,7 @@ export class OrderService {
for (const item of items) {
if (item.id) { // update
const currentItem = order.items.find(i => Number(i.id) == item.id)
const currentItem = order.items.find(i => i.id == item.id)
if (!currentItem) {
throw new BadGatewayException("Order Item not found")
@@ -206,7 +206,7 @@ export class OrderService {
return orderItem
}
async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) {
async updateOrderItemAsUser(userId: string, orderId: string, itemId: string, dto: UpdateOrderItemDtoAsUser) {
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
@@ -227,7 +227,7 @@ export class OrderService {
return updated
}
async updateOrderItemAsAdmin(orderId: string, itemId: number, dto: UpdateOrderItemDtoAsAdmin) {
async updateOrderItemAsAdmin(orderId: string, itemId: string, dto: UpdateOrderItemDtoAsAdmin) {
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
@@ -240,7 +240,7 @@ export class OrderService {
return updated
}
async createPrintForm(orderId: string, itemId: number, dto: CreatePrintFormDto) {
async createPrintForm(orderId: string, itemId: string, dto: CreatePrintFormDto) {
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
@@ -249,7 +249,7 @@ export class OrderService {
return updated
}
async removeOrderItemAsAdmin(orderId: string, itemId: number) {
async removeOrderItemAsAdmin(orderId: string, itemId: string) {
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
@@ -262,7 +262,7 @@ export class OrderService {
return { message: 'success' }
}
async removeOrderItemAsUser(userId: string, orderId: string, itemId: number) {
async removeOrderItemAsUser(userId: string, orderId: string, itemId: string) {
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
@@ -363,7 +363,7 @@ export class OrderService {
// return order
// }
async confirmOrderItem(userId: string, orderId: string, orderItemId: number) {
async confirmOrderItem(userId: string, orderId: string, orderItemId: string) {
const orderItem = await this.findOrderItemOrFail(orderId, orderItemId)
@@ -379,7 +379,7 @@ export class OrderService {
return orderItem
}
async assignDesigner(orderId: string, orderItemId: number, designerId: string) {
async assignDesigner(orderId: string, orderItemId: string, designerId: string) {
const orderItem = await this.findOrderItemOrFail(orderId, orderItemId)
@@ -651,7 +651,7 @@ export class OrderService {
return order
}
async findOrderItemOrFail(orderId: string, itemId: number) {
async findOrderItemOrFail(orderId: string, itemId: string) {
const orderItem = await this.orderItemRepository.findOne({ id: itemId, order: { id: orderId } },
{ populate: ['order', 'order.user'] })
if (!orderItem) {
@@ -660,7 +660,7 @@ export class OrderService {
return orderItem
}
async findOrderItemOrFailByItemId(itemId: number) {
async findOrderItemOrFailByItemId(itemId: string) {
const orderItem = await this.orderItemRepository.findOne({ id: itemId },
{ populate: ['order', 'order.user'] })
if (!orderItem) {
@@ -1,4 +1,4 @@
import { Entity, ManyToOne, Property, Enum, Index, PrimaryKey } from '@mikro-orm/core';
import { Entity, ManyToOne, Property, Enum, Index, PrimaryKey, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Order } from '../../order/entities/order.entity';
import { PaymentGatewayEnum, PaymentMethodEnum, PaymentStatusEnum } from '../interface/payment';
@@ -7,6 +7,8 @@ import { Admin } from 'src/modules/admin/entities/admin.entity';
@Entity({ tableName: 'payments' })
export class Payment extends BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt'
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid()
@@ -33,21 +33,21 @@ export class PrintController {
@Get('section/:id')
@ApiOperation({ summary: 'Find one section' })
findOne(@Param('id') id: string) {
return this.printService.finOrFail(+id);
return this.printService.finOrFail(id);
}
@Patch('section/:id')
@Permissions(PermissionEnum.MANAGE_PRINT)
@ApiOperation({ summary: 'Update section' })
update(@Param('id') id: string, @Body() dto: UpdateSectionDto) {
return this.printService.update(+id, dto);
return this.printService.update(id, dto);
}
@Delete('section/:id')
@Permissions(PermissionEnum.MANAGE_PRINT)
@ApiOperation({ summary: 'Remove section' })
remove(@Param('id') id: string) {
return this.printService.remove(+id);
return this.printService.remove(id);
}
}
+2 -10
View File
@@ -1,22 +1,14 @@
import { Entity, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'print' })
export class Print extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: number
// @OneToMany(() => Field, (attrValue) => attrValue.entityId, { cascade: [Cascade.PERSIST, Cascade.REMOVE] })
// fields = new Collection<Field>(this)
// @Property({ type: 'json', nullable: true })
// fields?: string[]
[OptionalProps]?: 'createdAt' | 'deletedAt'
@Property()
title: string;
@Property({ type: 'int', nullable: true })
order?: number;
}
+3 -3
View File
@@ -34,7 +34,7 @@ export class PrintService {
}
async update(sectionId: number, dto: UpdateSectionDto) {
async update(sectionId: string, dto: UpdateSectionDto) {
const print = await this.finOrFail(sectionId)
@@ -63,7 +63,7 @@ export class PrintService {
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
const print = await this.finOrFail(id);
print.deletedAt = new Date();
@@ -73,7 +73,7 @@ export class PrintService {
}
// ============== Helpers ==========
async finOrFail(printId: number) {
async finOrFail(printId: string) {
const printSection = await this.printRepository.findOne({ id: printId })
if (!printSection) {
@@ -61,7 +61,7 @@ export class ProductController {
@ApiOperation({ summary: 'Get a category by id' })
@ApiParam({ name: 'id', required: true })
findCategoryById(@Param('id') id: string) {
return this.categoryService.findById(+id);
return this.categoryService.findById(id);
}
@Patch('admin/category/:id')
@@ -71,7 +71,7 @@ export class ProductController {
@ApiParam({ name: 'id', required: true })
@ApiBody({ type: UpdateCategoryDto })
updateCategory(@Param('id') id: string, @Body() dto: UpdateCategoryDto) {
return this.categoryService.update(+id, dto);
return this.categoryService.update(id, dto);
}
@Delete('admin/category/:id')
@@ -80,7 +80,7 @@ export class ProductController {
@ApiOperation({ summary: 'Delete (soft) a category' })
@ApiParam({ name: 'id', required: true })
removeCategory(@Param('id') id: string,) {
return this.categoryService.remove(+id);
return this.categoryService.remove(id);
}
/* =============================== PRODUCT ================================= */
@@ -124,7 +124,7 @@ export class ProductController {
@ApiOperation({ summary: 'Get a product by id' })
@ApiParam({ name: 'id', required: true })
findById(@Param('id') id: string,) {
return this.productService.findById(+id);
return this.productService.findById(id);
}
@Patch('admin/products/:id')
@@ -133,7 +133,7 @@ export class ProductController {
@ApiOperation({ summary: 'Update a product' })
@ApiParam({ name: 'id', required: true })
update(@Param('id') id: string, @Body() updateproductDto: UpdateproductDto) {
return this.productService.update(+id, updateproductDto);
return this.productService.update(id, updateproductDto);
}
@Delete('admin/products/:id')
@@ -142,7 +142,7 @@ export class ProductController {
@ApiOperation({ summary: 'Delete (soft) a product' })
@ApiParam({ name: 'id', required: true })
remove(@Param('id') id: string,) {
return this.productService.remove(+id);
return this.productService.remove(id);
}
@@ -8,9 +8,9 @@ export class CreateCategoryDto {
title: string;
@IsOptional()
@IsInt()
@IsString()
@ApiPropertyOptional()
parentId?: number;
parentId?: string;
@IsOptional()
@IsBoolean()
@@ -13,9 +13,9 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
export class CreateproductDto {
@IsNotEmpty()
@IsInt()
@IsString()
@ApiProperty()
categoryId: number;
categoryId: string;
@IsOptional()
@IsString()
@@ -1,10 +1,10 @@
import { Entity, Index, Property, Collection, OneToMany, ManyToOne, PrimaryKey } from '@mikro-orm/core';
// import { Product } from './product.entity';
import { Entity, Index, Property, Collection, OneToMany, ManyToOne, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
@Entity({ tableName: 'categories' })
export class Category extends BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt'
@ManyToOne(() => Category, { nullable: true })
parent?: Category | null
@@ -12,8 +12,6 @@ export class Category extends BaseEntity {
@OneToMany(() => Category, (c) => c.parent)
children = new Collection<Category>(this)
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint
@Property()
title: string;
@@ -1,17 +1,15 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { Entity, ManyToOne, OptionalProps, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Category } from './category.entity';
@Entity({ tableName: 'products' })
export class Product extends BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt'
@ManyToOne(() => Category)
category: Category
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: number
@Property()
title: string;
@@ -42,7 +42,7 @@ export class CategoryService {
}
async update(categoryId: number, dto: UpdateCategoryDto) {
async update(categoryId: string, dto: UpdateCategoryDto) {
const category = await this.categoryRepository.findOne({ id: categoryId })
@@ -74,7 +74,7 @@ export class CategoryService {
return this.categoryRepository.findAllPaginated(dto);
}
async findById(catId: number): Promise<Category> {
async findById(catId: string): Promise<Category> {
const category = await this.categoryRepository.findOne({ id: catId },
{ populate: ['children', 'parent'] });
@@ -83,7 +83,7 @@ export class CategoryService {
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
const category = await this.findById(id);
if (!category) {
throw new NotFoundException(productMessage.NOT_FOUND);
@@ -1,4 +1,4 @@
import { BadRequestException, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateproductDto } from '../dto/create-product.dto';
import { FindproductsDto } from '../dto/find-products.dto';
import { ProductRepository } from '../repositories/product.repository';
@@ -44,7 +44,7 @@ export class ProductService {
}
async update(productId: number, createproductDto: Partial<CreateproductDto>) {
async update(productId: string, createproductDto: Partial<CreateproductDto>) {
const { categoryId, ...rest } = createproductDto;
const product = await this.productRepository.findOne({ id: productId })
if (!product) {
@@ -70,7 +70,7 @@ export class ProductService {
return this.productRepository.findAllPaginated(dto);
}
async findById(productId: number) {
async findById(productId: string) {
const product = await this.productRepository.findOne({ id: productId },
{ populate: ['category'] });
@@ -79,7 +79,7 @@ export class ProductService {
return product;
}
async remove(id: number): Promise<void> {
async remove(id: string): Promise<void> {
const product = await this.productRepository.findOne({ id });
if (!product) {
throw new NotFoundException(productMessage.NOT_FOUND);
@@ -90,7 +90,7 @@ export class ProductService {
}
async findProductsByIds(productIds: number[]) {
async findProductsByIds(productIds: string[]) {
const products = await this.productRepository.find({
id: { $in: productIds }
})
@@ -98,7 +98,7 @@ export class ProductService {
return products
}
async findOneOrFail(productId: number) {
async findOneOrFail(productId: string) {
const product = await this.productRepository.findOne({ id: productId });
if (!product) throw new NotFoundException(productMessage.NOT_FOUND);
return product;
@@ -1,4 +1,4 @@
import { Entity, Property, Unique, ManyToMany, Collection, PrimaryKey, Enum } from '@mikro-orm/core';
import { Entity, Property, Unique, ManyToMany, Collection, PrimaryKey, Enum, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Role } from './role.entity';
import { ulid } from 'ulid';
@@ -6,9 +6,8 @@ import { PermissionEnum } from 'src/common/enums/permission.enum';
@Entity({ tableName: 'permissions' })
export class Permission extends BaseEntity {
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid()
[OptionalProps]?: 'createdAt' | 'deletedAt'
@Enum(() => PermissionEnum)
name!: PermissionEnum;
+2 -11
View File
@@ -1,16 +1,13 @@
import { Collection, Entity, ManyToMany, OneToMany, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core';
import { Permission } from './permission.entity';
import { RolePermission } from './rolePermission.entity';
import { ulid } from 'ulid';
import { Admin } from 'src/modules/admin/entities/admin.entity';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity({ tableName: 'roles' })
export class Role {
export class Role extends BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'admins';
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid()
@Property({ unique: true })
name: string;
@@ -26,10 +23,4 @@ export class Role {
@OneToMany(() => Admin, (admin) => admin.role)
admins: Admin
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
createdAt: Date = new Date();
@Property({ nullable: true, columnType: 'timestamptz' })
deletedAt?: Date;
}
@@ -11,6 +11,4 @@ export class RolePermission {
@ManyToOne(() => Permission, { primary: true })
permission!: Permission;
// **نکته:** اینجا *فقط* همین دو خصوصیت کافی است —
// از تعریف جداگانه‌ی roleId / permissionId با @PrimaryKey خودداری کن مگر بخوای نام/نوع ستون را سفارشی کنی.
}
@@ -20,7 +20,7 @@ export class TicketController {
@UseGuards(AuthGuard)
@ApiOperation({ summary: "create ticket ==> user route" })
createTicket(@Param('id') id: string, @Body() createDto: CreateTicketDto, @UserId() userId: string) {
return this.ticketsService.createTicketAsUser(+id, createDto, userId);
return this.ticketsService.createTicketAsUser(id, createDto, userId);
}
@@ -28,7 +28,7 @@ export class TicketController {
@UseGuards(AuthGuard)
@ApiOperation({ summary: "Get all order item tickets ==> user route" })
getTickets(@Query() queryDto: FindTicketQueryDto, @Param('id') id: string, @UserId() userId: string) {
return this.ticketsService.getTickets(+id, queryDto, userId);
return this.ticketsService.getTickets(id, queryDto, userId);
}
@@ -36,14 +36,14 @@ export class TicketController {
@UseGuards(AuthGuard)
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
updateTicket(@Param('id') id: string, @UserId() userId: string, @Body() body: UpdateTicketDto) {
return this.ticketsService.updateTicketAsUser(+id, userId, body);
return this.ticketsService.updateTicketAsUser(id, userId, body);
}
@Delete('public/ticket/:id')
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
@UseGuards(AuthGuard)
deleteTicket(@Param('id') id: string, @UserId() userId: string) {
return this.ticketsService.deleteTicketAsUSer(+id, userId);
return this.ticketsService.deleteTicketAsUSer(id, userId);
}
/*=========================== Admin ============================== */
@@ -51,7 +51,7 @@ export class TicketController {
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: "create ticket ==> admin route" })
createTicketAsAdmin(@Param('id') itemId: string, @Body() createDto: CreateTicketDto, @AdminId() adminId: string) {
return this.ticketsService.createTicketAsAdmin(+itemId, createDto, adminId);
return this.ticketsService.createTicketAsAdmin(itemId, createDto, adminId);
}
@@ -59,20 +59,20 @@ export class TicketController {
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: "Get all order item tickets ==> admin route" })
getTicketsAsAdmin(@Query() queryDto: FindTicketQueryDto, @Param('id') itemId: string) {
return this.ticketsService.getTickets(+itemId, queryDto);
return this.ticketsService.getTickets(itemId, queryDto);
}
@Patch('admin/ticket/:id')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: "Update Ticket ==> admin route" })
updateTicketAsAdmin(@Param('id') id: string, @AdminId() adminId: string, @Body() body: UpdateTicketDto) {
return this.ticketsService.updateTicketAsAdmin(+id, adminId, body);
return this.ticketsService.updateTicketAsAdmin(id, adminId, body);
}
@Delete('admin/ticket/:id')
@UseGuards(AdminAuthGuard)
@ApiOperation({ summary: "Delete Ticket ==> admin route" })
deleteTicketAsAdmin(@Param('id') id: string, @AdminId() adminId: string) {
return this.ticketsService.deleteTicketAsAdmin(+id, adminId);
return this.ticketsService.deleteTicketAsAdmin(id, adminId);
}
}
+3 -4
View File
@@ -9,13 +9,12 @@ import { User } from "../../user/entities/user.entity";
import { Admin } from 'src/modules/admin/entities/admin.entity';
import { AttachmentType } from '../interfaces/ticket';
import { OrderItem } from 'src/modules/order/entities/order-item.entity';
import { BaseEntity } from 'src/common/entities/base.entity';
@Entity()
export class Ticket {
[OptionalProps]?: 'createdAt'
export class Ticket extends BaseEntity{
[OptionalProps]?: 'createdAt' | 'deletedAt'
@PrimaryKey({ type: "bigint", autoincrement: true })
id: bigint;
@Property({ type: 'text' })
content: string;
+10 -10
View File
@@ -25,7 +25,7 @@ export class TicketService {
private readonly em: EntityManager,
) { }
async createTicketAsAdmin(orderItemId: number, dto: CreateTicketDto, adminId: string) {
async createTicketAsAdmin(orderItemId: string, dto: CreateTicketDto, adminId: string) {
const orderItem = await this.findOrderItemOrFail(orderItemId)
@@ -39,7 +39,7 @@ export class TicketService {
}
async createTicketAsUser(orderItemId: number, dto: CreateTicketDto, userId: string) {
async createTicketAsUser(orderItemId: string, dto: CreateTicketDto, userId: string) {
const orderItem = await this.findOrderItemOrFail(orderItemId)
@@ -51,7 +51,7 @@ export class TicketService {
}
async updateTicketAsUser(ticketId: number, userId: string, dto: UpdateTicketDto) {
async updateTicketAsUser(ticketId: string, userId: string, dto: UpdateTicketDto) {
const ticket = await this.findOneOrFail(ticketId)
@@ -62,7 +62,7 @@ export class TicketService {
return { message: 'success' }
}
async updateTicketAsAdmin(ticketId: number, adminId: string, dto: UpdateTicketDto) {
async updateTicketAsAdmin(ticketId: string, adminId: string, dto: UpdateTicketDto) {
const ticket = await this.findOneOrFail(ticketId)
@@ -73,7 +73,7 @@ export class TicketService {
return { message: 'success' }
}
async deleteTicketAsUser(ticketId: number, userId: string) {
async deleteTicketAsUser(ticketId: string, userId: string) {
const ticket = await this.findOneOrFail(ticketId)
@@ -84,7 +84,7 @@ export class TicketService {
return { message: 'success' }
}
async deleteTicketAsAdmin(ticketId: number, adminId: string) {
async deleteTicketAsAdmin(ticketId: string, adminId: string) {
const ticket = await this.findOneOrFail(ticketId)
@@ -95,7 +95,7 @@ export class TicketService {
return { message: 'success' }
}
async deleteTicketAsUSer(ticketId: number, userId: string) {
async deleteTicketAsUSer(ticketId: string, userId: string) {
const ticket = await this.findOneOrFail(ticketId)
if (ticket.orderItem.order.user.id !== userId) {
throw new BadRequestException("You can not Delete this Ticket")
@@ -108,7 +108,7 @@ export class TicketService {
// ================ Core Logic ============
async getTickets(orderItemId: number, queryDto: FindTicketQueryDto, userId?: string) {
async getTickets(orderItemId: string, queryDto: FindTicketQueryDto, userId?: string) {
const orderItem = await this.findOrderItemOrFail(orderItemId)
if (userId) {
@@ -161,7 +161,7 @@ export class TicketService {
// ================ Helpers =================
async findOneOrFail(ticketId: number) {
async findOneOrFail(ticketId: string) {
const ticket = await this.ticketsRepository.findOne({ id: ticketId },
{ populate: ['orderItem', 'orderItem.order', 'user'] })
if (!ticket) {
@@ -190,7 +190,7 @@ export class TicketService {
}
}
async findOrderItemOrFail(orderItemId: number) {
async findOrderItemOrFail(orderItemId: string) {
const orderItem = await this.orderItemRepository.findOne({ id: orderItemId }, { populate: ['order', 'order.user'] })
if (!orderItem) {
@@ -5,7 +5,7 @@ import { FindTicketQueryDto } from '../DTO/search-ticket-query.dto';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
class FindOrdersOpts extends FindTicketQueryDto {
orderItemId: number;
orderItemId: string;
};
@Injectable()
@@ -1,4 +1,4 @@
import { Entity, Property, ManyToOne, Index, Enum, PrimaryKey } from '@mikro-orm/core';
import { Entity, Property, ManyToOne, Index, Enum, PrimaryKey, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { User } from './user.entity';
import { CreditTransactionType } from '../interface/user';
@@ -6,8 +6,7 @@ import { CreditTransactionType } from '../interface/user';
@Entity({ tableName: 'credit_transactions' })
@Index({ properties: ['user'] })
export class CreditTransaction extends BaseEntity {
@PrimaryKey({ type: 'bigint', autoincrement: true })
id: bigint
[OptionalProps]?: 'createdAt' | 'deletedAt'
@ManyToOne(() => User)
user!: User;
+3 -3
View File
@@ -1,4 +1,4 @@
import { Entity, Index, Property, OneToMany, Collection, Cascade, PrimaryKey } from '@mikro-orm/core';
import { Entity, Index, Property, OneToMany, Collection, Cascade, PrimaryKey, OptionalProps } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Order } from 'src/modules/order/entities/order.entity';
import { normalizePhone } from '../../util/phone.util';
@@ -6,11 +6,11 @@ import { ulid } from 'ulid';
@Entity({ tableName: 'users' })
export class User extends BaseEntity {
[OptionalProps]?: 'createdAt' | 'deletedAt'
@OneToMany(() => Order, order => order.user)
orders = new Collection<Order>(this);
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
id: string = ulid();
@Property({ nullable: true })
firstName?: string;