Skip to content

Commit

Permalink
feat: auto add env var ep payment (#80)
Browse files Browse the repository at this point in the history
* feat: unknown error message resolver

* feat: devkit tree utilities

* feat: fix for auth issue and added ep payment .env.local auto update

* refactor: removed console log

* refactor: remove catch

* refactor: formatting

* chore: adding logging

* feat: change the handling of already existing ep payments gateways

* chore: changeset

* feat: fetch stores before building catalog prompts

* feat: environment variable shared utilities

* feat: warning for non-composable projects

* fix: expiration checker

* feat: improved error handling

* chore: changeset
  • Loading branch information
field123 authored Sep 29, 2023
1 parent c31ed0b commit b79f574
Show file tree
Hide file tree
Showing 38 changed files with 657 additions and 229 deletions.
8 changes: 8 additions & 0 deletions .changeset/lovely-berries-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"composable-cli": minor
---

- auto adds ep payments environment variables so the user no longer needs to add them on their own
- changes the way authentication was handled
- adds some additional logging and error handling

6 changes: 6 additions & 0 deletions .changeset/shiny-dolphins-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@elasticpath/composable-common": patch
"composable-cli": patch
---

changed the handling of already existing ep payments gateways
1 change: 1 addition & 0 deletions packages/composable-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"ink-big-text": "1",
"ink-gradient": "2",
"ink-link": "2",
"ink-table": "^3.0.0",
"inquirer": "8.2.4",
"node-fetch": "2.7.0",
"open": "8",
Expand Down
19 changes: 13 additions & 6 deletions packages/composable-cli/src/commands/config/config-command.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import yargs from "yargs"
import Conf from "conf"
import { CommandContext, CommandHandlerFunction } from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
ConfigCommandArguments,
ConfigCommandData,
Expand All @@ -16,7 +15,7 @@ export function configClearCommand(store: Conf): void {
export function createConfigCommand(
ctx: CommandContext,
): yargs.CommandModule<{}, ConfigCommandArguments> {
const { store, logger } = ctx
const { store, logger, handleErrors } = ctx

return {
command: "config",
Expand All @@ -26,16 +25,24 @@ export function createConfigCommand(
.command({
command: "list",
describe: "List all stored configuration",
handler: (_args) => {
handler: handleErrors(async (_args) => {
logger.info(JSON.stringify(store.store, null, 2))
},
return {
success: true,
data: {},
}
}),
})
.command({
command: "clear",
describe: "Clear all stored configuration",
handler: (_args) => {
handler: handleErrors(async (_args) => {
configClearCommand(store)
},
return {
success: true,
data: {},
}
}),
})
.example("$0 config list", "list all stored configuration")
.example("$0 config clear", "clear all stored configuration")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import { renderInk } from "../../lib/ink/render-ink"
import React from "react"
import {
Expand All @@ -17,19 +16,20 @@ import { Feedback } from "../ui/feedback/feedback"
import { trackCommandHandler } from "../../util/track-command-handler"
import { isTTY } from "../../util/is-tty"
export function createFeedbackCommand(
ctx: CommandContext
ctx: CommandContext,
): yargs.CommandModule<RootCommandArguments, FeedbackCommandArguments> {
const { handleErrors } = ctx
return {
command: "feedback",
describe: "Feedback to the Composable CLI",
handler: handleErrors(
trackCommandHandler(ctx, createFeedbackCommandHandler)
trackCommandHandler(ctx, createFeedbackCommandHandler),
),
}
}

export function createFeedbackCommandHandler(
_ctx: CommandContext
_ctx: CommandContext,
): CommandHandlerFunction<
FeedbackCommandData,
FeedbackCommandError,
Expand Down
23 changes: 18 additions & 5 deletions packages/composable-cli/src/commands/generate/d2c/d2c-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
CommandHandlerFunction,
CommandResult,
} from "../../../types/command"
import { handleErrors } from "../../../util/error-handler"
import { getRegion, resolveHostFromRegion } from "../../../util/resolve-region"
import { createApplicationKeys } from "../../../util/create-client-secret"
import { renderInk } from "../../../lib/ink/render-ink"
Expand All @@ -48,7 +47,10 @@ import { detect } from "../../../lib/detect-package-manager"
import { createAlgoliaIntegrationCommandHandler } from "../../integration/algolia/algolia-integration-command"
import boxen from "boxen"
import { getCredentials } from "../../../lib/authentication/get-token"
import { createEPPaymentsCommandHandler } from "../../payments/ep-payments/ep-payments-command"
import {
createEPPaymentsCommandHandler,
isAlreadyExistsError,
} from "../../payments/ep-payments/ep-payments-command"

export function createD2CCommand(
ctx: CommandContext,
Expand Down Expand Up @@ -110,7 +112,9 @@ export function createD2CCommand(

return addSchemaOptionsToCommand(result, options)
},
handler: handleErrors(trackCommandHandler(ctx, createD2CCommandHandler)),
handler: ctx.handleErrors(
trackCommandHandler(ctx, createD2CCommandHandler),
),
}
}

Expand Down Expand Up @@ -551,13 +555,22 @@ export function createD2CCommandHandler(
...args,
})

if (!result.success && isAlreadyExistsError(result.error)) {
notes.push({
title: "EP Payments setup",
description: `The EP Payments integration was already setup. It was using the account id ${colors.bold.green(
result.error.accountId,
)}`,
})
}

if (result.success) {
notes.push({
title: "EP Payments setup",
description: `Don't forget to add your EP Payment variables to .env.local ${colors.bold.green(
`\nNEXT_PUBLIC_STRIPE_ACCOUNT_ID=${gatheredOptions.epPaymentsStripeAccountId}`,
`\nNEXT_PUBLIC_STRIPE_ACCOUNT_ID=${result.data.accountId}`,
)}${colors.bold.green(
`\nNEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${gatheredOptions.epPaymentsStripePublishableKey}`,
`\nNEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=${result.data.publishableKey}`,
)}`,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
GenerateCommandArguments,
GenerateCommandData,
Expand Down Expand Up @@ -67,7 +66,7 @@ export function createGenerateCommand(
.demandCommand(1)
.strict()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createGenerateCommandHandler),
),
}
Expand Down Expand Up @@ -125,7 +124,7 @@ export function createActiveStoreMiddleware(
return
}

return handleErrors(createSetStoreCommandHandler(ctx))(args)
return ctx.handleErrors(createSetStoreCommandHandler(ctx))(args)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {
InsightsCommandArguments,
InsightsCommandData,
Expand All @@ -30,7 +29,7 @@ export function createInsightsCommand(
})
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createInsightsCommandHandler),
),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
AlgoliaIntegrationCommandError,
} from "./algolia-integration.types"
import { CommandContext, CommandHandlerFunction } from "../../../types/command"
import { handleErrors } from "../../../util/error-handler"
import { trackCommandHandler } from "../../../util/track-command-handler"
import {
createActiveStoreMiddleware,
Expand Down Expand Up @@ -35,7 +34,10 @@ import {
algoliaIntegrationSetupSchema,
} from "./utility/integration-hub/setup-algolia-schema"
import boxen from "boxen"
import { buildCatalogPrompts } from "../../../lib/catalog/build-catalog-prompts"
import {
buildCatalogPrompts,
getActiveStoreCatalogs,
} from "../../../lib/catalog/build-catalog-prompts"
import {
getCatalogRelease,
publishCatalog,
Expand All @@ -47,6 +49,7 @@ import {
doesIndexExist,
} from "./utility/algolia/algolia"
import { logging } from "@angular-devkit/core"
import { attemptToAddEnvVariables } from "../../../lib/devkit/add-env-variables"

export function createAlgoliaIntegrationCommand(
ctx: CommandContext,
Expand All @@ -73,7 +76,7 @@ export function createAlgoliaIntegrationCommand(
.fail(false)
.help()
},
handler: handleErrors(
handler: ctx.handleErrors(
trackCommandHandler(ctx, createAlgoliaIntegrationCommandHandler),
),
}
Expand Down Expand Up @@ -182,7 +185,41 @@ export function createAlgoliaIntegrationCommandHandler(
}
}

const catalogsPrompts = await buildCatalogPrompts(ctx.requester)
const catalogsResult = await getActiveStoreCatalogs(ctx.requester)

if (!catalogsResult.success) {
logger.error("Failed to fetch catalogs for active store")
return {
success: false,
error: {
code: "FAILED_TO_FETCH_CATALOGS",
message: "Failed to fetch catalogs for active store",
},
}
}

const catalogs = catalogsResult.data

if (catalogs.length < 1) {
logger.warn(
boxen(
"The Algolia integration will only work correctly if you have a published catalog in your store. We were not able to find any catalogs in your store to publish. Please add a catalog and then rerun the `int algolia` command.\n\nLearn more about catalogs and publishing https://elasticpath.dev/docs/pxm/catalogs/catalogs",
{
padding: 1,
margin: 1,
},
),
)
return {
success: false,
error: {
code: "FAILED_TO_FIND_ANY_CATALOGS",
message: "There were not catalogs in the store",
},
}
}

const catalogsPrompts = await buildCatalogPrompts(catalogs)

if (!catalogsPrompts.success) {
return {
Expand Down Expand Up @@ -258,6 +295,17 @@ export function createAlgoliaIntegrationCommandHandler(
catalog.id.split("-")[0]
}`

const envVarResult = await attemptToAddEnvVariables(ctx, spinner, {
NEXT_PUBLIC_ALGOLIA_INDEX_NAME: algoliaIndexName,
})

if (!envVarResult.success) {
return {
success: false,
error: envVarResult.error,
}
}

logger.info(
boxen(
`Published catalog should have an Algolia index of ${colors.bold.green(
Expand All @@ -270,13 +318,6 @@ export function createAlgoliaIntegrationCommandHandler(
),
)

// TODO: tell the user the name of the published indexes so they can add them to their .env.local file
// - need to wait for the users integration publish job to be finished.
// - indexes are made up of the <catalog-name>_<first-section-uuid> e.g. Default_11ce355f
// - example with space in name "Default Catalog" -> Default_Catalog_11ce355f
// - check if the user has an .env.local file in the directory they have executed the command from
// - better yet prompt the user to ask if they want that done for them.

spinner.start(`Checking Algolia index exists...`)
while (true) {
const indexCheckResult = await doesIndexExist({
Expand All @@ -291,17 +332,6 @@ export function createAlgoliaIntegrationCommandHandler(
await timer(3000)
}

// if (!indexCheckResult) {
// spinner.fail(`Failed to check Algolia index`)
// return {
// success: false,
// error: {
// code: "FAILED_TO_CHECK_ALGOLIA_INDEX",
// message: "Failed to check Algolia index",
// }
// }
// }

spinner.text = `Found index ${algoliaIndexName} performing additional setup...`

const additionalAlgoliaSetupResult = await additionalAlgoliaSetup({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@ import {
CommandHandlerFunction,
RootCommandArguments,
} from "../../types/command"
import { handleErrors } from "../../util/error-handler"
import {IntegrationCommandArguments, IntegrationCommandData, IntegrationCommandError
import {
IntegrationCommandArguments,
IntegrationCommandData,
IntegrationCommandError,
} from "./integration.types"
import { trackCommandHandler } from "../../util/track-command-handler"
import { createAlgoliaIntegrationCommand } from "./algolia/algolia-integration-command"
import { createComposableProjectMiddleware } from "../../lib/composable-project-middleware"

export function createIntegrationCommand(
ctx: CommandContext
ctx: CommandContext,
): yargs.CommandModule<RootCommandArguments, IntegrationCommandArguments> {
return {
command: "integration",
aliases: ["int"],
describe: "setup Elastic Path integrations for your storefront",
builder: (yargs) => {
return yargs
.middleware(createComposableProjectMiddleware(ctx))
.command(createAlgoliaIntegrationCommand(ctx))
.help()
.demandCommand(1)
.strict()
},
handler: handleErrors(
trackCommandHandler(ctx, createIntegrationCommandHandler)
handler: ctx.handleErrors(
trackCommandHandler(ctx, createIntegrationCommandHandler),
),
}
}

export function createIntegrationCommandHandler(
_ctx: CommandContext
_ctx: CommandContext,
): CommandHandlerFunction<
IntegrationCommandData,
IntegrationCommandError,
Expand Down
Loading

0 comments on commit b79f574

Please sign in to comment.