feat: add product variant management functionality

- Implemented CreateProductVariantDto and UpdateProductVariantDto for product variant data transfer.
- Developed ProductVariantsController to handle CRUD operations for product variants.
- Created ProductVariantsService to interact with the database using Prisma.
- Added ProductVariantsModule to encapsulate the product variant feature.
- Included response mapping for consistent API responses.
This commit is contained in:
2025-12-06 17:48:10 +03:30
parent 7cb9d7d037
commit a782d61890
68 changed files with 4155 additions and 3334 deletions
@@ -0,0 +1,83 @@
/*
Warnings:
- You are about to drop the column `alertQuantity` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `attachmentId` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `discount` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `imageUrl` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `isActive` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `isFeatured` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `productInfoId` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `quantity` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `unit` on the `Products` table. All the data in the column will be lost.
- You are about to drop the `Product_info` table. If the table is not empty, all the data it contains will be lost.
- Made the column `createdAt` on table `Products` required. This step will fail if there are existing NULL values in that column.
- Made the column `updatedAt` on table `Products` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE `Product_info` DROP FOREIGN KEY `Product_info_brandId_fkey`;
-- DropForeignKey
ALTER TABLE `Product_info` DROP FOREIGN KEY `Product_info_categoryId_fkey`;
-- DropForeignKey
ALTER TABLE `Product_info` DROP FOREIGN KEY `Product_info_supplierId_fkey`;
-- DropForeignKey
ALTER TABLE `Products` DROP FOREIGN KEY `Products_productInfoId_fkey`;
-- DropIndex
DROP INDEX `Products_productInfoId_fkey` ON `Products`;
-- AlterTable
ALTER TABLE `Products` DROP COLUMN `alertQuantity`,
DROP COLUMN `attachmentId`,
DROP COLUMN `discount`,
DROP COLUMN `imageUrl`,
DROP COLUMN `isActive`,
DROP COLUMN `isFeatured`,
DROP COLUMN `productInfoId`,
DROP COLUMN `quantity`,
DROP COLUMN `unit`,
ADD COLUMN `brandId` INTEGER NULL,
ADD COLUMN `categoryId` INTEGER NULL,
ADD COLUMN `metaData` JSON NULL,
ADD COLUMN `productType` VARCHAR(50) NULL DEFAULT 'simple',
MODIFY `createdAt` TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0),
MODIFY `updatedAt` TIMESTAMP(0) NOT NULL;
-- DropTable
DROP TABLE `Product_info`;
-- CreateTable
CREATE TABLE `Product_Variants` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`basePrice` DECIMAL(10, 2) NOT NULL,
`salePrice` DECIMAL(10, 2) NOT NULL,
`description` TEXT NULL,
`barcode` VARCHAR(100) NULL,
`imageUrl` VARCHAR(255) NULL,
`unit` VARCHAR(10) NULL,
`quantity` DECIMAL(10, 2) NULL DEFAULT 0.00,
`alertQuantity` DECIMAL(10, 2) NULL DEFAULT 5.00,
`isActive` BOOLEAN NOT NULL DEFAULT true,
`isFeatured` BOOLEAN NOT NULL DEFAULT false,
`createdAt` TIMESTAMP(0) NULL,
`updatedAt` TIMESTAMP(0) NULL,
`deletedAt` TIMESTAMP(0) NULL,
`productId` INTEGER NOT NULL,
UNIQUE INDEX `products_barcode_unique`(`barcode`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- AddForeignKey
ALTER TABLE `Product_Variants` ADD CONSTRAINT `Product_Variants_productId_fkey` FOREIGN KEY (`productId`) REFERENCES `Products`(`id`) ON DELETE RESTRICT ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Products` ADD CONSTRAINT `Products_brandId_fkey` FOREIGN KEY (`brandId`) REFERENCES `Product_brands`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
-- AddForeignKey
ALTER TABLE `Products` ADD CONSTRAINT `Products_categoryId_fkey` FOREIGN KEY (`categoryId`) REFERENCES `Product_categories`(`id`) ON DELETE SET NULL ON UPDATE NO ACTION;
@@ -0,0 +1,10 @@
/*
Warnings:
- You are about to drop the column `metaData` on the `Products` table. All the data in the column will be lost.
- You are about to drop the column `productType` on the `Products` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE `Products` DROP COLUMN `metaData`,
DROP COLUMN `productType`;
+19 -25
View File
@@ -35,26 +35,25 @@ model Role {
@@map("Roles")
}
model Product {
model ProductVariant {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
basePrice Decimal @db.Decimal(10, 2)
salePrice Decimal @db.Decimal(10, 2)
description String? @db.Text
sku String? @unique(map: "products_sku_unique") @db.VarChar(100)
barcode String? @unique(map: "products_barcode_unique") @db.VarChar(100)
imageUrl String? @db.VarChar(255)
attachmentId BigInt? @db.UnsignedBigInt
unit String @db.VarChar(10)
discount Decimal @default(0.00) @db.Decimal(10, 2)
quantity Decimal @default(0.00) @db.Decimal(10, 2)
alertQuantity Decimal @default(5.00) @db.Decimal(10, 2)
unit String? @db.VarChar(10)
quantity Decimal? @default(0.00) @db.Decimal(10, 2)
alertQuantity Decimal? @default(5.00) @db.Decimal(10, 2)
isActive Boolean @default(true)
isFeatured Boolean @default(false)
createdAt DateTime? @db.Timestamp(0)
updatedAt DateTime? @db.Timestamp(0)
deletedAt DateTime? @db.Timestamp(0)
productInfo ProductInfo @relation("Product_ProductInfo", fields: [productInfoId], references: [id], onUpdate: NoAction)
productInfoId Int
product Product @relation("Product_Variant", fields: [productId], references: [id], onUpdate: NoAction)
productId Int
// is_stock_managed Boolean @default(true)
// created_by Int?
// collection_product collection_product[]
@@ -64,32 +63,29 @@ model Product {
// purchase_items purchase_items[]
// sale_items sale_items[]
// @@index([attachment_id], map: "products_attachment_id_foreign")
@@map("Products")
@@map("Product_Variants")
}
model ProductInfo {
model Product {
id Int @id @default(autoincrement())
name String @db.VarChar(255)
barcode String? @unique(map: "products_barcode_unique") @db.VarChar(100)
sku String? @unique(map: "products_sku_unique") @db.VarChar(100)
description String? @db.Text
productType String? @default("simple") @db.VarChar(50)
metaData Json?
// productType String? @default("simple") @db.VarChar(50)
createdAt DateTime @default(now()) @db.Timestamp(0)
updatedAt DateTime @updatedAt @db.Timestamp(0)
deletedAt DateTime? @db.Timestamp(0)
products Product[] @relation("Product_ProductInfo")
variants ProductVariant[] @relation("Product_Variant")
brand ProductBrand? @relation("ProductInfo_Brand", fields: [brandId], references: [id], onUpdate: NoAction)
brand ProductBrand? @relation("Product_Brand", fields: [brandId], references: [id], onUpdate: NoAction)
brandId Int?
category ProductCategory? @relation("ProductInfo_Category", fields: [categoryId], references: [id], onUpdate: NoAction)
category ProductCategory? @relation("Product_Category", fields: [categoryId], references: [id], onUpdate: NoAction)
categoryId Int?
supplier Supplier @relation("ProductInfo_Supplier", fields: [supplierId], references: [id], onUpdate: NoAction)
supplierId Int
@@map("Product_info")
@@map("Products")
}
model ProductBrand {
@@ -101,7 +97,7 @@ model ProductBrand {
updatedAt DateTime @updatedAt @db.Timestamp(0)
deletedAt DateTime? @db.Timestamp(0)
productInfo ProductInfo[] @relation("ProductInfo_Brand")
products Product[] @relation("Product_Brand")
@@map("Product_brands")
}
@@ -115,7 +111,7 @@ model ProductCategory {
updatedAt DateTime @updatedAt @db.Timestamp(0)
deletedAt DateTime? @db.Timestamp(0)
productInfo ProductInfo[] @relation("ProductInfo_Category")
products Product[] @relation("Product_Category")
@@map("Product_categories")
}
@@ -135,8 +131,6 @@ model Supplier {
updatedAt DateTime? @db.Timestamp(0)
deletedAt DateTime? @db.Timestamp(0)
productInfo ProductInfo[] @relation("ProductInfo_Supplier")
@@map("Suppliers")
}
+22
View File
@@ -0,0 +1,22 @@
import { prisma } from '../src/lib/prisma'
async function main() {
console.log('first')
await prisma.role.upsert({
where: { id: 0, name: 'Admin' },
update: {},
create: {
name: 'Admin',
},
})
}
main()
.then(async () => {
await prisma.$disconnect()
})
.catch(async e => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})