Files
dkala-api/src/seeders/delivery-methods.seeder.ts
T
2026-02-08 15:31:12 +03:30

53 lines
1.9 KiB
TypeScript

import type { EntityManager } from '@mikro-orm/core';
import { Delivery } from '../modules/delivery/entities/delivery.entity';
import type { Shop } from '../../../shops/entities/shop.entity';
import { deliveryMethodsData } from './data/delivery-methods.data';
import { DeliveryFeeTypeEnum } from 'src/modules/delivery/interface/delivery';
export class DeliveryMethodsSeeder {
async run(em: EntityManager, shops: Shop[]): Promise<void> {
for (const shop of shops) {
if (!shop) continue;
for (const methodData of deliveryMethodsData) {
const existing = await em.findOne(Delivery, {
shop: { id: shop.id },
method: methodData.method,
});
if (!existing) {
const delivery = em.create(Delivery, {
shop,
method: methodData.method,
description: methodData.description,
deliveryFee: methodData.deliveryFee,
minOrderPrice: methodData.minOrderPrice,
enabled: methodData.enabled,
order: methodData.order,
deliveryFeeType: DeliveryFeeTypeEnum.FIXED,
perKilometerFee: null,
distanceBasedMinCost: null,
});
em.persist(delivery);
} else {
// Update existing delivery method if needed
if (
existing.description !== methodData.description ||
existing.deliveryFee !== methodData.deliveryFee ||
existing.minOrderPrice !== methodData.minOrderPrice ||
existing.order !== methodData.order
) {
existing.description = methodData.description;
existing.deliveryFee = methodData.deliveryFee;
existing.minOrderPrice = methodData.minOrderPrice;
existing.order = methodData.order;
em.persist(existing);
}
}
}
}
await em.flush();
}
}