shiment id

This commit is contained in:
morteza-mortezai
2025-12-02 16:49:10 +03:30
parent 1345f81b3a
commit b8065bf53e
2 changed files with 64 additions and 93 deletions
+63 -92
View File
@@ -1,4 +1,3 @@
import { cleanMongoData, extractDumpData, getCollectionData } from "./utils/extractDumpData";
import { DeliveryType } from "../../common/enums/shipment.enum"; import { DeliveryType } from "../../common/enums/shipment.enum";
import { Logger } from "../../core/logging/logger"; import { Logger } from "../../core/logging/logger";
import { ShipmentModel } from "../../modules/shipment/models/shipment.model"; import { ShipmentModel } from "../../modules/shipment/models/shipment.model";
@@ -7,100 +6,72 @@ export const SeedShipment = async (logger: Logger) => {
try { try {
logger.info("ShipmentMethod seeding started"); logger.info("ShipmentMethod seeding started");
// Extract data from dump // Remove all existing shipment documents
const dumpData = extractDumpData(); await ShipmentModel.deleteMany({});
const shipmentsData = getCollectionData(dumpData, "shipments"); logger.info("Existing shipment collection cleared");
if (shipmentsData.length === 0) { const fallbackProviders = [
logger.warn("No shipment data found in dump, using fallback data"); {
const fallbackProviders = [ name: "پست",
{ description: "ارسال توسط پست",
name: "پست", costs: [
description: "ارسال توسط پست", { weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 },
costs: [ { weightRange: { min: 1, max: 5 }, cost_first: 80000, cost_rest: 2000 },
{ weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 }, { weightRange: { min: 5, max: 10 }, cost_first: 100000, cost_rest: 2000 },
{ weightRange: { min: 1, max: 5 }, cost_first: 80000, cost_rest: 2000 }, ],
{ weightRange: { min: 5, max: 10 }, cost_first: 100000, cost_rest: 2000 }, deliveryTime: 5,
], deliveryType: DeliveryType.Standard,
deliveryTime: 5, },
deliveryType: DeliveryType.Standard, {
}, name: "چاپار",
{ description: "ارائه بهترین راه حل های جهانی لجستیک چاپار",
name: "چاپار", costs: [
description: "ارائه بهترین راه حل های جهانی لجستیک چاپار", { weightRange: { min: 0, max: 2 }, cost_first: 40000, cost_rest: 2000 },
costs: [ { weightRange: { min: 2, max: 6 }, cost_first: 80000, cost_rest: 2000 },
{ weightRange: { min: 0, max: 2 }, cost_first: 40000, cost_rest: 2000 }, { weightRange: { min: 6, max: 12 }, cost_first: 100000, cost_rest: 2000 },
{ weightRange: { min: 2, max: 6 }, cost_first: 80000, cost_rest: 2000 }, ],
{ weightRange: { min: 6, max: 12 }, cost_first: 100000, cost_rest: 2000 }, deliveryTime: 6,
], deliveryType: DeliveryType.Standard,
deliveryTime: 6, },
deliveryType: DeliveryType.Standard, {
}, name: "پیک",
{ description: "ارسال توسط پیک",
name: "پیک", costs: [
description: "ارسال توسط پیک", { weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 },
costs: [ { weightRange: { min: 1, max: 3 }, cost_first: 80000, cost_rest: 2000 },
{ weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 }, { weightRange: { min: 3, max: 6 }, cost_first: 100000, cost_rest: 2000 },
{ weightRange: { min: 1, max: 3 }, cost_first: 80000, cost_rest: 2000 }, ],
{ weightRange: { min: 3, max: 6 }, cost_first: 100000, cost_rest: 2000 }, deliveryTime: 0,
], deliveryType: DeliveryType.SameDay,
deliveryTime: 0, },
deliveryType: DeliveryType.SameDay, {
}, name: "تیپاکس",
{ description: "ارسال بسته های پستی در سریعترین زمان، از درب منزل با امکان پرداخت هزینه بعد از ارسال و پوشش تمام نقاط ایران",
name: "تیپاکس", costs: [
description: "ارسال بسته های پستی در سریعترین زمان، از درب منزل با امکان پرداخت هزینه بعد از ارسال و پوشش تمام نقاط ایران", { weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 },
costs: [ { weightRange: { min: 1, max: 5 }, cost_first: 80000, cost_rest: 2000 },
{ weightRange: { min: 0, max: 1 }, cost_first: 40000, cost_rest: 2000 }, { weightRange: { min: 5, max: 10 }, cost_first: 100000, cost_rest: 2000 },
{ weightRange: { min: 1, max: 5 }, cost_first: 80000, cost_rest: 2000 }, ],
{ weightRange: { min: 5, max: 10 }, cost_first: 100000, cost_rest: 2000 }, deliveryTime: 2,
], deliveryType: DeliveryType.Express,
deliveryTime: 2, },
deliveryType: DeliveryType.Express, {
}, name: "باربری",
{ description: "ارسال توسط باربری",
name: "باربری", costs: [
description: "ارسال توسط باربری", { weightRange: { min: 0, max: 10 }, cost_first: 40000, cost_rest: 2000 },
costs: [ { weightRange: { min: 10, max: 20 }, cost_first: 80000, cost_rest: 2000 },
{ weightRange: { min: 0, max: 10 }, cost_first: 40000, cost_rest: 2000 }, { weightRange: { min: 20, max: 50 }, cost_first: 100000, cost_rest: 2000 },
{ weightRange: { min: 10, max: 20 }, cost_first: 80000, cost_rest: 2000 }, ],
{ weightRange: { min: 20, max: 50 }, cost_first: 100000, cost_rest: 2000 }, deliveryTime: 7,
], deliveryType: DeliveryType.Standard,
deliveryTime: 7, },
deliveryType: DeliveryType.Standard, ];
},
];
for (const provider of fallbackProviders) { for (const provider of fallbackProviders) {
await ShipmentModel.create(provider); await ShipmentModel.create(provider);
}
logger.info("Shipment Providers seeded successfully with fallback data");
return;
}
// Clean and prepare shipment data (only non-deleted)
const cleanedShipments = shipmentsData
.map((shipment) => {
const cleaned = cleanMongoData(shipment);
if (cleaned.deleted) {
return null;
}
return {
name: cleaned.name,
description: cleaned.description,
costs: cleaned.costs,
deliveryTime: cleaned.deliveryTime,
deliveryType: cleaned.deliveryType as DeliveryType,
};
})
.filter((s) => s !== null);
if (cleanedShipments.length > 0) {
await ShipmentModel.insertMany(cleanedShipments);
logger.info(`Shipment Providers seeded successfully (${cleanedShipments.length} providers)`);
} else {
logger.warn("No valid shipments found after filtering");
} }
logger.info("Shipment Providers seeded successfully");
} catch (error) { } catch (error) {
logger.error("Error seeding Shipment providers:", error); logger.error("Error seeding Shipment providers:", error);
} }
+1 -1
View File
@@ -12,7 +12,7 @@ class ShipmentRepository extends BaseRepository<IShipmentProviders> {
const skip = (page - 1) * limit; const skip = (page - 1) * limit;
const count = await this.model.countDocuments({ deleted: false }); const count = await this.model.countDocuments({ deleted: false });
const shipper = await this.model.find({ deleted: false }).skip(skip).limit(limit); const shipper = await this.model.find({ deleted: false }).skip(skip).limit(limit).lean();
return { shipper, count }; return { shipper, count };
} }