mock-data.service.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {
  2. Channel,
  3. CreateAddressInput,
  4. CreateChannel,
  5. CreateCustomerInput,
  6. CreateShippingMethod,
  7. CurrencyCode,
  8. LanguageCode,
  9. ProductVariant,
  10. } from '@vendure/common/lib/generated-types';
  11. import faker from 'faker/locale/en_GB';
  12. import gql from 'graphql-tag';
  13. import { CREATE_CHANNEL } from '../../../admin-ui/src/app/data/definitions/settings-definitions';
  14. import { CREATE_SHIPPING_METHOD } from '../../../admin-ui/src/app/data/definitions/shipping-definitions';
  15. import { defaultShippingCalculator } from '../src/config/shipping-method/default-shipping-calculator';
  16. import { defaultShippingEligibilityChecker } from '../src/config/shipping-method/default-shipping-eligibility-checker';
  17. import { Customer } from '../src/entity/customer/customer.entity';
  18. import { SimpleGraphQLClient } from './simple-graphql-client';
  19. // tslint:disable:no-console
  20. /**
  21. * A service for creating mock data via the GraphQL API.
  22. */
  23. export class MockDataService {
  24. apiUrl: string;
  25. constructor(private client: SimpleGraphQLClient, private logging = true) {
  26. // make the generated results deterministic
  27. faker.seed(1);
  28. }
  29. async populateCustomers(count: number = 5): Promise<any> {
  30. for (let i = 0; i < count; i++) {
  31. const firstName = faker.name.firstName();
  32. const lastName = faker.name.lastName();
  33. const query1 = gql`
  34. mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  35. createCustomer(input: $input, password: $password) {
  36. id
  37. emailAddress
  38. }
  39. }
  40. `;
  41. const variables1 = {
  42. input: {
  43. firstName,
  44. lastName,
  45. emailAddress: faker.internet.email(firstName, lastName),
  46. phoneNumber: faker.phone.phoneNumber(),
  47. } as CreateCustomerInput,
  48. password: 'test',
  49. };
  50. const customer: { id: string; emailAddress: string } | void = await this.client
  51. .query(query1, variables1)
  52. .then((data: any) => data.createCustomer, err => this.log(err));
  53. if (customer) {
  54. const query2 = gql`
  55. mutation($customerId: ID!, $input: CreateAddressInput!) {
  56. createCustomerAddress(customerId: $customerId, input: $input) {
  57. id
  58. streetLine1
  59. }
  60. }
  61. `;
  62. const variables2 = {
  63. input: {
  64. fullName: `${firstName} ${lastName}`,
  65. streetLine1: faker.address.streetAddress(),
  66. city: faker.address.city(),
  67. province: faker.address.county(),
  68. postalCode: faker.address.zipCode(),
  69. countryCode: 'GB',
  70. } as CreateAddressInput,
  71. customerId: customer.id,
  72. };
  73. await this.client.query(query2, variables2).catch(err => this.log(err));
  74. }
  75. }
  76. this.log(`Created ${count} Customers`);
  77. }
  78. private log(...args: any[]) {
  79. if (this.logging) {
  80. console.log(...args);
  81. }
  82. }
  83. }