mock-data.service.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import * as faker from 'faker/locale/en_GB';
  2. import * as fs from 'fs-extra';
  3. import gql from 'graphql-tag';
  4. import * as path from 'path';
  5. import {
  6. AddOptionGroupToProduct,
  7. AdjustmentSource,
  8. AdjustmentType,
  9. Asset,
  10. CreateAddressInput,
  11. CreateAdjustmentSource,
  12. CreateCustomerInput,
  13. CreateFacet,
  14. CreateFacetValueWithFacetInput,
  15. CreateProduct,
  16. CreateProductOptionGroup,
  17. GenerateProductVariants,
  18. LanguageCode,
  19. ProductTranslationInput,
  20. ProductVariant,
  21. UpdateProductVariants,
  22. } from 'shared/generated-types';
  23. import { CREATE_ADJUSTMENT_SOURCE } from '../../admin-ui/src/app/data/definitions/adjustment-source-definitions';
  24. import { CREATE_FACET } from '../../admin-ui/src/app/data/definitions/facet-definitions';
  25. import {
  26. ADD_OPTION_GROUP_TO_PRODUCT,
  27. CREATE_PRODUCT,
  28. CREATE_PRODUCT_OPTION_GROUP,
  29. GENERATE_PRODUCT_VARIANTS,
  30. UPDATE_PRODUCT_VARIANTS,
  31. } from '../../admin-ui/src/app/data/definitions/product-definitions';
  32. import { taxAction } from '../src/config/adjustment/required-adjustment-actions';
  33. import { taxCondition } from '../src/config/adjustment/required-adjustment-conditions';
  34. import { Channel } from '../src/entity/channel/channel.entity';
  35. import { Customer } from '../src/entity/customer/customer.entity';
  36. import { SimpleGraphQLClient } from './simple-graphql-client';
  37. // tslint:disable:no-console
  38. /**
  39. * A service for creating mock data via the GraphQL API.
  40. */
  41. export class MockDataService {
  42. apiUrl: string;
  43. constructor(private client: SimpleGraphQLClient, private logging = true) {
  44. // make the generated results deterministic
  45. faker.seed(1);
  46. }
  47. async populateChannels(channelCodes: string[]): Promise<Channel[]> {
  48. const channels: Channel[] = [];
  49. for (const code of channelCodes) {
  50. const channel = await this.client.query<any>(gql`
  51. mutation {
  52. createChannel(code: "${code}") {
  53. id
  54. code
  55. token
  56. }
  57. }
  58. `);
  59. channels.push(channel.createChannel);
  60. this.log(`Created Channel: ${channel.createChannel.code}`);
  61. }
  62. return channels;
  63. }
  64. async populateOptions(): Promise<string> {
  65. return this.client
  66. .query<CreateProductOptionGroup.Mutation, CreateProductOptionGroup.Variables>(
  67. CREATE_PRODUCT_OPTION_GROUP,
  68. {
  69. input: {
  70. code: 'size',
  71. translations: [
  72. { languageCode: LanguageCode.en, name: 'Size' },
  73. { languageCode: LanguageCode.de, name: 'Größe' },
  74. ],
  75. options: [
  76. {
  77. code: 'small',
  78. translations: [
  79. { languageCode: LanguageCode.en, name: 'Small' },
  80. { languageCode: LanguageCode.de, name: 'Klein' },
  81. ],
  82. },
  83. {
  84. code: 'large',
  85. translations: [
  86. { languageCode: LanguageCode.en, name: 'Large' },
  87. { languageCode: LanguageCode.de, name: 'Groß' },
  88. ],
  89. },
  90. ],
  91. },
  92. },
  93. )
  94. .then(data => {
  95. this.log('Created option group:', data.createProductOptionGroup.name);
  96. return data.createProductOptionGroup.id;
  97. });
  98. }
  99. async populateTaxCategories() {
  100. const taxCategories = [
  101. { name: 'Standard Tax', rate: 20 },
  102. { name: 'Reduced Tax', rate: 5 },
  103. { name: 'Zero Tax', rate: 0 },
  104. ];
  105. const results: AdjustmentSource.Fragment[] = [];
  106. for (const category of taxCategories) {
  107. const result = await this.client.query<
  108. CreateAdjustmentSource.Mutation,
  109. CreateAdjustmentSource.Variables
  110. >(CREATE_ADJUSTMENT_SOURCE, {
  111. input: {
  112. name: category.name,
  113. type: AdjustmentType.TAX,
  114. enabled: true,
  115. conditions: [
  116. {
  117. code: taxCondition.code,
  118. arguments: [],
  119. },
  120. ],
  121. actions: [
  122. {
  123. code: taxAction.code,
  124. arguments: [category.rate.toString()],
  125. },
  126. ],
  127. },
  128. });
  129. results.push(result.createAdjustmentSource);
  130. }
  131. this.log(`Created ${results.length} tax categories`);
  132. return results;
  133. }
  134. async populateCustomers(count: number = 5): Promise<any> {
  135. for (let i = 0; i < count; i++) {
  136. const firstName = faker.name.firstName();
  137. const lastName = faker.name.lastName();
  138. const query1 = gql`
  139. mutation CreateCustomer($input: CreateCustomerInput!, $password: String) {
  140. createCustomer(input: $input, password: $password) {
  141. id
  142. emailAddress
  143. }
  144. }
  145. `;
  146. const variables1 = {
  147. input: {
  148. firstName,
  149. lastName,
  150. emailAddress: faker.internet.email(firstName, lastName),
  151. phoneNumber: faker.phone.phoneNumber(),
  152. } as CreateCustomerInput,
  153. password: 'test',
  154. };
  155. const customer: Customer | void = await this.client
  156. .query(query1, variables1)
  157. .then((data: any) => data.createCustomer as Customer, err => this.log(err));
  158. if (customer) {
  159. const query2 = gql`
  160. mutation($customerId: ID!, $input: CreateAddressInput!) {
  161. createCustomerAddress(customerId: $customerId, input: $input) {
  162. id
  163. streetLine1
  164. }
  165. }
  166. `;
  167. const variables2 = {
  168. input: {
  169. fullName: `${firstName} ${lastName}`,
  170. streetLine1: faker.address.streetAddress(),
  171. city: faker.address.city(),
  172. province: faker.address.county(),
  173. postalCode: faker.address.zipCode(),
  174. country: faker.address.country(),
  175. } as CreateAddressInput,
  176. customerId: customer.id,
  177. };
  178. await this.client.query(query2, variables2).then(
  179. data => {
  180. this.log(`Created Customer ${i + 1}:`, data);
  181. return data as Customer;
  182. },
  183. err => this.log(err),
  184. );
  185. }
  186. }
  187. }
  188. async populateAssets(): Promise<Asset[]> {
  189. const fileNames = await fs.readdir(path.join(__dirname, 'assets'));
  190. const filePaths = fileNames.map(fileName => path.join(__dirname, 'assets', fileName));
  191. return this.client.uploadAssets(filePaths).then(response => {
  192. console.log(`Created ${response.createAssets.length} Assets`);
  193. return response.createAssets;
  194. });
  195. }
  196. async populateProducts(
  197. count: number = 5,
  198. optionGroupId: string,
  199. assets: Asset[],
  200. taxCategories: AdjustmentSource.Fragment[],
  201. ): Promise<any> {
  202. for (let i = 0; i < count; i++) {
  203. const query = CREATE_PRODUCT;
  204. const name = faker.commerce.productName();
  205. const slug = name.toLowerCase().replace(/\s+/g, '-');
  206. const description = faker.lorem.sentence();
  207. const languageCodes = [LanguageCode.en, LanguageCode.de];
  208. // get 2 (pseudo) random asset ids
  209. const randomAssets = this.shuffleArray(assets).slice(0, 2);
  210. const variables: CreateProduct.Variables = {
  211. input: {
  212. translations: languageCodes.map(code =>
  213. this.makeProductTranslation(code, name, slug, description),
  214. ),
  215. assetIds: randomAssets.map(a => a.id),
  216. featuredAssetId: randomAssets[0].id,
  217. },
  218. };
  219. const product = await this.client
  220. .query<CreateProduct.Mutation, CreateProduct.Variables>(query, variables)
  221. .then(
  222. data => {
  223. this.log(`Created Product ${i + 1}:`, data.createProduct.name);
  224. return data;
  225. },
  226. err => this.log(err),
  227. );
  228. if (product) {
  229. await this.client.query<AddOptionGroupToProduct.Mutation, AddOptionGroupToProduct.Variables>(
  230. ADD_OPTION_GROUP_TO_PRODUCT,
  231. {
  232. productId: product.createProduct.id,
  233. optionGroupId,
  234. },
  235. );
  236. const prodWithVariants = await this.makeProductVariant(
  237. product.createProduct.id,
  238. taxCategories[0],
  239. );
  240. const variants = prodWithVariants.generateVariantsForProduct.variants;
  241. for (const variant of variants) {
  242. const variantEN = variant.translations[0];
  243. const variantDE = { ...variantEN };
  244. variantDE.languageCode = LanguageCode.de;
  245. variantDE.name = variantDE.name.replace(LanguageCode.en, LanguageCode.de);
  246. delete variantDE.id;
  247. variant.translations.push(variantDE);
  248. }
  249. await this.client.query<UpdateProductVariants.Mutation, UpdateProductVariants.Variables>(
  250. UPDATE_PRODUCT_VARIANTS,
  251. {
  252. input: variants.map(({ id, translations, sku, price }) => ({
  253. id,
  254. translations,
  255. sku,
  256. price,
  257. })),
  258. },
  259. );
  260. }
  261. }
  262. }
  263. async populateFacets() {
  264. await this.client.query<CreateFacet.Mutation, CreateFacet.Variables>(CREATE_FACET, {
  265. input: {
  266. code: 'brand',
  267. translations: [
  268. {
  269. languageCode: LanguageCode.en,
  270. name: 'Brand',
  271. },
  272. {
  273. languageCode: LanguageCode.en,
  274. name: 'Marke',
  275. },
  276. ],
  277. values: this.makeFacetValues(10),
  278. },
  279. });
  280. this.log('Created "brand" Facet');
  281. }
  282. private makeFacetValues(count: number): CreateFacetValueWithFacetInput[] {
  283. return Array.from({ length: count }).map(() => {
  284. const brand = faker.company.companyName();
  285. return {
  286. code: brand.replace(/\s/g, '_'),
  287. translations: [
  288. {
  289. languageCode: LanguageCode.en,
  290. name: brand,
  291. },
  292. {
  293. languageCode: LanguageCode.de,
  294. name: brand,
  295. },
  296. ],
  297. };
  298. });
  299. }
  300. private makeProductTranslation(
  301. languageCode: LanguageCode,
  302. name: string,
  303. slug: string,
  304. description: string,
  305. ): ProductTranslationInput {
  306. return {
  307. languageCode,
  308. name: `${languageCode} ${name}`,
  309. slug: `${languageCode} ${slug}`,
  310. description: `${languageCode} ${description}`,
  311. };
  312. }
  313. private async makeProductVariant(
  314. productId: string,
  315. taxCategory: AdjustmentSource.Fragment,
  316. ): Promise<GenerateProductVariants.Mutation> {
  317. const query = GENERATE_PRODUCT_VARIANTS;
  318. return this.client.query<GenerateProductVariants.Mutation, GenerateProductVariants.Variables>(query, {
  319. productId,
  320. defaultTaxCategoryId: taxCategory.id,
  321. defaultSku: faker.random.alphaNumeric(5),
  322. defaultPrice: faker.random.number({
  323. min: 100,
  324. max: 1000,
  325. }),
  326. });
  327. }
  328. private log(...args: any[]) {
  329. if (this.logging) {
  330. console.log(...args);
  331. }
  332. }
  333. /**
  334. * Deterministacally randomize array element order. Returns a new
  335. * shuffled array and leaves the input array intact.
  336. * Using Durstenfeld shuffle algorithm.
  337. *
  338. * Source: https://stackoverflow.com/a/12646864/772859
  339. */
  340. private shuffleArray<T>(array: T[]): T[] {
  341. const clone = array.slice(0);
  342. for (let i = clone.length - 1; i > 0; i--) {
  343. const j = Math.floor((faker.random.number(1000) / 1000) * (i + 1));
  344. const temp = clone[i];
  345. clone[i] = clone[j];
  346. clone[j] = temp;
  347. }
  348. return clone;
  349. }
  350. }