seed
This commit is contained in:
@@ -10,6 +10,10 @@ export class CreateRestaurantDto {
|
|||||||
@IsString()
|
@IsString()
|
||||||
slug!: string;
|
slug!: string;
|
||||||
|
|
||||||
|
@ApiProperty({ example: 'zhivan.example.com', description: 'دامنه رستوران' })
|
||||||
|
@IsString()
|
||||||
|
domain!: string;
|
||||||
|
|
||||||
@ApiPropertyOptional({ example: 'https://example.com/logo.png', description: 'آدرس تصویر لوگو' })
|
@ApiPropertyOptional({ example: 'https://example.com/logo.png', description: 'آدرس تصویر لوگو' })
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsUrl()
|
@IsUrl()
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { Restaurant } from '../modules/restaurants/entities/restaurant.entity';
|
|||||||
import { User } from '../modules/users/entities/user.entity';
|
import { User } from '../modules/users/entities/user.entity';
|
||||||
import { Permission, PermissionTitles } from '../common/enums/permission.enum';
|
import { Permission, PermissionTitles } from '../common/enums/permission.enum';
|
||||||
import { PaymentMethod } from '../modules/payments/entities/payment-method.entity';
|
import { PaymentMethod } from '../modules/payments/entities/payment-method.entity';
|
||||||
import { RestaurantPaymentMethod } from '../modules/payments/entities/restaurant-payment-method.entity';
|
import { PaymentMethodEnum, PaymentGatewayEnum } from '../modules/payments/interface/payment';
|
||||||
import { Delivery } from '../modules/delivery/entities/delivery.entity';
|
import { Delivery } from '../modules/delivery/entities/delivery.entity';
|
||||||
import { DeliveryName } from '../modules/delivery/interface/delivery';
|
import { DeliveryName } from '../modules/delivery/interface/delivery';
|
||||||
import { Schedule } from '../modules/restaurants/entities/schedule.entity';
|
import { Schedule } from '../modules/restaurants/entities/schedule.entity';
|
||||||
@@ -94,61 +94,7 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
await em.findOne(Role, { name: 'accountant' });
|
await em.findOne(Role, { name: 'accountant' });
|
||||||
const superAdmin = await em.findOne(Role, { name: 'superAdmin' });
|
const superAdmin = await em.findOne(Role, { name: 'superAdmin' });
|
||||||
|
|
||||||
// 3. Create Payment Methods
|
// 3. Payment Methods will be created per restaurant (see section 5.5)
|
||||||
const paymentMethodsData = [
|
|
||||||
{
|
|
||||||
name: 'پرداخت در محل',
|
|
||||||
keyName: 'payment-on-delivery',
|
|
||||||
description: 'پرداخت در محل تحویل',
|
|
||||||
isActive: true,
|
|
||||||
order: 1,
|
|
||||||
isOnline: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'نقدی',
|
|
||||||
keyName: 'cash',
|
|
||||||
description: 'پرداخت نقدی',
|
|
||||||
isActive: true,
|
|
||||||
order: 2,
|
|
||||||
isOnline: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'زرینپال',
|
|
||||||
keyName: 'zarinpal',
|
|
||||||
description: 'درگاه پرداخت زرینپال',
|
|
||||||
isActive: true,
|
|
||||||
order: 3,
|
|
||||||
isOnline: true,
|
|
||||||
paymentUrl: 'https://www.zarinpal.com/pg/StartPay',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const methodData of paymentMethodsData) {
|
|
||||||
let paymentMethod = await em.findOne(PaymentMethod, { keyName: methodData.keyName });
|
|
||||||
if (!paymentMethod) {
|
|
||||||
paymentMethod = em.create(PaymentMethod, methodData);
|
|
||||||
em.persist(paymentMethod);
|
|
||||||
} else {
|
|
||||||
// Update existing payment method if needed
|
|
||||||
if (
|
|
||||||
paymentMethod.name !== methodData.name ||
|
|
||||||
paymentMethod.description !== methodData.description ||
|
|
||||||
paymentMethod.isOnline !== methodData.isOnline ||
|
|
||||||
paymentMethod.order !== methodData.order
|
|
||||||
) {
|
|
||||||
paymentMethod.name = methodData.name;
|
|
||||||
paymentMethod.description = methodData.description;
|
|
||||||
paymentMethod.isOnline = methodData.isOnline;
|
|
||||||
paymentMethod.order = methodData.order;
|
|
||||||
if (methodData.paymentUrl) {
|
|
||||||
paymentMethod.paymentUrl = methodData.paymentUrl;
|
|
||||||
}
|
|
||||||
em.persist(paymentMethod);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await em.flush();
|
|
||||||
|
|
||||||
// 4. Delivery methods will be created per restaurant (see section 5.5.5)
|
// 4. Delivery methods will be created per restaurant (see section 5.5.5)
|
||||||
|
|
||||||
@@ -158,6 +104,7 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
const restaurant = em.create(Restaurant, {
|
const restaurant = em.create(Restaurant, {
|
||||||
name: 'ژیوان',
|
name: 'ژیوان',
|
||||||
slug: 'zhivan',
|
slug: 'zhivan',
|
||||||
|
domain: 'zhivan.example.com',
|
||||||
isActive: true,
|
isActive: true,
|
||||||
phone: '09123456789',
|
phone: '09123456789',
|
||||||
serviceArea: {
|
serviceArea: {
|
||||||
@@ -181,6 +128,7 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
const restaurant = em.create(Restaurant, {
|
const restaurant = em.create(Restaurant, {
|
||||||
name: 'بوته',
|
name: 'بوته',
|
||||||
slug: 'boote',
|
slug: 'boote',
|
||||||
|
domain: 'boote.example.com',
|
||||||
isActive: true,
|
isActive: true,
|
||||||
phone: '09123456790',
|
phone: '09123456790',
|
||||||
serviceArea: {
|
serviceArea: {
|
||||||
@@ -205,26 +153,78 @@ export class DatabaseSeeder extends Seeder {
|
|||||||
const zhivan = await em.findOne(Restaurant, { slug: 'zhivan' });
|
const zhivan = await em.findOne(Restaurant, { slug: 'zhivan' });
|
||||||
const boote = await em.findOne(Restaurant, { slug: 'boote' });
|
const boote = await em.findOne(Restaurant, { slug: 'boote' });
|
||||||
|
|
||||||
// 5.5. Create Restaurant Payment Methods
|
// 5.5. Create Payment Methods for each Restaurant
|
||||||
const allRestaurants = await em.find(Restaurant, {});
|
const allRestaurants = await em.find(Restaurant, {});
|
||||||
const allPaymentMethods = await em.find(PaymentMethod, { isActive: true });
|
|
||||||
|
const paymentMethodsData = [
|
||||||
|
{
|
||||||
|
title: 'پرداخت در محل',
|
||||||
|
method: PaymentMethodEnum.CardOnDelivery,
|
||||||
|
description: 'پرداخت در محل تحویل',
|
||||||
|
enabled: true,
|
||||||
|
order: 1,
|
||||||
|
gateway: null,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'نقدی',
|
||||||
|
method: PaymentMethodEnum.Cash,
|
||||||
|
description: 'پرداخت نقدی',
|
||||||
|
enabled: true,
|
||||||
|
order: 2,
|
||||||
|
gateway: null,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'زرینپال',
|
||||||
|
method: PaymentMethodEnum.Online,
|
||||||
|
description: 'درگاه پرداخت زرینپال',
|
||||||
|
enabled: true,
|
||||||
|
order: 3,
|
||||||
|
gateway: PaymentGatewayEnum.ZarinPal,
|
||||||
|
merchantId: 'test-merchant-id',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
for (const restaurant of allRestaurants) {
|
for (const restaurant of allRestaurants) {
|
||||||
if (!restaurant) continue;
|
if (!restaurant) continue;
|
||||||
|
|
||||||
for (const paymentMethod of allPaymentMethods) {
|
for (const methodData of paymentMethodsData) {
|
||||||
const existing = await em.findOne(RestaurantPaymentMethod, {
|
const existing = await em.findOne(PaymentMethod, {
|
||||||
restaurant: { id: restaurant.id },
|
restaurant: { id: restaurant.id },
|
||||||
paymentMethod: { id: paymentMethod.id },
|
method: methodData.method,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
const restaurantPaymentMethod = em.create(RestaurantPaymentMethod, {
|
const paymentMethod = em.create(PaymentMethod, {
|
||||||
restaurant,
|
restaurant,
|
||||||
paymentMethod,
|
title: methodData.title,
|
||||||
isActive: true,
|
method: methodData.method,
|
||||||
|
description: methodData.description,
|
||||||
|
enabled: methodData.enabled,
|
||||||
|
order: methodData.order,
|
||||||
|
gateway: methodData.gateway,
|
||||||
|
merchantId: methodData.merchantId,
|
||||||
});
|
});
|
||||||
em.persist(restaurantPaymentMethod);
|
em.persist(paymentMethod);
|
||||||
|
} else {
|
||||||
|
// Update existing payment method if needed
|
||||||
|
if (
|
||||||
|
existing.title !== methodData.title ||
|
||||||
|
existing.description !== methodData.description ||
|
||||||
|
existing.enabled !== methodData.enabled ||
|
||||||
|
existing.order !== methodData.order
|
||||||
|
) {
|
||||||
|
existing.title = methodData.title;
|
||||||
|
existing.description = methodData.description;
|
||||||
|
existing.enabled = methodData.enabled;
|
||||||
|
existing.order = methodData.order;
|
||||||
|
if (methodData.gateway !== undefined) {
|
||||||
|
existing.gateway = methodData.gateway;
|
||||||
|
}
|
||||||
|
if (methodData.merchantId !== undefined) {
|
||||||
|
existing.merchantId = methodData.merchantId;
|
||||||
|
}
|
||||||
|
em.persist(existing);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user