From 9b542e78379692c98b3f266081988d6f0d461788 Mon Sep 17 00:00:00 2001 From: dominik-zeglen Date: Mon, 23 Mar 2020 18:59:10 +0100 Subject: [PATCH] Update types --- schema.graphql | 28 +++++++- src/discounts/mutations.ts | 67 ++++++++++--------- src/discounts/types/DiscountErrorFragment.ts | 15 +++++ src/discounts/types/SaleCataloguesAdd.ts | 6 +- src/discounts/types/SaleCataloguesRemove.ts | 6 +- src/discounts/types/SaleCreate.ts | 6 +- src/discounts/types/SaleDelete.ts | 6 +- src/discounts/types/SaleUpdate.ts | 6 +- src/discounts/types/VoucherCataloguesAdd.ts | 6 +- .../types/VoucherCataloguesRemove.ts | 6 +- src/discounts/types/VoucherCreate.ts | 6 +- src/discounts/types/VoucherDelete.ts | 6 +- src/discounts/types/VoucherUpdate.ts | 6 +- src/types/globalTypes.ts | 8 +++ src/utils/errors/discounts.ts | 35 ++++++++++ 15 files changed, 153 insertions(+), 60 deletions(-) create mode 100644 src/discounts/types/DiscountErrorFragment.ts create mode 100644 src/utils/errors/discounts.ts diff --git a/schema.graphql b/schema.graphql index 4edc7a797..49c6bdb44 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1509,6 +1509,20 @@ input DigitalContentUrlCreateInput { content: ID! } +type DiscountError { + field: String + message: String + code: DiscountErrorCode! +} + +enum DiscountErrorCode { + ALREADY_EXISTS + INVALID + NOT_FOUND + REQUIRED + UNIQUE +} + enum DiscountStatusEnum { ACTIVE EXPIRED @@ -3816,6 +3830,7 @@ type Sale implements Node { type SaleAddCatalogues { errors: [Error!]! sale: Sale + discountErrors: [DiscountError!]! } type SaleBulkDelete { @@ -3836,11 +3851,13 @@ type SaleCountableEdge { type SaleCreate { errors: [Error!]! + discountErrors: [DiscountError!]! sale: Sale } type SaleDelete { errors: [Error!]! + discountErrors: [DiscountError!]! sale: Sale } @@ -3865,6 +3882,7 @@ input SaleInput { type SaleRemoveCatalogues { errors: [Error!]! sale: Sale + discountErrors: [DiscountError!]! } enum SaleSortField { @@ -3905,6 +3923,7 @@ enum SaleType { type SaleUpdate { errors: [Error!]! + discountErrors: [DiscountError!]! sale: Sale } @@ -3920,15 +3939,15 @@ input SeoInput { type ServiceAccount implements Node & ObjectWithMetadata { id: ID! + name: String created: DateTime isActive: Boolean + permissions: [PermissionDisplay] tokens: [ServiceAccountToken] privateMetadata: [MetadataItem]! metadata: [MetadataItem]! privateMeta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `privetaMetadata` field instead. ") meta: [MetaStore]! @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.11. use the `metadata` field instead. ") - permissions: [PermissionDisplay] - name: String } type ServiceAccountClearPrivateMeta { @@ -4701,6 +4720,7 @@ type Voucher implements Node { type VoucherAddCatalogues { errors: [Error!]! voucher: Voucher + discountErrors: [DiscountError!]! } type VoucherBulkDelete { @@ -4721,11 +4741,13 @@ type VoucherCountableEdge { type VoucherCreate { errors: [Error!]! + discountErrors: [DiscountError!]! voucher: Voucher } type VoucherDelete { errors: [Error!]! + discountErrors: [DiscountError!]! voucher: Voucher } @@ -4765,6 +4787,7 @@ input VoucherInput { type VoucherRemoveCatalogues { errors: [Error!]! voucher: Voucher + discountErrors: [DiscountError!]! } enum VoucherSortField { @@ -4808,6 +4831,7 @@ enum VoucherTypeEnum { type VoucherUpdate { errors: [Error!]! + discountErrors: [DiscountError!]! voucher: Voucher } diff --git a/src/discounts/mutations.ts b/src/discounts/mutations.ts index 0a3529b52..aa5007519 100644 --- a/src/discounts/mutations.ts +++ b/src/discounts/mutations.ts @@ -38,13 +38,20 @@ import { VoucherCreate, VoucherCreateVariables } from "./types/VoucherCreate"; import { VoucherDelete, VoucherDeleteVariables } from "./types/VoucherDelete"; import { VoucherUpdate, VoucherUpdateVariables } from "./types/VoucherUpdate"; +const discountErrorFragment = gql` + fragment DiscountErrorFragment on DiscountError { + code + field + } +`; + const saleUpdate = gql` + ${discountErrorFragment} ${saleFragment} mutation SaleUpdate($input: SaleInput!, $id: ID!) { saleUpdate(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } sale { ...SaleFragment @@ -57,6 +64,7 @@ export const TypedSaleUpdate = TypedMutation( ); const saleCataloguesAdd = gql` + ${discountErrorFragment} ${saleDetailsFragment} mutation SaleCataloguesAdd( $input: CatalogueInput! @@ -67,9 +75,8 @@ const saleCataloguesAdd = gql` $last: Int ) { saleCataloguesAdd(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } sale { ...SaleDetailsFragment @@ -83,6 +90,7 @@ export const TypedSaleCataloguesAdd = TypedMutation< >(saleCataloguesAdd); const saleCataloguesRemove = gql` + ${discountErrorFragment} ${saleDetailsFragment} mutation SaleCataloguesRemove( $input: CatalogueInput! @@ -93,9 +101,8 @@ const saleCataloguesRemove = gql` $last: Int ) { saleCataloguesRemove(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } sale { ...SaleDetailsFragment @@ -109,12 +116,12 @@ export const TypedSaleCataloguesRemove = TypedMutation< >(saleCataloguesRemove); const saleCreate = gql` + ${discountErrorFragment} ${saleFragment} mutation SaleCreate($input: SaleInput!) { saleCreate(input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } sale { ...SaleFragment @@ -127,11 +134,11 @@ export const TypedSaleCreate = TypedMutation( ); const saleDelete = gql` + ${discountErrorFragment} mutation SaleDelete($id: ID!) { saleDelete(id: $id) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } } } @@ -156,12 +163,12 @@ export const TypedSaleBulkDelete = TypedMutation< >(saleBulkDelete); const voucherUpdate = gql` + ${discountErrorFragment} ${voucherFragment} mutation VoucherUpdate($input: VoucherInput!, $id: ID!) { voucherUpdate(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } voucher { ...VoucherFragment @@ -175,6 +182,7 @@ export const TypedVoucherUpdate = TypedMutation< >(voucherUpdate); const voucherCataloguesAdd = gql` + ${discountErrorFragment} ${voucherDetailsFragment} mutation VoucherCataloguesAdd( $input: CatalogueInput! @@ -185,9 +193,8 @@ const voucherCataloguesAdd = gql` $last: Int ) { voucherCataloguesAdd(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } voucher { ...VoucherDetailsFragment @@ -201,6 +208,7 @@ export const TypedVoucherCataloguesAdd = TypedMutation< >(voucherCataloguesAdd); const voucherCataloguesRemove = gql` + ${discountErrorFragment} ${voucherDetailsFragment} mutation VoucherCataloguesRemove( $input: CatalogueInput! @@ -211,9 +219,8 @@ const voucherCataloguesRemove = gql` $last: Int ) { voucherCataloguesRemove(id: $id, input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } voucher { ...VoucherDetailsFragment @@ -227,12 +234,12 @@ export const TypedVoucherCataloguesRemove = TypedMutation< >(voucherCataloguesRemove); const voucherCreate = gql` + ${discountErrorFragment} ${voucherFragment} mutation VoucherCreate($input: VoucherInput!) { voucherCreate(input: $input) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } voucher { ...VoucherFragment @@ -246,11 +253,11 @@ export const TypedVoucherCreate = TypedMutation< >(voucherCreate); const voucherDelete = gql` + ${discountErrorFragment} mutation VoucherDelete($id: ID!) { voucherDelete(id: $id) { - errors { - field - message + errors: discountErrors { + ...DiscountErrorFragment } } } diff --git a/src/discounts/types/DiscountErrorFragment.ts b/src/discounts/types/DiscountErrorFragment.ts new file mode 100644 index 000000000..966c8c2ea --- /dev/null +++ b/src/discounts/types/DiscountErrorFragment.ts @@ -0,0 +1,15 @@ +/* tslint:disable */ +/* eslint-disable */ +// This file was automatically generated and should not be edited. + +import { DiscountErrorCode } from "./../../types/globalTypes"; + +// ==================================================== +// GraphQL fragment: DiscountErrorFragment +// ==================================================== + +export interface DiscountErrorFragment { + __typename: "DiscountError"; + code: DiscountErrorCode; + field: string | null; +} diff --git a/src/discounts/types/SaleCataloguesAdd.ts b/src/discounts/types/SaleCataloguesAdd.ts index 379a0e70d..fbafa2d1f 100644 --- a/src/discounts/types/SaleCataloguesAdd.ts +++ b/src/discounts/types/SaleCataloguesAdd.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { CatalogueInput, SaleType } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, SaleType } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: SaleCataloguesAdd // ==================================================== export interface SaleCataloguesAdd_saleCataloguesAdd_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface SaleCataloguesAdd_saleCataloguesAdd_sale_products_edges_node_productType { diff --git a/src/discounts/types/SaleCataloguesRemove.ts b/src/discounts/types/SaleCataloguesRemove.ts index c28077190..c9ed70d38 100644 --- a/src/discounts/types/SaleCataloguesRemove.ts +++ b/src/discounts/types/SaleCataloguesRemove.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { CatalogueInput, SaleType } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, SaleType } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: SaleCataloguesRemove // ==================================================== export interface SaleCataloguesRemove_saleCataloguesRemove_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface SaleCataloguesRemove_saleCataloguesRemove_sale_products_edges_node_productType { diff --git a/src/discounts/types/SaleCreate.ts b/src/discounts/types/SaleCreate.ts index f27b8224e..89434ac87 100644 --- a/src/discounts/types/SaleCreate.ts +++ b/src/discounts/types/SaleCreate.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { SaleInput, SaleType } from "./../../types/globalTypes"; +import { SaleInput, DiscountErrorCode, SaleType } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: SaleCreate // ==================================================== export interface SaleCreate_saleCreate_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface SaleCreate_saleCreate_sale { diff --git a/src/discounts/types/SaleDelete.ts b/src/discounts/types/SaleDelete.ts index 7c6c03bfb..df7270162 100644 --- a/src/discounts/types/SaleDelete.ts +++ b/src/discounts/types/SaleDelete.ts @@ -2,14 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. +import { DiscountErrorCode } from "./../../types/globalTypes"; + // ==================================================== // GraphQL mutation operation: SaleDelete // ==================================================== export interface SaleDelete_saleDelete_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface SaleDelete_saleDelete { diff --git a/src/discounts/types/SaleUpdate.ts b/src/discounts/types/SaleUpdate.ts index de26294af..e8bc45b6b 100644 --- a/src/discounts/types/SaleUpdate.ts +++ b/src/discounts/types/SaleUpdate.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { SaleInput, SaleType } from "./../../types/globalTypes"; +import { SaleInput, DiscountErrorCode, SaleType } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: SaleUpdate // ==================================================== export interface SaleUpdate_saleUpdate_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface SaleUpdate_saleUpdate_sale { diff --git a/src/discounts/types/VoucherCataloguesAdd.ts b/src/discounts/types/VoucherCataloguesAdd.ts index c4875f8ad..e2beb9a45 100644 --- a/src/discounts/types/VoucherCataloguesAdd.ts +++ b/src/discounts/types/VoucherCataloguesAdd.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { CatalogueInput, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCataloguesAdd // ==================================================== export interface VoucherCataloguesAdd_voucherCataloguesAdd_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface VoucherCataloguesAdd_voucherCataloguesAdd_voucher_countries { diff --git a/src/discounts/types/VoucherCataloguesRemove.ts b/src/discounts/types/VoucherCataloguesRemove.ts index 3c80a2062..a60d2b84b 100644 --- a/src/discounts/types/VoucherCataloguesRemove.ts +++ b/src/discounts/types/VoucherCataloguesRemove.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { CatalogueInput, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; +import { CatalogueInput, DiscountErrorCode, DiscountValueTypeEnum, VoucherTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCataloguesRemove // ==================================================== export interface VoucherCataloguesRemove_voucherCataloguesRemove_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface VoucherCataloguesRemove_voucherCataloguesRemove_voucher_countries { diff --git a/src/discounts/types/VoucherCreate.ts b/src/discounts/types/VoucherCreate.ts index 60d2d1af7..ca12f1c87 100644 --- a/src/discounts/types/VoucherCreate.ts +++ b/src/discounts/types/VoucherCreate.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { VoucherInput, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherInput, DiscountErrorCode, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherCreate // ==================================================== export interface VoucherCreate_voucherCreate_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface VoucherCreate_voucherCreate_voucher_countries { diff --git a/src/discounts/types/VoucherDelete.ts b/src/discounts/types/VoucherDelete.ts index a903ef9e7..0ae51b7ff 100644 --- a/src/discounts/types/VoucherDelete.ts +++ b/src/discounts/types/VoucherDelete.ts @@ -2,14 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. +import { DiscountErrorCode } from "./../../types/globalTypes"; + // ==================================================== // GraphQL mutation operation: VoucherDelete // ==================================================== export interface VoucherDelete_voucherDelete_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface VoucherDelete_voucherDelete { diff --git a/src/discounts/types/VoucherUpdate.ts b/src/discounts/types/VoucherUpdate.ts index 853f8705d..a1285c1ad 100644 --- a/src/discounts/types/VoucherUpdate.ts +++ b/src/discounts/types/VoucherUpdate.ts @@ -2,16 +2,16 @@ /* eslint-disable */ // This file was automatically generated and should not be edited. -import { VoucherInput, DiscountValueTypeEnum } from "./../../types/globalTypes"; +import { VoucherInput, DiscountErrorCode, DiscountValueTypeEnum } from "./../../types/globalTypes"; // ==================================================== // GraphQL mutation operation: VoucherUpdate // ==================================================== export interface VoucherUpdate_voucherUpdate_errors { - __typename: "Error"; + __typename: "DiscountError"; + code: DiscountErrorCode; field: string | null; - message: string | null; } export interface VoucherUpdate_voucherUpdate_voucher_countries { diff --git a/src/types/globalTypes.ts b/src/types/globalTypes.ts index 9c0a39481..2d20b6682 100644 --- a/src/types/globalTypes.ts +++ b/src/types/globalTypes.ts @@ -346,6 +346,14 @@ export enum CountryCode { ZW = "ZW", } +export enum DiscountErrorCode { + ALREADY_EXISTS = "ALREADY_EXISTS", + INVALID = "INVALID", + NOT_FOUND = "NOT_FOUND", + REQUIRED = "REQUIRED", + UNIQUE = "UNIQUE", +} + export enum DiscountStatusEnum { ACTIVE = "ACTIVE", EXPIRED = "EXPIRED", diff --git a/src/utils/errors/discounts.ts b/src/utils/errors/discounts.ts new file mode 100644 index 000000000..5641330ee --- /dev/null +++ b/src/utils/errors/discounts.ts @@ -0,0 +1,35 @@ +import { IntlShape, defineMessages } from "react-intl"; + +import { DiscountErrorFragment } from "@saleor/discounts/types/DiscountErrorFragment"; +import { DiscountErrorCode } from "@saleor/types/globalTypes"; +import { commonMessages } from "@saleor/intl"; +import commonErrorMessages from "./common"; + +const messages = defineMessages({ + alreadyExists: { + defaultMessage: "Promo code already exists", + description: "error message" + } +}); + +function getDiscountErrorMessage( + err: Omit | undefined, + intl: IntlShape +): string { + if (err) { + switch (err.code) { + case DiscountErrorCode.ALREADY_EXISTS: + return intl.formatMessage(messages.alreadyExists); + case DiscountErrorCode.REQUIRED: + return intl.formatMessage(commonMessages.requiredField); + case DiscountErrorCode.INVALID: + return intl.formatMessage(commonErrorMessages.invalid); + default: + return intl.formatMessage(commonErrorMessages.unknownError); + } + } + + return undefined; +} + +export default getDiscountErrorMessage;