Skip to content

Commit

Permalink
Use GraphQL schema to generate typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyratox committed Oct 26, 2021
1 parent 9ca0d97 commit 584fe39
Show file tree
Hide file tree
Showing 12 changed files with 15,073 additions and 820 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ dist


out*.json
excel*.json
excel*.json
data/
6 changes: 6 additions & 0 deletions codegen.config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema: schema.graphql
generates:
./schema.ts:
plugins:
- typescript
- typescript-operations
68 changes: 43 additions & 25 deletions data-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import {
Record,
ProductPrototype,
ProductVariantPrototype,
AttributeFacet,
Facet,
BulkDiscount,
ID,
LanguageCode,
OptionGroup,
FacetValue,
Unpacked,
FacetPrototype,
FacetValuePrototype,
OptionGroupPrototype,
} from "./types";
import {
IMPORT_OPTION_GROUPS,
Expand All @@ -20,6 +19,16 @@ import {
CATEGORY_FACET_CODE,
RESELLER_DISCOUNT_FACET_CODE,
} from "./data-utils/facets";
import {
CreateFacetInput,
CreateFacetValueInput,
CreateProductOptionGroupInput,
Facet,
FacetValue,
LanguageCode,
Maybe,
ProductOptionGroup,
} from "./schema";

export const SLUGIFY_OPTIONS = { lower: true, strict: true };
export const SEPERATOR = "|";
Expand Down Expand Up @@ -60,9 +69,12 @@ const getFloatingPointValue = <T>(
};

const findItemByUnknownLocaleString = async <
ObjectTranslation extends { languageCode: LanguageCode; name: string },
ObjectTranslation extends {
languageCode: LanguageCode;
name?: Maybe<string>;
},
Obj extends { code: string; translations: ObjectTranslation[] },
ItemTranslation extends { languageCode: LanguageCode; name: string },
ItemTranslation extends { languageCode: LanguageCode; name?: Maybe<string> },
Item extends { code: string; translations: ItemTranslation[] }
>(
object: Obj,
Expand All @@ -77,11 +89,11 @@ const findItemByUnknownLocaleString = async <
suggestions = suggestions.filter(
(s) =>
!s.translations.find((t) => t.languageCode === languageCode) ||
s.translations.find((t) => t.name.trim().toLowerCase() === v)
s.translations.find((t) => (t?.name || "").trim().toLowerCase() === v)
);

const betterSuggestions = suggestions.filter((s) =>
s.translations.find((t) => t.name.trim().toLowerCase() === v)
s.translations.find((t) => (t?.name || "").trim().toLowerCase() === v)
);

const matches =
Expand All @@ -90,7 +102,9 @@ const findItemByUnknownLocaleString = async <
? betterSuggestions
: suggestions
: items.filter((item) =>
item.translations.find((t) => v === t.name.trim().toLowerCase())
item.translations.find(
(t) => v === (t?.name || "").trim().toLowerCase()
)
);

const untranslated = items.filter(
Expand Down Expand Up @@ -123,7 +137,10 @@ Wählen Sie die entsprechende Option aus.`,
}
};

export const tableToProducts = async (records: Record[], facets: Facet[]) => {
export const tableToProducts = async (
records: Record[],
facets: FacetPrototype[]
) => {
const products: (ProductPrototype & {
translationId?: ID;
initialVariantSku?: ID;
Expand All @@ -143,7 +160,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
)}`
);
}
const id: ID = record[column];
const id: ID = record[column].toString();

column = IMPORT_ATTRIBUTE_COLUMNS.parentId.find(inRecord);
if (!column) {
Expand Down Expand Up @@ -181,15 +198,15 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
let languageCode: LanguageCode;
switch (languageField) {
case "fr":
languageCode = "fr";
languageCode = LanguageCode.Fr;
break;
case "de":
default:
languageCode = "de";
languageCode = LanguageCode.De;
}

column = IMPORT_ATTRIBUTE_COLUMNS.translationId.find(inRecord);
const translationId = column && record[column];
const translationId = column && record[column].toString();

column = IMPORT_ATTRIBUTE_COLUMNS.name.find(inRecord);
const nameField = column && record[column].toString().trim();
Expand Down Expand Up @@ -243,6 +260,8 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
);
}

price = Math.round(price * 100) / 100;

