From b2645008023acb7af7386dc6ee1700bac2cc7697 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Mon, 9 Mar 2026 12:46:39 +0330 Subject: [PATCH] business --- src/modules/business/business.service.ts | 11 ++- .../business/dto/create-business.dto.ts | 24 ++++++- .../business/entities/business.entity.ts | 57 +++++++++++++++- .../repositories/business.repository.ts | 5 ++ src/modules/catalogue/catalogue.service.ts | 18 ++++- .../catalogue/dto/create-catalogue.dto.ts | 28 +++++++- .../catalogue/entities/catalogue.entity.ts | 34 +++++++++- .../catalogue/enums/company-status.enum.ts | 12 ++++ .../repositories/catalogue.repository.ts | 67 +++++++++++++++++++ src/modules/orders/orders.module.ts | 3 +- 10 files changed, 248 insertions(+), 11 deletions(-) create mode 100644 src/modules/business/repositories/business.repository.ts create mode 100644 src/modules/catalogue/enums/company-status.enum.ts create mode 100644 src/modules/catalogue/repositories/catalogue.repository.ts diff --git a/src/modules/business/business.service.ts b/src/modules/business/business.service.ts index 145055e..cb9d050 100644 --- a/src/modules/business/business.service.ts +++ b/src/modules/business/business.service.ts @@ -1,11 +1,18 @@ import { Injectable } from '@nestjs/common'; import { CreateBusinessDto } from './dto/create-business.dto'; import { UpdateBusinessDto } from './dto/update-business.dto'; +import { EntityManager } from '@mikro-orm/postgresql'; +import { Business } from './entities/business.entity'; @Injectable() export class BusinessService { - create(createBusinessDto: CreateBusinessDto) { - return 'This action adds a new business'; + constructor( + private readonly em: EntityManager, + ) { } + + create(dto: CreateBusinessDto) { + const business = this.em.create(Business, dto) + return this.em.persistAndFlush(business) } findAll() { diff --git a/src/modules/business/dto/create-business.dto.ts b/src/modules/business/dto/create-business.dto.ts index bda6171..a257cfd 100644 --- a/src/modules/business/dto/create-business.dto.ts +++ b/src/modules/business/dto/create-business.dto.ts @@ -1 +1,23 @@ -export class CreateBusinessDto {} + +import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum } from 'class-validator'; +import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; + + +export class CreateBusinessDto { + @IsString() + @ApiProperty({ example: 'ایرانی' }) + danakSubscriptionId: string; + + + @IsString() + @ApiProperty() + name: string; + + @IsString() + @ApiProperty() + slug: string; + + + +} diff --git a/src/modules/business/entities/business.entity.ts b/src/modules/business/entities/business.entity.ts index 82719bc..d9ffa03 100644 --- a/src/modules/business/entities/business.entity.ts +++ b/src/modules/business/entities/business.entity.ts @@ -1 +1,56 @@ -export class Business {} +import { Cascade, Collection, Entity, EntityRepositoryType, OneToMany, Opt, Property, Unique } from "@mikro-orm/core"; + +import { BaseEntity } from "../../../common/entities/base.entity"; +import { Catalogue } from "src/modules/catalogue/entities/catalogue.entity"; +import { BusinessRepository } from "../repositories/business.repository"; + +@Entity({ repository: () => BusinessRepository }) +export class Business extends BaseEntity { + @Unique() + @Property({ type: "uuid", nullable: false }) + danakSubscriptionId!: string; + + @Property({ type: "varchar", length: 255, nullable: false }) + name!: string; + + @Property({ type: "varchar", length: 255, nullable: false }) + slug!: string; + + @Property({ type: "varchar", length: 255, nullable: true }) + domain?: string; + + @Property({ type: "boolean", default: false }) + isDomainVerified: boolean & Opt; + + @Property({ type: "varchar", length: 255, nullable: true }) + domainVerificationToken?: string; + + @Property({ type: "datetime", nullable: true }) + domainVerifiedAt?: Date; + + @Property({ type: "varchar", length: 255, nullable: true }) + logoUrl?: string; + + + //========================= + + @OneToMany(() => Catalogue, (catalogue) => catalogue.business, { cascade: [Cascade.ALL] }) + companies = new Collection(this); + + + + // @OneToMany(() => Ticket, (ticket) => ticket.business) + // tickets = new Collection(this); + + // @OneToMany(() => Announcement, (announcement) => announcement.business) + // announcements = new Collection(this); + + // @OneToMany(() => Criticism, (criticism) => criticism.business) + // criticisms = new Collection(this); + + + // @OneToMany(() => User, (user) => user.business) + // users = new Collection(this); + + [EntityRepositoryType]?: BusinessRepository; +} diff --git a/src/modules/business/repositories/business.repository.ts b/src/modules/business/repositories/business.repository.ts new file mode 100644 index 0000000..8afcdc0 --- /dev/null +++ b/src/modules/business/repositories/business.repository.ts @@ -0,0 +1,5 @@ +import { EntityRepository } from "@mikro-orm/postgresql"; + +import { Business } from "../entities/business.entity"; + +export class BusinessRepository extends EntityRepository {} diff --git a/src/modules/catalogue/catalogue.service.ts b/src/modules/catalogue/catalogue.service.ts index de3e69e..2ad1bfe 100644 --- a/src/modules/catalogue/catalogue.service.ts +++ b/src/modules/catalogue/catalogue.service.ts @@ -1,11 +1,23 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { CreateCatalogueDto } from './dto/create-catalogue.dto'; import { UpdateCatalogueDto } from './dto/update-catalogue.dto'; +import { EntityManager } from '@mikro-orm/postgresql'; +import { Catalogue } from './entities/catalogue.entity'; +import { Business } from '../business/entities/business.entity'; @Injectable() export class CatalogueService { - create(createCatalogueDto: CreateCatalogueDto) { - return 'This action adds a new catalogue'; + constructor( + private readonly em: EntityManager, + ) { } + + async create(dto: CreateCatalogueDto) { + const business = await this.em.findOne(Business, {}) + if (!business) { + throw new BadRequestException() + } + const catalogue = this.em.create(Catalogue, { ...dto, business }) + return this.em.persistAndFlush(catalogue) } findAll() { diff --git a/src/modules/catalogue/dto/create-catalogue.dto.ts b/src/modules/catalogue/dto/create-catalogue.dto.ts index 71488bc..1262843 100644 --- a/src/modules/catalogue/dto/create-catalogue.dto.ts +++ b/src/modules/catalogue/dto/create-catalogue.dto.ts @@ -1 +1,27 @@ -export class CreateCatalogueDto {} + +import { IsString, IsOptional, IsBoolean, IsInt, Min, IsEnum } from 'class-validator'; +import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger'; +import { Type } from 'class-transformer'; +import { CatalogueSize, CatalogueStatus } from '../enums/company-status.enum'; + +export class CreateCatalogueDto { + @IsString() + @ApiProperty({ example: 'ایرانی' }) + name: string; + + + @IsString() + @ApiProperty() + content: string; + + + @IsEnum(() => CatalogueStatus) + @ApiProperty() + status: CatalogueStatus; + + + @IsEnum(() => CatalogueSize) + @ApiProperty() + size: CatalogueSize; + +} diff --git a/src/modules/catalogue/entities/catalogue.entity.ts b/src/modules/catalogue/entities/catalogue.entity.ts index d834380..ce8c85d 100644 --- a/src/modules/catalogue/entities/catalogue.entity.ts +++ b/src/modules/catalogue/entities/catalogue.entity.ts @@ -1 +1,33 @@ -export class Catalogue {} +import { + Cascade, Collection, Entity, EntityRepositoryType, Enum, + ManyToOne, OneToMany, OneToOne, Opt, Property +} from "@mikro-orm/core"; + +import { BaseEntity } from "../../../common/entities/base.entity"; +import { Business } from "src/modules/business/entities/business.entity"; +import { CatalogueSize, CatalogueStatus } from "../enums/company-status.enum"; +import { CatalogueRepository } from "../repositories/catalogue.repository"; + +@Entity({ repository: () => CatalogueRepository }) +export class Catalogue extends BaseEntity { + @Property({ type: "varchar", length: 255, unique: true }) + name!: string; + + @Property({ type: "json" }) + content!: string; + + @Enum({ items: () => CatalogueStatus, nullable: false }) + status!: CatalogueStatus; + + @Enum({ items: () => CatalogueSize, nullable: false }) + size!: CatalogueSize; + + //----------------------------------- + + @ManyToOne(() => Business, { deleteRule: "restrict" }) + business!: Business; + + //----------------------------------- + + [EntityRepositoryType]?: CatalogueRepository; +} diff --git a/src/modules/catalogue/enums/company-status.enum.ts b/src/modules/catalogue/enums/company-status.enum.ts new file mode 100644 index 0000000..dc618d6 --- /dev/null +++ b/src/modules/catalogue/enums/company-status.enum.ts @@ -0,0 +1,12 @@ +export enum CatalogueStatus { + APPROVED = "APPROVED", + PENDING = "PENDING", + REJECTED = "REJECTED", +} + + +export enum CatalogueSize { + A4 = 'a4', + A3 = 'a3', + A5 = 'a5' +} \ No newline at end of file diff --git a/src/modules/catalogue/repositories/catalogue.repository.ts b/src/modules/catalogue/repositories/catalogue.repository.ts new file mode 100644 index 0000000..26eba18 --- /dev/null +++ b/src/modules/catalogue/repositories/catalogue.repository.ts @@ -0,0 +1,67 @@ +import { EntityRepository, FilterQuery } from "@mikro-orm/postgresql"; + + +import { Catalogue } from "../entities/catalogue.entity"; +import { CatalogueStatus } from "../enums/company-status.enum"; +export class CatalogueRepository extends EntityRepository { + // + // async getCataloguesForAdmin(queryDto: CompanyListQueryDto, businessId: string) { + // const { } = queryDto + + // const where: FilterQuery = { + // deletedAt: null, + // business: { id: businessId }, + // }; + + // if (queryDto.status) { + // where.status = queryDto.status; + // } + // if (queryDto.isActive !== undefined) { + // where.isActive = queryDto.isActive === 1; + // } + // if (queryDto.q) { + // where.name = { $ilike: `%${queryDto.q}%` }; + // } + + // if (queryDto.registrationDate) { + // where.createdAt = { + // $gte: queryDto.registrationDate, + // $lte: queryDto.registrationDate, + // }; + // } + // if (queryDto.name) { + // where.name = { $ilike: `%${queryDto.name}%` }; + // } + + // return this.findAndCount(where, { + // populate: [], + // limit, + // offset: skip, + // orderBy: { createdAt: "DESC" }, + // fields: [ + // "id", + // "name", + // "isActive", + // "dateOfEstablishment", + // "status", + // "createdAt", + // ], + // }); + // } + + // async getCataloguesForPublic(queryDto: CompanyListPublicQueryDto, businessId: string) { + // const where: FilterQuery = { + // deletedAt: null, + // business: { id: businessId }, + // isActive: true, + // status: CatalogueStatus.APPROVED, + // }; + + + // return this.find(where, { + // populate: [], + // orderBy: { createdAt: "DESC" }, + // fields: ["id", "name", "description", "profileImageUrl", "coverImageUrl", "createdAt"], + // }); + // } +} diff --git a/src/modules/orders/orders.module.ts b/src/modules/orders/orders.module.ts index 6208faf..b763908 100644 --- a/src/modules/orders/orders.module.ts +++ b/src/modules/orders/orders.module.ts @@ -16,7 +16,6 @@ import { PaymentsModule } from '../payments/payments.module'; import { JwtModule } from '@nestjs/jwt'; import { OrderRepository } from './repositories/order.repository'; import { OrderListeners } from './listeners/order.listeners'; -import { OrdersCrone } from './crone/order.crone'; import { AdminModule } from '../admin/admin.module'; import { NotificationsModule } from '../notifications/notifications.module'; import { InventoryModule } from '../inventory/inventory.module'; @@ -36,7 +35,7 @@ import { UserModule } from '../users/user.module'; forwardRef(() => UserModule) ], controllers: [OrdersController], - providers: [OrdersService, OrderRepository, OrderListeners, OrdersCrone], + providers: [OrdersService, OrderRepository, OrderListeners], exports: [OrderRepository, OrdersService], }) export class OrdersModule { }