feat: implement correction and original send functionality for Nama provider

- Added new DTOs for correction requests and responses in `nama-provider.dto.ts`.
- Updated `nama-provider.adapter.ts` to include `originalSend` and `correctionSend` methods.
- Enhanced `nama-provider.util.ts` with mapping functions for correction requests.
- Created operational guidelines for agents in `AGENT.md`.
- Updated Prisma migrations to support new invoice types and relationships.
- Introduced new service and DTO for creating sales invoices in `sale-invoice-create.service.ts` and `sale-invoice-create.dto.ts`.
- Added utility for handling Prisma errors in `prisma-error.util.ts`.
This commit is contained in:
2026-05-05 22:42:09 +03:30
parent 4af07fe3e8
commit 658496320b
43 changed files with 2808 additions and 1354 deletions
@@ -0,0 +1,24 @@
/*
Warnings:
- You are about to drop the column `type` on the `sale_invoice_tsp_attempts` table. All the data in the column will be lost.
- A unique constraint covering the columns `[ref_id]` on the table `sales_invoices` will be added. If there are existing duplicate values, this will fail.
- Added the required column `type` to the `sales_invoices` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `sale_invoice_tsp_attempts` DROP COLUMN `type`;
-- AlterTable
ALTER TABLE `sales_invoices` ADD COLUMN `main_id` VARCHAR(50) NULL,
ADD COLUMN `ref_id` VARCHAR(50) NULL,
ADD COLUMN `type` ENUM('ORIGINAL', 'CORRECTION', 'REVOKE', 'RETURN') NOT NULL;
-- CreateIndex
CREATE UNIQUE INDEX `sales_invoices_ref_id_key` ON `sales_invoices`(`ref_id`);
-- CreateIndex
CREATE INDEX `sales_invoices_ref_id_idx` ON `sales_invoices`(`ref_id`);
-- AddForeignKey
ALTER TABLE `sales_invoices` ADD CONSTRAINT `sales_invoices_ref_id_fkey` FOREIGN KEY (`ref_id`) REFERENCES `sales_invoices`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
@@ -0,0 +1,43 @@
/*
Warnings:
- You are about to drop the column `error_message` on the `sale_invoice_tsp_attempts` table. All the data in the column will be lost.
- You are about to drop the column `tax_id` on the `sale_invoice_tsp_attempts` table. All the data in the column will be lost.
- A unique constraint covering the columns `[tax_id]` on the table `sales_invoices` will be added. If there are existing duplicate values, this will fail.
- Added the required column `message` to the `sale_invoice_tsp_attempts` table without a default value. This is not possible if the table is not empty.
*/
-- 1) Add new columns in a backward-compatible way
ALTER TABLE `sale_invoice_tsp_attempts`
ADD COLUMN `message` TEXT NULL;
ALTER TABLE `sales_invoices`
ADD COLUMN `tax_id` VARCHAR(32) NULL;
-- 2) Backfill message from old error_message, fallback to a default text
UPDATE `sale_invoice_tsp_attempts`
SET `message` = COALESCE(`error_message`, 'وضعیت ارسال فاکتور ثبت شد.')
WHERE `message` IS NULL;
-- 3) Backfill tax_id to sales_invoices from attempts
UPDATE `sales_invoices` si
JOIN `sale_invoice_tsp_attempts` sita ON sita.`invoice_id` = si.`id`
SET si.`tax_id` = sita.`tax_id`
WHERE sita.`tax_id` IS NOT NULL
AND si.`tax_id` IS NULL;
-- 4) Enforce required constraint after backfill
ALTER TABLE `sale_invoice_tsp_attempts`
MODIFY `message` TEXT NOT NULL;
-- 5) Remove old indexes/columns after successful data move
DROP INDEX `sale_invoice_tsp_attempts_tax_id_idx` ON `sale_invoice_tsp_attempts`;
DROP INDEX `sale_invoice_tsp_attempts_tax_id_key` ON `sale_invoice_tsp_attempts`;
ALTER TABLE `sale_invoice_tsp_attempts`
DROP COLUMN `error_message`,
DROP COLUMN `tax_id`;
-- 6) Add indexes on new tax_id location
CREATE UNIQUE INDEX `sales_invoices_tax_id_key` ON `sales_invoices`(`tax_id`);
CREATE INDEX `sales_invoices_tax_id_idx` ON `sales_invoices`(`tax_id`);
+2 -3
View File
@@ -167,12 +167,11 @@ enum TspProviderResponseStatus {
FAILURE
NOT_SEND
QUEUED
REVOKED
}
enum TspProviderRequestType {
MAIN
UPDATE
ORIGINAL
CORRECTION
REVOKE
RETURN
}
+18 -12
View File
@@ -1,9 +1,11 @@
model SalesInvoice {
id String @id @default(uuid())
code String @unique @db.VarChar(100)
total_amount Decimal @db.Decimal(15, 2)
invoice_number Int @db.Int()
invoice_date DateTime @default(now()) @db.Timestamp(0)
id String @id @default(uuid())
code String @unique @db.VarChar(100)
total_amount Decimal @db.Decimal(15, 2)
invoice_number Int @db.Int()
invoice_date DateTime @default(now()) @db.Timestamp(0)
type TspProviderRequestType
tax_id String? @unique @db.VarChar(32)
notes String? @db.Text
unknown_customer Json? @db.Json
@@ -11,6 +13,10 @@ model SalesInvoice {
created_at DateTime @default(now()) @db.Timestamp(0)
updated_at DateTime @updatedAt @db.Timestamp(0)
main_id String? @db.VarChar(50)
ref_id String? @unique @db.VarChar(50)
reference_invoice SalesInvoice? @relation("SalesInvoiceReference", fields: [ref_id], references: [id])
customer_id String?
customer Customer? @relation(fields: [customer_id], references: [id])
@@ -20,11 +26,14 @@ model SalesInvoice {
pos_id String
pos Pos @relation(fields: [pos_id], references: [id])
items SalesInvoiceItem[]
payments SalesInvoicePayment[]
tsp_attempts SaleInvoiceTspAttempts[]
referenced_by SalesInvoice? @relation("SalesInvoiceReference")
items SalesInvoiceItem[]
payments SalesInvoicePayment[]
tsp_attempts SaleInvoiceTspAttempts[]
@@unique([invoice_number, pos_id])
@@index([ref_id])
@@index([tax_id])
@@map("sales_invoices")
}
@@ -62,12 +71,10 @@ model SaleInvoiceTspAttempts {
attempt_no Int
status TspProviderResponseStatus
tax_id String? @unique @db.VarChar(191)
type TspProviderRequestType
request_payload Json?
response_payload Json?
error_message String? @db.Text
message String @db.Text
sent_at DateTime? @db.Timestamp(0)
received_at DateTime? @db.Timestamp(0)
@@ -78,7 +85,6 @@ model SaleInvoiceTspAttempts {
@@unique([invoice_id, attempt_no])
@@index([status])
@@index([tax_id])
@@index([invoice_id])
@@map("sale_invoice_tsp_attempts")
}