From 164db10e08346291ecb20f9235a2eadf1b110e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rard=20Le=20Cloerec?= Date: Fri, 9 Apr 2021 10:19:47 +0200 Subject: [PATCH] fix: signup password validation feat: add search by brands (WIP) --- components/common/Layout/Layout.tsx | 1 + components/common/Navbar/Navbar.tsx | 6 +- .../api/catalog/handlers/get-products.ts | 55 ++++++++--------- framework/aquilacms/api/checkout.ts | 9 ++- .../api/customers/handlers/signup.ts | 2 +- framework/aquilacms/common/get-all-pages.ts | 14 ----- framework/aquilacms/common/get-site-info.ts | 61 +++++++++++++------ framework/aquilacms/product/use-search.tsx | 3 +- 8 files changed, 81 insertions(+), 70 deletions(-) diff --git a/components/common/Layout/Layout.tsx b/components/common/Layout/Layout.tsx index 54749c46b..669880e5a 100644 --- a/components/common/Layout/Layout.tsx +++ b/components/common/Layout/Layout.tsx @@ -43,6 +43,7 @@ interface Props { pages?: Page[] commerceFeatures: Record } + categories: any[] } const Layout: FC = ({ diff --git a/components/common/Navbar/Navbar.tsx b/components/common/Navbar/Navbar.tsx index fcf9f1e10..1c2ab6dd3 100644 --- a/components/common/Navbar/Navbar.tsx +++ b/components/common/Navbar/Navbar.tsx @@ -5,7 +5,7 @@ import { Searchbar, UserNav } from '@components/common' import NavbarRoot from './NavbarRoot' import s from './Navbar.module.css' -const Navbar: FC = () => ( +const Navbar: FC = ({ categories }: any) => (
@@ -19,7 +19,7 @@ const Navbar: FC = () => ( All - + {/* Clothes @@ -27,7 +27,7 @@ const Navbar: FC = () => ( Shoes - + */}
diff --git a/framework/aquilacms/api/catalog/handlers/get-products.ts b/framework/aquilacms/api/catalog/handlers/get-products.ts index f22096fc0..3d309ed12 100644 --- a/framework/aquilacms/api/catalog/handlers/get-products.ts +++ b/framework/aquilacms/api/catalog/handlers/get-products.ts @@ -1,6 +1,4 @@ -import { Product } from '@commerce/types' import { normalizeProduct } from '../../../lib/normalize' -import getAllProducts from '../../../product/get-all-products' import type { ProductsHandlers } from '../products' const SORT: { [key: string]: string | undefined } = { @@ -22,7 +20,9 @@ const getProducts: ProductsHandlers['getProducts'] = async ({ } let sort = {} - if (search) filter['$text'] = { $search: search } + if (search) { + filter['$text'] = { $search: search } + } if (category) { const cat: any = await config.storeApiFetch('/v2/category', { @@ -44,13 +44,29 @@ const getProducts: ProductsHandlers['getProducts'] = async ({ const productIds: string[] = cat.productsList .filter((p: any) => p.checked) .map((p: any) => p.id) - filter = { - $and: [ - filter, - { - _id: { $in: productIds }, - }, - ], + if (filter['$and']) { + filter['$and'].push({ + _id: { $in: productIds }, + }) + } else { + filter = { + $and: [ + filter, + { + _id: { $in: productIds }, + }, + ], + } + } + } + + if (brand) { + if (filter['$and']) { + filter['$and'].push({ 'trademark.code': brand }) + } else { + filter = { + $and: [filter, { 'trademark.code': brand }], + } } } @@ -61,8 +77,6 @@ const getProducts: ProductsHandlers['getProducts'] = async ({ if (sortValue && direction) { switch (sortValue) { case 'latest': - // 'desc' - console.log(`sort by ${sortValue} not implemented`) case 'trending': // 'desc' console.log(`sort by ${sortValue} not implemented`) @@ -72,23 +86,6 @@ const getProducts: ProductsHandlers['getProducts'] = async ({ } } } - console.log( - JSON.stringify({ - lang: 'en', - PostBody: { - filter, - structure: { - canonical: 1, - reviews: 1, - stock: 1, - universe: 1, - }, - sort, - page: 1, - limit: LIMIT, - }, - }) - ) const { datas } = await config.storeApiFetch('/v2/products', { method: 'POST', body: JSON.stringify({ diff --git a/framework/aquilacms/api/checkout.ts b/framework/aquilacms/api/checkout.ts index 1d7d1d01a..5e0e4ab84 100644 --- a/framework/aquilacms/api/checkout.ts +++ b/framework/aquilacms/api/checkout.ts @@ -14,7 +14,6 @@ const checkoutApi: AquilacmsApiHandler = async (req, res, config) => { const { cookies } = req const cartId = cookies[config.cartCookie] const token = cookies[config.customerCookie] - console.log(config.customerCookie, cookies) try { if (!cartId) { @@ -22,9 +21,15 @@ const checkoutApi: AquilacmsApiHandler = async (req, res, config) => { return } + if (!token) + return res.status(401).json({ + data: null, + errors: [{ message: 'You need to be logged in to continue' }], + }) + if (fullCheckout) { const { data } = await config.storeApiFetch( - `/v/carts/${cartId}/redirect_urls`, + `/v3/carts/${cartId}/redirect_urls`, { method: 'POST', } diff --git a/framework/aquilacms/api/customers/handlers/signup.ts b/framework/aquilacms/api/customers/handlers/signup.ts index 676314909..ca37bdd2e 100644 --- a/framework/aquilacms/api/customers/handlers/signup.ts +++ b/framework/aquilacms/api/customers/handlers/signup.ts @@ -13,7 +13,7 @@ const signup: SignupHandlers['signup'] = async ({ lastName: Joi.string().min(1), email: Joi.string().email({ tlds: false }), password: Joi.string().pattern( - new RegExp('^(?=.*d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^wd:])([^s]){6,}$') + new RegExp(/^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[^\d\s:])([^\s]){6,}$/) ), }) diff --git a/framework/aquilacms/common/get-all-pages.ts b/framework/aquilacms/common/get-all-pages.ts index 265cfc347..2e20d24e3 100644 --- a/framework/aquilacms/common/get-all-pages.ts +++ b/framework/aquilacms/common/get-all-pages.ts @@ -33,20 +33,6 @@ async function getAllPages({ preview?: boolean } = {}): Promise { config = getConfig(config) - // const { datas } = await config.storeApiFetch('/v2/categories', { - // method: 'POST', - // body: JSON.stringify({ - // lang: 'en', - // PostBody: { - // filter: { - // action: 'page', - // }, - // limit: 10, - // page: 1, - // }, - // }), - // }) - const { datas }: { datas: AquilacmsStatic[] } = await config.storeApiFetch( '/v2/statics', { diff --git a/framework/aquilacms/common/get-site-info.ts b/framework/aquilacms/common/get-site-info.ts index fa921ef3b..709d09049 100644 --- a/framework/aquilacms/common/get-site-info.ts +++ b/framework/aquilacms/common/get-site-info.ts @@ -23,34 +23,57 @@ async function getSiteInfo({ config?: AquilacmsConfig preview?: boolean } = {}): Promise { - // TODO: config = getConfig(config) - const { datas } = await config.storeApiFetch('/v2/categories', { - method: 'POST', - body: JSON.stringify({ - lang: 'en', - PostBody: { - filter: { - action: 'catalog', + const [p1, p2]: { datas: any[]; count: number }[] = await Promise.all([ + config.storeApiFetch('/v2/categories', { + method: 'POST', + body: JSON.stringify({ + lang: 'en', + PostBody: { + filter: { + action: 'catalog', + }, + limit: 10, + page: 1, + structure: { + code: 1, + slug: 1, + name: 1, + }, }, - limit: 10, - page: 1, - structure: { - code: 1, - slug: 1, - name: 1, - }, - }, + }), }), - }) + config.storeApiFetch('/v2/trademarks', { + method: 'POST', + body: JSON.stringify({ + PostBody: { + limit: 10, + structure: { + _id: 1, + name: 1, + code: 1, + }, + }, + }), + }), + ]) + + const categories = p1.datas + const brands = p2.datas return { - categories: datas.map((c: any) => ({ + categories: categories.map((c: any) => ({ path: c.slug['en'], entityId: c._id, name: c.name, })), - brands: [], + brands: brands.map((b: any) => ({ + node: { + entityId: b._id, + path: `brands/${b.code}`, + name: b.name, + }, + })), } } diff --git a/framework/aquilacms/product/use-search.tsx b/framework/aquilacms/product/use-search.tsx index 0fd059834..5e6853b27 100644 --- a/framework/aquilacms/product/use-search.tsx +++ b/framework/aquilacms/product/use-search.tsx @@ -26,8 +26,7 @@ export const handler: SWRHook< if (search) url.searchParams.set('search', search) if (categoryId) url.searchParams.set('category', String(categoryId)) - if (Number.isInteger(brandId)) - url.searchParams.set('brand', String(brandId)) + if (brandId) url.searchParams.set('brand', String(brandId)) if (sort) url.searchParams.set('sort', sort) return fetch({