feat(database): initialize database schema and triggers

- Created initial database schema with tables for Users, Roles, Products, and related entities.
- Added foreign key constraints to maintain data integrity across tables.
- Implemented triggers for managing stock movements on insert, update, and delete operations.
- Developed scripts to dump existing triggers and pull trigger definitions from the database.
This commit is contained in:
2025-12-10 16:54:08 +03:30
parent 807f6c2087
commit 41d7dd8802
26 changed files with 1108 additions and 515 deletions
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'
import { ResponseMapper } from '../common/response/response-mapper'
import { Prisma } from '../generated/prisma/client'
import { PrismaService } from '../prisma/prisma.service'
import { CreatePurchaseReceiptDto } from './dto/create-purchase-receipt.dto'
@@ -8,17 +9,32 @@ export class PurchaseReceiptsService {
constructor(private prisma: PrismaService) {}
async create(dto: CreatePurchaseReceiptDto) {
const payload: any = { ...dto }
if (Object.prototype.hasOwnProperty.call(payload, 'supplierId')) {
payload.supplier = { connect: { id: Number(payload.supplierId) } }
delete payload.supplierId
const data: Prisma.PurchaseReceiptCreateInput = {
code: dto.code,
totalAmount: new Prisma.Decimal(dto.totalAmount),
description: dto.description ?? null,
supplier: { connect: { id: dto.supplierId } },
inventory: { connect: { id: dto.inventoryId } },
items: dto.items?.length
? {
create: dto.items.map((item, idx) => {
const mapped = {
count: new Prisma.Decimal(item.count ?? 0),
fee: new Prisma.Decimal(item.fee ?? 0),
total: new Prisma.Decimal(item.total ?? 0),
description: item.description ?? null,
product: { connect: { id: item.productId } },
}
console.log(`[create] Step 3: Mapped item ${idx}:`, mapped)
return mapped
}),
}
: undefined,
}
if (Object.prototype.hasOwnProperty.call(payload, 'inventoryId')) {
payload.inventory = { connect: { id: Number(payload.inventoryId) } }
delete payload.inventoryId
}
const item = await this.prisma.purchaseReceipt.create({ data: payload })
const item = await this.prisma.purchaseReceipt.create({
data,
include: { items: true },
})
return ResponseMapper.create(item)
}