From f98fc51ac03e507569a8ebbe8f135dc07ad5bf9c Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Mon, 6 Apr 2020 12:26:43 -0700 Subject: [PATCH 1/5] refactor: update translations Signed-off-by: Will Lopez --- src/i18n/en.json | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/i18n/en.json b/src/i18n/en.json index 4f3c878..9b0980b 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -9,6 +9,7 @@ "discountError": "Sorry, there was an error applying discount." }, "discounts": { + "title": "Discount Codes", "enterItHere": "Have a code? Enter it here.", "discountLabel": "Discount Code", "submitted": "Discount saved and applied", @@ -29,16 +30,17 @@ "settingsSaveFailure": "Failed to save settings.", "confirmRateDelete": "Confirm discount rate deletion." }, - "discountGrid": { - "code": "Code", - "label": "Label", - "discountMethod": "Method", - "discount": "Discount", - "calculation": { - "method": "Calculation" - }, - "conditions": { - "redemptionLimit": "Limit" + "discountsTable": { + "headers": { + "code": "Code", + "discountMethod": "Method", + "discount": "Discount", + "calculation": { + "method": "Calculation" + }, + "conditions": { + "redemptionLimit": "Limit" + } } } } From c65c85c2b8c537d26f26d50dbe4fc17425ff3f97 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Tue, 14 Apr 2020 17:03:18 -0700 Subject: [PATCH 2/5] refactor: update translations Signed-off-by: Will Lopez --- src/i18n/en.json | 114 ++++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 46 deletions(-) diff --git a/src/i18n/en.json b/src/i18n/en.json index 9b0980b..913ec29 100644 --- a/src/i18n/en.json +++ b/src/i18n/en.json @@ -1,49 +1,71 @@ -[{ - "language": "English", - "i18n": "en", - "ns": "discount-codes", - "translation": { - "discount-codes": { - "checkoutPayment": { - "applyDiscount": "Apply", - "discountError": "Sorry, there was an error applying discount." - }, - "discounts": { - "title": "Discount Codes", - "enterItHere": "Have a code? Enter it here.", - "discountLabel": "Discount Code", - "submitted": "Discount saved and applied", - "applied": "applied", - "code": "code", - "codeIsExpired": "Code is expired", - "codeIsInvalid": "Code is invalid", - "multiShopError": "Discounts cannot be applied to a multi-shop cart or order" - }, - "admin": { - "paymentSettings": { - "codesLabel": "Codes" - }, - "settings": { - "codesLabel": "Codes", - "noCustomDiscountCodesFound": "No discount codes found.", - "settingsSaveSuccess": "Settings saved successfully.", - "settingsSaveFailure": "Failed to save settings.", - "confirmRateDelete": "Confirm discount rate deletion." - }, - "discountsTable": { - "headers": { - "code": "Code", - "discountMethod": "Method", - "discount": "Discount", - "calculation": { - "method": "Calculation" - }, - "conditions": { - "redemptionLimit": "Limit" +[ + { + "language": "English", + "i18n": "en", + "ns": "discount-codes", + "translation": { + "discount-codes": { + "checkoutPayment": { + "applyDiscount": "Apply", + "discountError": "Sorry, there was an error applying discount." + }, + "discounts": { + "title": "Discount Codes", + "enterItHere": "Have a code? Enter it here.", + "discountLabel": "Discount Code", + "submitted": "Discount saved and applied", + "applied": "applied", + "code": "code", + "codeIsExpired": "Code is expired", + "codeIsInvalid": "Code is invalid", + "multiShopError": "Discounts cannot be applied to a multi-shop cart or order" + }, + "admin": { + "paymentSettings": { + "codesLabel": "Codes" + }, + "discountCode": { + "addDiscount": "Add Discount", + "addDiscountModalTitle": "Add a New Discount Code", + "confirmRateDelete": "Confirm discount rate deletion.", + "title": "Discount Codes", + "noCustomDiscountCodesFound": "No discount codes found", + "failure": "The operation failed", + "createSuccess": "A new discount code has been successfully created", + "deleteSuccess": "Discount code successfully deleted", + "updateSuccess": "Discount code successfully updated", + "settingsSaveSuccess": "Settings saved successfully.", + "form": { + "calculationMethod": "Calculation Method", + "code": "Discount Code", + "codePlaceholder": "Discount code", + "deleteDialogTitle": "Delete Discount Code", + "deleteMessage": "Are you sure you want to delete this discount code?", + "discount": "Discount", + "discountPlaceholder": "Discount value, i.e. a value 10 will translate to 10% off", + "accountLimit": "Account Limit", + "accountLimitPlaceholder": "How many times a user can redeem this discount", + "redemptionLimit": "Total Limit", + "redemptionLimitPlaceholder": "The total number of times this discount can be redeemed" + } + }, + "discountsTable": { + "headers": { + "code": "Code", + "discountMethod": "Method", + "discount": "Discount", + "calculation": { + "method": "Calculation" + }, + "conditions": { + "accountLimit": "Account Limit", + "redemptionLimit": "Total Limit" + } + }, + "filterPlaceholder": "Filter discount codes" + } + } } - } } - } } - } -}] +] From c623f9611586a22308ad80679f678bd030557901 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Tue, 14 Apr 2020 17:03:33 -0700 Subject: [PATCH 3/5] refactor: add searchField param to discountCodes query Signed-off-by: Will Lopez --- src/queries/discountCodes.js | 28 +++++++++++++++++++++++++--- src/resolvers/Query/discountCodes.js | 5 +++-- src/schemas/schema.graphql | 9 +++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/queries/discountCodes.js b/src/queries/discountCodes.js index 33dc9db..ac5f4af 100644 --- a/src/queries/discountCodes.js +++ b/src/queries/discountCodes.js @@ -5,15 +5,37 @@ * @summary Query the Discounts collection * @param {Object} context - an object containing the per-request state * @param {String} shopId - ID of Shop to query against + * @param {Object} filters - filters to be applied * @returns {Promise} DiscountCodes object Promise */ -export default async function discountCodes(context, shopId) { +export default async function discountCodes(context, shopId, filters) { const { collections } = context; const { Discounts } = collections; await context.validatePermissions("reaction:legacy:discounts", "read", { shopId }); - return Discounts.find({ + // Create the mongo selector from the provided filters + let selector = { shopId - }); + } + + // filter by searchField + if (filters.searchField) { + const cond = { + $regex: filters.searchField, + $options: "i" + }; + selector = { + ...selector, + $or: [{ + code: cond + }, { + label: cond + }, { + description: cond + }] + }; + } + + return Discounts.find(selector); } diff --git a/src/resolvers/Query/discountCodes.js b/src/resolvers/Query/discountCodes.js index 1378bb0..5457c5a 100644 --- a/src/resolvers/Query/discountCodes.js +++ b/src/resolvers/Query/discountCodes.js @@ -10,16 +10,17 @@ import { decodeShopOpaqueId } from "../../xforms/id.js"; * @param {Object} _ - unused * @param {Object} args - an object of all arguments that were sent by the client * @param {String} args.shopId - id of the shop + * @param {Object} args.filters - query filters * @param {Object} context - an object containing the per-request state * @param {Object} info Info about the GraphQL request * @returns {Promise} An array of discount codes */ export default async function discountCodes(_, args, context, info) { - const { shopId: opaqueShopId, ...connectionArgs } = args; + const { shopId: opaqueShopId, filters, ...connectionArgs } = args; const shopId = decodeShopOpaqueId(opaqueShopId); - const query = await context.queries.discountCodes(context, shopId); + const query = await context.queries.discountCodes(context, shopId, filters); return getPaginatedResponse(query, connectionArgs, { includeHasNextPage: wasFieldRequested("pageInfo.hasNextPage", info), diff --git a/src/schemas/schema.graphql b/src/schemas/schema.graphql index 25c78a3..6c5c706 100644 --- a/src/schemas/schema.graphql +++ b/src/schemas/schema.graphql @@ -152,6 +152,12 @@ input RemoveDiscountCodeFromCartInput { token: String } +"Input type for filters to be applied to an discount codes list" +input DiscountCodeFilterInput { + "Keywords typed by the user in the search input field" + searchField: String +} + "Response from the `applyDiscountCodeToCart` mutation" type ApplyDiscountCodeToCartPayload { "The updated cart with discount code applied" @@ -370,6 +376,9 @@ extend type Query { "Provide a shop ID from which you want to get discount codes" shopId: ID! + "Filters to apply to a discount codes query" + filters: DiscountCodeFilterInput + "Return only results that come after this cursor. Use this with `first` to specify the number of results to return." after: ConnectionCursor, From bc9acd196adec4244f666d86a5df1e24c0e91f02 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Tue, 14 Apr 2020 17:06:23 -0700 Subject: [PATCH 4/5] fix: lint Signed-off-by: Will Lopez --- src/queries/discountCodes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/queries/discountCodes.js b/src/queries/discountCodes.js index ac5f4af..c1ff80d 100644 --- a/src/queries/discountCodes.js +++ b/src/queries/discountCodes.js @@ -15,9 +15,9 @@ export default async function discountCodes(context, shopId, filters) { await context.validatePermissions("reaction:legacy:discounts", "read", { shopId }); // Create the mongo selector from the provided filters - let selector = { + let selector = { shopId - } + }; // filter by searchField if (filters.searchField) { From 66c2d29cea0f88e11c3e45bb92832cfa9a0a2578 Mon Sep 17 00:00:00 2001 From: Will Lopez Date: Tue, 14 Apr 2020 17:09:16 -0700 Subject: [PATCH 5/5] refactor: check for filters existence Signed-off-by: Will Lopez --- src/queries/discountCodes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/queries/discountCodes.js b/src/queries/discountCodes.js index c1ff80d..bc76df2 100644 --- a/src/queries/discountCodes.js +++ b/src/queries/discountCodes.js @@ -20,7 +20,7 @@ export default async function discountCodes(context, shopId, filters) { }; // filter by searchField - if (filters.searchField) { + if (filters && filters.searchField) { const cond = { $regex: filters.searchField, $options: "i"