mock-data.service.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import * as faker from 'faker/locale/en_GB';
  2. import { Connection, createConnection } from 'typeorm';
  3. import { PasswordService } from '../src/auth/password.service';
  4. import { Role } from '../src/auth/role';
  5. import { Address } from '../src/entity/address/address.entity';
  6. import { Administrator } from '../src/entity/administrator/administrator.entity';
  7. import { Customer } from '../src/entity/customer/customer.entity';
  8. import { ProductOptionGroupTranslation } from '../src/entity/product-option-group/product-option-group-translation.entity';
  9. import { ProductOptionGroup } from '../src/entity/product-option-group/product-option-group.entity';
  10. import { ProductOptionTranslation } from '../src/entity/product-option/product-option-translation.entity';
  11. import { ProductOption } from '../src/entity/product-option/product-option.entity';
  12. import { ProductVariantTranslation } from '../src/entity/product-variant/product-variant-translation.entity';
  13. import { ProductVariant } from '../src/entity/product-variant/product-variant.entity';
  14. import { ProductTranslation } from '../src/entity/product/product-translation.entity';
  15. import { Product } from '../src/entity/product/product.entity';
  16. import { User } from '../src/entity/user/user.entity';
  17. import { LanguageCode } from '../src/locale/language-code';
  18. // tslint:disable:no-console
  19. /**
  20. * A Class used for generating mock data directly into the database via TypeORM.
  21. */
  22. export class MockDataService {
  23. connection: Connection;
  24. async populate(): Promise<any> {
  25. this.connection = await this.connect();
  26. await this.clearAllTables();
  27. await this.populateCustomersAndAddresses();
  28. await this.populateAdministrators();
  29. const sizeOptionGroup = await this.populateOptions();
  30. await this.populateProducts(sizeOptionGroup);
  31. }
  32. async connect(): Promise<Connection> {
  33. this.connection = await createConnection({
  34. type: 'mysql',
  35. entities: ['./**/entity/**/*.entity.ts'],
  36. synchronize: true,
  37. logging: false,
  38. host: '192.168.99.100',
  39. port: 3306,
  40. username: 'root',
  41. password: '',
  42. database: 'test',
  43. });
  44. return this.connection;
  45. }
  46. async clearAllTables() {
  47. await this.connection.synchronize(true);
  48. console.log('Cleared all tables');
  49. }
  50. async populateOptions(): Promise<ProductOptionGroup> {
  51. const sizeGroup = new ProductOptionGroup();
  52. sizeGroup.code = 'size';
  53. const sizeGroupEN = new ProductOptionGroupTranslation();
  54. sizeGroupEN.languageCode = LanguageCode.EN;
  55. sizeGroupEN.name = 'Size';
  56. await this.connection.manager.save(sizeGroupEN);
  57. const sizeGroupDE = new ProductOptionGroupTranslation();
  58. sizeGroupDE.languageCode = LanguageCode.DE;
  59. sizeGroupDE.name = 'Größe';
  60. await this.connection.manager.save(sizeGroupDE);
  61. sizeGroup.translations = [sizeGroupEN, sizeGroupDE];
  62. await this.connection.manager.save(sizeGroup);
  63. await this.populateSizeOptions(sizeGroup);
  64. console.log('created size options');
  65. return sizeGroup;
  66. }
  67. private async populateSizeOptions(sizeGroup: ProductOptionGroup) {
  68. const sizeSmall = new ProductOption();
  69. sizeSmall.code = 'small';
  70. const sizeSmallEN = new ProductOptionTranslation();
  71. sizeSmallEN.languageCode = LanguageCode.EN;
  72. sizeSmallEN.name = 'Small';
  73. await this.connection.manager.save(sizeSmallEN);
  74. const sizeSmallDE = new ProductOptionTranslation();
  75. sizeSmallDE.languageCode = LanguageCode.DE;
  76. sizeSmallDE.name = 'Klein';
  77. await this.connection.manager.save(sizeSmallDE);
  78. sizeSmall.translations = [sizeSmallEN, sizeSmallDE];
  79. sizeSmall.group = sizeGroup;
  80. await this.connection.manager.save(sizeSmall);
  81. const sizeLarge = new ProductOption();
  82. sizeLarge.code = 'large';
  83. const sizeLargeEN = new ProductOptionTranslation();
  84. sizeLargeEN.languageCode = LanguageCode.EN;
  85. sizeLargeEN.name = 'Large';
  86. await this.connection.manager.save(sizeLargeEN);
  87. const sizeLargeDE = new ProductOptionTranslation();
  88. sizeLargeDE.languageCode = LanguageCode.DE;
  89. sizeLargeDE.name = 'Groß';
  90. await this.connection.manager.save(sizeLargeDE);
  91. sizeLarge.translations = [sizeLargeEN, sizeLargeDE];
  92. sizeLarge.group = sizeGroup;
  93. await this.connection.manager.save(sizeLarge);
  94. sizeGroup.options = [sizeSmall, sizeLarge];
  95. }
  96. async populateProducts(optionGroup: ProductOptionGroup) {
  97. for (let i = 0; i < 5; i++) {
  98. const addOption = i === 2 || i === 4;
  99. const product = new Product();
  100. product.image = faker.image.imageUrl();
  101. const name = faker.commerce.productName();
  102. const slug = name.toLowerCase().replace(/\s+/g, '-');
  103. const description = faker.lorem.sentence();
  104. const translation1 = this.makeProductTranslation(LanguageCode.EN, name, slug, description);
  105. const translation2 = this.makeProductTranslation(LanguageCode.DE, name, slug, description);
  106. await this.connection.manager.save(translation1);
  107. await this.connection.manager.save(translation2);
  108. // 1 - 4 variants
  109. const variantCount = Math.floor(Math.random() * 4) + 1;
  110. const variants: ProductVariant[] = [];
  111. for (let j = 0; j < variantCount; j++) {
  112. const variant = new ProductVariant();
  113. const variantName = `${name} variant ${j + 1}`;
  114. variant.image = faker.image.imageUrl();
  115. variant.price = faker.random.number({ min: 100, max: 12000 });
  116. const variantTranslation1 = this.makeProductVariantTranslation(LanguageCode.EN, variantName);
  117. const variantTranslation2 = this.makeProductVariantTranslation(LanguageCode.DE, variantName);
  118. await this.connection.manager.save(variantTranslation1);
  119. await this.connection.manager.save(variantTranslation2);
  120. if (addOption) {
  121. variant.options = [optionGroup.options[0]];
  122. } else {
  123. variant.options = [];
  124. }
  125. variant.translations = [variantTranslation1, variantTranslation2];
  126. await this.connection.manager.save(variant);
  127. console.log(`${j + 1}. created product variant ${variantName}`);
  128. variants.push(variant);
  129. }
  130. if (addOption) {
  131. product.optionGroups = [optionGroup];
  132. }
  133. product.variants = variants;
  134. product.translations = [translation1, translation2];
  135. await this.connection.manager.save(product);
  136. console.log(`${i + 1}. created product & translations for ${translation1.name}`);
  137. }
  138. }
  139. async populateCustomersAndAddresses() {
  140. const passwordService = new PasswordService();
  141. for (let i = 0; i < 5; i++) {
  142. const customer = new Customer();
  143. customer.firstName = faker.name.firstName();
  144. customer.lastName = faker.name.lastName();
  145. customer.emailAddress = faker.internet.email(customer.firstName, customer.lastName);
  146. customer.phoneNumber = faker.phone.phoneNumber();
  147. const user = new User();
  148. user.passwordHash = await passwordService.hash('test');
  149. user.identifier = customer.emailAddress;
  150. user.roles = [Role.Customer];
  151. await this.connection.manager.save(user);
  152. const address = new Address();
  153. address.fullName = `${customer.firstName} ${customer.lastName}`;
  154. address.streetLine1 = faker.address.streetAddress();
  155. address.city = faker.address.city();
  156. address.province = faker.address.county();
  157. address.postalCode = faker.address.zipCode();
  158. address.country = faker.address.countryCode();
  159. await this.connection.manager.save(address);
  160. customer.addresses = [address];
  161. customer.user = user;
  162. await this.connection.manager.save(customer);
  163. console.log('created customer, user and address for ' + customer.firstName + ' ' + customer.lastName);
  164. }
  165. }
  166. async populateAdministrators() {
  167. const passwordService = new PasswordService();
  168. const user = new User();
  169. user.passwordHash = await passwordService.hash('admin');
  170. user.identifier = 'admin';
  171. user.roles = [Role.Superadmin];
  172. await this.connection.manager.save(user);
  173. const administrator = new Administrator();
  174. administrator.emailAddress = 'admin@test.com';
  175. administrator.firstName = 'Super';
  176. administrator.lastName = 'Admin';
  177. administrator.user = user;
  178. await this.connection.manager.save(administrator);
  179. }
  180. private makeProductTranslation(
  181. langCode: LanguageCode,
  182. name: string,
  183. slug: string,
  184. description: string,
  185. ): ProductTranslation {
  186. const productTranslation = new ProductTranslation();
  187. productTranslation.languageCode = langCode;
  188. productTranslation.name = `${langCode} ${name}`;
  189. productTranslation.slug = `${langCode} ${slug}`;
  190. productTranslation.description = `${langCode} ${description}`;
  191. return productTranslation;
  192. }
  193. private makeProductVariantTranslation(langCode: LanguageCode, name: string): ProductVariantTranslation {
  194. const productVariantTranslation = new ProductVariantTranslation();
  195. productVariantTranslation.languageCode = langCode;
  196. productVariantTranslation.name = `${langCode} ${name}`;
  197. return productVariantTranslation;
  198. }
  199. }