From 903641323a9b8c1a8c151cfd051e0ba255d61bf7 Mon Sep 17 00:00:00 2001 From: vijayakumarv-trika <77378833+vijayakumarv-trika@users.noreply.github.com> Date: Thu, 5 Aug 2021 14:31:30 +0530 Subject: [PATCH 1/2] SignUp enabled --- framework/commerce/types/signup.ts | 5 ++ .../elasticpath/api/endpoints/signup/index.ts | 19 ++++++- .../api/endpoints/signup/signup.ts | 54 +++++++++++++++++++ framework/elasticpath/api/index.ts | 10 +++- .../elasticpath/api/operations/signup.ts | 46 ++++++++++++++++ framework/elasticpath/auth/use-signup.tsx | 41 +++++++++++--- framework/elasticpath/types/signup.ts | 10 ++++ 7 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 framework/elasticpath/api/endpoints/signup/signup.ts create mode 100644 framework/elasticpath/api/operations/signup.ts create mode 100644 framework/elasticpath/types/signup.ts diff --git a/framework/commerce/types/signup.ts b/framework/commerce/types/signup.ts index 4e23da6c0..fa64250d5 100644 --- a/framework/commerce/types/signup.ts +++ b/framework/commerce/types/signup.ts @@ -24,3 +24,8 @@ export type SignupSchema = { } } } + +export type SignupOperation = { + data: { result?: string } + variables: unknown +} \ No newline at end of file diff --git a/framework/elasticpath/api/endpoints/signup/index.ts b/framework/elasticpath/api/endpoints/signup/index.ts index 491bf0ac9..7dcd6a357 100644 --- a/framework/elasticpath/api/endpoints/signup/index.ts +++ b/framework/elasticpath/api/endpoints/signup/index.ts @@ -1 +1,18 @@ -export default function noopApi(...args: any[]): void {} +import { GetAPISchema, createEndpoint } from '@commerce/api' +import signupEndpoint from '@commerce/api/endpoints/signup' +import type { SignupSchema } from '../../../../commerce/types/signup' +import type { ElasticpathAPI } from '../..' +import signup from './signup' + +export type SignupAPI = GetAPISchema + +export type SignupEndpoint = SignupAPI['endpoint'] + +export const handlers: SignupEndpoint['handlers'] = { signup } + +const signupApi = createEndpoint({ + handler: signupEndpoint, + handlers, +}) + +export default signupApi diff --git a/framework/elasticpath/api/endpoints/signup/signup.ts b/framework/elasticpath/api/endpoints/signup/signup.ts new file mode 100644 index 000000000..9b8033258 --- /dev/null +++ b/framework/elasticpath/api/endpoints/signup/signup.ts @@ -0,0 +1,54 @@ +import type { SignupEndpoint } from '.' + +const MoltinGateway = require('@moltin/sdk').gateway +const Moltin = MoltinGateway({ + client_id: process.env.NEXT_PUBLIC_ELASTICPATH_CLIENTID, + client_secret: process.env.ELASTICPATH_SECRET +}) + +const signup: SignupEndpoint['handlers']['signup'] = async ({ + res, + body: { firstName, lastName, email, password }, + config, + commerce, +}) => { + // TODO: Add proper validations with something like Ajv + if (!(firstName && lastName && email && password)) { + return res.status(400).json({ + data: null, + errors: [{ message: 'Invalid request' }], + }) + } + // TODO: validate the firstname, lastname, password and email + // Passwords must be at least 7 characters and contain both alphabetic + // and numeric characters. + const customer = { + type: 'customer', + name: firstName + lastName, + email: email, + password: password + } + try { + let customerAPI = await Moltin.Customers.Create(customer) + return res.status(200).json(customerAPI); + } catch (error) { + let errorData = error.errors[0]; + // Check if the email and password didn't match an existing account + if (errorData.status == 404) { + return res.status(401).json({ + data: null, + errors: [ + { + message: + 'Account not created. Please try again', + code: 'invalid_credentials', + }, + ], + }) + } + + throw error + } +} + +export default signup diff --git a/framework/elasticpath/api/index.ts b/framework/elasticpath/api/index.ts index 2a47a8d6a..9a753efb3 100644 --- a/framework/elasticpath/api/index.ts +++ b/framework/elasticpath/api/index.ts @@ -7,8 +7,11 @@ import { import createFetcher from './utils/fetch-local' import type { LoginAPI } from './endpoints/login' +import type { SignupAPI } from './endpoints/signup' import login from './operations/login' +import signup from './operations/signup' + import getAllPages from './operations/get-all-pages' import getPage from './operations/get-page' import getSiteInfo from './operations/get-site-info' @@ -17,6 +20,9 @@ import getAllProductPaths from './operations/get-all-product-paths' import getAllProducts from './operations/get-all-products' import getProduct from './operations/get-product' +import { Login } from '@commerce/types' +import { Signup } from '@commerce/types' + const API_URL = process.env.NEXT_PUBLIC_ELASTICPATH_BASE const STOREID = process.env.NEXT_PUBLIC_ELASTICPATH_STOREID const SECRET = process.env.NEXT_PUBLIC_ELASTICPATH_SECRET @@ -45,7 +51,7 @@ const config: any = { const operations = { login, - + signup, getAllPages, getPage, getSiteInfo, @@ -64,7 +70,7 @@ export const provider = { config, operations } export type Provider = typeof provider export type APIs = - | LoginAPI + | LoginAPI | SignupAPI export type ElasticpathAPI

= CommerceAPI

diff --git a/framework/elasticpath/api/operations/signup.ts b/framework/elasticpath/api/operations/signup.ts new file mode 100644 index 000000000..72a59d872 --- /dev/null +++ b/framework/elasticpath/api/operations/signup.ts @@ -0,0 +1,46 @@ +import type { ServerResponse } from 'http' +import type { + OperationContext, + OperationOptions, +} from '@commerce/api/operations' +import type { SignupOperation } from '../../types/signup' +import { Provider, ElasticpathConfig } from '..' + +export default function signupOperation({ + commerce, +}: OperationContext) { + async function signup(opts: { + variables: T['variables'] + config?: Partial + res: ServerResponse + }): Promise + + async function signup( + opts: { + variables: T['variables'] + config?: Partial + res: ServerResponse + } & OperationOptions + ): Promise + + async function signup({ + variables, + res: response, + config: cfg, + }: { + query?: string + variables: T['variables'] + res: ServerResponse + config?: Partial + }): Promise { + const config = commerce.getConfig(cfg) + + const { data } = await config.fetch('account', 'signup', [variables]) + + return { + result: data, + } + } + + return signup +} diff --git a/framework/elasticpath/auth/use-signup.tsx b/framework/elasticpath/auth/use-signup.tsx index e9ad13458..bc594b8bd 100644 --- a/framework/elasticpath/auth/use-signup.tsx +++ b/framework/elasticpath/auth/use-signup.tsx @@ -1,19 +1,46 @@ import { useCallback } from 'react' import useCustomer from '../customer/use-customer' import { MutationHook } from '@commerce/utils/types' +import { CommerceError } from '@commerce/utils/errors' import useSignup, { UseSignup } from '@commerce/auth/use-signup' +import { constants } from 'buffer' export default useSignup as UseSignup export const handler: MutationHook = { fetchOptions: { - query: '', + url: '/api/signup', + method: 'POST', }, - async fetcher() { - return null + async fetcher({ input: { firstName, lastName, email, password }, options, fetch }) { + console.log("input", firstName) + if (!(firstName && lastName && email && password)) { + throw new CommerceError({ + message: + 'A first name, last name, email and password are required to login', + }) + } + + return fetch({ + ...options, + variables: { + firstName, + lastName, + email, + password + }, + }); + }, + useHook: ({ fetch }) => () => { + const { revalidate } = useCustomer() + + return useCallback( + async function signup(input) { + const data = await fetch({ input }) + await revalidate() + return data + }, + [fetch, revalidate] + ) }, - useHook: - ({ fetch }) => - () => - () => {}, } diff --git a/framework/elasticpath/types/signup.ts b/framework/elasticpath/types/signup.ts new file mode 100644 index 000000000..a63270c03 --- /dev/null +++ b/framework/elasticpath/types/signup.ts @@ -0,0 +1,10 @@ +import * as Core from '@commerce/types/signup' + +export * from '@commerce/types/signup' + +export type SignupOperation = Core.SignupOperation & { + variables: { + email: string + password: string + } +} From 045f6bcc759d0900f891ff874db43057b6ec3ed1 Mon Sep 17 00:00:00 2001 From: vijayakumarv-trika <77378833+vijayakumarv-trika@users.noreply.github.com> Date: Thu, 5 Aug 2021 16:12:04 +0530 Subject: [PATCH 2/2] Code changes --- framework/elasticpath/api/endpoints/signup/signup.ts | 2 +- framework/elasticpath/auth/use-signup.tsx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/elasticpath/api/endpoints/signup/signup.ts b/framework/elasticpath/api/endpoints/signup/signup.ts index 9b8033258..bb7f4ff0a 100644 --- a/framework/elasticpath/api/endpoints/signup/signup.ts +++ b/framework/elasticpath/api/endpoints/signup/signup.ts @@ -34,7 +34,7 @@ const signup: SignupEndpoint['handlers']['signup'] = async ({ } catch (error) { let errorData = error.errors[0]; // Check if the email and password didn't match an existing account - if (errorData.status == 404) { + if (errorData.status == 409) { return res.status(401).json({ data: null, errors: [ diff --git a/framework/elasticpath/auth/use-signup.tsx b/framework/elasticpath/auth/use-signup.tsx index bc594b8bd..cb423ceb0 100644 --- a/framework/elasticpath/auth/use-signup.tsx +++ b/framework/elasticpath/auth/use-signup.tsx @@ -3,7 +3,6 @@ import useCustomer from '../customer/use-customer' import { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' import useSignup, { UseSignup } from '@commerce/auth/use-signup' -import { constants } from 'buffer' export default useSignup as UseSignup