Browse Source

chore(server): Create facet with mock data

Michael Bromley 7 years ago
parent
commit
165f94932a

File diff suppressed because it is too large
+ 0 - 0
schema.json


+ 44 - 0
server/mock-data/mock-data.service.ts

@@ -1,6 +1,10 @@
 import * as faker from 'faker/locale/en_GB';
 import gql from 'graphql-tag';
 import {
+    CreateFacet,
+    CreateFacetValueInput,
+    CreateFacetValueWithFacetInput,
+    CreateFacetVariables,
     CreateProduct,
     CreateProductOptionGroup,
     CreateProductOptionGroupVariables,
@@ -13,6 +17,7 @@ import {
     UpdateProductVariantsVariables,
 } from 'shared/generated-types';
 
+import { CREATE_FACET } from '../../admin-ui/src/app/data/mutations/facet-mutations';
 import {
     CREATE_PRODUCT,
     CREATE_PRODUCT_OPTION_GROUP,
@@ -210,6 +215,45 @@ export class MockDataService {
         }
     }
 
+    async populateFacets() {
+        await this.client.query<CreateFacet, CreateFacetVariables>(CREATE_FACET, {
+            input: {
+                code: 'brand',
+                translations: [
+                    {
+                        languageCode: LanguageCode.en,
+                        name: 'Brand',
+                    },
+                    {
+                        languageCode: LanguageCode.en,
+                        name: 'Marke',
+                    },
+                ],
+                values: this.makeFacetValues(10),
+            },
+        });
+        this.log('Created "brand" Facet');
+    }
+
+    private makeFacetValues(count: number): CreateFacetValueWithFacetInput[] {
+        return Array.from({ length: count }).map(() => {
+            const brand = faker.company.companyName();
+            return {
+                code: brand.replace(/\s/g, '_'),
+                translations: [
+                    {
+                        languageCode: LanguageCode.en,
+                        name: brand,
+                    },
+                    {
+                        languageCode: LanguageCode.de,
+                        name: brand,
+                    },
+                ],
+            };
+        });
+    }
+
     private makeProductTranslation(
         languageCode: LanguageCode,
         name: string,

+ 1 - 0
server/mock-data/populate.ts

@@ -32,6 +32,7 @@ export async function populate(
     await mockDataClientService.populateOptions();
     await mockDataClientService.populateProducts(options.productCount);
     await mockDataClientService.populateCustomers(options.customerCount);
+    await mockDataClientService.populateFacets();
     await mockDataClientService.populateAdmins();
     return app;
 }

+ 5 - 0
server/src/entity/facet-value/facet-value.graphql

@@ -22,6 +22,11 @@ input FacetValueTranslationInput {
     name: String!
 }
 
+input CreateFacetValueWithFacetInput {
+    code: String!
+    translations: [FacetValueTranslationInput!]!
+}
+
 input CreateFacetValueInput {
     facetId: ID!
     code: String!

+ 1 - 1
server/src/entity/facet/facet.graphql

@@ -26,7 +26,7 @@ input FacetTranslationInput {
 input CreateFacetInput {
     code: String!
     translations: [FacetTranslationInput!]!
-    values: [CreateFacetValueInput!]
+    values: [CreateFacetValueWithFacetInput!]
 }
 
 input UpdateFacetInput {

+ 10 - 2
server/src/service/facet-value.service.ts

@@ -1,6 +1,11 @@
 import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
-import { CreateFacetValueInput, LanguageCode, UpdateFacetValueInput } from 'shared/generated-types';
+import {
+    CreateFacetValueInput,
+    CreateFacetValueWithFacetInput,
+    LanguageCode,
+    UpdateFacetValueInput,
+} from 'shared/generated-types';
 import { ID } from 'shared/shared-types';
 import { Connection } from 'typeorm';
 
@@ -38,7 +43,10 @@ export class FacetValueService {
             .then(facetValue => facetValue && translateDeep(facetValue, lang));
     }
 
-    async create(facet: Facet, createFacetValueDto: CreateFacetValueInput): Promise<Translated<FacetValue>> {
+    async create(
+        facet: Facet,
+        createFacetValueDto: CreateFacetValueInput | CreateFacetValueWithFacetInput,
+    ): Promise<Translated<FacetValue>> {
         const save = createTranslatable(FacetValue, FacetValueTranslation, fv => (fv.facet = facet));
         const facetValue = await save(this.connection, createFacetValueDto);
         return assertFound(this.findOne(facetValue.id, DEFAULT_LANGUAGE_CODE));

+ 6 - 1
shared/generated-types.ts

@@ -1447,7 +1447,7 @@ export interface CreateFacetCustomFieldsInput {
 export interface CreateFacetInput {
   code: string;
   translations: FacetTranslationInput[];
-  values?: CreateFacetValueInput[] | null;
+  values?: CreateFacetValueWithFacetInput[] | null;
   customFields?: CreateFacetCustomFieldsInput | null;
 }
 
@@ -1463,6 +1463,11 @@ export interface CreateFacetValueInput {
   customFields?: CreateFacetValueCustomFieldsInput | null;
 }
 
+export interface CreateFacetValueWithFacetInput {
+  code: string;
+  translations: FacetValueTranslationInput[];
+}
+
 export interface CreateProductCustomFieldsInput {
   infoUrl?: string | null;
   downloadable?: boolean | null;

Some files were not shown because too many files changed in this diff