Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove version checking on AvaTax register endpoint #1592

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-melons-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-avatax": patch
---

Remove deprecated Saleor version checking on AvaTax register endpoint. Currently Saleor checks if app can be installed for given env on manifest fetching.
2 changes: 0 additions & 2 deletions apps/avatax/saleor-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,3 @@ switch (process.env.APL) {
export const saleorApp = new SaleorApp({
apl,
});

export const REQUIRED_SALEOR_VERSION = ">=3.19 <4";
3 changes: 1 addition & 2 deletions apps/avatax/src/pages/api/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { wrapWithLoggerContext } from "@saleor/apps-logger/node";
import { withOtel } from "@saleor/apps-otel";

import packageJson from "../../../package.json";
import { REQUIRED_SALEOR_VERSION } from "../../../saleor-app";
import { appWebhooks } from "../../../webhooks";
import { loggerContext } from "../../logger-context";

Expand All @@ -30,7 +29,7 @@ export default wrapWithLoggerContext(
id: "saleor.app.avatax",
name: "AvaTax",
permissions: ["HANDLE_TAXES", "MANAGE_ORDERS"],
requiredSaleorVersion: REQUIRED_SALEOR_VERSION,
requiredSaleorVersion: ">=3.19 <4",
supportUrl: "https://github.com/saleor/apps/discussions",
tokenTargetUrl: `${apiBaseURL}/api/register`,
version: packageJson.version,
Expand Down
55 changes: 2 additions & 53 deletions apps/avatax/src/pages/api/register.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { createAppRegisterHandler } from "@saleor/app-sdk/handlers/next";
import { wrapWithLoggerContext } from "@saleor/apps-logger/node";
import { withOtel } from "@saleor/apps-otel";
import { SaleorVersionCompatibilityValidator } from "@saleor/apps-shared";
import { gql } from "urql";

import { SaleorVersionQuery } from "../../../generated/graphql";
import { REQUIRED_SALEOR_VERSION, saleorApp } from "../../../saleor-app";
import { createInstrumentedGraphqlClient } from "../../lib/create-instrumented-graphql-client";
import { saleorApp } from "../../../saleor-app";
import { createLogger } from "../../logger";
import { loggerContext } from "../../logger-context";

const logger = createLogger("createAppRegisterHandler");

const allowedUrlsPattern = process.env.ALLOWED_DOMAIN_PATTERN;

const SaleorVersion = gql`
query SaleorVersion {
shop {
version
}
}
`;

/**
* Required endpoint, called by Saleor to install app.
* It will exchange tokens with app, so saleorApp.apl will contain token
Expand All @@ -45,50 +33,11 @@ export default wrapWithLoggerContext(
return true;
},
],
onAuthAplSaved: async (req, context) => {
onAuthAplSaved: async (_req, context) => {
logger.info("AvaTax app configuration set up successfully", {
saleorApiUrl: context.authData.saleorApiUrl,
});
},
/**
* TODO Unify with all apps - shared code. Consider moving to app-sdk
*/
async onRequestVerified(req, { authData: { token, saleorApiUrl }, respondWithError }) {
try {
const client = createInstrumentedGraphqlClient({
saleorApiUrl,
token,
});

const saleorVersion = await client
.query<SaleorVersionQuery>(SaleorVersion, {})
.toPromise()
.then((res) => {
return res.data?.shop.version;
});

logger.debug("Received saleor version from Shop query", { saleorVersion });

if (!saleorVersion) {
throw new Error("Saleor Version couldn't be fetched from the API");
}

new SaleorVersionCompatibilityValidator(REQUIRED_SALEOR_VERSION).validateOrThrow(
saleorVersion,
);
} catch (e: unknown) {
const message = (e as Error)?.message ?? "Unknown error";

logger.debug("Failed validating semver, will respond with error and status 400", {
message,
});

throw respondWithError({
message: message,
status: 400,
});
}
},
}),
"/api/register",
),
Expand Down
Loading