generate-past-orders.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import {
  2. bootstrapWorker,
  3. CustomerService,
  4. isGraphQlErrorResult,
  5. Logger,
  6. OrderService,
  7. ProductVariantService,
  8. RequestContextService,
  9. ShippingMethodService,
  10. TransactionalConnection,
  11. } from '@vendure/core';
  12. import dayjs from 'dayjs';
  13. import { devConfig } from '../dev-config';
  14. const loggerCtx = 'DataSync script';
  15. generatePastOrders()
  16. .then(() => process.exit(0))
  17. .catch(() => process.exit(1));
  18. async function generatePastOrders() {
  19. const { app } = await bootstrapWorker(devConfig);
  20. const requestContextService = app.get(RequestContextService);
  21. const orderService = app.get(OrderService);
  22. const customerService = app.get(CustomerService);
  23. const productVariantService = app.get(ProductVariantService);
  24. const shippingMethodService = app.get(ShippingMethodService);
  25. const connection = app.get(TransactionalConnection);
  26. const ctx = await requestContextService.create({
  27. apiType: 'shop',
  28. });
  29. const ctxAdmin = await requestContextService.create({
  30. apiType: 'admin',
  31. });
  32. const { items: variants } = await productVariantService.findAll(ctxAdmin, { take: 500 });
  33. const { items: customers } = await customerService.findAll(ctxAdmin, { take: 500 }, ['user']);
  34. const DAYS_TO_COVER = 30;
  35. for (let i = DAYS_TO_COVER; i > 0; i--) {
  36. const numberOfOrders = Math.floor(Math.random() * 10) + 5;
  37. Logger.info(
  38. `Generating ${numberOfOrders} orders for ${dayjs().subtract(i, 'day').format('YYYY-MM-DD')}`,
  39. );
  40. for (let j = 0; j < numberOfOrders; j++) {
  41. const customer = getRandomItem(customers);
  42. if (!customer.user) {
  43. continue;
  44. }
  45. const order = await orderService.create(ctx, customer.user.id);
  46. const result = await orderService.addItemToOrder(
  47. ctx,
  48. order.id,
  49. getRandomItem(variants).id,
  50. Math.floor(Math.random() * 3) + 1,
  51. );
  52. if (isGraphQlErrorResult(result)) {
  53. Logger.error(result.message);
  54. continue;
  55. }
  56. const eligibleShippingMethods = await orderService.getEligibleShippingMethods(ctx, order.id);
  57. await orderService.setShippingMethod(ctx, order.id, [getRandomItem(eligibleShippingMethods).id]);
  58. const transitionResult = await orderService.transitionToState(ctx, order.id, 'ArrangingPayment');
  59. if (isGraphQlErrorResult(transitionResult)) {
  60. Logger.error(transitionResult.message);
  61. continue;
  62. }
  63. const eligiblePaymentMethods = await orderService.getEligiblePaymentMethods(ctx, order.id);
  64. const paymentResult = await orderService.addPaymentToOrder(ctx, order.id, {
  65. method: getRandomItem(eligiblePaymentMethods).code,
  66. metadata: {},
  67. });
  68. if (isGraphQlErrorResult(paymentResult)) {
  69. Logger.error(paymentResult.message);
  70. continue;
  71. }
  72. const randomHourOfDay = Math.floor(Math.random() * 24);
  73. const placedAt = dayjs().subtract(i, 'day').startOf('day').add(randomHourOfDay, 'hour').toDate();
  74. await connection.getRepository(ctx, 'Order').update(order.id, {
  75. orderPlacedAt: placedAt,
  76. });
  77. }
  78. }
  79. }
  80. // get random item from array
  81. function getRandomItem<T>(array: T[]): T {
  82. return array[Math.floor(Math.random() * array.length)];
  83. }