feat(invoices): add public invoices module with controller and service
refactor: remove unused trigger logs module and related files refactor: simplify good snapshot handling in sale invoice creation
This commit is contained in:
+2
-4
@@ -7,10 +7,9 @@ import { AuthModule } from './modules/auth/auth.module'
|
||||
import { CatalogModule } from './modules/catalog/catalog.module'
|
||||
import { ConsumerModule } from './modules/consumer/consumer.module'
|
||||
import { EnumsModule } from './modules/enums/enums.module'
|
||||
import { PublicInvoicesModule } from './modules/invoices/invoices.module'
|
||||
import { PartnerModule } from './modules/partners/partners.module'
|
||||
import { PosModule } from './modules/pos/pos.module'
|
||||
import { SalesInvoicePaymentsModule } from './modules/pos/sales-invoices/sales-invoice-payments/sales-invoice-payments.module'
|
||||
import { TriggerLogsModule } from './modules/trigger-logs/trigger-logs.module'
|
||||
import { UploaderModule } from './modules/uploader/uploader.module'
|
||||
import { PrismaModule } from './prisma/prisma.module'
|
||||
import { RedisModule } from './redis/redis.module'
|
||||
@@ -26,8 +25,7 @@ import { RedisModule } from './redis/redis.module'
|
||||
ConsumerModule,
|
||||
PosModule,
|
||||
PartnerModule,
|
||||
SalesInvoicePaymentsModule,
|
||||
TriggerLogsModule,
|
||||
PublicInvoicesModule,
|
||||
UploaderModule,
|
||||
ApplicationModule,
|
||||
],
|
||||
|
||||
@@ -98,14 +98,6 @@ export const select: SalesInvoiceSelect = {
|
||||
notes: true,
|
||||
payload: true,
|
||||
good_snapshot: true,
|
||||
good: {
|
||||
select: {
|
||||
name: true,
|
||||
barcode: true,
|
||||
image_url: true,
|
||||
category: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
payments: {
|
||||
|
||||
@@ -411,11 +411,7 @@ export class SharedSaleInvoiceCreateService {
|
||||
sku_vat: goodsById.get(item.good_id!)?.sku.VAT || null,
|
||||
payload: item.payload ? JSON.parse(JSON.stringify(item.payload)) : undefined,
|
||||
good_snapshot: item.good_id
|
||||
? JSON.parse(
|
||||
JSON.stringify({
|
||||
good: goodsById.get(item.good_id) || null,
|
||||
}),
|
||||
)
|
||||
? JSON.parse(JSON.stringify(goodsById.get(item.good_id) || null))
|
||||
: undefined,
|
||||
})),
|
||||
},
|
||||
|
||||
@@ -3,6 +3,7 @@ import { EnumKeyMaker } from './enum'
|
||||
import { GuildKeyMaker } from './guild'
|
||||
import { PartnerKeyMaker } from './partner'
|
||||
import { PosKeyMaker } from './pos'
|
||||
import { PublicSaleInvoiceKeyMaker } from './publicSaleInvoice'
|
||||
|
||||
// Keep backward-compatible static methods while separating key builders by domain.
|
||||
export class RedisKeyMaker extends PartnerKeyMaker {
|
||||
@@ -25,6 +26,8 @@ export class RedisKeyMaker extends PartnerKeyMaker {
|
||||
|
||||
static enumsAll = EnumKeyMaker.enumsAll
|
||||
static enumsValues = EnumKeyMaker.enumsValues
|
||||
|
||||
static publicSaleInvoice = PublicSaleInvoiceKeyMaker.invoice
|
||||
}
|
||||
|
||||
export { ConsumerKeyMaker, EnumKeyMaker, GuildKeyMaker, PartnerKeyMaker, PosKeyMaker }
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
export class PublicSaleInvoiceKeyMaker {
|
||||
static invoice(id: string): string {
|
||||
return `publicSaleInvoices:${id}`
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { Public } from '@/common/decorators/public.decorator'
|
||||
import { Controller, Get, Param } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { InvoicesService } from './invoices.service'
|
||||
|
||||
@ApiTags('PublicInvoices')
|
||||
@Controller('public-invoices')
|
||||
export class InvoicesController {
|
||||
constructor(private readonly service: InvoicesService) {}
|
||||
|
||||
@Get(':id')
|
||||
@Public()
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.service.findOne(id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { InvoicesController } from './invoices.controller'
|
||||
import { InvoicesService } from './invoices.service'
|
||||
|
||||
@Module({
|
||||
controllers: [InvoicesController],
|
||||
providers: [InvoicesService, PrismaService],
|
||||
})
|
||||
export class PublicInvoicesModule {}
|
||||
@@ -0,0 +1,143 @@
|
||||
import { RedisKeyMaker, translateEnumValue } from '@/common/utils'
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { SalesInvoiceSelect } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { RedisService } from '@/redis/redis.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@Injectable()
|
||||
export class InvoicesService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly redisService: RedisService,
|
||||
) {}
|
||||
|
||||
private readonly summarySelect: SalesInvoiceSelect = {
|
||||
id: true,
|
||||
code: true,
|
||||
invoice_date: true,
|
||||
invoice_number: true,
|
||||
notes: true,
|
||||
tax_id: true,
|
||||
total_amount: true,
|
||||
type: true,
|
||||
pos: {
|
||||
select: {
|
||||
name: true,
|
||||
complex: {
|
||||
select: {
|
||||
name: true,
|
||||
branch_code: true,
|
||||
business_activity: {
|
||||
select: {
|
||||
name: true,
|
||||
guild: {
|
||||
select: {
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
payments: {
|
||||
select: {
|
||||
amount: true,
|
||||
paid_at: true,
|
||||
payment_method: true,
|
||||
terminal_info: {
|
||||
select: {
|
||||
stan: true,
|
||||
rrn: true,
|
||||
transaction_date_time: true,
|
||||
customer_card_no: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
unknown_customer: true,
|
||||
customer: {
|
||||
select: {
|
||||
type: true,
|
||||
legal: {
|
||||
select: {
|
||||
name: true,
|
||||
economic_code: true,
|
||||
postal_code: true,
|
||||
registration_number: true,
|
||||
},
|
||||
},
|
||||
individual: {
|
||||
select: {
|
||||
economic_code: true,
|
||||
first_name: true,
|
||||
last_name: true,
|
||||
mobile_number: true,
|
||||
national_id: true,
|
||||
postal_code: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
items: {
|
||||
select: {
|
||||
good_snapshot: true,
|
||||
measure_unit_text: true,
|
||||
notes: true,
|
||||
quantity: true,
|
||||
total_amount: true,
|
||||
unit_price: true,
|
||||
discount: true,
|
||||
payload: true,
|
||||
sku_code: true,
|
||||
sku_vat: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
private readonly select: SalesInvoiceSelect = {
|
||||
...this.summarySelect,
|
||||
tsp_attempts: {
|
||||
orderBy: {
|
||||
created_at: 'desc',
|
||||
},
|
||||
select: {
|
||||
status: true,
|
||||
},
|
||||
take: 1,
|
||||
},
|
||||
}
|
||||
|
||||
private readonly where = () => ({})
|
||||
|
||||
private readonly invoiceMapper = (invoice: any) => {
|
||||
const { tsp_attempts, type, ...rest } = invoice || {}
|
||||
|
||||
return {
|
||||
...rest,
|
||||
type: translateEnumValue('TspProviderRequestType', type),
|
||||
status: translateEnumValue(
|
||||
'TspProviderResponseStatus',
|
||||
invoice.tsp_attempts?.[0]?.status || TspProviderResponseStatus.NOT_SEND,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
await this.redisService.delete(RedisKeyMaker.publicSaleInvoice(id))
|
||||
return await this.redisService.getAndSet(
|
||||
RedisKeyMaker.publicSaleInvoice(id),
|
||||
'single',
|
||||
async () => {
|
||||
const item = await this.prisma.salesInvoice.findUnique({
|
||||
where: { ...this.where(), id },
|
||||
select: this.select,
|
||||
})
|
||||
return this.invoiceMapper(item)
|
||||
},
|
||||
60 * 60,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
|
||||
export class CreateTriggerLogDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
name: string
|
||||
}
|
||||
|
||||
export class UpdateTriggerLogDto extends PartialType(CreateTriggerLogDto) {}
|
||||
@@ -1,5 +0,0 @@
|
||||
import type { TriggerLogsService } from '../trigger-logs.service'
|
||||
|
||||
export type TriggerLogsServiceCreateResponseDto = Awaited<ReturnType<TriggerLogsService['create']>>
|
||||
export type TriggerLogsServiceFindAllResponseDto = Awaited<ReturnType<TriggerLogsService['findAll']>>
|
||||
export type TriggerLogsServiceFindOneResponseDto = Awaited<ReturnType<TriggerLogsService['findOne']>>
|
||||
@@ -1,22 +0,0 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { TriggerLogsService } from './trigger-logs.service'
|
||||
|
||||
@Controller('trigger-logs')
|
||||
export class TriggerLogsController {
|
||||
constructor(private readonly triggerLogsService: TriggerLogsService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.triggerLogsService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('id') id: string) {
|
||||
return this.triggerLogsService.findOne(+id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() data: any) {
|
||||
return this.triggerLogsService.create(data)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { TriggerLogsController } from './trigger-logs.controller'
|
||||
import { TriggerLogsService } from './trigger-logs.service'
|
||||
|
||||
@Module({
|
||||
controllers: [TriggerLogsController],
|
||||
providers: [TriggerLogsService],
|
||||
})
|
||||
export class TriggerLogsModule {}
|
||||
@@ -1,19 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@Injectable()
|
||||
export class TriggerLogsService {
|
||||
findAll() {
|
||||
// TODO: Implement fetching all trigger logs
|
||||
return []
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
// TODO: Implement fetching a single trigger log
|
||||
return {}
|
||||
}
|
||||
|
||||
create(data: any) {
|
||||
// TODO: Implement trigger log creation
|
||||
return data
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user