populate-customers.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. import { INestApplicationContext } from '@nestjs/common';
  2. import { CustomerService, isGraphQlErrorResult, RequestContext } from '@vendure/core';
  3. import { getSuperadminContext } from '../utils/get-superadmin-context';
  4. import { MockDataService } from './mock-data.service';
  5. /**
  6. * Creates customers with addresses by making API calls to the Admin API.
  7. */
  8. export async function populateCustomers(
  9. app: INestApplicationContext,
  10. count: number,
  11. loggingFn: (message: string) => void,
  12. ) {
  13. const customerService = app.get(CustomerService);
  14. const customerData = MockDataService.getCustomers(count);
  15. const ctx = await getSuperadminContext(app);
  16. const password = 'test';
  17. for (const { customer, address } of customerData) {
  18. try {
  19. const createdCustomer = await customerService.create(ctx, customer, password);
  20. if (isGraphQlErrorResult(createdCustomer)) {
  21. loggingFn(`Failed to create customer: ${createdCustomer.message}`);
  22. continue;
  23. }
  24. await customerService.createAddress(ctx, createdCustomer.id, address);
  25. } catch (e: any) {
  26. loggingFn(`Failed to create customer: ${JSON.stringify(e.message)}`);
  27. }
  28. }
  29. }