mock-data-client.service.ts 9.1 KB

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