column = IMPORT_ATTRIBUTE_COLUMNS.minimumOrderQuantity.find(inRecord);
const minimumOrderQuantity: number = getIntegerValue(
column && record[column],
Expand Down Expand Up @@ -341,9 +360,8 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
//modify some option group columns before processing them alltogether

const unitColumn = IMPORT_ATTRIBUTE_COLUMNS.unit.find(inRecord);
const quantityPerUnitColumn = IMPORT_ATTRIBUTE_COLUMNS.quantityPerUnit.find(
inRecord
);
const quantityPerUnitColumn =
IMPORT_ATTRIBUTE_COLUMNS.quantityPerUnit.find(inRecord);

if (quantityPerUnitColumn && unitColumn) {
const unit = record[unitColumn];
Expand Down Expand Up @@ -386,7 +404,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
bulkDiscounts = JSON.parse(bulkDiscountsField).map(
({ qty, ppu }: { qty: string | number; ppu: string | number }) => ({
quantity: parseInt(qty.toString()),
price: Math.floor(parseFloat(ppu.toString()) * 100),
price: Math.round(parseFloat(ppu.toString()) * 100),
})
);
} catch (e) {
Expand All @@ -409,7 +427,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {

if (pricePerUnit > 0 && quantity > 0) {
bulkDiscounts.push({
price: pricePerUnit * 100,
price: Math.round(pricePerUnit * 100),
quantity: quantity,
});
}
Expand Down Expand Up @@ -441,7 +459,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {

products.push({
previousIds: [id],
translationId,
translationId: translationId?.toString(),
sku,
translations: [
{
Expand Down Expand Up @@ -469,7 +487,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
}

//import option groups
const groups: OptionGroup[] = [];
const groups: OptionGroupPrototype[] = [];

IMPORT_OPTION_GROUPS.forEach((attribute) => {
const columnKey = attribute.columnKeys.find(inRecord);
Expand All @@ -496,12 +514,12 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {

const facetValueCodes: string[] = [];

//add category facets
//add category facets values
for (const c of categories) {
const f = facets.find((f) => f.code === CATEGORY_FACET_CODE);

if (f) {
let suggestions: FacetValue[] = [];
let suggestions: FacetValuePrototype[] = [];
if (translationId) {
const existingVariant = variants.find(
(v) => v.translationId === translationId
Expand Down Expand Up @@ -546,7 +564,7 @@ export const tableToProducts = async (records: Record[], facets: Facet[]) => {
const f = facets.find((f) => f.code === RESELLER_DISCOUNT_FACET_CODE);

if (f) {
let suggestions: FacetValue[] = [];
let suggestions: FacetValuePrototype[] = [];
if (translationId) {
const existingVariant = variants.find(
(v) => v.translationId === translationId
Expand Down
66 changes: 33 additions & 33 deletions data-utils/attributes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LanguageCode } from "types";
import { LanguageCode } from "../schema";

interface ImportFacet {
translations: {
Expand All @@ -12,88 +12,88 @@ interface ImportFacet {
export const IMPORT_OPTION_GROUPS: ImportFacet[] = [
{
translations: [
{ languageCode: "de", name: "Ausführung" },
{ languageCode: "fr", name: "Modèle" },
{ languageCode: LanguageCode.De, name: "Ausführung" },
{ languageCode: LanguageCode.Fr, name: "Modèle" },
],
code: "model",
columnKeys: ["Ausführung", "Ausfuehrung", "Modèle", "Modele"],
},
{
translations: [
{ languageCode: "de", name: "Pfeilrichtung" },
{ languageCode: "fr", name: "Sens de la flèche" },
{ languageCode: LanguageCode.De, name: "Pfeilrichtung" },
{ languageCode: LanguageCode.Fr, name: "Sens de la flèche" },
],
code: "arrow-direction",
columnKeys: ["Pfeilrichtung", "Sens de la flèche", "Sens de la fleche"],
},
{
translations: [
{ languageCode: "de", name: "Grösse" },
{ languageCode: "fr", name: "Taille" },
{ languageCode: LanguageCode.De, name: "Grösse" },
{ languageCode: LanguageCode.Fr, name: "Taille" },
],
code: "size",
columnKeys: ["Grösse", "Produkt Grösse", "Taille"],
},
{
translations: [
{ languageCode: "de", name: "Jahr" },
{ languageCode: "fr", name: "An" },
{ languageCode: LanguageCode.De, name: "Jahr" },
{ languageCode: LanguageCode.Fr, name: "An" },
],
code: "year",
columnKeys: ["Jahr", "An"],
},
{
translations: [
{ languageCode: "de", name: "Farbe" },
{ languageCode: "fr", name: "Couleur" },
{ languageCode: LanguageCode.De, name: "Farbe" },
{ languageCode: LanguageCode.Fr, name: "Couleur" },
],
code: "color",
columnKeys: ["Farbe", "Couleur"],
},
{
translations: [
{ languageCode: "de", name: "Format" },
{ languageCode: "fr", name: "Format" },
{ languageCode: LanguageCode.De, name: "Format" },
{ languageCode: LanguageCode.Fr, name: "Format" },
],
code: "format",
columnKeys: ["Format"],
},
{
translations: [
{ languageCode: "de", name: "Leuchtdichte" },
{ languageCode: "fr", name: "Luminance" },
{ languageCode: LanguageCode.De, name: "Leuchtdichte" },
{ languageCode: LanguageCode.Fr, name: "Luminance" },
],
code: "luminance",
columnKeys: ["Leuchtdichte_mcd", "Leuchtdichte", "Luminance"],
},
{
translations: [
{ languageCode: "de", name: "Material" },
{ languageCode: "fr", name: "Matériau" },
{ languageCode: LanguageCode.De, name: "Material" },
{ languageCode: LanguageCode.Fr, name: "Matériau" },
],
code: "material",
columnKeys: ["Material", "Produkt Material", "Matériau", "Materiau"],
},
{
translations: [
{ languageCode: "de", name: "Norm" },
{ languageCode: "fr", name: "Norme" },
{ languageCode: LanguageCode.De, name: "Norm" },
{ languageCode: LanguageCode.Fr, name: "Norme" },
],
code: "norm",
columnKeys: ["Norm", "Norme"],
},
{
translations: [
{ languageCode: "de", name: "PSPA Klasse" },
{ languageCode: "fr", name: "PSPA Classe" },
{ languageCode: LanguageCode.De, name: "PSPA Klasse" },
{ languageCode: LanguageCode.Fr, name: "PSPA Classe" },
],
code: "pspa-class",
columnKeys: ["PSPA_Class", "Pspa-klasse"],
},
{
translations: [
{ languageCode: "de", name: "Ursprungsland" },
{ languageCode: "fr", name: "Pays d'origine" },
{ languageCode: LanguageCode.De, name: "Ursprungsland" },
{ languageCode: LanguageCode.Fr, name: "Pays d'origine" },
],
code: "country",
columnKeys: [
Expand All @@ -105,8 +105,8 @@ export const IMPORT_OPTION_GROUPS: ImportFacet[] = [
},
{
translations: [
{ languageCode: "de", name: "Druckeigenschaft(-en)" },
{ languageCode: "fr", name: "Propriétés d'impression" },
{ languageCode: LanguageCode.De, name: "Druckeigenschaft(-en)" },
{ languageCode: LanguageCode.Fr, name: "Propriétés d'impression" },
],
code: "print-property",
columnKeys: [
Expand All @@ -117,32 +117,32 @@ export const IMPORT_OPTION_GROUPS: ImportFacet[] = [
},
{
translations: [
{ languageCode: "de", name: "Einheit" },
{ languageCode: "fr", name: "Unité" },
{ languageCode: LanguageCode.De, name: "Einheit" },
{ languageCode: LanguageCode.Fr, name: "Unité" },
],
code: "unit",
columnKeys: ["Einheit", "Produkt Einheit", "Unité"],
},
{
translations: [
{ languageCode: "de", name: "Symbolnummer" },
{ languageCode: "fr", name: "Numéro de symbole" },
{ languageCode: LanguageCode.De, name: "Symbolnummer" },
{ languageCode: LanguageCode.Fr, name: "Numéro de symbole" },
],
code: "symbol-number",
columnKeys: ["Symbolnummer", "Numéro de symbole"],
},
{
translations: [
{ languageCode: "de", name: "Inhalt" },
{ languageCode: "fr", name: "Contenu" },
{ languageCode: LanguageCode.De, name: "Inhalt" },
{ languageCode: LanguageCode.Fr, name: "Contenu" },
],
code: "content",
columnKeys: ["Inhalt", "Contenu"],
},
{
translations: [
{ languageCode: "de", name: "Variante" },
{ languageCode: "fr", name: "Variante" },
{ languageCode: LanguageCode.De, name: "Variante" },
{ languageCode: LanguageCode.Fr, name: "Variante" },
],
code: "variant",
columnKeys: ["Variante", "Variante"],
Expand Down
Loading

0 comments on commit 584fe39

Please sign in to comment.