소스 검색

feat(dev-server): Enhance past orders generation with retry logic and configurable limits

David Höck 9 달 전
부모
커밋
50a9dc84c5
1개의 변경된 파일92개의 추가작업 그리고 42개의 파일을 삭제
  1. 92 42
      packages/dev-server/scripts/generate-past-orders.ts

+ 92 - 42
packages/dev-server/scripts/generate-past-orders.ts

@@ -20,6 +20,9 @@ generatePastOrders()
     .catch(() => process.exit(1));
     .catch(() => process.exit(1));
 
 
 const DAYS_TO_COVER = 30;
 const DAYS_TO_COVER = 30;
+const MIN_ORDERS_PER_DAY = 5;
+const MAX_ORDERS_PER_DAY = 10;
+const MAX_RETRIES = 3;
 
 
 // This script generates a large number of past Orders over the past <DAYS_TO_COVER> days.
 // This script generates a large number of past Orders over the past <DAYS_TO_COVER> days.
 // It is useful for testing scenarios where there are a large number of Orders in the system.
 // It is useful for testing scenarios where there are a large number of Orders in the system.
@@ -43,54 +46,101 @@ async function generatePastOrders() {
     const { items: customers } = await customerService.findAll(ctxAdmin, { take: 500 }, ['user']);
     const { items: customers } = await customerService.findAll(ctxAdmin, { take: 500 }, ['user']);
 
 
     for (let i = DAYS_TO_COVER; i > 0; i--) {
     for (let i = DAYS_TO_COVER; i > 0; i--) {
-        const numberOfOrders = Math.floor(Math.random() * 10) + 5;
-        Logger.info(
-            `Generating ${numberOfOrders} orders for ${dayjs()
-                .subtract(30 - i, 'day')
-                .format('YYYY-MM-DD')}`,
-        );
-        for (let j = 0; j < numberOfOrders; j++) {
+        const targetDate = dayjs().subtract(DAYS_TO_COVER - i, 'day');
+        const numberOfOrders =
+            Math.floor(Math.random() * (MAX_ORDERS_PER_DAY - MIN_ORDERS_PER_DAY + 1)) + MIN_ORDERS_PER_DAY;
+        Logger.info(`Generating ${numberOfOrders} orders for ${targetDate.format('YYYY-MM-DD')}`);
+
+        let successfulOrders = 0;
+        let retryCount = 0;
+
+        while (successfulOrders < numberOfOrders && retryCount < MAX_RETRIES) {
             const customer = getRandomItem(customers);
             const customer = getRandomItem(customers);
             if (!customer.user) {
             if (!customer.user) {
-                continue;
-            }
-            const order = await orderService.create(ctx, customer.user.id);
-            const result = await orderService.addItemToOrder(
-                ctx,
-                order.id,
-                getRandomItem(variants).id,
-                Math.floor(Math.random() * 3) + 1,
-            );
-            if (isGraphQlErrorResult(result)) {
-                Logger.error(result.message);
-                continue;
-            }
-            const eligibleShippingMethods = await orderService.getEligibleShippingMethods(ctx, order.id);
-            await orderService.setShippingMethod(ctx, order.id, [getRandomItem(eligibleShippingMethods).id]);
-            const transitionResult = await orderService.transitionToState(ctx, order.id, 'ArrangingPayment');
-            if (isGraphQlErrorResult(transitionResult)) {
-                Logger.error(transitionResult.message);
+                retryCount++;
                 continue;
                 continue;
             }
             }
 
 
-            const eligiblePaymentMethods = await orderService.getEligiblePaymentMethods(ctx, order.id);
-            const paymentResult = await orderService.addPaymentToOrder(ctx, order.id, {
-                method: getRandomItem(eligiblePaymentMethods).code,
-                metadata: {},
-            });
-            if (isGraphQlErrorResult(paymentResult)) {
-                Logger.error(paymentResult.message);
-                continue;
+            try {
+                const order = await orderService.create(ctx, customer.user.id);
+                const result = await orderService.addItemToOrder(
+                    ctx,
+                    order.id,
+                    getRandomItem(variants).id,
+                    Math.floor(Math.random() * 3) + 1,
+                );
+
+                if (isGraphQlErrorResult(result)) {
+                    Logger.error(`Failed to add item to order: ${result.message}`);
+                    retryCount++;
+                    continue;
+                }
+
+                const eligibleShippingMethods = await orderService.getEligibleShippingMethods(ctx, order.id);
+                if (eligibleShippingMethods.length === 0) {
+                    Logger.error('No eligible shipping methods found');
+                    retryCount++;
+                    continue;
+                }
+
+                await orderService.setShippingMethod(ctx, order.id, [
+                    getRandomItem(eligibleShippingMethods).id,
+                ]);
+                const transitionResult = await orderService.transitionToState(
+                    ctx,
+                    order.id,
+                    'ArrangingPayment',
+                );
+
+                if (isGraphQlErrorResult(transitionResult)) {
+                    Logger.error(`Failed to transition order state: ${transitionResult.message}`);
+                    retryCount++;
+                    continue;
+                }
+
+                const eligiblePaymentMethods = await orderService.getEligiblePaymentMethods(ctx, order.id);
+                if (eligiblePaymentMethods.length === 0) {
+                    Logger.error('No eligible payment methods found');
+                    retryCount++;
+                    continue;
+                }
+
+                const paymentResult = await orderService.addPaymentToOrder(ctx, order.id, {
+                    method: getRandomItem(eligiblePaymentMethods).code,
+                    metadata: {},
+                });
+
+                if (isGraphQlErrorResult(paymentResult)) {
+                    Logger.error(`Failed to add payment: ${paymentResult.message}`);
+                    retryCount++;
+                    continue;
+                }
+
+                const randomHourOfDay = Math.floor(Math.random() * 24);
+                const placedAt = targetDate.startOf('day').add(randomHourOfDay, 'hour').toDate();
+
+                await connection.getRepository(ctx, 'Order').update(order.id, {
+                    orderPlacedAt: placedAt,
+                });
+
+                successfulOrders++;
+                retryCount = 0; // Reset retry count on success
+            } catch (error: unknown) {
+                Logger.error(
+                    `Error creating order: ${error instanceof Error ? error.message : String(error)}`,
+                );
+                retryCount++;
             }
             }
-            const randomHourOfDay = Math.floor(Math.random() * 24);
-            const placedAt = dayjs()
-                .subtract(DAYS_TO_COVER - i, 'day')
-                .startOf('day')
-                .add(randomHourOfDay, 'hour')
-                .toDate();
-            await connection.getRepository(ctx, 'Order').update(order.id, {
-                orderPlacedAt: placedAt,
-            });
+        }
+
+        if (successfulOrders < numberOfOrders) {
+            Logger.warn(
+                `Failed to generate all ${numberOfOrders} orders for ${targetDate.format('YYYY-MM-DD')}. Generated ${successfulOrders} orders.`,
+            );
+        } else {
+            Logger.info(
+                `Successfully generated ${successfulOrders} orders for ${targetDate.format('YYYY-MM-DD')}`,
+            );
         }
         }
     }
     }
 }
 }