From 7047fe98518361eb9680601cc36f29aaf909e81c Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Thu, 12 Aug 2021 10:40:42 +0200 Subject: [PATCH 1/3] Minor fixes (#9) --- src/cognito/cognito-client.ts | 5 +++-- src/datasources/category-api.ts | 13 ++++++------- src/datasources/market-api.ts | 1 + src/schema.ts | 6 ++++++ src/types/product-types.ts | 5 +++++ 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/cognito/cognito-client.ts b/src/cognito/cognito-client.ts index f360676..c844789 100644 --- a/src/cognito/cognito-client.ts +++ b/src/cognito/cognito-client.ts @@ -135,9 +135,10 @@ const verifyToken = async ( if (claim.token_use === 'access') { // Access token, check for scopes const accessClaim = claim as AccessTokenClaim; - console.debug(`accessClaim confirmed for ${accessClaim.username}`); + const who = accessClaim.username ?? accessClaim.client_id; + console.debug(`accessClaim confirmed for ${who}`); return { - userName: accessClaim.username, + userName: who, isValid: true, idToken: null, accessToken: accessClaim, diff --git a/src/datasources/category-api.ts b/src/datasources/category-api.ts index 055aea7..72d6ae6 100644 --- a/src/datasources/category-api.ts +++ b/src/datasources/category-api.ts @@ -43,7 +43,7 @@ WHERE product_category.product_id = ? /** * TODO: should we have these? - * category keywords and texts + * category keywords * SELECT category_keyword.category_id, keywords.value FROM category_keyword @@ -52,12 +52,11 @@ WHERE product_category.product_id = ? JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id * * category texts - SELECT name, - text - FROM categorydata cd - JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id - WHERE category_id = ? - AND locale_id = ?; + SELECT name, text + FROM categorydata cd + JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id + WHERE category_id = ? + AND locale_id = ?; * category teaser images??? diff --git a/src/datasources/market-api.ts b/src/datasources/market-api.ts index 55f56c9..fa2872a 100644 --- a/src/datasources/market-api.ts +++ b/src/datasources/market-api.ts @@ -3,6 +3,7 @@ import { Market } from '../types/market-types'; const MINUTE = 60; +// TODO: perhaps rename to MarketLocale API and add market_locales and locales here. export class MarketAPI extends SQLDataSource { constructor(config) { super(config); diff --git a/src/schema.ts b/src/schema.ts index ef4f9c1..0d31dfd 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -132,6 +132,9 @@ const typeDefs = gql` customerEmail: String customerCellphone: String flyerIds: String + """ + marketName and localeName will go away and be replaced with real objects + """ marketName: String localeName: String billingInformation: ContactInformation @@ -139,6 +142,9 @@ const typeDefs = gql` billingAddressId: Int deliveryAddressId: Int rows: [OrderRow] + """ + pwinty needs to be updated with market being a subtype + """ market: Market } diff --git a/src/types/product-types.ts b/src/types/product-types.ts index 02d46c0..7f93f03 100644 --- a/src/types/product-types.ts +++ b/src/types/product-types.ts @@ -85,6 +85,11 @@ export interface ProductFields { printFileWidth?: number; printFileHeight?: number; printFileDpi?: number; + // Below exists for Canvas frame in DB but in shop limits.php is used + // minHeight: number; + // minWidth: number; + // maxHeight: number; + // maxWidth: number; } export function getProductGroupStringFromString(group: string): string { From 58ed7fbf6a6b457bf9a3037365741cc137f77c5d Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Thu, 12 Aug 2021 10:51:02 +0200 Subject: [PATCH 2/3] P5 7617 cleanup market and locale (#10) * Minor fixes * Cleanup with adding Locale type * Add locales to test --- src/__test__/all.test.ts | 18 ++++++++--- .../{market-api.ts => market-locale-api.ts} | 29 +++++++++++++++-- src/index.ts | 6 ++-- src/resolvers/index.ts | 5 ++- src/resolvers/markets-locale-resolver.ts | 31 +++++++++++++++++++ src/resolvers/markets-resolver.ts | 19 ------------ src/resolvers/orders-resolver.ts | 11 +++++-- src/resolvers/products-resolver.ts | 6 ++-- src/schema.ts | 27 +++++++++------- ...market-types.ts => market-locale-types.ts} | 7 +++++ 10 files changed, 115 insertions(+), 44 deletions(-) rename src/datasources/{market-api.ts => market-locale-api.ts} (53%) create mode 100644 src/resolvers/markets-locale-resolver.ts delete mode 100644 src/resolvers/markets-resolver.ts rename src/types/{market-types.ts => market-locale-types.ts} (55%) diff --git a/src/__test__/all.test.ts b/src/__test__/all.test.ts index 7d52512..3b5a111 100644 --- a/src/__test__/all.test.ts +++ b/src/__test__/all.test.ts @@ -214,6 +214,12 @@ describe('GraphQL Apollo tests', () => { ); const marketControlData = market_response.rows[0]; + // Locale + const locale_response = await allContainers.db.raw( + `SELECT * FROM locales WHERE value='${orderControlData.locale}';`, + ); + const localeControlData = locale_response.rows[0]; + // Order row const orderrow_response = await allContainers.db.raw( `SELECT * FROM "order-rows" WHERE orderid = 1;`, @@ -230,8 +236,10 @@ describe('GraphQL Apollo tests', () => { { order(id: 1) { id - localeName - marketName + locale { + id + value + } market { id name @@ -254,8 +262,6 @@ describe('GraphQL Apollo tests', () => { // Order expect(order.id).toBe(`${orderControlData.id}`); - expect(order.marketName).toBe(orderControlData.country_code); - expect(order.localeName).toBe(orderControlData.locale); // Market expect(order.market.id).toBe(`${marketControlData.id}`); @@ -266,6 +272,10 @@ describe('GraphQL Apollo tests', () => { parseFloat(marketControlData.price_adjustment), ); + // Locale + expect(order.locale.id).toBe(`${localeControlData.id}`); + expect(order.locale.value).toBe(localeControlData.value); + // Order Rows expect(order.rows.length).toBe(orderRowsControlData.length); expect(order.rows[0].id).toBe('' + orderRowsControlData[0].rowid); diff --git a/src/datasources/market-api.ts b/src/datasources/market-locale-api.ts similarity index 53% rename from src/datasources/market-api.ts rename to src/datasources/market-locale-api.ts index fa2872a..648544b 100644 --- a/src/datasources/market-api.ts +++ b/src/datasources/market-locale-api.ts @@ -1,10 +1,10 @@ import { SQLDataSource } from 'datasource-sql'; -import { Market } from '../types/market-types'; +import { Market, Locale } from '../types/market-locale-types'; const MINUTE = 60; // TODO: perhaps rename to MarketLocale API and add market_locales and locales here. -export class MarketAPI extends SQLDataSource { +export class MarketLocaleAPI extends SQLDataSource { constructor(config) { super(config); } @@ -33,4 +33,29 @@ export class MarketAPI extends SQLDataSource { .from('markets') .cache(MINUTE * 5); } + + async getLocaleById(id: number): Promise { + return this.knex + .select('*') + .from('locales') + .where('id', id) + .first() + .cache(MINUTE * 5); + } + + async getLocaleByValue(value: string): Promise { + return this.knex + .select('*') + .from('locales') + .where('value', value) + .first() + .cache(MINUTE * 5); + } + + async getLocales(): Promise> { + return this.knex + .select('*') + .from('locales') + .cache(MINUTE * 5); + } } diff --git a/src/index.ts b/src/index.ts index 569bf71..95c3a68 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import { ProductAPI } from './datasources/product-api'; import { DesignerAPI } from './datasources/designer-api'; import { CategoryAPI } from './datasources/category-api'; import { KeywordAPI } from './datasources/keyword-api'; -import { MarketAPI } from './datasources/market-api'; +import { MarketLocaleAPI } from './datasources/market-locale-api'; import { ImageServerApi } from './datasources/imageserver-api'; import { InteriorAPI } from './datasources/interior-api'; @@ -26,7 +26,7 @@ const designerApi = new DesignerAPI(knexConfig); const imageServerApi = new ImageServerApi(); const interiorApi = new InteriorAPI(knexConfig, imageServerApi); const keywordApi = new KeywordAPI(knexConfig); -const marketApi = new MarketAPI(knexConfig); +const marketLocaleApi = new MarketLocaleAPI(knexConfig); const orderApi = new OrderAPI(knexConfig); const productApi = new ProductAPI(knexConfig); @@ -36,7 +36,7 @@ const dataSources = () => ({ interiorApi, imageServerApi, keywordApi, - marketApi, + marketLocaleApi, orderApi, productApi, }); diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index ecb2812..3cd51aa 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -7,7 +7,10 @@ import { import { designerQueryTypeDefs, designerTypeDefs } from './designers-resolver'; import { categoryQueryTypeDefs, categoryTypeDefs } from './categories-resolver'; import { keywordsQueryTypeDefs, keywordTypeDefs } from './keywords-resolver'; -import { marketsQueryTypeDefs, marketTypeDefs } from './markets-resolver'; +import { + marketsQueryTypeDefs, + marketTypeDefs, +} from './markets-locale-resolver'; import { DateTimeResolver, DateResolver, JSONResolver } from 'graphql-scalars'; import { interiorsQueryTypeDefs, interiorTypeDefs } from './interiors-resolver'; diff --git a/src/resolvers/markets-locale-resolver.ts b/src/resolvers/markets-locale-resolver.ts new file mode 100644 index 0000000..137119b --- /dev/null +++ b/src/resolvers/markets-locale-resolver.ts @@ -0,0 +1,31 @@ +import { IResolverObject } from 'graphql-tools'; +import { MarketLocaleAPI } from '../datasources/market-locale-api'; + +const Market: IResolverObject = {}; + +async function getMarkets(_, _args, { dataSources }) { + return (dataSources.marketLocaleApi).getMarkets(); +} + +async function getMarket(_, { name }, { dataSources }) { + return (dataSources.marketLocaleApi).getMarketByName(name); +} + +const Locale: IResolverObject = {}; + +async function getLocales(_, _args, { dataSources }) { + return (dataSources.marketLocaleApi).getLocales(); +} + +async function getLocale(_, { value }, { dataSources }) { + return (dataSources.marketLocaleApi).getLocaleByValue(value); +} + +export const marketTypeDefs = { Market, Locale }; + +export const marketsQueryTypeDefs = { + markets: getMarkets, + market: getMarket, + locales: getLocales, + locale: getLocale, +}; diff --git a/src/resolvers/markets-resolver.ts b/src/resolvers/markets-resolver.ts deleted file mode 100644 index 1ffbac1..0000000 --- a/src/resolvers/markets-resolver.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { IResolverObject } from 'graphql-tools'; -import { MarketAPI } from '../datasources/market-api'; - -const Market: IResolverObject = {}; - -async function getMarkets(_, _args, { dataSources }) { - return (dataSources.marketApi).getMarkets(); -} - -async function getMarket(_, { name }, { dataSources }) { - return (dataSources.marketApi).getMarketByName(name); -} - -export const marketTypeDefs = { Market }; - -export const marketsQueryTypeDefs = { - markets: getMarkets, - market: getMarket, -}; diff --git a/src/resolvers/orders-resolver.ts b/src/resolvers/orders-resolver.ts index 4eabec6..d0c8659 100644 --- a/src/resolvers/orders-resolver.ts +++ b/src/resolvers/orders-resolver.ts @@ -1,5 +1,5 @@ import { IResolverObject } from 'graphql-tools'; -import { MarketAPI } from '../datasources/market-api'; +import { MarketLocaleAPI } from '../datasources/market-locale-api'; import { OrderAPI } from '../datasources/order-api'; import { FilterInput } from '../types/types'; @@ -15,7 +15,14 @@ const Order: IResolverObject = { return (dataSources.orderApi).getOrderRowsByOrderId(id); }, async market({ market }, _args, { dataSources }) { - return (dataSources.marketApi).getMarketByName(market); + return (dataSources.marketLocaleApi).getMarketByName( + market, + ); + }, + async locale({ locale }, _args, { dataSources }) { + return (dataSources.marketLocaleApi).getLocaleByValue( + locale, + ); }, }; diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index c9ea854..215cf4f 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -5,12 +5,14 @@ import { CategoryAPI } from '../datasources/category-api'; import { Category } from '../types/category-types'; import { KeywordAPI } from '../datasources/keyword-api'; import { Keyword } from '../types/keyword-types'; -import { MarketAPI } from '../datasources/market-api'; +import { MarketLocaleAPI } from '../datasources/market-locale-api'; import { InteriorAPI } from '../datasources/interior-api'; const ProductBlacklist: IResolverObject = { async market(parent, _args, { dataSources }) { - return (dataSources.marketApi).getMarketById(parent.marketId); + return (dataSources.marketLocaleApi).getMarketById( + parent.marketId, + ); }, }; diff --git a/src/schema.ts b/src/schema.ts index 0d31dfd..07c8610 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -21,6 +21,8 @@ const typeDefs = gql` designer(id: Int!): Designer markets: [Market] market(name: String!): Market + locales: [Locale] + locale(value: String!): Locale interiors(filter: InteriorsFilterInput): [Interior] } @@ -52,7 +54,7 @@ const typeDefs = gql` offset: Int! } """ - Later replace with implements ListResult + Later replace with implements ListResult when products support it """ type OrderResult { total: Int! @@ -63,10 +65,17 @@ const typeDefs = gql` type Market { id: ID! - name: String - vat: Float - currency: String - priceAdjustment: Float + name: String! + vat: Float! + currency: String! + priceAdjustment: Float! + } + + type Locale { + id: ID! + value: String! + name: String! + fallbackId: Int } type Address { @@ -132,11 +141,6 @@ const typeDefs = gql` customerEmail: String customerCellphone: String flyerIds: String - """ - marketName and localeName will go away and be replaced with real objects - """ - marketName: String - localeName: String billingInformation: ContactInformation deliveryInformation: ContactInformation billingAddressId: Int @@ -146,6 +150,8 @@ const typeDefs = gql` pwinty needs to be updated with market being a subtype """ market: Market + locale: Locale + rows: [OrderRow] } type OrderRow { @@ -298,7 +304,6 @@ const typeDefs = gql` id: ID! groupId: Int! group: ProductGroup! - marketId: Int! market: Market! inserted: DateTime! updated: DateTime! diff --git a/src/types/market-types.ts b/src/types/market-locale-types.ts similarity index 55% rename from src/types/market-types.ts rename to src/types/market-locale-types.ts index f7b2665..a8e4cef 100644 --- a/src/types/market-types.ts +++ b/src/types/market-locale-types.ts @@ -5,3 +5,10 @@ export interface Market { currency: string; priceAdjustment: number; } + +export interface Locale { + id: number; + value: string; + fallbackId: number; + name: string; +} From 72997e26f1d2fb11a7763aed4fe0aebd6faa4a2a Mon Sep 17 00:00:00 2001 From: Niklas Fondberg Date: Thu, 12 Aug 2021 10:59:13 +0200 Subject: [PATCH 3/3] Hot fix remove double rows def --- src/schema.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/schema.ts b/src/schema.ts index 07c8610..ee81355 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -145,7 +145,6 @@ const typeDefs = gql` deliveryInformation: ContactInformation billingAddressId: Int deliveryAddressId: Int - rows: [OrderRow] """ pwinty needs to be updated with market being a subtype """