mock-data-client.service.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import * as faker from 'faker/locale/en_GB';
  2. import { request } from 'graphql-request';
  3. import { ID } from '../../shared/shared-types';
  4. import { PasswordService } from '../src/auth/password.service';
  5. import { VendureConfig } from '../src/config/vendure-config';
  6. import { CreateAddressDto } from '../src/entity/address/address.dto';
  7. import { CreateAdministratorDto } from '../src/entity/administrator/administrator.dto';
  8. import { CreateCustomerDto } from '../src/entity/customer/customer.dto';
  9. import { Customer } from '../src/entity/customer/customer.entity';
  10. import { CreateProductOptionGroupDto } from '../src/entity/product-option-group/product-option-group.dto';
  11. import { CreateProductVariantDto } from '../src/entity/product-variant/create-product-variant.dto';
  12. import { CreateProductDto } from '../src/entity/product/product.dto';
  13. import { Product } from '../src/entity/product/product.entity';
  14. import { LanguageCode } from '../src/locale/language-code';
  15. import { TranslationInput } from '../src/locale/locale-types';
  16. // tslint:disable:no-console
  17. /**
  18. * A service for creating mock data via the GraphQL API.
  19. */
  20. export class MockDataClientService {
  21. apiUrl: string;
  22. constructor(config: VendureConfig) {
  23. this.apiUrl = `http://localhost:${config.port}/${config.apiPath}`;
  24. }
  25. async populateOptions(): Promise<any> {
  26. const query = `mutation($input: CreateProductOptionGroupInput) {
  27. createProductOptionGroup(input: $input) { id }
  28. }`;
  29. const variables = {
  30. input: {
  31. code: 'size',
  32. translations: [{ languageCode: 'en', name: 'Size' }, { languageCode: 'de', name: 'Größe' }],
  33. options: [
  34. {
  35. code: 'small',
  36. translations: [
  37. { languageCode: 'en', name: 'Small' },
  38. { languageCode: 'de', name: 'Klein' },
  39. ],
  40. },
  41. {
  42. code: 'large',
  43. translations: [
  44. { languageCode: 'en', name: 'Large' },
  45. { languageCode: 'de', name: 'Groß' },
  46. ],
  47. },
  48. ],
  49. } as CreateProductOptionGroupDto,
  50. };
  51. await request(this.apiUrl, query, variables).then(
  52. data => console.log('Created Administrator:', data),
  53. err => console.log(err),
  54. );
  55. console.log('created size options');
  56. }
  57. async populateAdmins(): Promise<any> {
  58. const query = `mutation($input: CreateAdministratorInput!) {
  59. createAdministrator(input: $input) { id, emailAddress }
  60. }`;
  61. const variables = {
  62. input: {
  63. firstName: 'Super',
  64. lastName: 'Admin',
  65. emailAddress: 'admin@test.com',
  66. password: 'test',
  67. } as CreateAdministratorDto,
  68. };
  69. await request(this.apiUrl, query, variables).then(
  70. data => console.log('Created Administrator:', data),
  71. err => console.log(err),
  72. );
  73. }
  74. async populateCustomers(count: number = 5): Promise<any> {
  75. const passwordService = new PasswordService();
  76. for (let i = 0; i < count; i++) {
  77. const firstName = faker.name.firstName();
  78. const lastName = faker.name.lastName();
  79. const query1 = `mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  80. createCustomer(input: $input, password: $password) { id, emailAddress }
  81. }`;
  82. const variables1 = {
  83. input: {
  84. firstName,
  85. lastName,
  86. emailAddress: faker.internet.email(firstName, lastName),
  87. phoneNumber: faker.phone.phoneNumber(),
  88. } as CreateCustomerDto,
  89. password: 'test',
  90. };
  91. const customer: Customer | void = await request(this.apiUrl, query1, variables1).then(
  92. (data: any) => data.createCustomer as Customer,
  93. err => console.log(err),
  94. );
  95. if (customer) {
  96. const query2 = `mutation($customerId: ID!, $input: CreateAddressInput) {
  97. createCustomerAddress(customerId: $customerId, input: $input) {
  98. id
  99. streetLine1
  100. }
  101. }`;
  102. const variables2 = {
  103. input: {
  104. fullName: `${firstName} ${lastName}`,
  105. streetLine1: faker.address.streetAddress(),
  106. city: faker.address.city(),
  107. province: faker.address.county(),
  108. postalCode: faker.address.zipCode(),
  109. country: faker.address.country(),
  110. } as CreateAddressDto,
  111. customerId: customer.id,
  112. };
  113. await request(this.apiUrl, query2, variables2).then(
  114. data => {
  115. console.log(`Created Customer ${i + 1}:`, data);
  116. return data as Customer;
  117. },
  118. err => console.log(err),
  119. );
  120. }
  121. }
  122. }
  123. async populateProducts(count: number = 5): Promise<any> {
  124. for (let i = 0; i < count; i++) {
  125. const query = `mutation CreateProduct($input: CreateProductInput) {
  126. createProduct(input: $input) { id, name }
  127. }`;
  128. const name = faker.commerce.productName();
  129. const slug = name.toLowerCase().replace(/\s+/g, '-');
  130. const description = faker.lorem.sentence();
  131. const languageCodes = [LanguageCode.EN, LanguageCode.DE, LanguageCode.ES];
  132. const variables = {
  133. input: {
  134. image: faker.image.imageUrl(),
  135. optionGroupCodes: ['size'],
  136. translations: languageCodes.map(code =>
  137. this.makeProductTranslation(code, name, slug, description),
  138. ),
  139. } as CreateProductDto,
  140. };
  141. const product = await request<any>(this.apiUrl, query, variables).then(
  142. data => {
  143. console.log(`Created Product ${i + 1}:`, data);
  144. return data;
  145. },
  146. err => console.log(err),
  147. );
  148. await this.makeProductVariant(product.createProduct.id);
  149. }
  150. }
  151. private makeProductTranslation(
  152. languageCode: LanguageCode,
  153. name: string,
  154. slug: string,
  155. description: string,
  156. ): TranslationInput<Product> {
  157. return {
  158. languageCode,
  159. name: `${languageCode} ${name}`,
  160. slug: `${languageCode} ${slug}`,
  161. description: `${languageCode} ${description}`,
  162. };
  163. }
  164. private async makeProductVariant(productId: ID): Promise<any> {
  165. const query = `mutation GenerateVariants($productId: ID!) {
  166. generateVariantsForProduct(productId: $productId) {
  167. id
  168. name
  169. }
  170. }`;
  171. await request(this.apiUrl, query, { productId }).then(data => data, err => console.log(err));
  172. }
  173. }