create license management

This commit is contained in:
2026-04-06 13:31:40 +03:30
parent c02e7afca1
commit 1160d81cdb
42 changed files with 1233 additions and 522 deletions
@@ -0,0 +1,2 @@
-- DropIndex
DROP INDEX `accounts_password_key` ON `accounts`;
@@ -0,0 +1,17 @@
-- DropForeignKey
ALTER TABLE `licenses` DROP FOREIGN KEY `licenses_partner_id_fkey`;
-- DropIndex
DROP INDEX `licenses_partner_id_fkey` ON `licenses`;
-- AlterTable
ALTER TABLE `consumers` ADD COLUMN `license_id` VARCHAR(191) NULL;
-- AlterTable
ALTER TABLE `licenses` MODIFY `partner_id` VARCHAR(191) NULL;
-- AddForeignKey
ALTER TABLE `consumers` ADD CONSTRAINT `consumers_license_id_fkey` FOREIGN KEY (`license_id`) REFERENCES `licenses`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE `licenses` ADD CONSTRAINT `licenses_partner_id_fkey` FOREIGN KEY (`partner_id`) REFERENCES `partners`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
@@ -0,0 +1,14 @@
/*
Warnings:
- You are about to drop the column `pos_id` on the `licenses` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE `licenses` DROP FOREIGN KEY `licenses_pos_id_fkey`;
-- DropIndex
DROP INDEX `licenses_pos_id_fkey` ON `licenses`;
-- AlterTable
ALTER TABLE `licenses` DROP COLUMN `pos_id`;
+1 -1
View File
@@ -1,7 +1,7 @@
model Account {
id String @id @default(uuid())
username String @unique()
password String @unique()
password String
status AccountStatus
type AccountType
+2 -1
View File
@@ -4,10 +4,12 @@ model Consumer {
first_name String
last_name String
status ConsumerStatus @default(ACTIVE)
license_id String?
created_at DateTime @default(now()) @db.Timestamp(0)
updated_at DateTime @updatedAt() @db.Timestamp(0)
license License? @relation(fields: [license_id], references: [id])
accounts ConsumerAccount[]
businessActivities BusinessActivity[]
@@ -93,7 +95,6 @@ model Pos {
provider_id String?
provider Provider? @relation(fields: [provider_id], references: [id])
licenses License[]
permissionPos PermissionPos[]
salesInvoices SalesInvoice[]
+5 -4
View File
@@ -7,11 +7,12 @@ model License {
created_at DateTime @default(now()) @db.Timestamp(0)
updated_at DateTime @updatedAt() @db.Timestamp(0)
pos_id String
partner_id String
partner_id String?
pos Pos @relation(fields: [pos_id], references: [id])
partner Partner @relation(fields: [partner_id], references: [id])
// pos Pos @relation(fields: [pos_id], references: [id])
partner Partner? @relation(fields: [partner_id], references: [id])
consumers Consumer[]
@@map("licenses")
}
+14 -3
View File
@@ -1,7 +1,18 @@
// generator client {
// provider = "prisma-client-js"
// // output = "../../src/generated/prisma"
// // moduleFormat = "cjs"
// previewFeatures = ["views"]
// }
// datasource db {
// provider = "mysql"
// }
generator client {
provider = "prisma-client-js"
// output = "../../src/generated/prisma"
// moduleFormat = "cjs"
provider = "prisma-client"
output = "../../src/generated/prisma"
moduleFormat = "cjs"
previewFeatures = ["views"]
}
+276 -4
View File
@@ -1,4 +1,6 @@
import { PasswordUtil } from '@/common/utils/password.util'
import { GoodPricingModel, POSType, UnitType } from '@/generated/prisma/enums'
import { GoodCreateInput, GoodCreateManyInput } from '@/generated/prisma/models'
import { prisma } from '../src/lib/prisma'
async function main() {
@@ -28,8 +30,9 @@ async function main() {
},
})
const guildsCount = prisma.guild.count()
if (!guildsCount) {
// ****************** GUILD Start ****************** //
const guilds = await prisma.guild.findMany()
if (!guilds.length) {
const guilds = await prisma.guild.createMany({
data: [
{
@@ -42,9 +45,17 @@ async function main() {
},
],
})
}
const goldGuildId = guilds[0].id
// ****************** GUILD Good Categories Start ****************** //
const goldGuildId = guilds[0].id
const goldGuildGoodCategories = await prisma.goodCategory.findMany({
where: {
guild_id: goldGuildId,
is_default_guild_good: true,
},
})
if (!goldGuildGoodCategories) {
const categoryFactory = (name: string) => ({
name,
guild_id: goldGuildId,
@@ -62,6 +73,267 @@ async function main() {
],
})
}
// ****************** GUILD Good Start ****************** //
const goodFactory = (
name: string,
categoryId: string,
pricingModel: GoodPricingModel,
unitType: UnitType,
): GoodCreateManyInput => ({
name,
category_id: categoryId,
pricing_model: pricingModel,
sku: '',
unit_type: unitType,
is_default_guild_good: true,
})
const zivarCategory = await prisma.goodCategory.findFirst({
where: {
name: 'زیورآلات',
},
})
if (zivarCategory) {
const zivarGoods = await prisma.good.count({
where: {
category_id: zivarCategory?.id,
},
})
if (!zivarGoods) {
const goodItems: GoodCreateManyInput[] = []
goodItems.push(
...[
goodFactory(
'آویز گردنبند طلا',
zivarCategory.id,
GoodPricingModel.GOLD,
UnitType.GRAM,
),
goodFactory('النگو', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
goodFactory('انگشتر', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
goodFactory('دست‌بند', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
goodFactory('زنجیر', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
goodFactory('سرویس', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
goodFactory('گوشواره', zivarCategory.id, GoodPricingModel.GOLD, UnitType.GRAM),
],
)
await prisma.good.createMany({ data: goodItems })
}
}
const goldCategory = await prisma.goodCategory.findFirst({
where: {
name: 'طلا',
},
})
if (goldCategory) {
const goldGoods = await prisma.good.count({
where: {
category_id: goldCategory?.id,
},
})
if (!goldGoods) {
const goodItems: GoodCreateInput[] = []
goodItems.push(
...[
goodFactory(
'طلای آب شده',
goldCategory.id,
GoodPricingModel.GOLD,
UnitType.GRAM,
),
goodFactory(
'طلای شکسته',
goldCategory.id,
GoodPricingModel.GOLD,
UnitType.GRAM,
),
goodFactory(
'طلای مستعمل',
goldCategory.id,
GoodPricingModel.GOLD,
UnitType.GRAM,
),
],
)
await prisma.good.createMany({ data: goodItems })
}
}
const coinCategory = await prisma.goodCategory.findFirst({
where: {
name: 'سکه',
},
})
if (coinCategory) {
const coinGoods = await prisma.good.count({
where: {
category_id: coinCategory?.id,
},
})
if (!coinGoods) {
const goodItems: GoodCreateInput[] = []
goodItems.push(
...[
goodFactory(
'مسکوکات خارجی',
coinCategory.id,
GoodPricingModel.STANDARD,
UnitType.COUNT,
),
goodFactory(
'مسکوکات داخلی (پارسیان)',
coinCategory.id,
GoodPricingModel.STANDARD,
UnitType.COUNT,
),
goodFactory(
'تمام بهار آزادی (طرح جدید)',
coinCategory.id,
GoodPricingModel.STANDARD,
UnitType.COUNT,
),
goodFactory(
'تمام بهار آزادی (طرح قدیم)',
coinCategory.id,
GoodPricingModel.STANDARD,
UnitType.COUNT,
),
],
)
await prisma.good.createMany({ data: goodItems })
}
}
const shemshCategory = await prisma.goodCategory.findFirst({
where: {
name: 'شمش',
},
})
if (shemshCategory) {
const shemshGoods = await prisma.good.count({
where: {
category_id: shemshCategory?.id,
},
})
if (!shemshGoods) {
await prisma.good.create({
data: goodFactory(
'شمش استاندارد',
shemshCategory.id,
GoodPricingModel.GOLD,
UnitType.GRAM,
),
})
}
}
// ****************** BA Start ****************** //
const ba = await prisma.businessActivity.count()
if (!ba) {
await prisma.businessActivity.create({
data: {
name: 'طلا فروشی',
user: {
create: {
first_name: 'محمد',
last_name: 'زرگر',
mobile_number: '09120258155',
accounts: {
create: {
role: 'OWNER',
account: {
create: {
username: 'zargar',
password: await PasswordUtil.hash('123456'),
status: 'ACTIVE',
type: 'CONSUMER',
},
},
},
},
},
},
guild: {
connect: {
id: guilds[0].id,
},
},
complexes: {
create: {
name: 'فروشگاه طلای مرکزی',
address: 'تهران، خیابان جمهوری',
tax_id: '12312452345765',
pos_list: {
create: {
name: 'لاین ۱',
pos_type: POSType.WEB,
serial: '12312312',
status: 'ACTIVE',
},
},
},
},
},
})
}
// ****************** BA Start ****************** //
// ****************** partner Start ****************** //
const partner = await prisma.partner.count()
if (!partner) {
await prisma.partner.create({
data: {
name: 'تیس',
code: 'TIS',
license_quota: 5,
status: 'ACTIVE',
accounts: {
create: {
role: 'OWNER',
account: {
create: {
username: 'tis',
password: await PasswordUtil.hash('123456'),
status: 'ACTIVE',
type: 'PARTNER',
},
},
},
},
},
})
}
// ****************** provider Start ****************** //
const provider = await prisma.provider.count()
if (!provider) {
await prisma.provider.create({
data: {
name: 'توسن',
code: 'Tosan',
status: 'ACTIVE',
accounts: {
create: {
role: 'OWNER',
account: {
create: {
username: 'tosan',
password: await PasswordUtil.hash('123456'),
status: 'ACTIVE',
type: 'PROVIDER',
},
},
},
},
},
})
}
}
main()