mock-data-client.service.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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) => {
  93. console.log('Created Customer:', data);
  94. return data.createCustomer as Customer;
  95. },
  96. err => console.log(err),
  97. );
  98. if (customer) {
  99. const query2 = `mutation($customerId: ID!, $input: CreateAddressInput) {
  100. createCustomerAddress(customerId: $customerId, input: $input) {
  101. id
  102. streetLine1
  103. }
  104. }`;
  105. const variables2 = {
  106. input: {
  107. fullName: `${firstName} ${lastName}`,
  108. streetLine1: faker.address.streetAddress(),
  109. city: faker.address.city(),
  110. province: faker.address.county(),
  111. postalCode: faker.address.zipCode(),
  112. country: faker.address.country(),
  113. } as CreateAddressDto,
  114. customerId: customer.id,
  115. };
  116. await request(this.apiUrl, query2, variables2).then(
  117. data => {
  118. console.log('Created Customer:', data);
  119. return data as Customer;
  120. },
  121. err => console.log(err),
  122. );
  123. }
  124. }
  125. }
  126. async populateProducts(count: number = 5): Promise<any> {
  127. for (let i = 0; i < count; i++) {
  128. const query = `mutation CreateProduct($input: CreateProductInput) {
  129. createProduct(input: $input) { id, name }
  130. }`;
  131. const name = faker.commerce.productName();
  132. const slug = name.toLowerCase().replace(/\s+/g, '-');
  133. const description = faker.lorem.sentence();
  134. const languageCodes = [LanguageCode.EN, LanguageCode.DE, LanguageCode.ES];
  135. const variables = {
  136. input: {
  137. image: faker.image.imageUrl(),
  138. optionGroupCodes: ['size'],
  139. translations: languageCodes.map(code =>
  140. this.makeProductTranslation(code, name, slug, description),
  141. ),
  142. } as CreateProductDto,
  143. };
  144. const product = await request<any>(this.apiUrl, query, variables).then(
  145. data => {
  146. console.log('Created Product:', data);
  147. return data;
  148. },
  149. err => console.log(err),
  150. );
  151. await this.makeProductVariant(product.createProduct.id);
  152. }
  153. }
  154. private makeProductTranslation(
  155. languageCode: LanguageCode,
  156. name: string,
  157. slug: string,
  158. description: string,
  159. ): TranslationInput<Product> {
  160. return {
  161. languageCode,
  162. name: `${languageCode} ${name}`,
  163. slug: `${languageCode} ${slug}`,
  164. description: `${languageCode} ${description}`,
  165. };
  166. }
  167. private async makeProductVariant(productId: ID): Promise<any> {
  168. console.log('generating variants for', productId);
  169. const query = `mutation GenerateVariants($productId: ID!) {
  170. generateVariantsForProduct(productId: $productId) {
  171. id
  172. name
  173. }
  174. }`;
  175. await request(this.apiUrl, query, { productId }).then(
  176. data => {
  177. console.log('Created Variants:', data);
  178. return data;
  179. },
  180. err => console.log(err),
  181. );
  182. }
  183. }