feat: add response DTOs for various services across modules

- Created response DTOs for ConfigService, AppService, AuthService, CatalogsService, AccountsService, BusinessActivityComplexesService, ComplexPosesService, SalesInvoicesService, BusinessActivitiesService, ConsumerBusinessActivityGoodsService, and others.
- Implemented Create, FindAll, FindOne, and Update response types for services in consumer, partners, and POS modules.
- Added request DTOs for creating and updating goods in the consumer business activities module.
- Introduced filtering DTO for partner licenses.
- Enhanced response mapping for partner license activations.
This commit is contained in:
2026-04-27 10:45:39 +03:30
parent 34793295c9
commit dee96b6e91
208 changed files with 8058 additions and 2349 deletions
@@ -3,6 +3,7 @@ import { Body, Controller, Get, Param, Patch } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { AccountsService } from './accounts.service'
import { UpdateAccountDto } from './dto/account.dto'
import type { AccountsServiceFindAllResponseDto, AccountsServiceFindOneResponseDto, AccountsServiceUpdateResponseDto } from './dto/accounts-response.dto'
@ApiTags('PartnerAccounts')
@Controller('partner/accounts')
@@ -10,12 +11,12 @@ export class AccountsController {
constructor(private readonly service: AccountsService) {}
@Get()
async findAll(@TokenAccount('userId') consumerId: string) {
async findAll(@TokenAccount('userId') consumerId: string): Promise<AccountsServiceFindAllResponseDto> {
return this.service.findAll(consumerId)
}
@Get(':id')
async findOne(@Param('id') id: string) {
async findOne(@Param('id') id: string): Promise<AccountsServiceFindOneResponseDto> {
return this.service.findOne(id)
}
@@ -28,7 +29,7 @@ export class AccountsController {
// }
@Patch(':id')
async update(@Param('id') id: string, @Body() data: UpdateAccountDto) {
async update(@Param('id') id: string, @Body() data: UpdateAccountDto): Promise<AccountsServiceUpdateResponseDto> {
return this.service.update(id, data)
}
@@ -0,0 +1,6 @@
import type { AccountsService } from '../accounts.service'
export type AccountsServiceCreateResponseDto = Awaited<ReturnType<AccountsService['create']>>
export type AccountsServiceFindAllResponseDto = Awaited<ReturnType<AccountsService['findAll']>>
export type AccountsServiceFindOneResponseDto = Awaited<ReturnType<AccountsService['findOne']>>
export type AccountsServiceUpdateResponseDto = Awaited<ReturnType<AccountsService['update']>>
@@ -3,6 +3,7 @@ import { Body, Controller, Get, Param, Patch } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { AccountsService } from './accounts.service'
import { UpdateAccountDto } from './dto/account.dto'
import type { AccountsServiceFindAllResponseDto, AccountsServiceFindOneResponseDto, AccountsServiceUpdateResponseDto } from './dto/accounts-response.dto'
@ApiTags('PartnerConsumerAccounts')
@Controller('partner/consumers/:consumerId/accounts')
@@ -13,7 +14,7 @@ export class AccountsController {
async findAll(
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
) {
): Promise<AccountsServiceFindAllResponseDto> {
return this.accountsService.findAll(partnerId, consumerId)
}
@@ -22,7 +23,7 @@ export class AccountsController {
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
@Param('id') id: string,
) {
): Promise<AccountsServiceFindOneResponseDto> {
return this.accountsService.findOne(partnerId, consumerId, id)
}
@@ -41,7 +42,7 @@ export class AccountsController {
@Param('consumerId') consumerId: string,
@Param('id') id: string,
@Body() data: UpdateAccountDto,
) {
): Promise<AccountsServiceUpdateResponseDto> {
return this.accountsService.update(partnerId, consumerId, id, data)
}
@@ -42,7 +42,18 @@ export class AccountsService {
private readonly defaultWhere = (partner_id, consumer_id) => ({
consumer_id,
consumer: {
partner_id,
OR: [
{
legal: {
partner_id,
},
},
{
individual: {
partner_id,
},
},
],
},
})
@@ -0,0 +1,5 @@
import type { AccountsService } from '../accounts.service'
export type AccountsServiceFindAllResponseDto = Awaited<ReturnType<AccountsService['findAll']>>
export type AccountsServiceFindOneResponseDto = Awaited<ReturnType<AccountsService['findOne']>>
export type AccountsServiceUpdateResponseDto = Awaited<ReturnType<AccountsService['update']>>
@@ -3,6 +3,7 @@ import { Body, Controller, Param, Post } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { PartnerBusinessActivityAccountsChargeService } from './accounts-charge.service'
import { CreateBusinessActivityAccountsChargeDto } from './dto/accounts-charge.dto'
import type { PartnerBusinessActivityAccountsChargeServiceCreateResponseDto } from './dto/accounts-charge-response.dto'
@ApiTags('PartnerConsumerBAAccountsCharge')
@Controller(
@@ -17,7 +18,7 @@ export class PartnerBusinessActivityAccountsChargeController {
@Param('consumerId') consumerId: string,
@Param('businessActivityId') businessActivityId: string,
@Body() data: CreateBusinessActivityAccountsChargeDto,
) {
): Promise<PartnerBusinessActivityAccountsChargeServiceCreateResponseDto> {
return this.service.create(partnerId, consumerId, businessActivityId, data)
}
}
@@ -26,7 +26,18 @@ export class PartnerBusinessActivityAccountsChargeService {
id: business_activity_id,
consumer: {
id: consumer_id,
partner_id,
OR: [
{
legal: {
partner_id,
},
},
{
individual: {
partner_id,
},
},
],
},
},
OR: [
@@ -0,0 +1,3 @@
import type { PartnerBusinessActivityAccountsChargeService } from '../accounts-charge.service'
export type PartnerBusinessActivityAccountsChargeServiceCreateResponseDto = Awaited<ReturnType<PartnerBusinessActivityAccountsChargeService['create']>>
@@ -16,7 +16,7 @@ export class BusinessActivitiesController {
async findAll(
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
) {
): Promise<BusinessActivitiesServiceFindAllResponseDto> {
return this.businessActivitiesService.findAll(partnerId, consumerId)
}
@@ -25,7 +25,7 @@ export class BusinessActivitiesController {
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
@Param('id') id: string,
) {
): Promise<BusinessActivitiesServiceFindOneResponseDto> {
return this.businessActivitiesService.findOne(partnerId, consumerId, id)
}
@@ -34,7 +34,7 @@ export class BusinessActivitiesController {
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
@Body() data: CreateBusinessActivitiesDto,
) {
): Promise<BusinessActivitiesServiceCreateResponseDto> {
return this.businessActivitiesService.create(partnerId, consumerId, data)
}
@@ -44,7 +44,7 @@ export class BusinessActivitiesController {
@Param('consumerId') consumerId: string,
@Param('id') id: string,
@Body() data: UpdateBusinessActivityDto,
) {
): Promise<BusinessActivitiesServiceUpdateResponseDto> {
return this.businessActivitiesService.update(partnerId, consumerId, id, data)
}
@@ -53,3 +53,5 @@ export class BusinessActivitiesController {
// return this.businessActivitiesService.delete(id)
// }
}
import type { BusinessActivitiesServiceCreateResponseDto, BusinessActivitiesServiceFindAllResponseDto, BusinessActivitiesServiceFindOneResponseDto, BusinessActivitiesServiceUpdateResponseDto } from './dto/business-activities-response.dto'
@@ -15,7 +15,7 @@ export class BusinessActivitiesService {
).toISOString()
}
defaultSelect: BusinessActivitySelect = {
private readonly defaultSelect: BusinessActivitySelect = {
id: true,
name: true,
economic_code: true,
@@ -49,6 +49,24 @@ export class BusinessActivitiesService {
},
}
private readonly defaultWhere = (partner_id: string, consumer_id: string) => ({
consumer: {
id: consumer_id,
OR: [
{
legal: {
partner_id,
},
},
{
individual: {
partner_id,
},
},
],
},
})
private readonly prepareBusinessActivityResponse = (businessActivity: any) => {
const { license_activation, ...rest } = businessActivity
const { license, ...license_activation_rest } = license_activation
@@ -65,12 +83,7 @@ export class BusinessActivitiesService {
async findAll(partner_id: string, consumer_id: string) {
const businessActivities = await this.prisma.businessActivity.findMany({
where: {
consumer: {
id: consumer_id,
partner_id,
},
},
where: this.defaultWhere(partner_id, consumer_id),
select: this.defaultSelect,
})
return ResponseMapper.list(
@@ -81,10 +94,7 @@ export class BusinessActivitiesService {
async findOne(partner_id: string, consumer_id: string, id: string) {
const businessActivity = await this.prisma.businessActivity.findUnique({
where: {
consumer: {
id: consumer_id,
partner_id,
},
...this.defaultWhere(partner_id, consumer_id),
id,
},
select: this.defaultSelect,
@@ -171,7 +181,7 @@ export class BusinessActivitiesService {
async update(partner_id: string, consumer_id: string, id: string, data: any) {
const businessActivity = await this.prisma.businessActivity.update({
where: { id, consumer: { id: consumer_id, partner_id } },
where: { ...this.defaultWhere(partner_id, consumer_id), id },
data,
select: this.defaultSelect,
})
@@ -3,6 +3,7 @@ import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { BusinessActivityComplexesService } from './complexes.service'
import { CreateComplexDto, UpdateComplexDto } from './dto/complex.dto'
import type { BusinessActivityComplexesServiceCreateResponseDto, BusinessActivityComplexesServiceFindAllResponseDto, BusinessActivityComplexesServiceFindOneResponseDto, BusinessActivityComplexesServiceUpdateResponseDto } from './dto/complexes-response.dto'
@ApiTags('PartnerBusinessActivityComplexes')
@Controller(
@@ -16,7 +17,7 @@ export class BusinessActivityComplexesController {
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
@Param('businessActivityId') businessActivityId: string,
) {
): Promise<BusinessActivityComplexesServiceFindAllResponseDto> {
return this.service.findAll(partnerId, consumerId, businessActivityId)
}
@@ -26,7 +27,7 @@ export class BusinessActivityComplexesController {
@Param('consumerId') consumerId: string,
@Param('businessActivityId') businessActivityId: string,
@Param('id') id: string,
) {
): Promise<BusinessActivityComplexesServiceFindOneResponseDto> {
return this.service.findOne(partnerId, consumerId, businessActivityId, id)
}
@@ -35,7 +36,7 @@ export class BusinessActivityComplexesController {
@PartnerInfo('id') partnerId: string,
@Param('businessActivityId') businessActivityId: string,
@Body() data: CreateComplexDto,
) {
): Promise<BusinessActivityComplexesServiceCreateResponseDto> {
return this.service.create(partnerId, businessActivityId, data)
}
@@ -46,7 +47,7 @@ export class BusinessActivityComplexesController {
@Param('businessActivityId') businessActivityId: string,
@Param('id') id: string,
@Body() data: UpdateComplexDto,
) {
): Promise<BusinessActivityComplexesServiceUpdateResponseDto> {
return this.service.update(partnerId, consumerId, businessActivityId, id, data)
}
@@ -25,7 +25,18 @@ export class BusinessActivityComplexesService {
id: business_activity_id,
consumer: {
id: consumer_id,
partner_id,
OR: [
{
legal: {
partner_id,
},
},
{
individual: {
partner_id,
},
},
],
},
},
})
@@ -0,0 +1,6 @@
import type { BusinessActivityComplexesService } from '../complexes.service'
export type BusinessActivityComplexesServiceCreateResponseDto = Awaited<ReturnType<BusinessActivityComplexesService['create']>>
export type BusinessActivityComplexesServiceFindAllResponseDto = Awaited<ReturnType<BusinessActivityComplexesService['findAll']>>
export type BusinessActivityComplexesServiceFindOneResponseDto = Awaited<ReturnType<BusinessActivityComplexesService['findOne']>>
export type BusinessActivityComplexesServiceUpdateResponseDto = Awaited<ReturnType<BusinessActivityComplexesService['update']>>
@@ -0,0 +1,6 @@
import type { ComplexPosesService } from '../poses.service'
export type ComplexPosesServiceCreateResponseDto = Awaited<ReturnType<ComplexPosesService['create']>>
export type ComplexPosesServiceFindAllResponseDto = Awaited<ReturnType<ComplexPosesService['findAll']>>
export type ComplexPosesServiceFindOneResponseDto = Awaited<ReturnType<ComplexPosesService['findOne']>>
export type ComplexPosesServiceUpdateResponseDto = Awaited<ReturnType<ComplexPosesService['update']>>
@@ -3,6 +3,7 @@ import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { CreatePosDto, UpdatePosDto } from './dto/pos.dto'
import { ComplexPosesService } from './poses.service'
import type { ComplexPosesServiceCreateResponseDto, ComplexPosesServiceFindAllResponseDto, ComplexPosesServiceFindOneResponseDto, ComplexPosesServiceUpdateResponseDto } from './dto/poses-response.dto'
@ApiTags('AdminComplexPoses')
@Controller('partner/business_activities/:businessActivityId/complexes/:complexId/poses')
@@ -14,7 +15,7 @@ export class ComplexPosesController {
@PartnerInfo('id') partnerId: string,
@Param('businessActivityId') businessActivityId: string,
@Param('complexId') complexId: string,
) {
): Promise<ComplexPosesServiceFindAllResponseDto> {
return this.service.findAll(partnerId, businessActivityId, complexId)
}
@@ -24,7 +25,7 @@ export class ComplexPosesController {
@Param('businessActivityId') businessActivityId: string,
@Param('complexId') complexId: string,
@Param('id') id: string,
) {
): Promise<ComplexPosesServiceFindOneResponseDto> {
return this.service.findOne(partnerId, businessActivityId, complexId, id)
}
@@ -34,7 +35,7 @@ export class ComplexPosesController {
@Param('businessActivityId') businessActivityId: string,
@Param('complexId') complexId: string,
@Body() data: CreatePosDto,
) {
): Promise<ComplexPosesServiceCreateResponseDto> {
return this.service.create(partnerId, businessActivityId, complexId, data)
}
@@ -45,7 +46,7 @@ export class ComplexPosesController {
@Param('complexId') complexId: string,
@Param('id') id: string,
@Body() data: UpdatePosDto,
) {
): Promise<ComplexPosesServiceUpdateResponseDto> {
return this.service.update(partnerId, businessActivityId, complexId, id, data)
}
@@ -0,0 +1,6 @@
import type { BusinessActivitiesService } from '../business-activities.service'
export type BusinessActivitiesServiceCreateResponseDto = Awaited<ReturnType<BusinessActivitiesService['create']>>
export type BusinessActivitiesServiceFindAllResponseDto = Awaited<ReturnType<BusinessActivitiesService['findAll']>>
export type BusinessActivitiesServiceFindOneResponseDto = Awaited<ReturnType<BusinessActivitiesService['findOne']>>
export type BusinessActivitiesServiceUpdateResponseDto = Awaited<ReturnType<BusinessActivitiesService['update']>>
@@ -2,23 +2,24 @@ import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
import { PartnerConsumersService } from './consumers.service'
import { CreateConsumerDto, UpdateConsumerDto } from './dto/create-consumers.dto'
import type { PartnerConsumersServiceCreateResponseDto, PartnerConsumersServiceFindAllResponseDto, PartnerConsumersServiceFindOneResponseDto, PartnerConsumersServiceUpdateResponseDto } from './dto/consumers-response.dto'
@Controller('partner/consumers')
export class PartnerConsumersController {
constructor(private readonly service: PartnerConsumersService) {}
@Get()
async findAll(@PartnerInfo('id') partnerId: string) {
async findAll(@PartnerInfo('id') partnerId: string): Promise<PartnerConsumersServiceFindAllResponseDto> {
return this.service.findAll(partnerId)
}
@Get(':id')
async findOne(@PartnerInfo('id') partnerId: string, @Param('id') id: string) {
async findOne(@PartnerInfo('id') partnerId: string, @Param('id') id: string): Promise<PartnerConsumersServiceFindOneResponseDto> {
return this.service.findOne(partnerId, id)
}
@Post()
async create(@PartnerInfo('id') partnerId: string, @Body() data: CreateConsumerDto) {
async create(@PartnerInfo('id') partnerId: string, @Body() data: CreateConsumerDto): Promise<PartnerConsumersServiceCreateResponseDto> {
return this.service.create(partnerId, data)
}
@@ -27,7 +28,7 @@ export class PartnerConsumersController {
@Param('id') id: string,
@PartnerInfo('id') partnerId: string,
@Body() data: UpdateConsumerDto,
) {
): Promise<PartnerConsumersServiceUpdateResponseDto> {
return this.service.update(id, partnerId, data)
}
}
@@ -1,13 +1,19 @@
import { QUERY_CONSTANTS } from '@/common/queryConstants'
import mapConsumerWithLicenseUtil from '@/common/utils/mappers/consumer_mappers.util'
import { PasswordUtil } from '@/common/utils/password.util'
import {
AccountStatus,
AccountType,
ConsumerRole,
ConsumerStatus,
ConsumerType,
PartnerStatus,
} from '@/generated/prisma/enums'
import { ConsumerSelect, ConsumerWhereInput } from '@/generated/prisma/models'
import mapConsumerWithLicenseUtil from '@/modules/consumer/utils/mapConsumerWithLicense.util'
import {
ConsumerCreateInput,
ConsumerSelect,
ConsumerWhereInput,
} from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
@@ -25,16 +31,24 @@ export class PartnerConsumersService {
defaultSelect: ConsumerSelect = {
id: true,
first_name: true,
last_name: true,
mobile_number: true,
national_code: true,
...QUERY_CONSTANTS.CONSUMER.infoSelect,
status: true,
created_at: true,
}
private defaultWhere = (partner_id: string): ConsumerWhereInput => ({
partner_id,
OR: [
{
legal: {
partner_id,
},
},
{
individual: {
partner_id,
},
},
],
})
async findAll(partner_id: string, page = 1, perPage = 10) {
@@ -105,48 +119,102 @@ export class PartnerConsumersService {
throw new BadRequestException(`تعداد لایسنس‌های فعلی شما به پایان رسیده است.`)
}
const { username, password, license_starts_at, ...rest } = data
const { username, password, customer, ...rest } = data
const { individual, legal } = customer
const existingConsumer = await tx.consumer.findFirst({
where: {
partner_id,
OR: [
{ mobile_number: data.mobile_number },
{ national_code: data.national_code },
],
},
select: { id: true, first_name: true, last_name: true },
})
if (existingConsumer) {
throw new BadRequestException(
`مصرف کننده‌ی ${existingConsumer.first_name} ${existingConsumer.last_name} با شماره موبایل یا کد ملی وارد شده از قبل ثبت شده است.`,
)
}
return await tx.consumer.create({
data: {
...rest,
partner: {
connect: {
id: partner_id,
},
},
consumer_accounts: {
create: {
role: ConsumerRole.OWNER,
account: {
create: {
username,
password: await PasswordUtil.hash(password),
type: AccountType.CONSUMER,
status: AccountStatus.ACTIVE,
},
const createConsumerData: ConsumerCreateInput = {
...rest,
accounts: {
create: {
role: ConsumerRole.OWNER,
account: {
create: {
username,
password: await PasswordUtil.hash(password),
type: AccountType.CONSUMER,
status: AccountStatus.ACTIVE,
},
},
},
status: ConsumerStatus.ACTIVE,
},
status: ConsumerStatus.ACTIVE,
}
const partner = {
partner: { connect: { id: partner_id } },
}
if (!legal && !individual) {
throw new BadRequestException(
`برای ایجاد مصرف کننده باید یکی از اطلاعات فردی یا حقوقی وارد شود.`,
)
}
if (data.type === ConsumerType.INDIVIDUAL && !individual) {
throw new BadRequestException(
`برای ایجاد مصرف کننده با نوع حقیقی باید اطلاعات فردی وارد شود.`,
)
}
if (data.type === ConsumerType.LEGAL && !legal) {
throw new BadRequestException(
`برای ایجاد مصرف کننده با نوع حقوقی باید اطلاعات حقوقی وارد شود.`,
)
}
let consumerExistError = ''
if (legal) {
const existingConsumer = await tx.consumer.findFirst({
where: {
legal: { registration_code: legal.registration_code, partner_id },
},
select: {
id: true,
legal: {
select: {
name: true,
},
},
},
})
createConsumerData.legal = {
create: {
...legal,
...partner,
},
}
if (existingConsumer) {
consumerExistError = `مشتری با نام ${existingConsumer.legal!.name} با این شماره ثبت، از قبل توسط شما ایجاد شده است.`
}
} else if (individual) {
const existingConsumer = await tx.consumer.findFirst({
where: {
individual: { national_code: individual.national_code, partner_id },
},
select: {
id: true,
individual: {
select: {
first_name: true,
last_name: true,
},
},
},
})
createConsumerData.individual = {
create: {
...individual,
...partner,
},
}
if (existingConsumer) {
;`مشتری با نام ${existingConsumer.individual!.first_name} ${existingConsumer.individual!.last_name} یا این کد ملی، از قبل توسط شما ایجاد شده است.`
}
}
return await tx.consumer.create({
data: createConsumerData,
select: this.defaultSelect,
})
})
@@ -0,0 +1,6 @@
import type { PartnerConsumersService } from '../consumers.service'
export type PartnerConsumersServiceCreateResponseDto = Awaited<ReturnType<PartnerConsumersService['create']>>
export type PartnerConsumersServiceFindAllResponseDto = Awaited<ReturnType<PartnerConsumersService['findAll']>>
export type PartnerConsumersServiceFindOneResponseDto = Awaited<ReturnType<PartnerConsumersService['findOne']>>
export type PartnerConsumersServiceUpdateResponseDto = Awaited<ReturnType<PartnerConsumersService['update']>>
@@ -1,23 +1,12 @@
import { ConsumerStatus } from '@/generated/prisma/enums'
import { ConsumerIndividual, ConsumerLegal } from '@/generated/prisma/client'
import { ConsumerStatus, ConsumerType } from '@/generated/prisma/enums'
import { ApiProperty, OmitType, PartialType } from '@nestjs/swagger'
import { IsDateString, IsEnum, IsString } from 'class-validator'
import { IsEnum, IsObject, IsString } from 'class-validator'
export class CreateConsumerDto {
@IsString()
@ApiProperty({ required: true })
first_name: string
@IsString()
@ApiProperty({ required: true })
last_name: string
@IsString()
@ApiProperty({ required: true })
mobile_number: string
@IsString()
@ApiProperty({ required: true })
national_code: string
@IsEnum(ConsumerType)
@ApiProperty({ enum: ConsumerType, required: true })
type: ConsumerType
@IsString()
@ApiProperty({ required: true })
@@ -28,11 +17,11 @@ export class CreateConsumerDto {
password: string
@ApiProperty({ required: true })
@IsDateString(
{ strict: true },
{ message: 'invoice_date must be a valid ISO-8601 string' },
)
license_starts_at: Date
@IsObject()
customer: {
individual?: Omit<ConsumerIndividual, 'partner_id' | 'consumer_id'>
legal?: Omit<ConsumerLegal, 'partner_id' | 'consumer_id'>
}
}
export class UpdateConsumerDto extends OmitType(PartialType(CreateConsumerDto), [
'password',
@@ -0,0 +1,3 @@
import type { LicensesService } from '../licenses.service'
export type LicensesServiceFindAllResponseDto = Awaited<ReturnType<LicensesService['findAll']>>
@@ -2,6 +2,7 @@ import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
import { Controller, Get, Param } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { LicensesService } from './licenses.service'
import type { LicensesServiceFindAllResponseDto } from './dto/licenses-response.dto'
@ApiTags('AdminConsumerAccounts')
@Controller('admin/consumers/:consumerId/licenses')
@@ -12,7 +13,7 @@ export class LicensesController {
async findAll(
@PartnerInfo('id') partnerId: string,
@Param('consumerId') consumerId: string,
) {
): Promise<LicensesServiceFindAllResponseDto> {
return this.service.findAll(partnerId, consumerId)
}
+5 -1
View File
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
import { IsString } from 'class-validator'
import { IsOptional, IsString } from 'class-validator'
export class UpdatePartnerProfileDto {
@IsString()
@@ -9,4 +9,8 @@ export class UpdatePartnerProfileDto {
@IsString()
@ApiProperty({ required: true })
code: string
@IsOptional()
@ApiProperty({ required: false, type: 'string', format: 'binary' })
logo?: any
}
@@ -0,0 +1,5 @@
import type { PartnerService } from '../partners.service'
export type PartnerServiceGetInfoResponseDto = Awaited<ReturnType<PartnerService['getInfo']>>
export type PartnerServiceMeResponseDto = Awaited<ReturnType<PartnerService['me']>>
export type PartnerServiceUpdateInfoResponseDto = Awaited<ReturnType<PartnerService['updateInfo']>>
@@ -0,0 +1,38 @@
import { ApiPropertyOptional } from '@nestjs/swagger'
import { Type } from 'class-transformer'
import { IsOptional, IsString, Max, Min } from 'class-validator'
export class PartnerLicensesFilterDto {
@ApiPropertyOptional({ type: Number, example: 1 })
@Type(() => Number)
@Min(1)
@IsOptional()
page?: number
@ApiPropertyOptional({ type: Number, example: 10, description: 'Max value is 50.' })
@Type(() => Number)
@Min(1)
@Max(50)
@IsOptional()
perPage?: number
@ApiPropertyOptional({ type: String })
@IsString()
@IsOptional()
consumer_name?: string
@ApiPropertyOptional({ type: String })
@IsString()
@IsOptional()
consumer_mobile?: string
@ApiPropertyOptional({ type: String, example: '2026-01-01' })
@IsString()
@IsOptional()
expires_from?: string
@ApiPropertyOptional({ type: String, example: '2026-12-31' })
@IsString()
@IsOptional()
expires_to?: string
}
@@ -1,11 +1,15 @@
import { ApiProperty } from '@nestjs/swagger'
import { Paginated } from 'common/response/response-mapper'
import { ConsumerType } from '@/generated/prisma/enums'
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'
import type { PartnerLicensesService } from '../licenses.service'
export interface PartnerLicenseActivationConsumerResponseDto {
id: string
first_name: string
last_name: string
mobile_number: string
type: ConsumerType
first_name?: string | null
last_name?: string | null
mobile_number?: string | null
name?: string | null
registration_code?: string | null
}
export interface PartnerLicenseActivationBusinessActivityResponseDto {
@@ -41,21 +45,34 @@ export interface PartnerLicenseActivationResponseDto {
_count: PartnerLicenseActivationCountResponseDto
}
export type PartnerLicensesServiceFindAllResponseDto = Awaited<
ReturnType<PartnerLicensesService['findAll']>
>
export type PartnerLicenseActivationPaginatedResponseDto =
Paginated<PartnerLicenseActivationResponseDto>
PartnerLicensesServiceFindAllResponseDto
export class PartnerLicenseActivationConsumerResponseSchema {
@ApiProperty()
id: string
@ApiProperty()
first_name: string
@ApiProperty({ enum: ConsumerType })
type: ConsumerType
@ApiProperty()
last_name: string
@ApiPropertyOptional()
first_name?: string
@ApiProperty()
mobile_number: string
@ApiPropertyOptional()
last_name?: string
@ApiPropertyOptional()
mobile_number?: string
@ApiPropertyOptional()
name?: string
@ApiPropertyOptional()
registration_code?: string
}
export class PartnerLicenseActivationBusinessActivityResponseSchema {
@@ -1,7 +1,9 @@
import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
import { Controller, Get, Query } from '@nestjs/common'
import { ApiOkResponse, ApiQuery, ApiTags } from '@nestjs/swagger'
import { PartnerLicensesFilterDto } from './dto/licenses-filter.dto'
import { PartnerLicenseActivationPaginatedResponseSchema } from './dto/licenses-response.dto'
import type { PartnerLicensesServiceFindAllResponseDto } from './dto/licenses-response.dto'
import { PartnerLicensesService } from './licenses.service'
@ApiTags('PartnerLicenses')
@@ -36,14 +38,16 @@ export class PartnerLicensesController {
@Query('consumer_mobile') consumer_mobile?: string,
@Query('expires_from') expires_from?: string,
@Query('expires_to') expires_to?: string,
) {
return this.service.findAll(partnerId, {
): Promise<PartnerLicensesServiceFindAllResponseDto> {
const filter: PartnerLicensesFilterDto = {
page: Number(page) || 1,
perPage: Number(perPage) || 10,
consumer_name,
consumer_mobile,
expires_from,
expires_to,
})
}
return this.service.findAll(partnerId, filter)
}
}
@@ -0,0 +1,104 @@
import {
PartnerLicenseActivationBusinessActivityResponseDto,
PartnerLicenseActivationChargeTransactionResponseDto,
PartnerLicenseActivationConsumerResponseDto,
PartnerLicenseActivationCountResponseDto,
PartnerLicenseActivationLicenseResponseDto,
PartnerLicenseActivationResponseDto,
} from './dto/licenses-response.dto'
type PartnerLicenseActivationConsumerQueryResult = {
id: string
type: PartnerLicenseActivationConsumerResponseDto['type']
individual?: {
first_name: string
last_name: string
mobile_number: string
} | null
legal?: {
name: string
registration_code: string
} | null
}
type PartnerLicenseActivationBusinessActivityQueryResult = {
id: string
name: string
consumer: PartnerLicenseActivationConsumerQueryResult
}
type PartnerLicenseActivationChargeTransactionQueryResult = {
id: string
tracking_code: string
activation_expires_at: Date
}
type PartnerLicenseActivationLicenseQueryResult = {
id: string
accounts_limit: number
charge_transaction: PartnerLicenseActivationChargeTransactionQueryResult
}
type PartnerLicenseActivationQueryResult = {
id: string
starts_at: Date
expires_at: Date
created_at: Date
updated_at: Date
business_activity: PartnerLicenseActivationBusinessActivityQueryResult
license: PartnerLicenseActivationLicenseQueryResult
_count: PartnerLicenseActivationCountResponseDto
}
export const mapPartnerLicenseActivationConsumer = (
consumer: PartnerLicenseActivationConsumerQueryResult,
): PartnerLicenseActivationConsumerResponseDto => {
const individual = consumer.individual ?? null
const legal = consumer.legal ?? null
return {
id: consumer.id,
type: consumer.type,
first_name: individual?.first_name ?? null,
last_name: individual?.last_name ?? null,
mobile_number: individual?.mobile_number ?? null,
name: legal?.name ?? null,
registration_code: legal?.registration_code ?? null,
}
}
export const mapPartnerLicenseActivation = (
licenseActivation: PartnerLicenseActivationQueryResult,
): PartnerLicenseActivationResponseDto => {
const businessActivity: PartnerLicenseActivationBusinessActivityResponseDto = {
id: licenseActivation.business_activity.id,
name: licenseActivation.business_activity.name,
consumer: mapPartnerLicenseActivationConsumer(
licenseActivation.business_activity.consumer,
),
}
const chargeTransaction: PartnerLicenseActivationChargeTransactionResponseDto = {
id: licenseActivation.license.charge_transaction.id,
tracking_code: licenseActivation.license.charge_transaction.tracking_code,
activation_expires_at:
licenseActivation.license.charge_transaction.activation_expires_at,
}
const license: PartnerLicenseActivationLicenseResponseDto = {
id: licenseActivation.license.id,
accounts_limit: licenseActivation.license.accounts_limit,
charge_transaction: chargeTransaction,
}
return {
id: licenseActivation.id,
starts_at: licenseActivation.starts_at,
expires_at: licenseActivation.expires_at,
created_at: licenseActivation.created_at,
updated_at: licenseActivation.updated_at,
business_activity: businessActivity,
license,
_count: licenseActivation._count,
}
}
@@ -1,19 +1,8 @@
import { PrismaService } from '@/prisma/prisma.service'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
import {
PartnerLicenseActivationPaginatedResponseDto,
PartnerLicenseActivationResponseDto,
} from './dto/licenses-response.dto'
type FindPartnerLicensesParams = {
perPage?: number
page?: number
consumer_name?: string
consumer_mobile?: string
expires_from?: string
expires_to?: string
}
import { PartnerLicensesFilterDto } from './dto/licenses-filter.dto'
import { mapPartnerLicenseActivation } from './licenses.mapper'
@Injectable()
export class PartnerLicensesService {
@@ -24,16 +13,18 @@ export class PartnerLicensesService {
async findAll(
partner_id: string,
params: FindPartnerLicensesParams = {},
): Promise<PartnerLicenseActivationPaginatedResponseDto> {
params: PartnerLicensesFilterDto = {},
) {
const { perPage, page, consumer_name, consumer_mobile, expires_from, expires_to } =
params
const normalizedPage = Number.isFinite(page)
? Math.max(1, Math.floor(page as number))
const normalizedPageValue = Number(page ?? 1)
const normalizedPage = Number.isFinite(normalizedPageValue)
? Math.max(1, Math.floor(normalizedPageValue))
: 1
const requestedPerPage = Number.isFinite(perPage)
? Math.max(1, Math.floor(perPage as number))
const requestedPerPageValue = Number(perPage ?? this.defaultPerPage)
const requestedPerPage = Number.isFinite(requestedPerPageValue)
? Math.max(1, Math.floor(requestedPerPageValue))
: this.defaultPerPage
const normalizedPerPage = Math.min(requestedPerPage, this.maxPerPage)
@@ -50,7 +41,7 @@ export class PartnerLicensesService {
const consumerName = consumer_name?.trim()
const consumerMobile = consumer_mobile?.trim()
if (consumerName || consumerMobile) {
if (consumerName) {
andWhere.push({
business_activity: {
consumer: {
@@ -125,9 +116,20 @@ export class PartnerLicensesService {
consumer: {
select: {
id: true,
first_name: true,
last_name: true,
mobile_number: true,
type: true,
individual: {
select: {
first_name: true,
last_name: true,
mobile_number: true,
},
},
legal: {
select: {
name: true,
registration_code: true,
},
},
},
},
},
@@ -155,13 +157,12 @@ export class PartnerLicensesService {
await tx.licenseActivation.count({ where }),
])
return ResponseMapper.paginate<PartnerLicenseActivationResponseDto>(
licenseActivations,
{
total,
page: normalizedPage,
perPage: normalizedPerPage,
},
)
const mappedData = licenseActivations.map(mapPartnerLicenseActivation)
return ResponseMapper.paginate(mappedData, {
total,
page: normalizedPage,
perPage: normalizedPerPage,
})
}
}
+32 -7
View File
@@ -1,7 +1,15 @@
import { PartnerInfo } from '@/common/decorators/partnerInfo.decorator'
import type { IPartnerPayload } from '@/common/models/partnerPayload.model'
import { Controller, Get, Patch } from '@nestjs/common'
import { ApiTags } from '@nestjs/swagger'
import { multerImageOptions } from '@/multer.config'
import {
Body,
Controller,
Get,
Patch,
UploadedFile,
UseInterceptors,
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express'
import { ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger'
import { UpdatePartnerProfileDto } from './dto/partner.dto'
import { PartnerService } from './partners.service'
@@ -11,17 +19,34 @@ export class PartnerController {
constructor(private service: PartnerService) {}
@Get('')
async me(@PartnerInfo() { id, account_id }: IPartnerPayload) {
async me(@PartnerInfo() { id, account_id }: IPartnerPayload): Promise<PartnerServiceMeResponseDto> {
return this.service.me(id, account_id)
}
@Get('info')
async getInfo(@PartnerInfo('id') partnerId: string) {
async getInfo(@PartnerInfo('id') partnerId: string): Promise<PartnerServiceGetInfoResponseDto> {
return this.service.getInfo(partnerId)
}
@Patch('profile')
async update(@PartnerInfo('id') partnerId: string, data: UpdatePartnerProfileDto) {
return this.service.updateInfo(partnerId, data)
@UseInterceptors(FileInterceptor('logo', multerImageOptions))
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
logo: { type: 'string', format: 'binary' },
},
},
})
async update(
@PartnerInfo('id') partnerId: string,
@Body() data: UpdatePartnerProfileDto,
@UploadedFile() logo: Express.Multer.File,
): Promise<PartnerServiceUpdateInfoResponseDto> {
return this.service.updateInfo(partnerId, data, logo)
}
}
import type { IPartnerPayload } from '@/common/models/partnerPayload.model'
import type { PartnerServiceGetInfoResponseDto, PartnerServiceMeResponseDto, PartnerServiceUpdateInfoResponseDto } from './dto/partners-response.dto'
+7 -1
View File
@@ -1,5 +1,6 @@
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { UploaderModule } from '../uploader/uploader.module'
import { PartnerAccountsModule } from './accounts/accounts.module'
import { PartnerCustomersModule } from './consumers/consumers.module'
import { PartnerLicensesModule } from './licenses/licenses.module'
@@ -9,7 +10,12 @@ import { PartnerService } from './partners.service'
@Module({
controllers: [PartnerController],
imports: [PartnerAccountsModule, PartnerCustomersModule, PartnerLicensesModule],
imports: [
PartnerAccountsModule,
PartnerCustomersModule,
PartnerLicensesModule,
UploaderModule,
],
providers: [JwtService, PartnerService],
})
export class PartnerModule implements NestModule {
+44 -8
View File
@@ -1,12 +1,17 @@
import { UploadedFileTypes } from '@/common/enums/enums'
import { ResponseMapper } from '@/common/response/response-mapper'
import { PartnerAccountSelect } from '@/generated/prisma/models'
import { UploaderService } from '@/modules/uploader/uploader.service'
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { UpdatePartnerProfileDto } from './dto/partner.dto'
@Injectable()
export class PartnerService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly uploaderService: UploaderService,
) {}
private readonly defaultSelect: PartnerAccountSelect = {}
@@ -45,6 +50,7 @@ export class PartnerService {
id: true,
name: true,
code: true,
logo_url: true,
created_at: true,
license_charge_transactions: {
select: {
@@ -193,13 +199,43 @@ export class PartnerService {
})
}
async updateInfo(partner_id: string, data: UpdatePartnerProfileDto) {
const updatedPartner = await this.prisma.partner.update({
where: {
id: partner_id,
},
data,
select: this.defaultSelect,
async updateInfo(
partner_id: string,
data: UpdatePartnerProfileDto,
logo?: Express.Multer.File,
) {
const rest = { ...(data as any) }
delete rest.logo
const updatedPartner = await this.prisma.$transaction(async tx => {
let logo_url = ''
if (logo) {
const uploadedUrl = await this.uploaderService.uploadFile(
logo,
UploadedFileTypes.PARTNER_LOGO,
)
logo_url = uploadedUrl || ''
}
const prevLogo = await tx.partner.findUnique({
where: { id: partner_id },
select: { logo_url: true },
})
if (logo_url && prevLogo?.logo_url) {
this.uploaderService.deleteFile(prevLogo.logo_url, UploadedFileTypes.PARTNER_LOGO)
}
return tx.partner.update({
where: {
id: partner_id,
},
data: {
...rest,
...(logo_url ? { logo_url } : {}),
},
select: this.defaultSelect,
})
})
return ResponseMapper.single(updatedPartner)