@@ -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,12 +6,10 @@ 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) {
|
|
||||||
logger.warn("No shipment data found in dump, using fallback data");
|
|
||||||
const fallbackProviders = [
|
const fallbackProviders = [
|
||||||
{
|
{
|
||||||
name: "پست",
|
name: "پست",
|
||||||
@@ -74,33 +71,7 @@ export const SeedShipment = async (logger: Logger) => {
|
|||||||
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");
|
logger.info("Shipment Providers seeded successfully");
|
||||||
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");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Error seeding Shipment providers:", error);
|
logger.error("Error seeding Shipment providers:", error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,11 +67,11 @@ const productVariantSchema = new Schema<IProductVariant>(
|
|||||||
productVariantSchema.pre("save", function (next) {
|
productVariantSchema.pre("save", function (next) {
|
||||||
if (this.price) {
|
if (this.price) {
|
||||||
if (this.price.retailPrice) {
|
if (this.price.retailPrice) {
|
||||||
this.price.retailPrice = Math.round(this.price.retailPrice / 10000) * 10000;
|
this.price.retailPrice = Number(this.price.retailPrice);
|
||||||
const sellingPrice =
|
const sellingPrice =
|
||||||
this.price.discount_percent > 0 ? this.price.retailPrice * ((100 - this.price.discount_percent) / 100) : this.price.retailPrice;
|
this.price.discount_percent > 0 ? this.price.retailPrice * ((100 - this.price.discount_percent) / 100) : this.price.retailPrice;
|
||||||
|
|
||||||
this.price.selling_price = Math.round(sellingPrice / 10000) * 10000;
|
this.price.selling_price = Number(sellingPrice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,15 +84,15 @@ productVariantSchema.pre(["findOneAndUpdate", "updateOne", "updateMany"], functi
|
|||||||
|
|
||||||
// ensure price object exists in the update object
|
// ensure price object exists in the update object
|
||||||
if (update.price && update.price.retailPrice !== undefined) {
|
if (update.price && update.price.retailPrice !== undefined) {
|
||||||
const retailPrice = Math.round(update.price.retailPrice / 10000) * 10000;
|
const retailPrice = Number(update.price.retailPrice);
|
||||||
const discountPercent = Math.round(update.price.discount_percent) || 0;
|
const discountPercent = update.price.discount_percent || 0;
|
||||||
|
|
||||||
// calculate selling price based on retailPrice and discount_percent
|
// calculate selling price based on retailPrice and discount_percent
|
||||||
const sellingPrice = discountPercent > 0 ? retailPrice * ((100 - discountPercent) / 100) : retailPrice;
|
const sellingPrice = discountPercent > 0 ? retailPrice * ((100 - discountPercent) / 100) : retailPrice;
|
||||||
|
|
||||||
// set the calculated selling price in the update object
|
// set the calculated selling price in the update object
|
||||||
update.price.retailPrice = retailPrice;
|
update.price.retailPrice = retailPrice;
|
||||||
update.price.selling_price = Math.round(sellingPrice / 10000) * 10000;
|
update.price.selling_price = Number(sellingPrice);
|
||||||
update.price.discount_percent = discountPercent;
|
update.price.discount_percent = discountPercent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user