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