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:
@@ -1,5 +1,6 @@
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsInt, IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
import { ArrayMinSize, IsInt, IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
import { CreatePurchaseReceiptItemDto } from '../../purchase-receipt-items/dto/create-purchase-receipt-item.dto'
|
||||
|
||||
export class CreatePurchaseReceiptDto {
|
||||
@IsString()
|
||||
@@ -20,4 +21,8 @@ export class CreatePurchaseReceiptDto {
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
inventoryId: number
|
||||
|
||||
@Type(() => Array)
|
||||
@ArrayMinSize(1)
|
||||
items: Array<Omit<CreatePurchaseReceiptItemDto, 'receiptId'>>
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user