change source of account id in prepared token to domain account id, create consumer salesInvoiceModule

This commit is contained in:
2026-03-30 13:16:36 +03:30
parent c870a43e35
commit 9cf9209daa
22 changed files with 758 additions and 142 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ export class PosMiddleware implements NestMiddleware {
const consumerAccount = await this.prisma.consumerAccount.findUnique({
where: {
account_id: tokenAccount.account_id,
id: tokenAccount.account_id,
},
})
if (!consumerAccount) {
@@ -54,13 +54,14 @@ export class CreateSalesInvoiceDto {
@ApiProperty({ required: false })
@IsOptional()
@IsObject()
customer?:
| {
first_name: string
last_name: string
}
| CustomerIndividual
| CustomerLegal
customer?: {
customer_individual?: Omit<CustomerIndividual, 'complex_id' | 'customer_id'>
customer_legal?: Omit<CustomerLegal, 'complex_id' | 'customer_id'>
customer_unknown?: {
first_name: string
last_name: string
}
}
}
export class UpdateSalesInvoiceDto extends PartialType(CreateSalesInvoiceDto) {}
@@ -1,6 +1,7 @@
import { IPosPayload } from '@/common/models/posPayload.model'
import { SalesInvoiceCreateInput } from '@/generated/prisma/models'
import { PrismaService } from '@/prisma/prisma.service'
import { Injectable } from '@nestjs/common'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ResponseMapper } from 'common/response/response-mapper'
import type { CustomerIndividual, CustomerLegal } from 'generated/prisma/client'
import { CustomerType, PaymentMethodType } from 'generated/prisma/enums'
@@ -65,138 +66,128 @@ export class SalesInvoicesService {
if (customer_id) {
} else if (
customer_type === CustomerType.INDIVIDUAL &&
isCustomerIndividual(customer)
customer?.customer_individual
) {
let customerIndividual = await $tx.customerIndividual.findUnique({
const customer_individual = customer.customer_individual
const customerIndividual = await $tx.customerIndividual.upsert({
where: {
complex_id_national_id: {
complex_id,
national_id: customer.national_id,
national_id: customer_individual.national_id,
},
},
create: {
...customer_individual,
customer: {
create: {
type: CustomerType.INDIVIDUAL,
},
},
complex: {
connect: {
id: complex_id,
},
},
},
update: {},
select: {
customer_id: true,
},
})
if (customerIndividual) {
await $tx.customerIndividual.update({
where: {
complex_id_national_id: {
complex_id,
national_id: customer.national_id,
},
},
data: {
...customer,
},
})
} else {
const customerIndividual = await $tx.customerIndividual.create({
data: {
...customer,
},
})
const createdCustomer = await $tx.customer.create({
data: {
type: CustomerType.INDIVIDUAL,
customer_individuals: {
connect: {
customer_id: customerIndividual.customer_id,
},
},
},
})
if (createdCustomer && createdCustomer.id) {
newCustomerId = createdCustomer.id
}
if (!customerIndividual) {
throw new BadRequestException('متاسفانه مشکلی در اطلاعات مشتری وجود دارد.')
}
} else if (data.customer_type === CustomerType.LEGAL && isCustomerLegal(customer)) {
let customerLegal = await $tx.customerLegal.findUnique({
newCustomerId = customerIndividual.customer_id
} else if (data.customer_type === CustomerType.LEGAL && customer?.customer_legal) {
const customer_legal = customer.customer_legal
const customerLegal = await $tx.customerLegal.upsert({
where: {
complex_id_registration_number: {
complex_id,
registration_number: customer.registration_number,
registration_number: customer_legal.registration_number,
},
},
create: {
...customer_legal,
customer: {
create: {
type: CustomerType.LEGAL,
},
},
complex: {
connect: {
id: complex_id,
},
},
},
update: {},
select: {
customer_id: true,
},
})
if (customerLegal) {
await $tx.customerLegal.update({
where: {
complex_id_registration_number: {
complex_id,
registration_number: customer.registration_number,
},
},
data: {
...customer,
},
})
} else {
const customerLegal = await $tx.customerLegal.create({
data: {
...customer,
},
})
const createdCustomer = await $tx.customer.create({
data: {
type: CustomerType.LEGAL,
customer_individuals: {
connect: {
customer_id: customerLegal.customer_id,
},
},
},
})
if (createdCustomer && createdCustomer.id) {
newCustomerId = createdCustomer.id
}
if (!customerLegal) {
throw new BadRequestException('متاسفانه مشکلی در اطلاعات مشتری وجود دارد.')
}
newCustomerId = customerLegal.customer_id
} else if (data.customer_type === CustomerType.UNKNOWN) {
}
const salesInvoice = await $tx.salesInvoice.create({
data: {
...invoiceData,
total_amount: data.total_amount,
code: 'INV-' + Date.now(),
const salesInvoiceData: SalesInvoiceCreateInput = {
...invoiceData,
total_amount: data.total_amount,
code: 'INV-' + Date.now(),
items: {
createMany: {
data: data.items.map(item => ({
good_id: item.good_id,
quantity: item.quantity,
unit_price: item.unit_price,
total_amount: item.total_amount,
payload: JSON.parse(JSON.stringify(item.payload)),
unit_type: item.unit_type,
// pricing_model: item.pricingModel,
})),
},
},
unknown_customer: {},
payments: {
createMany: {
data: payments,
},
},
// customer: {
// connect: {
// id: newCustomerId,
// },
// },
account: {
connect: {
id: consumer_account_id,
},
},
pos: {
connect: {
id: pos_id,
},
items: {
createMany: {
data: data.items.map(item => ({
good_id: item.good_id,
quantity: item.quantity,
unit_price: item.unit_price,
total_amount: item.total_amount,
payload: item.payload
? JSON.parse(JSON.stringify(item.payload))
: undefined,
unit_type: item.unit_type,
// pricing_model: item.pricingModel,
})),
},
},
unknown_customer: {},
payments: {
createMany: {
data: payments,
},
},
// customer: {
// connect: {
// id: newCustomerId || undefined,
// },
// },
account: {
connect: {
id: consumer_account_id,
},
},
pos: {
connect: {
id: pos_id,
},
},
}
if (newCustomerId) {
salesInvoiceData.customer = {
connect: {
id: newCustomerId || undefined,
},
}
}
const salesInvoice = await $tx.salesInvoice.create({
data: salesInvoiceData,
})
return salesInvoice